Skip to content

Commit 88775b2

Browse files
committed
Ditch Typesafe config
1 parent 4237034 commit 88775b2

File tree

13 files changed

+652
-590
lines changed

13 files changed

+652
-590
lines changed

api/pom.xml

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,31 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<parent>
5-
<groupId>org.asynchttpclient</groupId>
6-
<artifactId>async-http-client-project</artifactId>
7-
<version>2.0.0-SNAPSHOT</version>
8-
</parent>
9-
<modelVersion>4.0.0</modelVersion>
10-
<artifactId>async-http-client-api</artifactId>
11-
<name>Asynchronous Http Client API</name>
12-
<description>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<parent>
4+
<groupId>org.asynchttpclient</groupId>
5+
<artifactId>async-http-client-project</artifactId>
6+
<version>2.0.0-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<artifactId>async-http-client-api</artifactId>
10+
<name>Asynchronous Http Client API</name>
11+
<description>
1312
The Async Http Client (AHC) API classes.
1413
</description>
1514

16-
<build>
17-
<plugins>
18-
<plugin>
19-
<groupId>org.apache.maven.plugins</groupId>
20-
<artifactId>maven-jar-plugin</artifactId>
21-
<version>2.2</version>
22-
<executions>
23-
<execution>
24-
<goals>
25-
<goal>test-jar</goal>
26-
</goals>
27-
</execution>
28-
</executions>
29-
</plugin>
30-
</plugins>
31-
</build>
32-
33-
<dependencies>
34-
<dependency>
35-
<groupId>com.typesafe</groupId>
36-
<artifactId>config</artifactId>
37-
<version>1.2.1</version>
38-
</dependency>
39-
<dependency>
40-
<groupId>org.slf4j</groupId>
41-
<artifactId>slf4j-api</artifactId>
42-
<version>1.7.6</version>
43-
</dependency>
44-
</dependencies>
45-
46-
</project>
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-jar-plugin</artifactId>
20+
<version>2.2</version>
21+
<executions>
22+
<execution>
23+
<goals>
24+
<goal>test-jar</goal>
25+
</goals>
26+
</execution>
27+
</executions>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
</project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* http://creativecommons.org/publicdomain/zero/1.0/
2121
*/
2222

23-
package org.asynchttpclient.providers.netty.chmv8;
23+
package org.asynchttpclient.chmv8;
2424

2525
import java.io.ObjectStreamField;
2626
import java.io.Serializable;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* http://creativecommons.org/publicdomain/zero/1.0/
2121
*/
2222

23-
package org.asynchttpclient.providers.netty.chmv8;
23+
package org.asynchttpclient.chmv8;
2424

2525
import java.util.concurrent.RecursiveAction;
2626

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* http://creativecommons.org/publicdomain/zero/1.0/
2121
*/
2222

23-
package org.asynchttpclient.providers.netty.chmv8;
23+
package org.asynchttpclient.chmv8;
2424

2525
import java.lang.Thread.UncaughtExceptionHandler;
2626
import java.util.ArrayList;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* http://creativecommons.org/publicdomain/zero/1.0/
2121
*/
2222

23-
package org.asynchttpclient.providers.netty.chmv8;
23+
package org.asynchttpclient.chmv8;
2424

2525
import java.io.Serializable;
2626
import java.lang.ref.ReferenceQueue;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* http://creativecommons.org/publicdomain/zero/1.0/
2121
*/
2222

23-
package org.asynchttpclient.providers.netty.chmv8;
23+
package org.asynchttpclient.chmv8;
2424

2525

2626
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* http://creativecommons.org/publicdomain/zero/1.0/
2121
*/
2222

23-
package org.asynchttpclient.providers.netty.chmv8;
23+
package org.asynchttpclient.chmv8;
2424

2525
import java.io.Serializable;
2626
import java.util.concurrent.atomic.AtomicLong;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* http://creativecommons.org/publicdomain/zero/1.0/
2121
*/
2222

23-
package org.asynchttpclient.providers.netty.chmv8;
23+
package org.asynchttpclient.chmv8;
2424

2525
import java.util.Random;
2626

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

3-
import com.typesafe.config.Config;
4-
import com.typesafe.config.ConfigFactory;
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import org.asynchttpclient.chmv8.ConcurrentHashMapV8;
512

