From: Tomas Cere Date: Tue, 7 Apr 2015 13:49:33 +0000 (+0200) Subject: BUG 2954 : Thread local DocumentBuilder's for XmlUtil X-Git-Tag: release/beryllium~609^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=a2c8cbb97e688871f105220bef7af2f9b4f311e8 BUG 2954 : Thread local DocumentBuilder's for XmlUtil Increases performance by reusing cached DocumentBuilder instances. Change-Id: I5aab6b4caa960878b647e6868377d62dd6262cd9 Signed-off-by: Tomas Cere --- diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java index 6408fce5d3..65754e6b34 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java @@ -68,6 +68,22 @@ public final class XmlUtil { BUILDER_FACTORY = factory; } + private static final ThreadLocal DEFAULT_DOM_BUILDER = new ThreadLocal(){ + @Override + protected DocumentBuilder initialValue() { + try { + return BUILDER_FACTORY.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw new IllegalStateException("Failed to create threadLocal dom builder", e); + } + } + + @Override + public void set(DocumentBuilder value) { + throw new UnsupportedOperationException(); + } + }; + private XmlUtil() { throw new UnsupportedOperationException("Utility class"); } @@ -90,13 +106,7 @@ public final class XmlUtil { // along with XmlElement public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException { - DocumentBuilder dBuilder; - try { - dBuilder = BUILDER_FACTORY.newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw new IllegalStateException("Failed to parse XML document", e); - } - Document doc = dBuilder.parse(xmlContent); + Document doc = DEFAULT_DOM_BUILDER.get().parse(xmlContent); doc.getDocumentElement().normalize(); return doc; @@ -107,12 +117,7 @@ public final class XmlUtil { } public static Document newDocument() { - try { - DocumentBuilder builder = BUILDER_FACTORY.newDocumentBuilder(); - return builder.newDocument(); - } catch (ParserConfigurationException e) { - throw new IllegalStateException("Failed to create document", e); - } + return DEFAULT_DOM_BUILDER.get().newDocument(); } public static Element createElement(final Document document, final String qName, final Optional namespaceURI) {