diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-03 16:52:07 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-03 16:52:07 +0100 |
commit | 1be8d7ee97a96f79e75f43710f8322611c5a1e24 (patch) | |
tree | dce84f10c9db94bad9530bc71a0d638f5f5409cd /same | |
parent | 1c7bbed107d0767536c93a092dbf192a6a45a620 (diff) |
Add Configuration system.
Diffstat (limited to 'same')
3 files changed, 70 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/config/Configuration.java b/same/src/main/java/com/orbekk/same/config/Configuration.java new file mode 100644 index 0000000..dce9dee --- /dev/null +++ b/same/src/main/java/com/orbekk/same/config/Configuration.java @@ -0,0 +1,52 @@ +package com.orbekk.same.config; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Configuration { + public final static String configurationProperty = + "com.orbekk.same.config.file"; + + Logger logger = LoggerFactory.getLogger(getClass()); + Properties configuration = new Properties(); + + private Configuration() { + // Use factory methods. + } + + public static Configuration load() { + Configuration configuration = new Configuration(); + configuration.loadDefault(); + return configuration; + } + + public void loadDefault() { + String filename = System.getProperty(configurationProperty); + if (filename != null) { + try { + configuration.load(new FileReader(filename)); + } catch (FileNotFoundException e) { + logger.error("Failed to load configuration. {}", e); + logger.error("Failed to load configuration. {}={}", + configurationProperty, filename); + } catch (IOException e) { + logger.error("Failed to load configuration. {}", e); + logger.error("Failed to load configuration. {}={}", + configurationProperty, filename); + } + } else { + logger.error("Failed to load configuration. {}={}", + configurationProperty, filename); + } + } + + public String getProperty(String name) { + return configuration.getProperty(name); + } +} diff --git a/same/src/test/java/com/orbekk/same/config/ConfigurationTest.java b/same/src/test/java/com/orbekk/same/config/ConfigurationTest.java new file mode 100644 index 0000000..e5b42aa --- /dev/null +++ b/same/src/test/java/com/orbekk/same/config/ConfigurationTest.java @@ -0,0 +1,17 @@ +package com.orbekk.same.config; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ConfigurationTest { + @Test public void testLoadConfiguration() { + String resources = "src/test/resources/"; + System.setProperty(Configuration.configurationProperty, + resources + "com/orbekk/same/config/test.properties"); + Configuration configuration = Configuration.load(); + assertEquals("Test Value", + configuration.getProperty("testProperty")); + + } +} diff --git a/same/src/test/resources/com/orbekk/same/config/test.properties b/same/src/test/resources/com/orbekk/same/config/test.properties new file mode 100644 index 0000000..45e1cc4 --- /dev/null +++ b/same/src/test/resources/com/orbekk/same/config/test.properties @@ -0,0 +1 @@ +testProperty=Test Value |