1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.concurrent.ConcurrentHashMap;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.ScheduledExecutorService;
  6. import java.util.concurrent.TimeUnit;
  7. public class EnvVariableThrottler {
  8. private final Map<String, String> envVariables; // Store environment variables and their validation status.
  9. private final Map<String, Long> lastAccessTime; // Last time an environment variable was accessed.
  10. private final Map<String, String> defaultValues; // Default values for environment variables.
  11. private final long throttleIntervalMillis; // Time interval for throttling (e.g., 1000 milliseconds = 1 second).
  12. private final ScheduledExecutorService scheduler; // Executor service for scheduling throttling tasks.
  13. public EnvVariableThrottler(long throttleIntervalMillis, Map<String, String> defaultValues) {
  14. this.envVariables = new ConcurrentHashMap<>();
  15. this.lastAccessTime = new ConcurrentHashMap<>();
  16. this.defaultValues = defaultValues;
  17. this.throttleIntervalMillis = throttleIntervalMillis;
  18. this.scheduler = Executors.newSingleThreadScheduledExecutor();
  19. }
  20. public String getOrDefault(String envVarName) {
  21. // Get the environment variable value.
  22. String envValue = System.getenv(envVarName);
  23. // If the environment variable is not set or is null, use the default value.
  24. if (envValue == null || envValue.isEmpty()) {
  25. return defaultValues.getOrDefault(envVarName, ""); // Return default value if not set.
  26. }
  27. // Check if the environment variable has been accessed within the throttle interval.
  28. long currentTime = System.currentTimeMillis();
  29. if (!lastAccessTime.containsKey(envVarName) || (currentTime - lastAccessTime.get(envVarName)) > throttleIntervalMillis) {
  30. // Validate the environment variable (e.g., check if it's a valid format).
  31. validateEnvVar(envVarName, envValue);
  32. lastAccessTime.put(envVarName, currentTime);
  33. }
  34. return envValue;
  35. }
  36. private void validateEnvVar(String envVarName, String envValue) {
  37. // Perform validation checks on the environment variable.
  38. // Example: Check if the value is a valid number.
  39. if (envVarName.equals("PORT")) {
  40. try {
  41. Integer.parseInt(envValue);
  42. } catch (NumberFormatException e) {
  43. System.err.println("Invalid PORT value. Using default: " + defaultValues.getOrDefault(envVarName, ""));
  44. envVariables.put(envVarName, defaultValues.getOrDefault(envVarName, ""));
  45. }
  46. }
  47. // Add more validation checks as needed.
  48. else {
  49. envVariables.put(envVarName, envValue); // Store env variable value after validation
  50. }
  51. }
  52. public void start() {
  53. scheduler.scheduleAtFixedRate(() -> {
  54. // Periodically check and reset the access time for all environment variables.
  55. for (String envVar : envVariables.keySet()) {
  56. lastAccessTime.remove(envVar);
  57. }
  58. }, throttleIntervalMillis, throttleIntervalMillis, TimeUnit.MILLISECONDS);
  59. }
  60. public void stop() {
  61. scheduler.shutdown();
  62. try {
  63. scheduler.awaitTermination(5, TimeUnit.SECONDS);
  64. } catch (InterruptedException e) {
  65. Thread.currentThread().interrupt();
  66. }
  67. }
  68. public static void main(String[] args) throws InterruptedException {
  69. // Example usage
  70. Map<String, String> defaults = new HashMap<>();
  71. defaults.put("PORT", "8080");
  72. defaults.put("DEBUG", "false");
  73. EnvVariableThrottler throttler = new EnvVariableThrottler(1000, defaults); // Throttle every 1 second.
  74. throttler.start();
  75. System.out.println("PORT: " + throttler.getOrDefault("PORT"));
  76. System.out.println("DEBUG: " + throttler.getOrDefault("DEBUG"));
  77. System.out.println("NON_EXISTENT_VAR: " + throttler.getOrDefault("NON_EXISTENT_VAR"));
  78. Thread.sleep(2000);
  79. System.out.println("PORT: " + throttler.getOrDefault("PORT")); // Should use the

Add your comment