Skip to content

Commit

Permalink
Align classloader used to create the JAXBContext
Browse files Browse the repository at this point in the history
This commit makes sure that JAXBContext.newInstance consistently use
the target class classloader to detect the necessary resources.

Previously, the current thread's context classloader was used, which
could lead to not finding the required JAXB components.

Closes gh-33158
  • Loading branch information
snicoll committed Jul 12, 2024
1 parent f8875ea commit 0f3f979
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,7 +51,7 @@ public Unmarshaller createUnmarshaller(Class<?> clazz) throws CodecException, JA
private JAXBContext getJaxbContext(Class<?> clazz) throws CodecException {
return this.jaxbContexts.computeIfAbsent(clazz, key -> {
try {
return JAXBContext.newInstance(clazz);
return createJaxbContext(clazz);
}
catch (JAXBException ex) {
throw new CodecException(
Expand All @@ -60,4 +60,20 @@ private JAXBContext getJaxbContext(Class<?> clazz) throws CodecException {
});
}

/**
* Create a {@link JAXBContext} for the given type, exposing the class
* ClassLoader as current thread context ClassLoader for the time of
* creating the context.
*/
private JAXBContext createJaxbContext(Class<?> clazz) throws JAXBException {
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(clazz.getClassLoader());
return JAXBContext.newInstance(clazz);
}
finally {
Thread.currentThread().setContextClassLoader(currentClassLoader);
}
}

}

0 comments on commit 0f3f979

Please sign in to comment.