diff --git a/jollyday-jaxb/src/main/java/de/focus_shift/jollyday/jaxb/JaxbConfigurationService.java b/jollyday-jaxb/src/main/java/de/focus_shift/jollyday/jaxb/JaxbConfigurationService.java
index 157b69d7a..048200dcb 100644
--- a/jollyday-jaxb/src/main/java/de/focus_shift/jollyday/jaxb/JaxbConfigurationService.java
+++ b/jollyday-jaxb/src/main/java/de/focus_shift/jollyday/jaxb/JaxbConfigurationService.java
@@ -10,7 +10,7 @@
public class JaxbConfigurationService implements ConfigurationService {
- private final XMLUtil xmlUtil = new XMLUtil();
+ private static final XMLUtil xmlUtil = new XMLUtil();
@Override
public Configuration getConfiguration(ManagerParameter parameter) {
diff --git a/jollyday-jaxb/src/main/java/de/focus_shift/jollyday/jaxb/XMLUtil.java b/jollyday-jaxb/src/main/java/de/focus_shift/jollyday/jaxb/XMLUtil.java
index 9ee20dde9..01f8d8192 100644
--- a/jollyday-jaxb/src/main/java/de/focus_shift/jollyday/jaxb/XMLUtil.java
+++ b/jollyday-jaxb/src/main/java/de/focus_shift/jollyday/jaxb/XMLUtil.java
@@ -24,7 +24,8 @@ public class XMLUtil {
private static final Logger LOG = LoggerFactory.getLogger(XMLUtil.class);
- private JAXBContextCreator contextCreator = new JAXBContextCreator();
+ private static JAXBContextCreator contextCreator = new JAXBContextCreator();
+ private static final JAXBContext jaxbContext = createJAXBContext(contextCreator);
/**
* Unmarshalls the configuration from the stream. Uses JAXB
for
@@ -39,8 +40,7 @@ public Configuration unmarshallConfiguration(InputStream stream) {
}
try {
- final JAXBContext ctx = createJAXBContext();
- final Unmarshaller um = ctx.createUnmarshaller();
+ final Unmarshaller um = jaxbContext.createUnmarshaller();
@SuppressWarnings("unchecked") final JAXBElement jaxbElement = (JAXBElement) um.unmarshal(stream);
return jaxbElement.getValue();
} catch (JAXBException exception) {
@@ -48,20 +48,6 @@ public Configuration unmarshallConfiguration(InputStream stream) {
}
}
- private JAXBContext createJAXBContext() throws JAXBException {
- JAXBContext ctx = null;
- try {
- ctx = contextCreator.create(XMLUtil.PACKAGE, ClassLoadingUtil.getClassloader());
- } catch (JAXBException e) {
- LOG.warn("Could not create JAXB context using the current threads context classloader. Falling back to ObjectFactory class classloader.");
- }
-
- if (ctx == null) {
- ctx = contextCreator.create(XMLUtil.PACKAGE, ObjectFactory.class.getClassLoader());
- }
- return ctx;
- }
-
/**
* Returns the {@link DayOfWeek} equivalent for the given weekday.
*
@@ -87,4 +73,23 @@ public JAXBContext create(String packageName, ClassLoader classLoader) throws JA
return JAXBContext.newInstance(packageName, classLoader);
}
}
+
+ private static JAXBContext createJAXBContext(final JAXBContextCreator contextCreator) {
+ JAXBContext ctx = null;
+ try {
+ ctx = contextCreator.create(XMLUtil.PACKAGE, ClassLoadingUtil.getClassloader());
+ } catch (JAXBException e) {
+ LOG.warn("Could not create JAXB context using the current classloader from ClassLoadingUtil. Falling back to ObjectFactory class classloader.");
+ }
+
+ if (ctx == null) {
+ try {
+ ctx = contextCreator.create(XMLUtil.PACKAGE, ObjectFactory.class.getClassLoader());
+ } catch (JAXBException exception) {
+ throw new IllegalStateException("Could not create JAXB context using ObjectFactory classloader.", exception);
+ }
+ }
+
+ return ctx;
+ }
}
diff --git a/jollyday-jaxb/src/test/java/de/focus_shift/jollyday/jaxb/test/XMLUtilTest.java b/jollyday-jaxb/src/test/java/de/focus_shift/jollyday/jaxb/test/XMLUtilTest.java
index 978802a7d..ac587e78d 100644
--- a/jollyday-jaxb/src/test/java/de/focus_shift/jollyday/jaxb/test/XMLUtilTest.java
+++ b/jollyday-jaxb/src/test/java/de/focus_shift/jollyday/jaxb/test/XMLUtilTest.java
@@ -41,9 +41,9 @@ void testUnmarshallConfigurationNullCheck() {
@Test
void testUnmarshallConfigurationException() throws IOException, JAXBException {
- when(contextCreator.create(eq("de.focus_shift.jollyday.jaxb.mapping"), any(ClassLoader.class))).thenThrow(new JAXBException("")).thenThrow(new JAXBException(""));
+ when(contextCreator.create(eq("de.focus_shift.jollyday.jaxb.mapping"), any(ClassLoader.class))).thenThrow(new JAXBException(""));
assertThatThrownBy(() -> xmlUtil.unmarshallConfiguration(inputStream)).isInstanceOf(IllegalStateException.class);
- verify(inputStream, never()).close();
+ verify(inputStream).close();
}
@Test
@@ -51,7 +51,7 @@ void testUnmarshallConfiguration() throws JAXBException {
final JAXBContext ctx = mock(JAXBContext.class);
final Unmarshaller unmarshaller = mock(Unmarshaller.class);
@SuppressWarnings("unchecked") final JAXBElement element = mock(JAXBElement.class);
- when(contextCreator.create(eq("de.focus_shift.jollyday.jaxb.mapping"), any(ClassLoader.class))).thenReturn(null).thenReturn(ctx);
+ when(contextCreator.create(eq("de.focus_shift.jollyday.jaxb.mapping"), any(ClassLoader.class))).thenReturn(ctx);
when(ctx.createUnmarshaller()).thenReturn(unmarshaller);
when(unmarshaller.unmarshal(inputStream)).thenReturn(element);
xmlUtil.unmarshallConfiguration(inputStream);