|
1 | 1 | package org.asynchttpclient.util;
|
2 | 2 |
|
3 |
| -import java.io.BufferedReader; |
4 | 3 | import java.io.IOException;
|
5 | 4 | 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; |
10 | 6 |
|
11 | 7 | import org.asynchttpclient.chmv8.ConcurrentHashMapV8;
|
12 | 8 |
|
@@ -39,60 +35,48 @@ public static class Config {
|
39 | 35 | public static final String CUSTOM_AHC_PROPERTIES = "ahc.properties";
|
40 | 36 |
|
41 | 37 | 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); |
44 | 40 |
|
45 | 41 | public void reload() {
|
46 | 42 | customProperties = parsePropertiesFile(CUSTOM_AHC_PROPERTIES);
|
47 | 43 | propsCache.clear();
|
48 | 44 | }
|
49 | 45 |
|
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); |
66 | 51 | }
|
67 | 52 | } catch (IOException e) {
|
68 | 53 | throw new IllegalArgumentException("Can't parse file", e);
|
69 | 54 | }
|
| 55 | + return props; |
70 | 56 | }
|
71 | 57 |
|
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>() { |
83 | 60 |
|
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 | + } |
87 | 70 |
|
88 |
| - public String getString(String key) { |
89 |
| - return propsCache.computeIfAbsent(key, computer); |
| 71 | + return value; |
| 72 | + } |
| 73 | + }); |
90 | 74 | }
|
91 |
| - |
| 75 | + |
92 | 76 | public int getInt(String key) {
|
93 | 77 | return Integer.parseInt(getString(key));
|
94 | 78 | }
|
95 |
| - |
| 79 | + |
96 | 80 | public boolean getBoolean(String key) {
|
97 | 81 | return Boolean.parseBoolean(getString(key));
|
98 | 82 | }
|
|
0 commit comments