티스토리 뷰

Skill/spring

PropertiesCheck.java

진열사랑 2025. 1. 13. 09:56

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);
}
}
}

'Skill > spring' 카테고리의 다른 글

ServiceAOP.java  (0) 2025.01.13
RedisConfig.java  (0) 2025.01.13
JasyptConfig.java  (0) 2025.01.13
AsyncConfig.java  (0) 2025.01.13
ServiceApplication  (0) 2025.01.09
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함