Skip to content

Commit a278107

Browse files
committed
Fix new config
1 parent 88775b2 commit a278107

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

api/src/main/java/org/asynchttpclient/util/AsyncPropertiesHelper.java

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package org.asynchttpclient.util;
22

3-
import java.io.BufferedReader;
43
import java.io.IOException;
54
import java.io.InputStream;
6-
import java.io.InputStreamReader;
7-
import java.util.Collections;
8-
import java.util.HashMap;
9-
import java.util.Map;
5+
import java.util.Properties;
106

117
import org.asynchttpclient.chmv8.ConcurrentHashMapV8;
128

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

4137
private final ConcurrentHashMapV8<String, String> propsCache = new ConcurrentHashMapV8<String, String>();
42-
private final Map<String, String> defaultProperties = parsePropertiesFile(DEFAULT_AHC_PROPERTIES);
43-
private Map<String, String> customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES);
38+
private final Properties defaultProperties = parsePropertiesFile(DEFAULT_AHC_PROPERTIES);
39+
private volatile Properties customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES);
4440

4541
public void reload() {
4642
customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES);
4743
propsCache.clear();
4844
}
4945

50-
private Map<String, String> parsePropertiesFile(String file) {
51-
52-
try {
53-
InputStream is = getClass().getClassLoader().getResourceAsStream(file);
54-
if (is == null) {
55-
return Collections.emptyMap();
56-
} else {
57-
Map<String, String> map = new HashMap<>();
58-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
59-
String line = null;
60-
while ((line = reader.readLine()) != null) {
61-
String[] part = line.split("=");
62-
map.put(part[0], part[1]);
63-
}
64-
return map;
65-
}
46+
private Properties parsePropertiesFile(String file) {
47+
Properties props = new Properties();
48+
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file)) {
49+
if (is != null) {
50+
props.load(is);
6651
}
6752
} catch (IOException e) {
6853
throw new IllegalArgumentException("Can't parse file", e);
6954
}
55+
return props;
7056
}
7157

72-
private final ConcurrentHashMapV8.Fun<String, String> computer = new ConcurrentHashMapV8.Fun<String, String>() {
73-
74-
@Override
75-
public String apply(String key) {
76-
String value = System.getProperty(key);
77-
if (value == null) {
78-
value = customProperties.get(key);
79-
}
80-
if (value == null) {
81-
value = defaultProperties.get(key);
82-
}
58+
public String getString(String key) {
59+
return propsCache.computeIfAbsent(key, new ConcurrentHashMapV8.Fun<String, String>() {
8360

84-
return value;
85-
}
86-
};
61+
@Override
62+
public String apply(String key) {
63+
String value = System.getProperty(key);
64+
if (value == null) {
65+
value = (String) customProperties.getProperty(key);
66+
}
67+
if (value == null) {
68+
value = (String) defaultProperties.getProperty(key);
69+
}
8770

88-
public String getString(String key) {
89-
return propsCache.computeIfAbsent(key, computer);
71+
return value;
72+
}
73+
});
9074
}
91-
75+
9276
public int getInt(String key) {
9377
return Integer.parseInt(getString(key));
9478
}
95-
79+
9680
public boolean getBoolean(String key) {
9781
return Boolean.parseBoolean(getString(key));
9882
}

extras/registry/src/main/java/org/asynchttpclient/extras/registry/AsyncImplHelper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ public class AsyncImplHelper {
3131
* the specified class couldn't be created.
3232
*/
3333
public static Class<AsyncHttpClient> getAsyncImplClass(String propertyName) {
34-
try {
35-
String asyncHttpClientImplClassName = AsyncPropertiesHelper.getAsyncHttpClientConfig().getString(propertyName);
34+
String asyncHttpClientImplClassName = AsyncPropertiesHelper.getAsyncHttpClientConfig().getString(propertyName);
35+
if (asyncHttpClientImplClassName != null) {
3636
Class<AsyncHttpClient> asyncHttpClientImplClass = AsyncImplHelper.getClass(asyncHttpClientImplClassName);
3737
return asyncHttpClientImplClass;
38-
} catch (Exception configException) {
39-
return null;
4038
}
39+
return null;
4140
}
4241

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

0 commit comments

Comments
 (0)