Use copy on write in JAXB store implementations

This commit is contained in:
Rene Pfeuffer
2019-12-04 17:16:17 +01:00
parent 3e3ab69b14
commit c4ed6f917d
2 changed files with 34 additions and 32 deletions

View File

@@ -317,8 +317,9 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
{ {
logger.debug("store configuration to {}", file); logger.debug("store configuration to {}", file);
try (IndentXMLStreamWriter writer = XmlStreams.createWriter(file)) CopyOnWrite.withTemporaryFile(
{ temp -> {
try (IndentXMLStreamWriter writer = XmlStreams.createWriter(temp)) {
writer.writeStartDocument(); writer.writeStartDocument();
// configuration start // configuration start
@@ -328,8 +329,7 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
for (Entry<String, V> e : entries.entrySet()) for (Entry<String, V> e : entries.entrySet()) {
{
// entry start // entry start
writer.writeStartElement(TAG_ENTRY); writer.writeStartElement(TAG_ENTRY);
@@ -342,7 +342,7 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
writer.writeEndElement(); writer.writeEndElement();
// value // value
JAXBElement<V> je = new JAXBElement<V>(QName.valueOf(TAG_VALUE), type, JAXBElement<V> je = new JAXBElement<>(QName.valueOf(TAG_VALUE), type,
e.getValue()); e.getValue());
m.marshal(je, writer); m.marshal(je, writer);
@@ -355,10 +355,9 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
writer.writeEndElement(); writer.writeEndElement();
writer.writeEndDocument(); writer.writeEndDocument();
} }
catch (Exception ex) },
{ file.toPath()
throw new StoreException("could not store configuration", ex); );
}
} }
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------

View File

@@ -113,7 +113,10 @@ public class JAXBConfigurationStore<T> extends AbstractStore<T> {
Marshaller marshaller = context.createMarshaller(); Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(object, configFile); CopyOnWrite.withTemporaryFile(
temp -> marshaller.marshal(object, temp.toFile()),
configFile.toPath()
);
} }
catch (JAXBException ex) { catch (JAXBException ex) {
throw new StoreException("failed to marshall object", ex); throw new StoreException("failed to marshall object", ex);