public class PropertiesCheck {
private static final Logger log = LoggerFactory.getLogger(PropertiesCheck.class);
String DEV = "dev";
String TST = "tst";
String PRD = "prd";
HashMap<String, PropertiesValue> properties = new HashMap<>();
public static void main(String[] args) throws IOException {
PropertiesCheck im = new PropertiesCheck();
im.check();
}
private void check() throws IOException {
loadProperties(DEV);
loadProperties(TST);
loadProperties(PRD);
printProperties();
}
private void printProperties() {
Set<String> keys = properties.keySet();
int validCount = 0;
int failCount = 0;
log.info("========================================================================================");
for (String key : keys) {
PropertiesValue value = properties.get(key);
if (!value.isPass()) {
failCount++;
log.info(MessageFormat.format("({0}) {1} : {2} / {3} / {4}",
value.isPass(), key, value.getDev() != null, value.getTst() != null, value.getPrd() != null));
} else {
validCount++;
}
}
log.info("application-[dev:tst:prd].properties file CHECK : PASS : {}, FAIL : {}", validCount, failCount);
log.info("========================================================================================");
}
private void loadProperties(String sysClf) throws IOException {
String fileName = getPath(String.format("application-%s.properties", sysClf));
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream(fileName)) {
props.load(fis);
} catch (IOException e) {
log.error("Error : {}", e.getMessage());
}
for (String key : props.stringPropertyNames()) {
PropertiesValue value = new PropertiesValue();
if (properties.containsKey(key)) {
value = properties.get(key);
}
switch(sysClf) {
case "dev" : value.setDev(props.getProperty(key)); break;
case "tst" : value.setTst(props.getProperty(key)); break;
case "prd" : value.setPrd(props.getProperty(key)); break;
default : break;
}
value.setValid();
properties.put(key, value);
}
}
private String getPath(String fileName) {
String filePath ="";
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
URL url = cl.getResource(fileName);
if (url == null)
log.error("null");
else {
filePath = url.getPath();
log.info("filePath : {}", filePath);
}
return filePath;
}
@Data
public class PropertiesValue {
boolean pass;
String dev;
String tst;
String prd;
public void setValid() {
this.pass = (dev != null && prd != null);
}
}
}