X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-persister-file-xml-adapter%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fpersist%2Fstorage%2Ffile%2Fxml%2Fmodel%2FConfig.java;h=e043e45feb94add6c36a0126f13a0b4ecf4b7690;hp=e629d20db52e0c2c65bf26c76aeb1e2dbce62eff;hb=f43b01b81319959b1907e3e04537f5169e7f33d8;hpb=b3f7fb20f6d550c574d1682a5eb1b39b675316d0 diff --git a/opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/Config.java b/opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/Config.java index e629d20db5..e043e45feb 100644 --- a/opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/Config.java +++ b/opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/Config.java @@ -7,13 +7,13 @@ */ package org.opendaylight.controller.config.persist.storage.file.xml.model; -import com.google.common.base.Charsets; import com.google.common.base.Optional; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.io.Files; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -22,6 +22,10 @@ import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.stream.StreamSource; import org.apache.commons.lang3.StringUtils; @XmlRootElement(name = "persisted-snapshots") @@ -29,7 +33,7 @@ public final class Config { private List snapshots; - Config(List snapshots) { + Config(final List snapshots) { this.snapshots = snapshots; } @@ -43,11 +47,11 @@ public final class Config { return snapshots; } - public void setSnapshots(List snapshots) { + public void setSnapshots(final List snapshots) { this.snapshots = snapshots; } - public void toXml(File to) { + public void toXml(final File to) { try { // TODO Moxy has to be used instead of default jaxb impl due to a bug @@ -59,12 +63,12 @@ public final class Config { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(this, to); - } catch (JAXBException e) { + } catch (final JAXBException e) { throw new PersistException("Unable to persist configuration", e); } } - public static Config fromXml(File from) { + public static Config fromXml(final File from) { if(isEmpty(from)) { return new Config(); } @@ -72,21 +76,24 @@ public final class Config { try { JAXBContext jaxbContext = JAXBContext.newInstance(Config.class); Unmarshaller um = jaxbContext.createUnmarshaller(); - - return (Config) um.unmarshal(from); - } catch (JAXBException e) { + XMLInputFactory xif = XMLInputFactory.newFactory(); + xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); + xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); + XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(from)); + return (Config) um.unmarshal(xsr); + } catch (JAXBException | XMLStreamException e) { throw new PersistException("Unable to restore configuration", e); } } - private static boolean isEmpty(File from) { + private static boolean isEmpty(final File from) { return from.length() == 0 || isBlank(from); } - private static boolean isBlank(File from) { + private static boolean isBlank(final File from) { try { - return StringUtils.isBlank(Files.toString(from, Charsets.UTF_8)); - } catch (IOException e) { + return StringUtils.isBlank(Files.toString(from, StandardCharsets.UTF_8)); + } catch (final IOException e) { throw new IllegalStateException("Unexpected error reading file" + from, e); } } @@ -96,14 +103,14 @@ public final class Config { return last == null ? Optional.absent() : Optional.of(last); } - public void addConfigSnapshot(ConfigSnapshot snap, int numberOfStoredBackups) { - if(shouldReplaceLast(numberOfStoredBackups) && snapshots.isEmpty() == false) { + public void addConfigSnapshot(final ConfigSnapshot snap, final int numberOfStoredBackups) { + if (shouldReplaceLast(numberOfStoredBackups) && !snapshots.isEmpty()) { snapshots.remove(0); } snapshots.add(snap); } - private boolean shouldReplaceLast(int numberOfStoredBackups) { + private boolean shouldReplaceLast(final int numberOfStoredBackups) { return numberOfStoredBackups == snapshots.size(); } }