Permalink
Browse files

[SM-2069]SourceTransformer should cache DocumentBuilder & Transformer…

…Factory

git-svn-id: https://svn.apache.org/repos/asf/servicemix/utils/trunk@1095259 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 330ae77 commit d83343a52cd02fd3473684d1a78ed1e8ab6c32c2 @ffang ffang committed Apr 20, 2011
Showing with 40 additions and 18 deletions.
  1. +40 −18 src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
@@ -22,6 +22,7 @@
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 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 @@
private DocumentBuilderFactory documentBuilderFactory;
- private TransformerFactory transformerFactory;
-
+
public SourceTransformer() {
}
@@ -484,33 +486,53 @@ public DocumentBuilderFactory createDocumentBuilderFactory() {
}
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));
}
+
}

0 comments on commit d83343a

Please sign in to comment.