cxf-fediz git commit: Modify DOMUtils just to use a static DocumentBuilderFactory

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

cxf-fediz git commit: Modify DOMUtils just to use a static DocumentBuilderFactory

coheigea
Administrator
Repository: cxf-fediz
Updated Branches:
  refs/heads/master 138052b5f -> 72fb626f8


Modify DOMUtils just to use a static DocumentBuilderFactory


Project: http://git-wip-us.apache.org/repos/asf/cxf-fediz/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf-fediz/commit/72fb626f
Tree: http://git-wip-us.apache.org/repos/asf/cxf-fediz/tree/72fb626f
Diff: http://git-wip-us.apache.org/repos/asf/cxf-fediz/diff/72fb626f

Branch: refs/heads/master
Commit: 72fb626f8f9a9dd8d4c6d35c78cf5f48d2555c33
Parents: 138052b
Author: Colm O hEigeartaigh <[hidden email]>
Authored: Fri May 19 11:05:17 2017 +0100
Committer: Colm O hEigeartaigh <[hidden email]>
Committed: Fri May 19 11:05:17 2017 +0100

----------------------------------------------------------------------
 .../core/processor/FederationProcessorImpl.java |  2 +-
 .../apache/cxf/fediz/core/util/DOMUtils.java    | 66 ++++++++------------
 2 files changed, 26 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/72fb626f/plugins/core/src/main/java/org/apache/cxf/fediz/core/processor/FederationProcessorImpl.java
----------------------------------------------------------------------
diff --git a/plugins/core/src/main/java/org/apache/cxf/fediz/core/processor/FederationProcessorImpl.java b/plugins/core/src/main/java/org/apache/cxf/fediz/core/processor/FederationProcessorImpl.java
index 4bd5741..5fc4893 100644
--- a/plugins/core/src/main/java/org/apache/cxf/fediz/core/processor/FederationProcessorImpl.java
+++ b/plugins/core/src/main/java/org/apache/cxf/fediz/core/processor/FederationProcessorImpl.java
@@ -126,7 +126,7 @@ public class FederationProcessorImpl extends AbstractFedizProcessor {
             el = doc.getDocumentElement();
 
         } catch (Exception e) {
-            LOG.warn("Failed to parse wresult: " + e.getMessage());
+            LOG.warn("Failed to parse wresult: " + e.getMessage(), e);
             throw new ProcessingException(TYPE.INVALID_REQUEST);
         }
 

http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/72fb626f/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java
----------------------------------------------------------------------
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 31120e2..effd00a 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
@@ -25,11 +25,8 @@ import java.io.OutputStream;
 import java.io.Reader;
 import java.io.StringReader;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Map;
-import java.util.WeakHashMap;
 
 import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
@@ -55,36 +52,37 @@ import org.xml.sax.EntityResolver;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * Few simple utils to read DOM. This is originally from the Jakarta Commons Modeler.
  *
  * @author Costin Manolache
  */
 public final class DOMUtils {
+    private static final Logger LOG = LoggerFactory.getLogger(DOMUtils.class);
+    
     private static final String XMLNAMESPACE = "xmlns";
 
-    private static final Map<ClassLoader, DocumentBuilder> DOCUMENT_BUILDERS = Collections
-        .synchronizedMap(new WeakHashMap<ClassLoader, DocumentBuilder>());
+    private static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
+    
+    static {
+        try {
+            DBF.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
 
-    private DOMUtils() {
+            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 DocumentBuilder getBuilder() throws ParserConfigurationException {
-        ClassLoader loader = Thread.currentThread().getContextClassLoader();
-        if (loader == null) {
-            loader = DOMUtils.class.getClassLoader();
-        }
-        if (loader == null) {
-            DocumentBuilderFactory dbf = createDocumentBuilderFactory();
-            return dbf.newDocumentBuilder();
-        }
-        DocumentBuilder builder = DOCUMENT_BUILDERS.get(loader);
-        if (builder == null) {
-            DocumentBuilderFactory dbf = createDocumentBuilderFactory();
-            builder = dbf.newDocumentBuilder();
-            DOCUMENT_BUILDERS.put(loader, builder);
-        }
-        return builder;
+    private DOMUtils() {
     }
 
     /**
@@ -424,32 +422,18 @@ public final class DOMUtils {
         }
     }
 
-    private static DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException {
-        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-        dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
-
-        dbf.setValidating(false);
-        dbf.setIgnoringComments(false);
-        dbf.setIgnoringElementContentWhitespace(true);
-        dbf.setNamespaceAware(true);
-        // dbf.setCoalescing(true);
-        // dbf.setExpandEntityReferences(true);
-
-        return dbf;
-    }
-
     /**
      * Read XML as DOM.
      */
     public static Document readXml(InputStream is) throws SAXException, IOException,
         ParserConfigurationException {
-        DocumentBuilder db = getBuilder();
+        DocumentBuilder db = DBF.newDocumentBuilder();
         return db.parse(is);
     }
 
     public static Document readXml(Reader is) throws SAXException, IOException, ParserConfigurationException {
         InputSource ips = new InputSource(is);
-        DocumentBuilder db = getBuilder();
+        DocumentBuilder db = DBF.newDocumentBuilder();
         return db.parse(ips);
     }
 
@@ -460,7 +444,7 @@ public final class DOMUtils {
         is2.setByteStream(is.getInputStream());
         is2.setCharacterStream(is.getReader());
 
-        DocumentBuilder db = getBuilder();
+        DocumentBuilder db = DBF.newDocumentBuilder();
         return db.parse(is2);
     }
 
@@ -475,7 +459,7 @@ public final class DOMUtils {
 
     public static DocumentBuilder createDocumentBuilder() {
         try {
-            return getBuilder();
+            return DBF.newDocumentBuilder();
         } catch (ParserConfigurationException e) {
             throw new RuntimeException("Couldn't find a DOM parser.", e);
         }
@@ -483,7 +467,7 @@ public final class DOMUtils {
 
     public static Document createDocument() {
         try {
-            return getBuilder().newDocument();
+            return DBF.newDocumentBuilder().newDocument();
         } catch (ParserConfigurationException e) {
             throw new RuntimeException("Couldn't find a DOM parser.", e);
         }