I did something like this and itβs not that difficult, you just subclass the Properties class and implement your own getProperty method, check the template and replace it if necessary.
//this makes the pattern ${sometext} static public final String JAVA_CONFIG_VARIABLE = "\\$\\{(.*)\\}"; @Override public String getProperty(String key) { String val = super.getProperty(key); if( val != null && val.indexOf("${") != -1 ) { //we have at least one replacable parm, let replace it val = replaceParms(val); } return val; } public final String replaceParms(String in) { if(in == null) return null; //this could be precompiled as Pattern is supposed to be thread safe Pattern pattern = Pattern.compile(JAVA_CONFIG_VARIABLE); Matcher matcher = pattern.matcher(in); StringBuffer buf = new StringBuffer(); while (matcher.find()) { String replaceStr = matcher.group(1); String prop = getProperty(replaceStr); //if it isn't in our properties file, check if it is a system property if (prop == null ) prop = System.getProperty(replaceStr); if( prop == null ) { System.out.printf("Failed to find property '%s' for '%s'\n", replaceStr, in); } else { matcher.appendReplacement(buf, prop); } } matcher.appendTail(buf); String result = buf.toString(); return result; }
Jere Jan 15 2018-12-15T00: 00Z
source share