613
public class AsyncPropertiesHelper {
7-
8-
public static final String ASYNC_HTTP_CLIENT_IMPL_PROPERTIES_FILE = "ahc.properties";
9-
public static final String DEFAULTAHC_PROPERTIES = "ahc-default.properties";
10-
11-
public static Config getAsyncHttpClientConfig(){
12-
return ConfigFactory.load(ASYNC_HTTP_CLIENT_IMPL_PROPERTIES_FILE)
13-
.withFallback(ConfigFactory.load(DEFAULTAHC_PROPERTIES));
14+
15+
private static volatile Config config;
16+
17+
public static Config getAsyncHttpClientConfig() {
18+
if (config == null) {
19+
config = new Config();
20+
}
21+
22+
return config;
1423
}
15-
24+
1625
/**
17-
* This method invalidates the property caches. So if a system property has been changed and the
18-
* effect of this change is to be seen then call reloadProperties() and then getAsyncHttpClientConfig()
19-
* to get the new property values.
26+
* This method invalidates the property caches. So if a system property has
27+
* been changed and the effect of this change is to be seen then call
28+
* reloadProperties() and then getAsyncHttpClientConfig() to get the new
29+
* property values.
2030
*/
21-
public static void reloadProperties(){
22-
ConfigFactory.invalidateCaches();
31+
public static void reloadProperties() {
32+
if (config != null)
33+
config.reload();
2334
}
2435

36+
public static class Config {
37+
38+
public static final String DEFAULT_AHC_PROPERTIES = "ahc-default.properties";
39+
public static final String CUSTOM_AHC_PROPERTIES = "ahc.properties";
40+
41+
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);
44+
45+
public void reload() {
46+
customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES);
47+
propsCache.clear();
48+
}
49+
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+
}
66+
}
67+
} catch (IOException e) {
68+
throw new IllegalArgumentException("Can't parse file", e);
69+
}
70+
}
71+
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+
}
83+
84+
return value;
85+
}
86+
};
87+
88+
public String getString(String key) {
89+
return propsCache.computeIfAbsent(key, computer);
90+
}
91+
92+
public int getInt(String key) {
93+
return Integer.parseInt(getString(key));
94+
}
95+
96+
public boolean getBoolean(String key) {
97+
return Boolean.parseBoolean(getString(key));
98+
}
99+
}
25100
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
*/
1313
package org.asynchttpclient.extras.registry;
1414

15-
1615
import org.asynchttpclient.AsyncHttpClient;
1716
import org.asynchttpclient.util.AsyncPropertiesHelper;
1817

19-
import com.typesafe.config.ConfigException;
20-
2118
import java.security.AccessController;
2219
import java.security.PrivilegedActionException;
2320
import java.security.PrivilegedExceptionAction;
@@ -26,7 +23,7 @@ public class AsyncImplHelper {
2623

2724
public static final String ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY = "org.async.http.client.impl";
2825
public static final String ASYNC_HTTP_CLIENT_REGISTRY_SYSTEM_PROPERTY = "org.async.http.client.registry.impl";
29-
26+
3027
/*
3128
* Returns the class specified by either a system property or a properties
3229
* file as the class to instantiated for the AsyncHttpClient. Returns null
@@ -38,7 +35,7 @@ public static Class<AsyncHttpClient> getAsyncImplClass(String propertyName) {
3835
String asyncHttpClientImplClassName = AsyncPropertiesHelper.getAsyncHttpClientConfig().getString(propertyName);
3936
Class<AsyncHttpClient> asyncHttpClientImplClass = AsyncImplHelper.getClass(asyncHttpClientImplClassName);
4037
return asyncHttpClientImplClass;
41-
}catch(ConfigException configException) {
38+
} catch (Exception configException) {
4239
return null;
4340
}
4441
}
@@ -53,17 +50,16 @@ public Class<AsyncHttpClient> run() throws ClassNotFoundException {
5350
try {
5451
return (Class<AsyncHttpClient>) cl.loadClass(asyncImplClassName);
5552
} catch (ClassNotFoundException e) {
56-
AsyncHttpClientFactory.logger.info("Couldn't find class : " + asyncImplClassName
57-
+ " in thread context classpath " + "checking system class path next", e);
53+
AsyncHttpClientFactory.logger.info("Couldn't find class : " + asyncImplClassName + " in thread context classpath " + "checking system class path next",
54+
e);
5855
}
5956

6057
cl = ClassLoader.getSystemClassLoader();
6158
return (Class<AsyncHttpClient>) cl.loadClass(asyncImplClassName);
6259
}
6360
});
6461
} catch (PrivilegedActionException e) {
65-
throw new AsyncHttpClientImplException("Class : " + asyncImplClassName + " couldn't be found in " + " the classpath due to : "
66-
+ e.getMessage(), e);
62+
throw new AsyncHttpClientImplException("Class : " + asyncImplClassName + " couldn't be found in " + " the classpath due to : " + e.getMessage(), e);
6763
}
6864
}
6965
}

0 commit comments

Comments
 (0)