Skip to content

Commit

Permalink
#560 Enhance AppConfig with additional methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Polevoy committed Dec 13, 2016
1 parent d2cde93 commit cddd9bd
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
93 changes: 93 additions & 0 deletions app-config/src/main/java/org/javalite/app_config/AppConfig.java
Expand Up @@ -39,6 +39,17 @@ public class AppConfig implements Map<String, String> {
private static Logger LOGGER = LoggerFactory.getLogger(AppConfig.class);
private static HashMap<String, Property> props = new HashMap<String, Property>();
private static HashMap<String, String> plainProps = new HashMap<String, String>();
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();
Expand Down Expand Up @@ -220,4 +231,86 @@ public Collection<String> values() {
public Set<Entry<String, String>> entrySet() {
throw new UnsupportedOperationException("Operation not supported, not a real map");
}


/**
* Returns current environment name as defined by environment variable <code>ACTIVE_ENV</code>.
*
* @return current environment name as defined by environment variable <code>ACTIVE_ENV</code>.
*/
public static String activeEnv() {
return activeEnv;
}

/**
* @return true if environment name as defined by environment variable <code>ACTIVE_ENV</code> is "testenv".
*/
public static boolean isInTestEnv() {
return "testenv".equals(activeEnv());
}

/**
* @return true if environment name as defined by environment variable <code>ACTIVE_ENV</code> is "production".
*/
public static boolean isInProduction() {
return "production".equals(activeEnv());
}

/**
* @return true if environment name as defined by environment variable <code>ACTIVE_ENV</code> is "development".
*/
public static boolean isInDevelopment() {
return "development".equals(activeEnv());
}

/**
* @return true if environment name as defined by environment variable <code>ACTIVE_ENV</code> 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<String> getKeys(String prefix) {
List<String> 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:
* <pre>
* prop.1=one
* prop.2=two
* </pre>
*
* .. and this method is called:
* <pre>
* List<String> props = AppConfig.getProperties("prop");
* </pre>
* then the resulting list will have all properties starting from <code>prop</code>.
* This method presumes consecutive numbers in the suffix.
*
* @param prefix prefix of numbered properties.
* @return list of property values.
*/
public static List<String> getProperties(String prefix) {
List<String> res = new ArrayList<>();
prefix += ".";
for (int i = 1; ; i++) {
String prop = p(prefix + i);
if (prop == null)
return res;
res.add(prop);
}
}
}
Expand Up @@ -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");
}

}
@@ -1,3 +1,6 @@
# this is an example of an environment -specific

first.name=John
first.name=John

prop.1=one
prop.2=two

0 comments on commit cddd9bd

Please sign in to comment.