Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
derTobsch committed Nov 11, 2024
1 parent 3ee13ce commit 2ccaca6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>JAXB</code> for
Expand All @@ -39,29 +40,14 @@ public Configuration unmarshallConfiguration(InputStream stream) {
}

try {
final JAXBContext ctx = createJAXBContext();
final Unmarshaller um = ctx.createUnmarshaller();
final Unmarshaller um = jaxbContext.createUnmarshaller();
@SuppressWarnings("unchecked") final JAXBElement<Configuration> jaxbElement = (JAXBElement<Configuration>) um.unmarshal(stream);
return jaxbElement.getValue();
} catch (JAXBException exception) {
throw new IllegalStateException("Cannot parse holidays XML file.", exception);
}
}

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.
*
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ 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
void testUnmarshallConfiguration() throws JAXBException {
final JAXBContext ctx = mock(JAXBContext.class);
final Unmarshaller unmarshaller = mock(Unmarshaller.class);
@SuppressWarnings("unchecked") final JAXBElement<Configuration> 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);
Expand Down

0 comments on commit 2ccaca6

Please sign in to comment.