/**
* Read application.conf and resolve overriden key using the play id mechanism.
*/
public static void readConfiguration() {
VirtualFile appRoot = VirtualFile.open(applicationPath);
conf = appRoot.child("conf/application.conf");
try {
configuration = IO.readUtf8Properties(conf.inputstream());
} catch (RuntimeException e) {
if (e.getCause() instanceof IOException) {
Logger.fatal("Cannot read application.conf");
System.exit(-1);
}
}
// Ok, check for instance specifics configuration
Properties newConfiguration = new OrderSafeProperties();
Pattern pattern = Pattern.compile("^%([a-zA-Z0-9_\\-]+)\\.(.*)$");
for (Object key : configuration.keySet()) {
Matcher matcher = pattern.matcher(key + "");
if (!matcher.matches()) {
newConfiguration.put(key, configuration.get(key).toString().trim());
}
}
for (Object key : configuration.keySet()) {
Matcher matcher = pattern.matcher(key + "");
if (matcher.matches()) {
String instance = matcher.group(1);
if (instance.equals(id)) {
newConfiguration.put(matcher.group(2), configuration.get(key).toString().trim());
}
}
}
configuration = newConfiguration;
// Resolve ${..}
pattern = Pattern.compile("\\$\\{([^}]+)}");
for (Object key : configuration.keySet()) {
String value = configuration.getProperty(key.toString());
Matcher matcher = pattern.matcher(value);
StringBuffer newValue = new StringBuffer(100);
while (matcher.find()) {
String jp = matcher.group(1);
String r;
if (jp.equals("application.path")) {
r = Play.applicationPath.getAbsolutePath();
} else if (jp.equals("play.path")) {
r = Play.frameworkPath.getAbsolutePath();
} else {
r = System.getProperty(jp);
if (r == null) {
Logger.warn("Cannot replace %s in configuration (%s=%s)", jp, key, value);
continue;
}
}
matcher.appendReplacement(newValue, r.replaceAll("\\\\", "\\\\\\\\"));
}
matcher.appendTail(newValue);
configuration.setProperty(key.toString(), newValue.toString());
}
// Include
Map<Object, Object> toInclude = new HashMap<Object, Object>(16);
for (Object key : configuration.keySet()) {
if (key.toString().startsWith("@include.")) {
try {
toInclude.putAll(IO.readUtf8Properties(appRoot.child("conf/" + configuration.getProperty(key.toString())).inputstream()));
} catch (Exception ex) {
Logger.warn("Missing include: %s", key);
}
}
}