From: Robert Varga Date: Thu, 12 Sep 2019 15:33:59 +0000 (+0200) Subject: Optimize anyxml output X-Git-Tag: release/magnesium~99 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=c4b4b98f56ab1d9821edb737912632c2867ae783;hp=78aefba8c580123d0763b483fab81d2a993e32eb Optimize anyxml output We do not need to instantiate a new factory every time we perform encoding, keep the factory as a shared constant. Change-Id: Ideff56076a1516784cd82f67438e5b740c4d3eeb Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractLithiumDataOutput.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractLithiumDataOutput.java index e8b7547a1e..2320775d5f 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractLithiumDataOutput.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractLithiumDataOutput.java @@ -28,7 +28,6 @@ import java.util.Optional; import java.util.Set; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; -import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.eclipse.jdt.annotation.NonNull; @@ -56,6 +55,7 @@ import org.slf4j.LoggerFactory; */ abstract class AbstractLithiumDataOutput extends AbstractNormalizedNodeDataOutput { private static final Logger LOG = LoggerFactory.getLogger(AbstractLithiumDataOutput.class); + private static final TransformerFactory TF = TransformerFactory.newInstance(); private static final ImmutableMap, Byte> KNOWN_TYPES = ImmutableMap., Byte>builder() .put(String.class, ValueTypes.STRING_TYPE) .put(Byte.class, ValueTypes.BYTE_TYPE) @@ -191,13 +191,13 @@ abstract class AbstractLithiumDataOutput extends AbstractNormalizedNodeDataOutpu @Override public final void domSourceValue(final DOMSource value) throws IOException { + final StringWriter writer = new StringWriter(); try { - StreamResult xmlOutput = new StreamResult(new StringWriter()); - TransformerFactory.newInstance().newTransformer().transform(value, xmlOutput); - writeObject(xmlOutput.getWriter().toString()); - } catch (TransformerException | TransformerFactoryConfigurationError e) { + TF.newTransformer().transform(value, new StreamResult(writer)); + } catch (TransformerException e) { throw new IOException("Error writing anyXml", e); } + writeObject(writer.toString()); } @Override