karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ioca...@apache.org
Subject git commit: [KARAF-2679] Remove state from spring and blueprint URL handlers. Use thread local TransformerFactory and DocumentBuilderFactory to prevent multiple threads of using the same instance.
Date Mon, 13 Jan 2014 12:33:04 GMT
Updated Branches:
  refs/heads/karaf-2.3.x f2a240caf -> 542893410


[KARAF-2679] Remove state from spring and blueprint URL handlers. Use thread local TransformerFactory
and DocumentBuilderFactory to prevent multiple threads of using the same instance.


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/54289341
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/54289341
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/54289341

Branch: refs/heads/karaf-2.3.x
Commit: 5428934100944ce79024e66a531b0a4bf582ff56
Parents: f2a240c
Author: Ioannis Canellos <iocanel@gmail.com>
Authored: Fri Jan 10 18:40:01 2014 +0200
Committer: Ioannis Canellos <iocanel@gmail.com>
Committed: Mon Jan 13 14:32:51 2014 +0200

----------------------------------------------------------------------
 .../blueprint/BlueprintDeploymentListener.java  | 16 +---
 .../blueprint/BlueprintTransformer.java         | 31 +------
 .../deployer/blueprint/BlueprintURLHandler.java | 11 +--
 .../spring/SpringDeploymentListener.java        | 16 +---
 .../deployer/spring/SpringTransformer.java      | 31 +------
 .../karaf/deployer/spring/SpringURLHandler.java | 16 +---
 .../java/org/apache/karaf/util/XmlUtils.java    | 98 ++++++++++++++++++++
 7 files changed, 118 insertions(+), 101 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/54289341/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintDeploymentListener.java
----------------------------------------------------------------------
diff --git a/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintDeploymentListener.java
b/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintDeploymentListener.java
index 5a9c6ec..88dd715 100644
--- a/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintDeploymentListener.java
+++ b/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintDeploymentListener.java
@@ -20,17 +20,15 @@ package org.apache.karaf.deployer.blueprint;
 import java.io.File;
 import java.net.URL;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
+import org.apache.karaf.util.XmlUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 
 import org.apache.felix.fileinstall.ArtifactUrlTransformer;
 import org.xml.sax.ErrorHandler;
