Merge branch '6.1.x'

This commit is contained in:
Stéphane Nicoll 2024-07-12 12:02:45 +02:00
commit 348764620d
1 changed files with 18 additions and 2 deletions

View File

@ -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.
@ -51,7 +51,7 @@ final class JaxbContextContainer {
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(
@ -60,4 +60,20 @@ final class JaxbContextContainer {
});
}
/**
* 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);
}
}
}