Author: dims
Date: Mon May  1 20:09:40 2006
New Revision: 398783

URL: http://svn.apache.org/viewcvs?rev=398783&view=rev
Log:
fix for WSCOMMONS-9 - Thread safety of 
XML{Input|Output}Factory.createXMLStream{Reader|Writer}

Added:
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/util/STAXUtils.java
Modified:
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/NodeImpl.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/jaxp/DocumentBuilderImpl.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMNodeImpl.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/xpath/DocumentNavigator.java

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java?rev=398783&r1=398782&r2=398783&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
 Mon May  1 20:09:40 2006
@@ -18,12 +18,12 @@
 
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.util.StAXUtils;
 import org.apache.axiom.soap.SOAP11Constants;
 import org.apache.axiom.soap.SOAP12Constants;
 
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.stream.FactoryConfigurationError;
-import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import java.io.OutputStream;
@@ -39,7 +39,6 @@
  * also looking at the OM tree whether it has any optimizable content.
  */
 public class MTOMXMLStreamWriter implements XMLStreamWriter {
-    private static XMLOutputFactory outputFactory = 
XMLOutputFactory.newInstance();
     private XMLStreamWriter xmlWriter;
     private OutputStream outStream;
     private LinkedList binaryNodeList = new LinkedList();
@@ -69,10 +68,10 @@
 
         if (format.isOptimized()) {
             bufferedSOAPBody = new StringWriter();
-            xmlWriter = outputFactory.createXMLStreamWriter(bufferedSOAPBody);
+            xmlWriter = StAXUtils.createXMLStreamWriter(bufferedSOAPBody);
         } else {
             try {
-                xmlWriter = outputFactory.createXMLStreamWriter(new 
java.io.BufferedWriter(new OutputStreamWriter(outStream,
+                xmlWriter = StAXUtils.createXMLStreamWriter(new 
java.io.BufferedWriter(new OutputStreamWriter(outStream,
                         format.getCharSetEncoding())));
             } catch (UnsupportedEncodingException e) {
                 throw new XMLStreamException(e);

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=398783&r1=398782&r2=398783&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
 Mon May  1 20:09:40 2006
@@ -24,12 +24,12 @@
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.util.StAXUtils;
 import org.apache.axiom.om.impl.OMContainerEx;
 import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
@@ -50,7 +50,6 @@
     private Log log = LogFactory.getLog(getClass());
     private boolean doDebug = false;
     private static int nsCount = 0;
-    private static XMLInputFactory inputFactory = 
XMLInputFactory.newInstance();
 
     /**
      * Constructor StAXOMBuilder.
@@ -85,7 +84,7 @@
      * @throws FileNotFoundException
      */
     public StAXOMBuilder(String filePath) throws XMLStreamException, 
FileNotFoundException {
-        this(inputFactory.createXMLStreamReader(new 
FileInputStream(filePath)));
+        this(StAXUtils.createXMLStreamReader(new FileInputStream(filePath)));
         doDebug = log.isDebugEnabled();
     }
 
@@ -94,7 +93,7 @@
      * @throws XMLStreamException
      */
     public StAXOMBuilder(InputStream inStream) throws XMLStreamException {
-        this(inputFactory.createXMLStreamReader(inStream));
+        this(StAXUtils.createXMLStreamReader(inStream));
         doDebug = log.isDebugEnabled();
     }
 

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/NodeImpl.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/NodeImpl.java?rev=398783&r1=398782&r2=398783&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/NodeImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/NodeImpl.java
 Mon May  1 20:09:40 2006
@@ -21,6 +21,7 @@
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.util.StAXUtils;
 import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 import org.w3c.dom.DOMException;
@@ -30,7 +31,6 @@
 import org.w3c.dom.NodeList;
 import org.w3c.dom.UserDataHandler;
 
-import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import java.io.OutputStream;
@@ -559,21 +559,21 @@
     }
 
     public void serialize(OutputStream output) throws XMLStreamException {
-        
serialize(XMLOutputFactory.newInstance().createXMLStreamWriter(output));
+        serialize(StAXUtils.createXMLStreamWriter(output));
     }
 
     public void serialize(Writer writer) throws XMLStreamException {
-        
serialize(XMLOutputFactory.newInstance().createXMLStreamWriter(writer));
+        serialize(StAXUtils.createXMLStreamWriter(writer));
     }
 
     public void serializeAndConsume(OutputStream output)
             throws XMLStreamException {
-        serializeAndConsume(XMLOutputFactory.newInstance()
+        serializeAndConsume(StAXUtils
                 .createXMLStreamWriter(output));
     }
 
     public void serializeAndConsume(Writer writer) throws XMLStreamException {
-        serializeAndConsume(XMLOutputFactory.newInstance()
+        serializeAndConsume(StAXUtils
                 .createXMLStreamWriter(writer));
     }
 
@@ -586,7 +586,7 @@
 
     public void serialize(Writer writer2, OMOutputFormat format)
             throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new 
MTOMXMLStreamWriter(XMLOutputFactory.newInstance()
+        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(StAXUtils
                 .createXMLStreamWriter(writer2));
         writer.setOutputFormat(format);
         internalSerialize(writer);
@@ -602,7 +602,7 @@
 
     public void serializeAndConsume(Writer writer2, OMOutputFormat format)
             throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new 
MTOMXMLStreamWriter(XMLOutputFactory.newInstance()
+        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(StAXUtils
                 .createXMLStreamWriter(writer2));
         writer.setOutputFormat(format);
         internalSerializeAndConsume(writer);

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/jaxp/DocumentBuilderImpl.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/jaxp/DocumentBuilderImpl.java?rev=398783&r1=398782&r2=398783&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/jaxp/DocumentBuilderImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/jaxp/DocumentBuilderImpl.java
 Mon May  1 20:09:40 2006
@@ -20,6 +20,7 @@
 import org.apache.axiom.om.impl.dom.DocumentImpl;
 import org.apache.axiom.om.impl.dom.ElementImpl;
 import org.apache.axiom.om.impl.dom.factory.OMDOMFactory;
+import org.apache.axiom.om.util.StAXUtils;
 import org.w3c.dom.DOMImplementation;
 import org.w3c.dom.Document;
 import org.xml.sax.EntityResolver;
@@ -28,7 +29,6 @@
 import org.xml.sax.SAXException;
 
 import javax.xml.parsers.DocumentBuilder;
-import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import java.io.File;
@@ -94,7 +94,7 @@
         try {
             OMDOMFactory factory = new OMDOMFactory();
             // Not really sure whether this will work :-?
-            XMLStreamReader reader = XMLInputFactory.newInstance()
+            XMLStreamReader reader = StAXUtils
                     .createXMLStreamReader(inputSource.getCharacterStream());
             StAXOMBuilder builder = new StAXOMBuilder(factory, reader);
             DocumentImpl doc = (DocumentImpl) builder.getDocument();
@@ -111,7 +111,7 @@
     public Document parse(InputStream is) throws SAXException, IOException {
         try {
             OMDOMFactory factory = new OMDOMFactory();
-            XMLStreamReader reader = XMLInputFactory.newInstance()
+            XMLStreamReader reader = StAXUtils
                     .createXMLStreamReader(is);
             StAXOMBuilder builder = new StAXOMBuilder(factory, reader);
             return (DocumentImpl) builder.getDocument();
@@ -126,7 +126,7 @@
     public Document parse(File file) throws SAXException, IOException {
         try {
             OMDOMFactory factory = new OMDOMFactory();
-            XMLStreamReader reader = XMLInputFactory.newInstance()
+            XMLStreamReader reader = StAXUtils
                     .createXMLStreamReader(new FileInputStream(file));
             StAXOMBuilder builder = new StAXOMBuilder(factory, reader);
             return (DocumentImpl) builder.getDocument();

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMNodeImpl.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMNodeImpl.java?rev=398783&r1=398782&r2=398783&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMNodeImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMNodeImpl.java
 Mon May  1 20:09:40 2006
@@ -22,11 +22,11 @@
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.util.StAXUtils;
 import org.apache.axiom.om.impl.OMContainerEx;
 import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 
-import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import java.io.OutputStream;
@@ -348,19 +348,19 @@
     }
 
     public void serialize(OutputStream output) throws XMLStreamException {
-        
serialize(XMLOutputFactory.newInstance().createXMLStreamWriter(output));
+        serialize(StAXUtils.createXMLStreamWriter(output));
     }
 
     public void serialize(Writer writer) throws XMLStreamException {
-        
serialize(XMLOutputFactory.newInstance().createXMLStreamWriter(writer));
+        serialize(StAXUtils.createXMLStreamWriter(writer));
     }
 
     public void serializeAndConsume(OutputStream output) throws 
XMLStreamException {
-        
serializeAndConsume(XMLOutputFactory.newInstance().createXMLStreamWriter(output));
+        serializeAndConsume(StAXUtils.createXMLStreamWriter(output));
     }
 
     public void serializeAndConsume(Writer writer) throws XMLStreamException {
-        
serializeAndConsume(XMLOutputFactory.newInstance().createXMLStreamWriter(writer));
+        serializeAndConsume(StAXUtils.createXMLStreamWriter(writer));
     }
 
     public void serialize(OutputStream output, OMOutputFormat format) throws 
XMLStreamException {
@@ -370,7 +370,7 @@
     }
 
     public void serialize(Writer writer2, OMOutputFormat format) throws 
XMLStreamException {
-        MTOMXMLStreamWriter writer = new 
MTOMXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(writer2));
+        MTOMXMLStreamWriter writer = new 
MTOMXMLStreamWriter(StAXUtils.createXMLStreamWriter(writer2));
         writer.setOutputFormat(format);
         internalSerialize(writer);
         writer.flush();
@@ -383,7 +383,7 @@
     }
 
     public void serializeAndConsume(Writer writer2, OMOutputFormat format) 
throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new 
MTOMXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(writer2));
+        MTOMXMLStreamWriter writer = new 
MTOMXMLStreamWriter(StAXUtils.createXMLStreamWriter(writer2));
         writer.setOutputFormat(format);
         internalSerializeAndConsume(writer);
         writer.flush();

Added: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/util/STAXUtils.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/util/STAXUtils.java?rev=398783&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/util/STAXUtils.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/util/STAXUtils.java
 Mon May  1 20:09:40 2006
@@ -0,0 +1,112 @@
+package org.apache.axiom.om.util;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.Stack;
+
+public class StAXUtils {
+
+    /**
+     * Pool of XMLOutputFactory instances
+     */
+    private static Stack xmlOutputFactoryPool = new Stack();
+
+    /**
+     * Pool of XMLInputFactory instances
+     */
+    private static Stack xmlInputFactoryPool = new Stack();
+
+    /**
+     * Gets an XMLInputFactory instance from pool.
+     *
+     * @return an XMLInputFactory instance.
+     */
+    synchronized public static XMLInputFactory getXMLInputFactory() {
+        if (!xmlInputFactoryPool.empty()) {
+            return (XMLInputFactory) xmlInputFactoryPool.pop();
+        }
+        return XMLInputFactory.newInstance();
+    }
+
+    /**
+     * Returns an XMLInputFactory instance for reuse.
+     *
+     * @param factory An XMLInputFactory instance that is available for reuse
+     */
+    synchronized public static void releaseXMLInputFactory(XMLInputFactory 
factory) {
+        xmlInputFactoryPool.push(factory);
+    }
+
+    public static XMLStreamReader createXMLStreamReader(InputStream in)
+            throws XMLStreamException {
+        XMLInputFactory inputFactory = getXMLInputFactory();
+        try {
+            return inputFactory.createXMLStreamReader(in);
+        } finally {
+            releaseXMLInputFactory(inputFactory);
+        }
+    }
+
+    public static XMLStreamReader createXMLStreamReader(Reader in)
+            throws XMLStreamException {
+        XMLInputFactory inputFactory = getXMLInputFactory();
+        try {
+            return inputFactory.createXMLStreamReader(in);
+        } finally {
+            releaseXMLInputFactory(inputFactory);
+        }
+    }
+
+    /**
+     * Gets an XMLOutputFactory instance from pool.
+     *
+     * @return an XMLOutputFactory instance.
+     */
+    synchronized public static XMLOutputFactory getXMLOutputFactory() {
+        if (!xmlOutputFactoryPool.empty()) {
+            return (XMLOutputFactory) xmlOutputFactoryPool.pop();
+        }
+        return XMLOutputFactory.newInstance();
+    }
+
+    /**
+     * Returns an XMLOutputFactory instance for reuse.
+     *
+     * @param factory An XMLOutputFactory instance that is available for reuse.
+     */
+    synchronized public static void releaseXMLOutputFactory(XMLOutputFactory 
factory) {
+        xmlOutputFactoryPool.push(factory);
+    }
+
+    public static XMLStreamWriter createXMLStreamWriter(OutputStream out)
+            throws XMLStreamException {
+        XMLOutputFactory outputFactory = getXMLOutputFactory();
+        try {
+            return outputFactory.createXMLStreamWriter(out);
+        } finally {
+            releaseXMLOutputFactory(outputFactory);
+        }
+    }
+
+    public static XMLStreamWriter createXMLStreamWriter(Writer out)
+            throws XMLStreamException {
+        XMLOutputFactory outputFactory = getXMLOutputFactory();
+        try {
+            return outputFactory.createXMLStreamWriter(out);
+        } finally {
+            releaseXMLOutputFactory(outputFactory);
+        }
+    }
+
+    public static void reset() {
+        xmlOutputFactoryPool.removeAllElements();
+        xmlInputFactoryPool.removeAllElements();
+    }
+}

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/xpath/DocumentNavigator.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/xpath/DocumentNavigator.java?rev=398783&r1=398782&r2=398783&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/xpath/DocumentNavigator.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/xpath/DocumentNavigator.java
 Mon May  1 20:09:40 2006
@@ -10,6 +10,7 @@
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMProcessingInstruction;
 import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.util.StAXUtils;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.axiom.om.impl.llom.OMNamespaceImpl;
 import org.jaxen.BaseXPath;
@@ -543,15 +544,23 @@
             throws FunctionCallException {
         try {
             XMLStreamReader parser;
-            XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
-            xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, 
Boolean.TRUE);
-            if (uri.indexOf(':') == -1) {
-                parser = xmlInputFactory.createXMLStreamReader(
-                                new FileInputStream(uri));
-            } else {
-                URL url = new URL(uri);
-                parser = xmlInputFactory.createXMLStreamReader(
-                                url.openStream());
+            XMLInputFactory xmlInputFactory = StAXUtils.getXMLInputFactory();
+            Boolean oldValue = (Boolean) 
xmlInputFactory.getProperty(XMLInputFactory.IS_COALESCING);
+            try {
+                xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, 
Boolean.TRUE);
+                if (uri.indexOf(':') == -1) {
+                    parser = xmlInputFactory.createXMLStreamReader(
+                            new FileInputStream(uri));
+                } else {
+                    URL url = new URL(uri);
+                    parser = xmlInputFactory.createXMLStreamReader(
+                            url.openStream());
+                }
+            } finally {
+                if (oldValue != null) {
+                    xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, 
oldValue);
+                }
+                StAXUtils.releaseXMLInputFactory(xmlInputFactory);
             }
             StAXOMBuilder builder =
                     new StAXOMBuilder(parser);


Reply via email to