servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ff...@apache.org
Subject svn commit: r1095259 - /servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
Date Wed, 20 Apr 2011 03:37:55 GMT
Author: ffang
Date: Wed Apr 20 03:37:54 2011
New Revision: 1095259

URL: http://svn.apache.org/viewvc?rev=1095259&view=rev
Log:
[SM-2069]SourceTransformer should cache DocumentBuilder & TransformerFactory

Modified:
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java

Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java?rev=1095259&r1=1095258&r2=1095259&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
(original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
Wed Apr 20 03:37:54 2011
@@ -22,6 +22,7 @@ import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.lang.ref.WeakReference;
 import java.lang.reflect.Constructor;
 
 import javax.jbi.messaging.MessagingException;
@@ -59,6 +60,8 @@ public class SourceTransformer {
 
     public static final String DEFAULT_CHARSET_PROPERTY = "org.apache.servicemix.default.charset";
     public static final String DEFAULT_VALIDATING_DTD_PROPERTY = "org.apache.servicemix.default.validating-dtd";
+    private static ThreadLocal<WeakReference<DocumentBuilder>> docBuilderCache
= new ThreadLocal<WeakReference<DocumentBuilder>>();
+    private static ThreadLocal<WeakReference<TransformerFactory>> transformerFactoryCache
= new ThreadLocal<WeakReference<TransformerFactory>>();
 
     /*
      * When converting a DOM tree to a SAXSource, we try to use Xalan internal
@@ -81,8 +84,7 @@ public class SourceTransformer {
 
     private DocumentBuilderFactory documentBuilderFactory;
 
-    private TransformerFactory transformerFactory;
-
+    	
     public SourceTransformer() {
     }
 
@@ -484,33 +486,53 @@ public class SourceTransformer {
     }
 
     public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
-        DocumentBuilderFactory factory = getDocumentBuilderFactory();
-        return factory.newDocumentBuilder();
+        WeakReference<DocumentBuilder> cachedDocBuilder = docBuilderCache.get();
+
+        DocumentBuilder docBuilder = null;
+        if (cachedDocBuilder != null) {
+            docBuilder = cachedDocBuilder.get();
+        }
+
+        if (docBuilder == null) {
+            docBuilder = getDocumentBuilderFactory().newDocumentBuilder();            
+            docBuilderCache.set(new WeakReference<DocumentBuilder>(docBuilder));
+        }
+
+        return docBuilder;
     }
 
     public Document createDocument() throws ParserConfigurationException {
         DocumentBuilder builder = createDocumentBuilder();
         return builder.newDocument();
     }
+    
+    public Transformer createTransfomer() throws TransformerConfigurationException {
+        return createTransformerFactory().newTransformer();
+    }
+    
+    public TransformerFactory createTransformerFactory() {        
+        WeakReference<TransformerFactory> cachedFactory = transformerFactoryCache.get();
 
-    public TransformerFactory getTransformerFactory() {
-        if (transformerFactory == null) {
-            transformerFactory = createTransformerFactory();
+        TransformerFactory factory = null;
+        if (cachedFactory != null) {
+            factory = cachedFactory.get();
         }
-        return transformerFactory;
-    }
 
-    public void setTransformerFactory(TransformerFactory transformerFactory) {
-        this.transformerFactory = transformerFactory;
-    }
+        if (factory == null) {
+            factory = TransformerFactory.newInstance();
+            transformerFactoryCache.set(new WeakReference<TransformerFactory>(factory));
+        }
 
-    public Transformer createTransfomer() throws TransformerConfigurationException {
-        TransformerFactory factory = getTransformerFactory();
-        return factory.newTransformer();
+        return factory;        
     }
-
-    public TransformerFactory createTransformerFactory() {
-        return TransformerFactory.newInstance();
+    
+    public TransformerFactory getTransformerFactory() {
+    	return createTransformerFactory();
+    }
+    	 
+    public void setTransformerFactory(TransformerFactory transformerFactory) {
+    	transformerFactoryCache.set(new WeakReference<TransformerFactory>(transformerFactory));
     }
 
+    
 }



Mime
View raw message