-import org.xml.sax.SAXParseException;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 /**
  * A deployment listener that listens for spring xml applications
@@ -40,8 +38,6 @@ public class BlueprintDeploymentListener implements ArtifactUrlTransformer
{
 
     private final Logger logger = LoggerFactory.getLogger(BlueprintDeploymentListener.class);
 
-    private DocumentBuilderFactory dbf;
-
     public boolean canHandle(File artifact) {
         try {
             if (artifact.isFile() && artifact.getName().endsWith(".xml")) {
@@ -68,12 +64,7 @@ public class BlueprintDeploymentListener implements ArtifactUrlTransformer
{
     }
 
     protected Document parse(File artifact) throws Exception {
-        if (dbf == null) {
-            dbf = DocumentBuilderFactory.newInstance();
-            dbf.setNamespaceAware(true);
-        }
-        DocumentBuilder db = dbf.newDocumentBuilder();
-        db.setErrorHandler(new ErrorHandler() {
+        return XmlUtils.parse(artifact, new ErrorHandler() {
             public void warning(SAXParseException exception) throws SAXException {
             }
             public void error(SAXParseException exception) throws SAXException {
@@ -82,7 +73,6 @@ public class BlueprintDeploymentListener implements ArtifactUrlTransformer
{
                 throw exception;
             }
         });
-        return db.parse(artifact);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/54289341/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintTransformer.java
----------------------------------------------------------------------
diff --git a/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintTransformer.java
b/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintTransformer.java
index a8f31aa..006acd4 100644
--- a/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintTransformer.java
+++ b/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintTransformer.java
@@ -33,18 +33,15 @@ import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 import java.util.zip.ZipEntry;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
 import org.apache.karaf.util.DeployerUtils;
 
+import org.apache.karaf.util.XmlUtils;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -55,11 +52,6 @@ import static org.apache.karaf.util.MvnUtils.getMvnPath;
 
 public class BlueprintTransformer {
 
-    static Transformer transformer;
-    static DocumentBuilderFactory dbf;
-    static TransformerFactory tf;
-
-
     public static void transform(URL url, OutputStream os) throws Exception {
         // Build dom document
         Document doc = parse(url);
@@ -116,28 +108,18 @@ public class BlueprintTransformer {
         e = new ZipEntry("OSGI-INF/blueprint/" + name);
         out.putNextEntry(e);
         // Copy the new DOM
-        if (tf == null) {
-            tf = TransformerFactory.newInstance();
-        }
-        tf.newTransformer().transform(new DOMSource(doc), new StreamResult(out));
+        XmlUtils.transform(new DOMSource(doc), new StreamResult(out));
         out.closeEntry();
         out.close();
     }
 
     public static Set<String> analyze(Source source) throws Exception {
-        if (transformer == null) {
-            if (tf == null) {
-                tf = TransformerFactory.newInstance();
-            }
-            Source s = new StreamSource(BlueprintTransformer.class.getResourceAsStream("extract.xsl"));
-            transformer = tf.newTransformer(s);
-        }
 
         Set<String> refers = new TreeSet<String>();
 
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         Result r = new StreamResult(bout);
-        transformer.transform(source, r);
+        XmlUtils.transform(new StreamSource(BlueprintTransformer.class.getResourceAsStream("extract.xsl")),
source, r);
 
         ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
         bout.close();
@@ -177,12 +159,7 @@ public class BlueprintTransformer {
     }
 
     protected static Document parse(URL url) throws Exception {
-        if (dbf == null) {
-            dbf = DocumentBuilderFactory.newInstance();
-            dbf.setNamespaceAware(true);
-        }
-        DocumentBuilder db = dbf.newDocumentBuilder();
-        return db.parse(url.toString());
+        return XmlUtils.parse(url.toString());
     }
 
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/54289341/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintURLHandler.java
----------------------------------------------------------------------
diff --git a/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintURLHandler.java
b/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintURLHandler.java
index 452f4cb..d379a5a 100644
--- a/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintURLHandler.java
+++ b/deployer/blueprint/src/main/java/org/apache/karaf/deployer/blueprint/BlueprintURLHandler.java
@@ -42,8 +42,6 @@ public class BlueprintURLHandler extends AbstractURLStreamHandlerService
{
 
 	private static String SYNTAX = "blueprint: bp-xml-uri";
 
-	private URL blueprintXmlURL;
-
     /**
      * Open the connection for the given URL.
      *
@@ -56,15 +54,10 @@ public class BlueprintURLHandler extends AbstractURLStreamHandlerService
{
 		if (url.getPath() == null || url.getPath().trim().length() == 0) {
 			throw new MalformedURLException ("Path cannot be null or empty. Syntax: " + SYNTAX );
 		}
-		blueprintXmlURL = new URL(url.getPath());
 
-		logger.debug("Blueprint xml URL is: [" + blueprintXmlURL + "]");
+		logger.debug("Blueprint xml URL is: [" + url.getPath() + "]");
 		return new Connection(url);
 	}
-	
-	public URL getBlueprintXmlURL() {
-		return blueprintXmlURL;
-	}
 
     public class Connection extends URLConnection {
 
@@ -80,7 +73,7 @@ public class BlueprintURLHandler extends AbstractURLStreamHandlerService
{
         public InputStream getInputStream() throws IOException {
             try {
                 ByteArrayOutputStream os = new ByteArrayOutputStream();
-                BlueprintTransformer.transform(blueprintXmlURL, os);
+                BlueprintTransformer.transform(new URL(url.getPath()), os);
                 os.close();
                 return new ByteArrayInputStream(os.toByteArray());
             } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/54289341/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringDeploymentListener.java
----------------------------------------------------------------------
diff --git a/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringDeploymentListener.java
b/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringDeploymentListener.java
index a332029..e1691e4 100644
--- a/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringDeploymentListener.java
+++ b/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringDeploymentListener.java
@@ -20,9 +20,7 @@ package org.apache.karaf.deployer.spring;
 import java.io.File;
 import java.net.URL;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
+import org.apache.karaf.util.XmlUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -40,8 +38,6 @@ public class SpringDeploymentListener implements ArtifactUrlTransformer
{
 
     private final Logger logger = LoggerFactory.getLogger(SpringDeploymentListener.class);
 
-    private DocumentBuilderFactory dbf;
-
     public boolean canHandle(File artifact) {
         try {
             if (artifact.isFile() && artifact.getName().endsWith(".xml")) {
@@ -68,21 +64,17 @@ public class SpringDeploymentListener implements ArtifactUrlTransformer
{
     }
 
     protected Document parse(File artifact) throws Exception {
-        if (dbf == null) {
-            dbf = DocumentBuilderFactory.newInstance();
-            dbf.setNamespaceAware(true);
-        }
-        DocumentBuilder db = dbf.newDocumentBuilder();
-        db.setErrorHandler(new ErrorHandler() {
+        return XmlUtils.parse(artifact, new ErrorHandler() {
             public void warning(SAXParseException exception) throws SAXException {
             }
+
             public void error(SAXParseException exception) throws SAXException {
             }
+
             public void fatalError(SAXParseException exception) throws SAXException {
                 throw exception;
             }
         });
-        return db.parse(artifact);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/54289341/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringTransformer.java
----------------------------------------------------------------------
diff --git a/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringTransformer.java
b/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringTransformer.java
index 8d3650e..279305e 100644
--- a/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringTransformer.java
+++ b/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringTransformer.java
@@ -33,18 +33,15 @@ import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 import java.util.zip.ZipEntry;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
 import org.apache.karaf.util.DeployerUtils;
 
+import org.apache.karaf.util.XmlUtils;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -55,10 +52,6 @@ import static org.apache.karaf.util.MvnUtils.getMvnPath;
 
 public class SpringTransformer {
 
-    static Transformer transformer;
-    static DocumentBuilderFactory dbf;
-    static TransformerFactory tf;
-
 
     public static void transform(URL url, OutputStream os) throws Exception {
         // Build dom document
@@ -113,28 +106,17 @@ public class SpringTransformer {
         e = new ZipEntry("META-INF/spring/" + name);
         out.putNextEntry(e);
         // Copy the new DOM
-        if (tf == null) {
-            tf = TransformerFactory.newInstance();
-        }
-        tf.newTransformer().transform(new DOMSource(doc), new StreamResult(out));
+        XmlUtils.transform(new DOMSource(doc), new StreamResult(out));
         out.closeEntry();
         out.close();
     }
 
     public static Set<String> analyze(Source source) throws Exception {
-        if (transformer == null) {
-            if (tf == null) {
-                tf = TransformerFactory.newInstance();
-            }
-            Source s = new StreamSource(SpringTransformer.class.getResourceAsStream("extract.xsl"));
-            transformer = tf.newTransformer(s);
-        }
-
         Set<String> refers = new TreeSet<String>();
 
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         Result r = new StreamResult(bout);
-        transformer.transform(source, r);
+        XmlUtils.transform(new StreamSource(SpringTransformer.class.getResourceAsStream("extract.xsl")),
source, r);
 
         ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
         bout.close();
@@ -174,12 +156,7 @@ public class SpringTransformer {
     }
 
     protected static Document parse(URL url) throws Exception {
-        if (dbf == null) {
-            dbf = DocumentBuilderFactory.newInstance();
-            dbf.setNamespaceAware(true);
-        }
-        DocumentBuilder db = dbf.newDocumentBuilder();
-        return db.parse(url.toString());
+        return XmlUtils.parse(url.toString());
     }
 
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/54289341/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringURLHandler.java
----------------------------------------------------------------------
diff --git a/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringURLHandler.java
b/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringURLHandler.java
index d471123..60c9b6f 100644
--- a/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringURLHandler.java
+++ b/deployer/spring/src/main/java/org/apache/karaf/deployer/spring/SpringURLHandler.java
@@ -19,9 +19,6 @@ package org.apache.karaf.deployer.spring;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
@@ -42,8 +39,6 @@ public class SpringURLHandler extends AbstractURLStreamHandlerService {
 
 	private static String SYNTAX = "spring: spring-xml-uri";
 
-	private URL springXmlURL;
-
     /**
      * Open the connection for the given URL.
      *
@@ -55,16 +50,11 @@ public class SpringURLHandler extends AbstractURLStreamHandlerService
{
 	public URLConnection openConnection(URL url) throws IOException {
 		if (url.getPath() == null || url.getPath().trim().length() == 0) {
 			throw new MalformedURLException ("Path cannot be null or empty. Syntax: " + SYNTAX );
-		}
-		springXmlURL = new URL(url.getPath());
+        }
 
-		logger.debug("Spring xml URL is: [" + springXmlURL + "]");
+		logger.debug("Spring xml URL is: [" + url.getPath() + "]");
 		return new Connection(url);
 	}
-	
-	public URL getSpringXmlURL() {
-		return springXmlURL;
-	}
 
     public class Connection extends URLConnection {
 
@@ -80,7 +70,7 @@ public class SpringURLHandler extends AbstractURLStreamHandlerService {
         public InputStream getInputStream() throws IOException {
             try {
                 ByteArrayOutputStream os = new ByteArrayOutputStream();
-                SpringTransformer.transform(springXmlURL, os);
+                SpringTransformer.transform(new URL(url.getPath()), os);
                 os.close();
                 return new ByteArrayInputStream(os.toByteArray());
             } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/54289341/util/src/main/java/org/apache/karaf/util/XmlUtils.java
----------------------------------------------------------------------
diff --git a/util/src/main/java/org/apache/karaf/util/XmlUtils.java b/util/src/main/java/org/apache/karaf/util/XmlUtils.java
new file mode 100644
index 0000000..b11cb3d
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/XmlUtils.java
@@ -0,0 +1,98 @@
+package org.apache.karaf.util;
+
+import org.w3c.dom.Document;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.*;
+import java.io.File;
+import java.io.IOException;
+
+
+public class XmlUtils {
+
+    private static final ThreadLocal<DocumentBuilderFactory> DOCUMENT_BUILDER_FACTORY
= new ThreadLocal<DocumentBuilderFactory>();
+    private static final ThreadLocal<TransformerFactory> TRANSFORMER_FACTORY = new
ThreadLocal<TransformerFactory>();
+
+    public static Document parse(String uri) throws TransformerException, IOException, SAXException,
ParserConfigurationException {
+        DocumentBuilder db = documentBuilder();
+        try {
+            return db.parse(uri);
+        } finally {
+            db.reset();
+        }
+    }
+
+    public static Document parse(File f) throws TransformerException, IOException, SAXException,
ParserConfigurationException {
+        DocumentBuilder db = documentBuilder();
+        try {
+            return db.parse(f);
+        } finally {
+            db.reset();
+        }
+    }
+    public static Document parse(File f, ErrorHandler errorHandler) throws TransformerException,
IOException, SAXException, ParserConfigurationException {
+        DocumentBuilder db = documentBuilder();
+        db.setErrorHandler(errorHandler);
+        try {
+            return db.parse(f);
+        } finally {
+            db.reset();
+        }
+    }
+
+    public static void transform(Source xmlSource, Result outputTarget) throws TransformerException
{
+        Transformer t = transformer();
+        try {
+            t.transform(xmlSource, outputTarget);
+        } finally {
+            t.reset();
+        }
+    }
+
+    public static void transform(Source xsltSource, Source xmlSource, Result outputTarget)
throws TransformerException {
+        Transformer t = transformer(xsltSource);
+        try {
+            t.transform(xmlSource, outputTarget);
+        } finally {
+            t.reset();
+        }
+    }
+
+    private static DocumentBuilder documentBuilder() throws ParserConfigurationException
{
+        DocumentBuilderFactory dbf;
+        if (DOCUMENT_BUILDER_FACTORY.get() == null) {
+            dbf = DocumentBuilderFactory.newInstance();
+            dbf.setNamespaceAware(true);
+            DOCUMENT_BUILDER_FACTORY.set(dbf);
+        } else {
+            dbf = DOCUMENT_BUILDER_FACTORY.get();
+        }
+        return dbf.newDocumentBuilder();
+    }
+
+    private static Transformer transformer() throws TransformerConfigurationException {
+        TransformerFactory tf;
+        if (TRANSFORMER_FACTORY.get() == null) {
+            tf = TransformerFactory.newInstance();
+            TRANSFORMER_FACTORY.set(tf);
+        } else {
+            tf = TRANSFORMER_FACTORY.get();
+        }
+        return tf.newTransformer();
+    }
+
+    private static Transformer transformer(Source xsltSource) throws TransformerConfigurationException
{
+        TransformerFactory tf;
+        if (TRANSFORMER_FACTORY.get() == null) {
+            tf = TransformerFactory.newInstance();
+            TRANSFORMER_FACTORY.set(tf);
+        } else {
+            tf = TRANSFORMER_FACTORY.get();
+        }
+        return tf.newTransformer(xsltSource);
+    }
+}


Mime
View raw message