import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class LogFileParameterInjector {
private static final String DEFAULT_LOG_FILE = "default.log";
private static final String DEVELOPMENT_LOG_FILE_PREFIX = "dev_";
/**
* Injects parameters from log files. Handles file not found gracefully.
*
* @param params A map of parameter names to default values.
* @return A map of parameter names to the injected values. Returns the original params if any file is not found.
*/
public static Map<String, String> injectParameters(Map<String, String> params) {
Map<String, String> injectedParams = new HashMap<>(params); // Create a copy to avoid modifying the original
for (Map.Entry<String, String> entry : params.entrySet()) {
String paramName = entry.getKey();
String defaultValue = entry.getValue();
String logFileName = DEVELOPMENT_LOG_FILE_PREFIX + paramName + ".log";
File logFile = new File(logFileName);
try {
String fileContent = FileUtils.readUtf8FileContent(logFile); // Assuming FileUtils.readUtf8FileContent is available or implement it
if (fileContent != null && !fileContent.isEmpty()) {
injectedParams.put(paramName, fileContent.trim());
}
} catch (Exception e) {
// Log file not found or error reading. Keep the default value.
System.err.println("Log file not found or error reading for " + logFileName + ". Using default value.");
}
}
return injectedParams;
}
//Helper class for reading file content
static class FileUtils {
public static String readUtf8FileContent(File file) throws Exception {
return java.nio.file.Files.readString(file.toPath(), java.nio.charset.StandardCharsets.UTF_8);
}
}
}
Add your comment