Skip to content

Commit

Permalink
Handle the situation when context class loader is null (#490)
Browse files Browse the repository at this point in the history
This fixes the errors when trying to load classpath references, like:

    Caused by: java.lang.NullPointerException
            at com.networknt.schema.uri.ClasspathURLStreamHandler$ClassPathURLConnection.getResourceAsStream(ClasspathURLStreamHandler.java:83) ~[amcrm-1.0-SNAPSHOT.jar:?]
            at com.networknt.schema.uri.ClasspathURLStreamHandler$ClassPathURLConnection.getInputStream(ClasspathURLStreamHandler.java:69) ~[amcrm-1.0-SNAPSHOT.jar:?]
            at java.net.URL.openStream(URL.java:1165) ~[?:?]
            at com.networknt.schema.uri.ClasspathURLFetcher.fetch(ClasspathURLFetcher.java:39) ~[amcrm-1.0-SNAPSHOT.jar:?]
            at com.networknt.schema.uri.URISchemeFetcher.fetch(URISchemeFetcher.java:50) ~[amcrm-1.0-SNAPSHOT.jar:?]
            at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:351) ~[amcrm-1.0-SNAPSHOT.jar:?]
            at com.networknt.schema.RefValidator.getRefSchema(RefValidator.java:82) ~[amcrm-1.0-SNAPSHOT.jar:?]
            at com.networknt.schema.RefValidator.<init>(RefValidator.java:45) ~[amcrm-1.0-SNAPSHOT.jar:?]

Co-authored-by: Viacheslav Tykhanovskyi <viacheslav.tykhanovskyi@multisafepay.com>
  • Loading branch information
vti and Viacheslav Tykhanovskyi authored Dec 6, 2021
1 parent 4ac0fbd commit 30d4b4b
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ private InputStream getResourceAsStream(URL pURL) throws IOException {
path = path.substring(1);
}

InputStream stream;
InputStream stream = null;
if (mHost != null) {
stream = mHost.getClassLoader().getResourceAsStream(path);
} else {
stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
stream = contextClassLoader.getResourceAsStream(path);
}
if (stream == null) {
stream = getClass().getClassLoader().getResourceAsStream(path);
}
Expand All @@ -96,4 +99,4 @@ private InputStream getResourceAsStream(URL pURL) throws IOException {
}


}
}

0 comments on commit 30d4b4b

Please sign in to comment.