Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5af18b4

Browse files
committed
Scan all non null ClassLoaders for config file,closeAsyncHttpClient#1345
Motivation:Class::getClassLoader can return null if the Class was loaded by thebootstrap ClassLoader.Modification:Scan all non null ClassLoaders.Result:No more NPE when class was loaded by the bootstrap ClassLoader
1 parent874756c commit5af18b4

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

‎client/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigHelper.java‎

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
importjava.io.IOException;
44
importjava.io.InputStream;
5+
importjava.util.ArrayList;
6+
importjava.util.List;
57
importjava.util.Properties;
68
importjava.util.concurrent.ConcurrentHashMap;
79

@@ -18,10 +20,8 @@ public static Config getAsyncHttpClientConfig() {
1820
}
1921

2022
/**
21-
* This method invalidates the property caches. So if a system property has
22-
* been changed and the effect of this change is to be seen then call
23-
* reloadProperties() and then getAsyncHttpClientConfig() to get the new
24-
* property values.
23+
* This method invalidates the property caches. So if a system property has been changed and the effect of this change is to be seen then call reloadProperties() and then
24+
* getAsyncHttpClientConfig() to get the new property values.
2525
*/
2626
publicstaticvoidreloadProperties() {
2727
if (config !=null)
@@ -34,30 +34,50 @@ public static class Config {
3434
publicstaticfinalStringCUSTOM_AHC_PROPERTIES ="ahc.properties";
3535

3636
privatefinalConcurrentHashMap<String,String>propsCache =newConcurrentHashMap<>();
37-
privatefinalPropertiesdefaultProperties =parsePropertiesFile(DEFAULT_AHC_PROPERTIES);
38-
privatevolatilePropertiescustomProperties =parsePropertiesFile(CUSTOM_AHC_PROPERTIES);
37+
privatefinalPropertiesdefaultProperties =parsePropertiesFile(DEFAULT_AHC_PROPERTIES,true);
38+
privatevolatilePropertiescustomProperties =parsePropertiesFile(CUSTOM_AHC_PROPERTIES,false);
3939

4040
publicvoidreload() {
41-
customProperties =parsePropertiesFile(CUSTOM_AHC_PROPERTIES);
41+
customProperties =parsePropertiesFile(CUSTOM_AHC_PROPERTIES,false);
4242
propsCache.clear();
4343
}
4444

45-
privatePropertiesparsePropertiesFile(Stringfile) {
45+
privatePropertiesparsePropertiesFile(Stringfile,booleanrequired) {
4646
Propertiesprops =newProperties();
47-
try (InputStreamis =Thread.currentThread().getContextClassLoader().getResourceAsStream(file)) {
47+
48+
List<ClassLoader>cls =newArrayList<>();
49+
50+
ClassLoadercl =Thread.currentThread().getContextClassLoader();
51+
if (cl !=null) {
52+
cls.add(cl);
53+
}
54+
cl =getClass().getClassLoader();
55+
if (cl !=null) {
56+
cls.add(cl);
57+
}
58+
cl =ClassLoader.getSystemClassLoader();
59+
if (cl !=null) {
60+
cls.add(cl);
61+
}
62+
63+
InputStreamis =null;
64+
for (ClassLoaderclassLoader :cls) {
65+
is =classLoader.getResourceAsStream(file);
4866
if (is !=null) {
67+
break;
68+
}
69+
}
70+
71+
if (is !=null) {
72+
try {
4973
props.load(is);
50-
}else {
51-
//Try loading from this class classloader instead, e.g. for OSGi environments.
52-
try(InputStreamis2 =this.getClass().getClassLoader().getResourceAsStream(file)) {
53-
if (is2 !=null) {
54-
props.load(is2);
55-
}
56-
}
74+
}catch (IOExceptione) {
75+
thrownewIllegalArgumentException("Can't parse config file " +file,e);
5776
}
58-
}catch (IOExceptione) {
59-
thrownewIllegalArgumentException("Can'tparsefile",e);
77+
}elseif (required) {
78+
thrownewIllegalArgumentException("Can'tlocate configfile " +file);
6079
}
80+
6181
returnprops;
6282
}
6383

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp