Skip to content

Commit

Permalink
Fix new config
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed May 28, 2015
1 parent 88775b2 commit a278107
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 45 deletions.
@@ -1,12 +1,8 @@
package org.asynchttpclient.util; package org.asynchttpclient.util;


import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.util.Properties;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


import org.asynchttpclient.chmv8.ConcurrentHashMapV8; import org.asynchttpclient.chmv8.ConcurrentHashMapV8;


Expand Down Expand Up @@ -39,60 +35,48 @@ public static class Config {
public static final String CUSTOM_AHC_PROPERTIES = "ahc.properties"; public static final String CUSTOM_AHC_PROPERTIES = "ahc.properties";


private final ConcurrentHashMapV8<String, String> propsCache = new ConcurrentHashMapV8<String, String>(); private final ConcurrentHashMapV8<String, String> propsCache = new ConcurrentHashMapV8<String, String>();
private final Map<String, String> defaultProperties = parsePropertiesFile(DEFAULT_AHC_PROPERTIES); private final Properties defaultProperties = parsePropertiesFile(DEFAULT_AHC_PROPERTIES);
private Map<String, String> customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES); private volatile Properties customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES);


public void reload() { public void reload() {
customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES); customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES);
propsCache.clear(); propsCache.clear();
} }


private Map<String, String> parsePropertiesFile(String file) { private Properties parsePropertiesFile(String file) {

Properties props = new Properties();
try { try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file)) {
InputStream is = getClass().getClassLoader().getResourceAsStream(file); if (is != null) {
if (is == null) { props.load(is);
return Collections.emptyMap();
} else {
Map<String, String> map = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
String line = null;
while ((line = reader.readLine()) != null) {
String[] part = line.split("=");
map.put(part[0], part[1]);
}
return map;
}
} }
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("Can't parse file", e); throw new IllegalArgumentException("Can't parse file", e);
} }
return props;
} }


private final ConcurrentHashMapV8.Fun<String, String> computer = new ConcurrentHashMapV8.Fun<String, String>() { public String getString(String key) {

return propsCache.computeIfAbsent(key, new ConcurrentHashMapV8.Fun<String, String>() {
@Override
public String apply(String key) {
String value = System.getProperty(key);
if (value == null) {
value = customProperties.get(key);
}
if (value == null) {
value = defaultProperties.get(key);
}


return value; @Override
} public String apply(String key) {
}; String value = System.getProperty(key);
if (value == null) {
value = (String) customProperties.getProperty(key);
}
if (value == null) {
value = (String) defaultProperties.getProperty(key);
}


public String getString(String key) { return value;
return propsCache.computeIfAbsent(key, computer); }
});
} }

public int getInt(String key) { public int getInt(String key) {
return Integer.parseInt(getString(key)); return Integer.parseInt(getString(key));
} }

public boolean getBoolean(String key) { public boolean getBoolean(String key) {
return Boolean.parseBoolean(getString(key)); return Boolean.parseBoolean(getString(key));
} }
Expand Down
Expand Up @@ -31,13 +31,12 @@ public class AsyncImplHelper {
* the specified class couldn't be created. * the specified class couldn't be created.
*/ */
public static Class<AsyncHttpClient> getAsyncImplClass(String propertyName) { public static Class<AsyncHttpClient> getAsyncImplClass(String propertyName) {
try { String asyncHttpClientImplClassName = AsyncPropertiesHelper.getAsyncHttpClientConfig().getString(propertyName);
String asyncHttpClientImplClassName = AsyncPropertiesHelper.getAsyncHttpClientConfig().getString(propertyName); if (asyncHttpClientImplClassName != null) {
Class<AsyncHttpClient> asyncHttpClientImplClass = AsyncImplHelper.getClass(asyncHttpClientImplClassName); Class<AsyncHttpClient> asyncHttpClientImplClass = AsyncImplHelper.getClass(asyncHttpClientImplClassName);
return asyncHttpClientImplClass; return asyncHttpClientImplClass;
} catch (Exception configException) {
return null;
} }
return null;
} }


private static Class<AsyncHttpClient> getClass(final String asyncImplClassName) { private static Class<AsyncHttpClient> getClass(final String asyncImplClassName) {
Expand Down

0 comments on commit a278107

Please sign in to comment.