Properties module can now take multiple properties files

This commit is contained in:
fjy 2014-06-03 10:34:38 -07:00
parent f7c4d6a24a
commit 8340a1b0a1
2 changed files with 28 additions and 24 deletions

View File

@ -85,6 +85,7 @@ import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -351,7 +352,7 @@ public class Initialization
return Guice.createInjector( return Guice.createInjector(
new DruidGuiceExtensions(), new DruidGuiceExtensions(),
new JacksonModule(), new JacksonModule(),
new PropertiesModule("runtime.properties"), new PropertiesModule(Arrays.asList("global.runtime.properties", "runtime.properties")),
new ConfigModule(), new ConfigModule(),
new Module() new Module()
{ {

View File

@ -33,6 +33,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties; import java.util.Properties;
/** /**
@ -41,11 +42,11 @@ public class PropertiesModule implements Module
{ {
private static final Logger log = new Logger(PropertiesModule.class); private static final Logger log = new Logger(PropertiesModule.class);
private final String propertiesFile; private final List<String> propertiesFiles;
public PropertiesModule(String propertiesFile) public PropertiesModule(List<String> propertiesFiles)
{ {
this.propertiesFile = propertiesFile; this.propertiesFiles = propertiesFiles;
} }
@Override @Override
@ -57,30 +58,32 @@ public class PropertiesModule implements Module
Properties props = new Properties(fileProps); Properties props = new Properties(fileProps);
props.putAll(systemProps); props.putAll(systemProps);
InputStream stream = ClassLoader.getSystemResourceAsStream(propertiesFile); for (String propertiesFile : propertiesFiles) {
try { InputStream stream = ClassLoader.getSystemResourceAsStream(propertiesFile);
if (stream == null) { try {
File workingDirectoryFile = new File(systemProps.getProperty("druid.properties.file", propertiesFile)); if (stream == null) {
if (workingDirectoryFile.exists()) { File workingDirectoryFile = new File(systemProps.getProperty("druid.properties.file", propertiesFile));
stream = new BufferedInputStream(new FileInputStream(workingDirectoryFile)); if (workingDirectoryFile.exists()) {
stream = new BufferedInputStream(new FileInputStream(workingDirectoryFile));
}
} }
}
if (stream != null) { if (stream != null) {
log.info("Loading properties from %s", propertiesFile); log.info("Loading properties from %s", propertiesFile);
try { try {
fileProps.load(new InputStreamReader(stream, Charsets.UTF_8)); fileProps.load(new InputStreamReader(stream, Charsets.UTF_8));
} }
catch (IOException e) { catch (IOException e) {
throw Throwables.propagate(e); throw Throwables.propagate(e);
}
} }
} }
} catch (FileNotFoundException e) {
catch (FileNotFoundException e) { log.wtf(e, "This can only happen if the .exists() call lied. That's f'd up.");
log.wtf(e, "This can only happen if the .exists() call lied. That's f'd up."); }
} finally {
finally { Closeables.closeQuietly(stream);
Closeables.closeQuietly(stream); }
} }
binder.bind(Properties.class).toInstance(props); binder.bind(Properties.class).toInstance(props);