|
1 | 1 | package org.asynchttpclient.util;
|
2 | 2 |
|
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; |
5 | 12 |
|
6 | 13 | 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; |
14 | 23 | }
|
15 |
| - |
| 24 | + |
16 | 25 | /**
|
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. |
20 | 30 | */
|
21 |
| - public static void reloadProperties(){ |
22 |
| - ConfigFactory.invalidateCaches(); |
| 31 | + public static void reloadProperties() { |
| 32 | + if (config != null) |
| 33 | + config.reload(); |
23 | 34 | }
|
24 | 35 |
|
| 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 | + } |
25 | 100 | }
|
0 commit comments