From cddd9bd705f1cb9c37968310f0b7a2c1d6e754d2 Mon Sep 17 00:00:00 2001 From: Igor Polevoy Date: Mon, 12 Dec 2016 22:27:46 -0600 Subject: [PATCH] #560 Enhance AppConfig with additional methods --- .../org/javalite/app_config/AppConfig.java | 93 +++++++++++++++++++ .../javalite/app_config/AppConfigTest.java | 17 ++++ .../app_config/development.properties | 5 +- 3 files changed, 114 insertions(+), 1 deletion(-) diff --git a/app-config/src/main/java/org/javalite/app_config/AppConfig.java b/app-config/src/main/java/org/javalite/app_config/AppConfig.java index 8feac8686..a6eec6fc3 100644 --- a/app-config/src/main/java/org/javalite/app_config/AppConfig.java +++ b/app-config/src/main/java/org/javalite/app_config/AppConfig.java @@ -39,6 +39,17 @@ public class AppConfig implements Map { private static Logger LOGGER = LoggerFactory.getLogger(AppConfig.class); private static HashMap props = new HashMap(); private static HashMap plainProps = new HashMap(); + private static final String activeEnv; + + static { + String env = System.getenv("ACTIVE_ENV"); + if (env == null) { + LOGGER.warn("Environment variable 'ACTIVE_ENV' not found, defaulting to 'development'"); + env = "development"; + } + activeEnv = env; + } + public AppConfig() { init(); @@ -220,4 +231,86 @@ public Collection values() { public Set> entrySet() { throw new UnsupportedOperationException("Operation not supported, not a real map"); } + + + /** + * Returns current environment name as defined by environment variable ACTIVE_ENV. + * + * @return current environment name as defined by environment variable ACTIVE_ENV. + */ + public static String activeEnv() { + return activeEnv; + } + + /** + * @return true if environment name as defined by environment variable ACTIVE_ENV is "testenv". + */ + public static boolean isInTestEnv() { + return "testenv".equals(activeEnv()); + } + + /** + * @return true if environment name as defined by environment variable ACTIVE_ENV is "production". + */ + public static boolean isInProduction() { + return "production".equals(activeEnv()); + } + + /** + * @return true if environment name as defined by environment variable ACTIVE_ENV is "development". + */ + public static boolean isInDevelopment() { + return "development".equals(activeEnv()); + } + + /** + * @return true if environment name as defined by environment variable ACTIVE_ENV is "staging". + */ + public static boolean isInStaging() { + return "development".equals(activeEnv()); + } + + /** + * Returns all keys that start with a prefix + * + * @param prefix prefix for properties. + */ + public static List getKeys(String prefix) { + List res = new ArrayList<>(); + for(String key: props.keySet()){ + if(key.startsWith(prefix)){ + res.add(key); + } + } + return res; + } + + + /** + * Return all numbered properties with a prefix. For instance if there is a file: + *
+     *     prop.1=one
+     *     prop.2=two
+     * 
+ * + * .. and this method is called: + *
+     *     List props = AppConfig.getProperties("prop");
+     * 
+ * then the resulting list will have all properties starting from prop. + * This method presumes consecutive numbers in the suffix. + * + * @param prefix prefix of numbered properties. + * @return list of property values. + */ + public static List getProperties(String prefix) { + List res = new ArrayList<>(); + prefix += "."; + for (int i = 1; ; i++) { + String prop = p(prefix + i); + if (prop == null) + return res; + res.add(prop); + } + } } diff --git a/app-config/src/test/java/org/javalite/app_config/AppConfigTest.java b/app-config/src/test/java/org/javalite/app_config/AppConfigTest.java index f9baccb29..be7cc604c 100644 --- a/app-config/src/test/java/org/javalite/app_config/AppConfigTest.java +++ b/app-config/src/test/java/org/javalite/app_config/AppConfigTest.java @@ -17,4 +17,21 @@ public void shouldGetNameInDevelopmentEnv() { public void shouldReadAsMapFromDevelopmentFile() { the(new AppConfig().get("first.name")).shouldBeEqual("John"); } + + @Test + public void shouldFindPropertiesWithPrefix(){ + + the(AppConfig.getProperties("prop")).shouldContain("one"); + the(AppConfig.getProperties("prop")).shouldContain("two"); + the(AppConfig.getProperties("prop")).shouldNotContain("John"); + } + + @Test + public void shouldFindKeysWithPrefix(){ + + the(AppConfig.getKeys("prop")).shouldContain("prop.1"); + the(AppConfig.getKeys("prop")).shouldContain("prop.1"); + the(AppConfig.getKeys("prop")).shouldNotContain("first.name"); + } + } diff --git a/app-config/src/test/resources/app_config/development.properties b/app-config/src/test/resources/app_config/development.properties index b13f85ef6..f20e09e14 100644 --- a/app-config/src/test/resources/app_config/development.properties +++ b/app-config/src/test/resources/app_config/development.properties @@ -1,3 +1,6 @@ # this is an example of an environment -specific -first.name=John \ No newline at end of file +first.name=John + +prop.1=one +prop.2=two \ No newline at end of file