import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
public class EnvVariableMonitor {
private static final Logger logger = Logger.getLogger(EnvVariableMonitor.class.getName());
private final Map<String, String> expectedVars;
public EnvVariableMonitor(Map<String, String> expectedVars) {
this.expectedVars = expectedVars;
}
public boolean validateEnvironment() {
for (Map.Entry<String, String> entry : expectedVars.entrySet()) {
String varName = entry.getKey();
String expectedValue = entry.getValue();
String actualValue = System.getenv(varName);
if (actualValue == null) {
logger.warning("Environment variable '" + varName + "' is missing.");
return false;
}
if (!actualValue.trim().equals(expectedValue.trim())) {
logger.severe("Environment variable '" + varName + "' has unexpected value. Expected: '" + expectedValue + "', Actual: '" + actualValue + "'.");
return false;
}
}
return true;
}
public static void main(String[] args) {
// Example Usage
Map<String, String> expectedVariables = new HashMap<>();
expectedVariables.put("DATABASE_URL", "jdbc:mysql://localhost:3306/mydb");
expectedVariables.put("API_KEY", "your_secret_api_key");
expectedVariables.put("DEBUG", "true");
EnvVariableMonitor monitor = new EnvVariableMonitor(expectedVariables);
if (monitor.validateEnvironment()) {
System.out.println("Environment variables are valid.");
} else {
System.out.println("Environment variables validation failed.");
}
}
}
Add your comment