Merge pull request #296 from bitsquare/cbeams

Add support for $HOME/.bitsquare/bitsquare.properties
This commit is contained in:
Chris Beams 2014-11-27 10:26:18 +01:00
commit 1ce151821a
2 changed files with 26 additions and 13 deletions

View file

@ -57,10 +57,11 @@ public class BitsquareEnvironment extends StandardEnvironment {
public static final String APP_DATA_DIR_CLEAN_KEY = "app.data.dir.clean"; public static final String APP_DATA_DIR_CLEAN_KEY = "app.data.dir.clean";
public static final String DEFAULT_APP_DATA_DIR_CLEAN = "false"; public static final String DEFAULT_APP_DATA_DIR_CLEAN = "false";
static final String BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME = "bitsquareDefaultProperties";
static final String BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME = "bitsquareClasspathProperties";
static final String BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME = "bitsquareFilesystemProperties";
static final String BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME = "bitsquareCommandLineProperties"; static final String BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME = "bitsquareCommandLineProperties";
static final String BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME = "bitsquareAppDirProperties";
static final String BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME = "bitsquareHomeDirProperties";
static final String BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME = "bitsquareClasspathProperties";
static final String BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME = "bitsquareDefaultProperties";
private final ResourceLoader resourceLoader = new DefaultResourceLoader(); private final ResourceLoader resourceLoader = new DefaultResourceLoader();
@ -87,7 +88,8 @@ public class BitsquareEnvironment extends StandardEnvironment {
MutablePropertySources propertySources = this.getPropertySources(); MutablePropertySources propertySources = this.getPropertySources();
propertySources.addFirst(commandLineProperties); propertySources.addFirst(commandLineProperties);
try { try {
propertySources.addLast(filesystemProperties()); propertySources.addLast(appDirProperties());
propertySources.addLast(homeDirProperties());
propertySources.addLast(classpathProperties()); propertySources.addLast(classpathProperties());
propertySources.addLast(defaultProperties()); propertySources.addLast(defaultProperties());
} catch (Exception ex) { } catch (Exception ex) {
@ -96,14 +98,24 @@ public class BitsquareEnvironment extends StandardEnvironment {
} }
PropertySource<?> filesystemProperties() throws Exception { PropertySource<?> appDirProperties() throws Exception {
String location = String.format("file:%s/bitsquare.properties", appDataDir); String location = String.format("file:%s/bitsquare.properties", appDataDir);
Resource resource = resourceLoader.getResource(location); Resource resource = resourceLoader.getResource(location);
if (!resource.exists()) if (!resource.exists())
return new PropertySource.StubPropertySource(BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME); return new PropertySource.StubPropertySource(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME);
return new ResourcePropertySource(BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME, resource); return new ResourcePropertySource(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME, resource);
}
PropertySource<?> homeDirProperties() throws Exception {
String location = String.format("file:%s/.bitsquare/bitsquare.properties", getProperty("user.home"));
Resource resource = resourceLoader.getResource(location);
if (!resource.exists())
return new PropertySource.StubPropertySource(BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME);
return new ResourcePropertySource(BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME, resource);
} }
PropertySource<?> classpathProperties() throws Exception { PropertySource<?> classpathProperties() throws Exception {

View file

@ -41,13 +41,13 @@ public class BitsquareEnvironmentTests {
PropertySource commandlineProps = new MockPropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME) PropertySource commandlineProps = new MockPropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)
.withProperty("key.x", "x.commandline"); .withProperty("key.x", "x.commandline");
PropertySource filesystemProps = new MockPropertySource(BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME) PropertySource filesystemProps = new MockPropertySource(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME)
.withProperty("key.x", "x.env") .withProperty("key.x", "x.env")
.withProperty("key.y", "y.env"); .withProperty("key.y", "y.env");
ConfigurableEnvironment env = new BitsquareEnvironment(commandlineProps) { ConfigurableEnvironment env = new BitsquareEnvironment(commandlineProps) {
@Override @Override
PropertySource<?> filesystemProperties() { PropertySource<?> appDirProperties() {
return filesystemProps; return filesystemProps;
} }
}; };
@ -56,10 +56,11 @@ public class BitsquareEnvironmentTests {
assertThat(propertySources.precedenceOf(named(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(propertySources.precedenceOf(named(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)), equalTo(0));
assertThat(propertySources.precedenceOf(named(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(propertySources.precedenceOf(named(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(1));
assertThat(propertySources.precedenceOf(named(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(2)); assertThat(propertySources.precedenceOf(named(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(2));
assertThat(propertySources.precedenceOf(named(BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME)), equalTo(3)); assertThat(propertySources.precedenceOf(named(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME)), equalTo(3));
assertThat(propertySources.precedenceOf(named(BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(4)); assertThat(propertySources.precedenceOf(named(BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME)), equalTo(4));
assertThat(propertySources.precedenceOf(named(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(5)); assertThat(propertySources.precedenceOf(named(BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(5));
assertThat(propertySources.size(), equalTo(6)); assertThat(propertySources.precedenceOf(named(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(6));
assertThat(propertySources.size(), equalTo(7));
assertThat(env.getProperty("key.x"), equalTo("x.commandline")); // commandline value wins due to precedence assertThat(env.getProperty("key.x"), equalTo("x.commandline")); // commandline value wins due to precedence
assertThat(env.getProperty("key.y"), equalTo("y.env")); // env value wins because it's the only one available assertThat(env.getProperty("key.y"), equalTo("y.env")); // env value wins because it's the only one available