cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [cxf-fediz] branch master updated: Switching static DocumentBuilderFactory to use a ThreadLocal instead
Date Tue, 22 May 2018 13:25:49 GMT
This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git


The following commit(s) were added to refs/heads/master by this push:
     new 62b61ec  Switching static DocumentBuilderFactory to use a ThreadLocal instead
62b61ec is described below

commit 62b61ec06bd5199a2e2712261199e513fd8db52a
Author: Colm O hEigeartaigh <coheigea@apache.org>
AuthorDate: Tue May 22 14:25:23 2018 +0100

    Switching static DocumentBuilderFactory to use a ThreadLocal instead
---
 .../org/apache/cxf/fediz/core/util/DOMUtils.java   | 60 ++++++++++------------
 1 file changed, 27 insertions(+), 33 deletions(-)

diff --git a/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java b/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java
index 9c533fc..fc43aef 100644
--- a/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java
+++ b/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java
@@ -65,23 +65,7 @@ public final class DOMUtils {
 
     private static final String XMLNAMESPACE = "xmlns";
 
-    private static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
-
-    static {
-        try {
-            DBF.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
-            DBF.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
-
-            DBF.setValidating(false);
-            DBF.setIgnoringComments(false);
-            DBF.setIgnoringElementContentWhitespace(true);
-            DBF.setNamespaceAware(true);
-            // DBF.setCoalescing(true);
-            // DBF.setExpandEntityReferences(true);
-        } catch (ParserConfigurationException ex) {
-            LOG.error("Error configuring DocumentBuilderFactory", ex);
-        }
-    }
+    private static final ThreadLocal<DocumentBuilder> DOC_BUILDER_TL = new MyThreadLocal();
 
     private DOMUtils() {
     }
@@ -428,14 +412,12 @@ public final class DOMUtils {
      */
     public static Document readXml(InputStream is) throws SAXException, IOException,
         ParserConfigurationException {
-        DocumentBuilder db = DBF.newDocumentBuilder();
-        return db.parse(is);
+        return createDocumentBuilder().parse(is);
     }
 
     public static Document readXml(Reader is) throws SAXException, IOException, ParserConfigurationException
{
         InputSource ips = new InputSource(is);
-        DocumentBuilder db = DBF.newDocumentBuilder();
-        return db.parse(ips);
+        return createDocumentBuilder().parse(ips);
     }
 
     public static Document readXml(StreamSource is) throws SAXException, IOException,
@@ -445,8 +427,7 @@ public final class DOMUtils {
         is2.setByteStream(is.getInputStream());
         is2.setCharacterStream(is.getReader());
 
-        DocumentBuilder db = DBF.newDocumentBuilder();
-        return db.parse(is2);
+        return createDocumentBuilder().parse(is2);
     }
 
     public static void writeXml(Node n, OutputStream os) throws TransformerException {
@@ -459,19 +440,13 @@ public final class DOMUtils {
     }
 
     public static DocumentBuilder createDocumentBuilder() {
-        try {
-            return DBF.newDocumentBuilder();
-        } catch (ParserConfigurationException e) {
-            throw new RuntimeException("Couldn't find a DOM parser.", e);
-        }
+        DocumentBuilder documentBuilder = DOC_BUILDER_TL.get();
+        documentBuilder.reset();
+        return documentBuilder;
     }
 
     public static Document createDocument() {
-        try {
-            return DBF.newDocumentBuilder().newDocument();
-        } catch (ParserConfigurationException e) {
-            throw new RuntimeException("Couldn't find a DOM parser.", e);
-        }
+        return createDocumentBuilder().newDocument();
     }
 
     public static String getPrefixRecursive(Element el, String ns) {
@@ -658,4 +633,23 @@ public final class DOMUtils {
     public static void addNamespacePrefix(Element element, String namespaceUri, String prefix)
{
         element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespaceUri);
     }
+
+    private static final class MyThreadLocal extends ThreadLocal<DocumentBuilder> {
+
+        @Override
+        protected DocumentBuilder initialValue() {
+            try {
+                DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
+                dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
+                dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",
true);
+                dfactory.setValidating(false);
+                dfactory.setNamespaceAware(true);
+
+                return dfactory.newDocumentBuilder();
+            } catch (ParserConfigurationException e) {
+                LOG.error("Error configuring DocumentBuilderFactory", e);
+                throw new RuntimeException(e);
+            }
+        }
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
coheigea@apache.org.

Mime
View raw message