You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by as...@apache.org on 2011/08/04 11:09:26 UTC

svn commit: r1153796 - /cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java

Author: asoldano
Date: Thu Aug  4 09:09:25 2011
New Revision: 1153796

URL: http://svn.apache.org/viewvc?rev=1153796&view=rev
Log:
[CXF-3675] Add a classloader based map of DocumentBuilder instances in DOMUtils to improve performances when building documents. This also restores the former behaviour, with the DocumentBuilder being cached in DOMUtils.

Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java?rev=1153796&r1=1153795&r2=1153796&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java Thu Aug  4 09:09:25 2011
@@ -25,8 +25,11 @@ 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;
@@ -62,9 +65,28 @@ import org.apache.cxf.common.util.String
 public final class DOMUtils {
     private static final String XMLNAMESPACE = "xmlns";
 
+    private static final Map<ClassLoader, DocumentBuilder> DOCUMENT_BUILDERS = Collections
+        .synchronizedMap(new WeakHashMap<ClassLoader, DocumentBuilder>());
+
     private DOMUtils() {
     }
 
+    private static DocumentBuilder getBuilder() throws ParserConfigurationException {
+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        if (loader == null) {
+            loader = DOMUtils.class.getClassLoader();
+        }
+        if (loader == null) {
+            return XMLUtils.getParser();
+        }
+        DocumentBuilder builder = DOCUMENT_BUILDERS.get(loader);
+        if (builder == null) {
+            builder = XMLUtils.getParser();
+            DOCUMENT_BUILDERS.put(loader, builder);
+        }
+        return builder;
+    }
+
     /**
      * This function is much like getAttribute, but returns null, not "", for a nonexistent attribute.
      * 
@@ -484,7 +506,7 @@ public final class DOMUtils {
 
     public static DocumentBuilder createDocumentBuilder() {
         try {
-            return XMLUtils.getParser();
+            return getBuilder();
         } catch (ParserConfigurationException e) {
             throw new RuntimeException("Couldn't find a DOM parser.", e);
         }
@@ -492,7 +514,7 @@ public final class DOMUtils {
 
     public static Document createDocument() {
         try {
-            return XMLUtils.getParser().newDocument();
+            return getBuilder().newDocument();
         } catch (ParserConfigurationException e) {
             throw new RuntimeException("Couldn't find a DOM parser.", e);
         }