fix duplicate ClassLoader logic

This commit is contained in:
Sebastian Sdorra
2020-04-28 14:41:31 +02:00
parent aa23f6f7ab
commit 1a367b2657

View File

@@ -67,25 +67,15 @@ final class TypedStoreContext<T> {
void withMarshaller(ThrowingConsumer<Marshaller> consumer) { void withMarshaller(ThrowingConsumer<Marshaller> consumer) {
Marshaller marshaller = createMarshaller(); Marshaller marshaller = createMarshaller();
ClassLoader contextClassLoader = null; withClassLoader(consumer, marshaller);
Optional<ClassLoader> classLoader = parameters.getClassLoader();
if (classLoader.isPresent()) {
contextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader.get());
}
try {
consumer.consume(marshaller);
} catch (Exception e) {
throw new StoreException("failure during work with marshaller", e);
} finally {
if (contextClassLoader != null) {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
} }
void withUnmarshaller(ThrowingConsumer<Unmarshaller> consumer) { void withUnmarshaller(ThrowingConsumer<Unmarshaller> consumer) {
Unmarshaller unmarshaller = createUnmarshaller(); Unmarshaller unmarshaller = createUnmarshaller();
withClassLoader(consumer, unmarshaller);
}
private <C> void withClassLoader(ThrowingConsumer<C> consumer, C consume) {
ClassLoader contextClassLoader = null; ClassLoader contextClassLoader = null;
Optional<ClassLoader> classLoader = parameters.getClassLoader(); Optional<ClassLoader> classLoader = parameters.getClassLoader();
if (classLoader.isPresent()) { if (classLoader.isPresent()) {
@@ -93,9 +83,9 @@ final class TypedStoreContext<T> {
Thread.currentThread().setContextClassLoader(classLoader.get()); Thread.currentThread().setContextClassLoader(classLoader.get());
} }
try { try {
consumer.consume(unmarshaller); consumer.consume(consume);
} catch (Exception e) { } catch (Exception e) {
throw new StoreException("failure during work with unmarshaller", e); throw new StoreException("failure during marshalling/unmarshalling", e);
} finally { } finally {
if (contextClassLoader != null) { if (contextClassLoader != null) {
Thread.currentThread().setContextClassLoader(contextClassLoader); Thread.currentThread().setContextClassLoader(contextClassLoader);