BUG-2459: do not instantiate factories in fastpath 60/13560/2
authorRobert Varga <rovarga@cisco.com>
Thu, 11 Dec 2014 10:08:26 +0000 (11:08 +0100)
committerRobert Varga <nite@hq.sk>
Fri, 12 Dec 2014 12:40:21 +0000 (12:40 +0000)
This caches the factories in static fields rather than instantiating
them on each call.

Change-Id: I4dfee7724320914f2c7c9724192e4653deea3cde
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XMLNetconfUtil.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java

index eaaf320b023c454f5638a0f2138d262c312d0d1d..6569dba714437f1f254e481e0913085b11e9552a 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.controller.netconf.util.xml;
 
 
 package org.opendaylight.controller.netconf.util.xml;
 
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
@@ -15,14 +16,17 @@ import javax.xml.xpath.XPathFactory;
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 
 public final class XMLNetconfUtil {
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 
 public final class XMLNetconfUtil {
+    private static final XPathFactory FACTORY = XPathFactory.newInstance();
+    private static final NamespaceContext NS_CONTEXT = new HardcodedNamespaceResolver("netconf",
+        XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
 
 
-    private XMLNetconfUtil() {}
+    private XMLNetconfUtil() {
+        throw new UnsupportedOperationException("Utility class");
+    }
 
 
-    public static XPathExpression compileXPath(String xPath) {
-        final XPathFactory xPathfactory = XPathFactory.newInstance();
-        final XPath xpath = xPathfactory.newXPath();
-        xpath.setNamespaceContext(new HardcodedNamespaceResolver("netconf",
-                XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
+    public static XPathExpression compileXPath(final String xPath) {
+        final XPath xpath = FACTORY.newXPath();
+        xpath.setNamespaceContext(NS_CONTEXT);
         try {
             return xpath.compile(xPath);
         } catch (final XPathExpressionException e) {
         try {
             return xpath.compile(xPath);
         } catch (final XPathExpressionException e) {
index 68c4d9fdab62413378eb2332ada310df601bf48e..ee5b27b2e7c704fc2531899b41241cb10f8ab15d 100644 (file)
@@ -43,7 +43,9 @@ public final class XmlUtil {
 
     public static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
     public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
 
     public static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
     public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
-    private static final DocumentBuilderFactory BUILDERFACTORY;
+    private static final DocumentBuilderFactory BUILDER_FACTORY;
+    private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
+    private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
     static {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
     static {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -51,32 +53,34 @@ public final class XmlUtil {
         factory.setCoalescing(true);
         factory.setIgnoringElementContentWhitespace(true);
         factory.setIgnoringComments(true);
         factory.setCoalescing(true);
         factory.setIgnoringElementContentWhitespace(true);
         factory.setIgnoringComments(true);
-        BUILDERFACTORY = factory;
+        BUILDER_FACTORY = factory;
     }
 
     }
 
-    private XmlUtil() {}
+    private XmlUtil() {
+        throw new UnsupportedOperationException("Utility class");
+    }
 
 
-    public static Element readXmlToElement(String xmlContent) throws SAXException, IOException {
+    public static Element readXmlToElement(final String xmlContent) throws SAXException, IOException {
         Document doc = readXmlToDocument(xmlContent);
         return doc.getDocumentElement();
     }
 
         Document doc = readXmlToDocument(xmlContent);
         return doc.getDocumentElement();
     }
 
-    public static Element readXmlToElement(InputStream xmlContent) throws SAXException, IOException {
+    public static Element readXmlToElement(final InputStream xmlContent) throws SAXException, IOException {
         Document doc = readXmlToDocument(xmlContent);
         return doc.getDocumentElement();
     }
 
         Document doc = readXmlToDocument(xmlContent);
         return doc.getDocumentElement();
     }
 
-    public static Document readXmlToDocument(String xmlContent) throws SAXException, IOException {
+    public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
     }
 
     // TODO improve exceptions throwing
     // along with XmlElement
 
         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
     }
 
     // TODO improve exceptions throwing
     // along with XmlElement
 
-    public static Document readXmlToDocument(InputStream xmlContent) throws SAXException, IOException {
+    public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
         DocumentBuilder dBuilder;
         try {
         DocumentBuilder dBuilder;
         try {
-            dBuilder = BUILDERFACTORY.newDocumentBuilder();
+            dBuilder = BUILDER_FACTORY.newDocumentBuilder();
         } catch (ParserConfigurationException e) {
             throw new IllegalStateException("Failed to parse XML document", e);
         }
         } catch (ParserConfigurationException e) {
             throw new IllegalStateException("Failed to parse XML document", e);
         }
@@ -86,20 +90,20 @@ public final class XmlUtil {
         return doc;
     }
 
         return doc;
     }
 
-    public static Element readXmlToElement(File xmlFile) throws SAXException, IOException {
+    public static Element readXmlToElement(final File xmlFile) throws SAXException, IOException {
         return readXmlToDocument(new FileInputStream(xmlFile)).getDocumentElement();
     }
 
     public static Document newDocument() {
         try {
         return readXmlToDocument(new FileInputStream(xmlFile)).getDocumentElement();
     }
 
     public static Document newDocument() {
         try {
-            DocumentBuilder builder = BUILDERFACTORY.newDocumentBuilder();
+            DocumentBuilder builder = BUILDER_FACTORY.newDocumentBuilder();
             return builder.newDocument();
         } catch (ParserConfigurationException e) {
             throw new IllegalStateException("Failed to create document", e);
         }
     }
 
             return builder.newDocument();
         } catch (ParserConfigurationException e) {
             throw new IllegalStateException("Failed to create document", e);
         }
     }
 
-    public static Element createElement(final Document document, String qName, Optional<String> namespaceURI) {
+    public static Element createElement(final Document document, final String qName, final Optional<String> namespaceURI) {
         if(namespaceURI.isPresent()) {
             final Element element = document.createElementNS(namespaceURI.get(), qName);
             String name = XMLNS_ATTRIBUTE_KEY;
         if(namespaceURI.isPresent()) {
             final Element element = document.createElementNS(namespaceURI.get(), qName);
             String name = XMLNS_ATTRIBUTE_KEY;
@@ -112,20 +116,20 @@ public final class XmlUtil {
         return document.createElement(qName);
     }
 
         return document.createElement(qName);
     }
 
-    public static Element createTextElement(Document document, String qName, String content, Optional<String> namespaceURI) {
+    public static Element createTextElement(final Document document, final String qName, final String content, final Optional<String> namespaceURI) {
         Element typeElement = createElement(document, qName, namespaceURI);
         typeElement.appendChild(document.createTextNode(content));
         return typeElement;
     }
 
         Element typeElement = createElement(document, qName, namespaceURI);
         typeElement.appendChild(document.createTextNode(content));
         return typeElement;
     }
 
-    public static Element createTextElementWithNamespacedContent(Document document, String qName, String prefix,
-                                                                 String namespace, String contentWithoutPrefix) {
+    public static Element createTextElementWithNamespacedContent(final Document document, final String qName, final String prefix,
+                                                                 final String namespace, final String contentWithoutPrefix) {
 
         return createTextElementWithNamespacedContent(document, qName, prefix, namespace, contentWithoutPrefix, Optional.<String>absent());
     }
 
 
         return createTextElementWithNamespacedContent(document, qName, prefix, namespace, contentWithoutPrefix, Optional.<String>absent());
     }
 
-    public static Element createTextElementWithNamespacedContent(Document document, String qName, String prefix,
-                                                                 String namespace, String contentWithoutPrefix, Optional<String> namespaceURI) {
+    public static Element createTextElementWithNamespacedContent(final Document document, final String qName, final String prefix,
+                                                                 final String namespace, final String contentWithoutPrefix, final Optional<String> namespaceURI) {
 
         String content = createPrefixedValue(XmlNetconfConstants.PREFIX, contentWithoutPrefix);
         Element element = createTextElement(document, qName, content, namespaceURI);
 
         String content = createPrefixedValue(XmlNetconfConstants.PREFIX, contentWithoutPrefix);
         Element element = createTextElement(document, qName, content, namespaceURI);
@@ -134,25 +138,25 @@ public final class XmlUtil {
         return element;
     }
 
         return element;
     }
 
-    public static String createPrefixedValue(String prefix, String value) {
+    public static String createPrefixedValue(final String prefix, final String value) {
         return prefix + ":" + value;
     }
 
         return prefix + ":" + value;
     }
 
-    public static String toString(Document document) {
+    public static String toString(final Document document) {
         return toString(document.getDocumentElement());
     }
 
         return toString(document.getDocumentElement());
     }
 
-    public static String toString(Element xml) {
+    public static String toString(final Element xml) {
         return toString(xml, false);
     }
 
         return toString(xml, false);
     }
 
-    public static String toString(XmlElement xmlElement) {
+    public static String toString(final XmlElement xmlElement) {
         return toString(xmlElement.getDomElement(), false);
     }
 
         return toString(xmlElement.getDomElement(), false);
     }
 
-    public static String toString(Element xml, boolean addXmlDeclaration) {
+    public static String toString(final Element xml, final boolean addXmlDeclaration) {
         try {
         try {
-            Transformer transformer = TransformerFactory.newInstance().newTransformer();
+            Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration ? "no" : "yes");
 
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration ? "no" : "yes");
 
@@ -166,26 +170,25 @@ public final class XmlUtil {
         }
     }
 
         }
     }
 
-    public static String toString(Document doc, boolean addXmlDeclaration) {
+    public static String toString(final Document doc, final boolean addXmlDeclaration) {
         return toString(doc.getDocumentElement(), addXmlDeclaration);
     }
 
         return toString(doc.getDocumentElement(), addXmlDeclaration);
     }
 
-    public static Schema loadSchema(InputStream... fromStreams) {
+    public static Schema loadSchema(final InputStream... fromStreams) {
         Source[] sources = new Source[fromStreams.length];
         int i = 0;
         for (InputStream stream : fromStreams) {
             sources[i++] = new StreamSource(stream);
         }
 
         Source[] sources = new Source[fromStreams.length];
         int i = 0;
         for (InputStream stream : fromStreams) {
             sources[i++] = new StreamSource(stream);
         }
 
-        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
         try {
         try {
-            return schemaFactory.newSchema(sources);
+            return SCHEMA_FACTORY.newSchema(sources);
         } catch (SAXException e) {
             throw new IllegalStateException("Failed to instantiate XML schema", e);
         }
     }
 
         } catch (SAXException e) {
             throw new IllegalStateException("Failed to instantiate XML schema", e);
         }
     }
 
-    public static Object evaluateXPath(XPathExpression expr, Object rootNode, QName returnType) {
+    public static Object evaluateXPath(final XPathExpression expr, final Object rootNode, final QName returnType) {
         try {
             return expr.evaluate(rootNode, returnType);
         } catch (XPathExpressionException e) {
         try {
             return expr.evaluate(rootNode, returnType);
         } catch (XPathExpressionException e) {
@@ -193,7 +196,7 @@ public final class XmlUtil {
         }
     }
 
         }
     }
 
-    public static Document createDocumentCopy(Document original) {
+    public static Document createDocumentCopy(final Document original) {
         final Document copiedDocument = newDocument();
         final Node copiedRoot = copiedDocument.importNode(original.getDocumentElement(), true);
         copiedDocument.appendChild(copiedRoot);
         final Document copiedDocument = newDocument();
         final Node copiedRoot = copiedDocument.importNode(original.getDocumentElement(), true);
         copiedDocument.appendChild(copiedRoot);