You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2015/05/23 16:13:57 UTC

svn commit: r1681352 - in /webservices/axiom/trunk: aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/ modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/ modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/b...

Author: veithen
Date: Sat May 23 14:13:57 2015
New Revision: 1681352

URL: http://svn.apache.org/r1681352
Log:
Correctly manage the stream created internally when constructing a builder from a system ID. Updated the relevant DOM test case to demonstrate the problem.

This change also paves the way for AXIOM-404.

Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java
    webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java
    webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java
    webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java
    webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/StAXSOAPModelBuilder.java
    webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java?rev=1681352&r1=1681351&r2=1681352&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AbstractOMMetaFactory.java Sat May 23 14:13:57 2015
@@ -18,7 +18,9 @@
  */
 package org.apache.axiom.om.impl.common.factory;
 
+import java.io.Closeable;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 
 import javax.xml.stream.XMLStreamException;
@@ -26,6 +28,7 @@ import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamSource;
 
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMFactory;
@@ -51,35 +54,62 @@ import org.xml.sax.InputSource;
  * ({@link org.apache.axiom.om.impl.builder.StAXOMBuilder} and its subclasses).
  */
 public abstract class AbstractOMMetaFactory implements OMMetaFactoryEx {
-    private static XMLStreamReader createXMLStreamReader(StAXParserConfiguration configuration, InputSource is) {
+    private final static class SourceInfo {
+        private final XMLStreamReader reader;
+        private final Closeable closeable;
+        
+        SourceInfo(XMLStreamReader reader, Closeable closeable) {
+            this.reader = reader;
+            this.closeable = closeable;
+        }
+
+        XMLStreamReader getReader() {
+            return reader;
+        }
+
+        Closeable getCloseable() {
+            return closeable;
+        }
+    }
+    
+    private static SourceInfo createXMLStreamReader(StAXParserConfiguration configuration, InputSource is) {
+        XMLStreamReader reader;
+        Closeable closeable;
         try {
             if (is.getByteStream() != null) {
                 String systemId = is.getSystemId();
                 String encoding = is.getEncoding();
                 if (systemId != null) {
                     if (encoding == null) {
-                        return StAXUtils.createXMLStreamReader(configuration, systemId, is.getByteStream());
+                        reader = StAXUtils.createXMLStreamReader(configuration, systemId, is.getByteStream());
+                        closeable = null;
                     } else {
                         throw new UnsupportedOperationException();
                     }
                 } else {
                     if (encoding == null) {
-                        return StAXUtils.createXMLStreamReader(configuration, is.getByteStream());
+                        reader = StAXUtils.createXMLStreamReader(configuration, is.getByteStream());
+                        closeable = null;
                     } else {
-                        return StAXUtils.createXMLStreamReader(configuration, is.getByteStream(), encoding);
+                        reader = StAXUtils.createXMLStreamReader(configuration, is.getByteStream(), encoding);
+                        closeable = null;
                     }
                 }
             } else if (is.getCharacterStream() != null) {
-                return StAXUtils.createXMLStreamReader(configuration, is.getCharacterStream());
+                reader = StAXUtils.createXMLStreamReader(configuration, is.getCharacterStream());
+                closeable = null;
             } else {
                 String systemId = is.getSystemId();
-                return StAXUtils.createXMLStreamReader(configuration, systemId, new URL(systemId).openConnection().getInputStream());
+                InputStream in = new URL(systemId).openConnection().getInputStream();
+                reader = StAXUtils.createXMLStreamReader(configuration, systemId, in);
+                closeable = in;
             }
         } catch (XMLStreamException ex) {
             throw new OMException(ex);
         } catch (IOException ex) {
             throw new OMException(ex);
         }
+        return new SourceInfo(reader, closeable);
     }
     
     private static XMLStreamReader getXMLStreamReader(XMLStreamReader originalReader) {
@@ -100,16 +130,30 @@ public abstract class AbstractOMMetaFact
     }
 
     public OMXMLParserWrapper createOMBuilder(OMFactory omFactory, StAXParserConfiguration configuration, InputSource is) {
-        StAXOMBuilder builder = new StAXOMBuilder(omFactory, createXMLStreamReader(configuration, is));
+        SourceInfo sourceInfo = createXMLStreamReader(configuration, is);
+        StAXOMBuilder builder = new StAXOMBuilder(omFactory, sourceInfo.getReader(),
+                sourceInfo.getCloseable());
         builder.setAutoClose(true);
         return builder;
     }
     
+    private static InputSource toInputSource(StreamSource source) {
+        InputSource is = new InputSource();
+        is.setByteStream(source.getInputStream());
+        is.setCharacterStream(source.getReader());
+        is.setPublicId(source.getPublicId());
+        is.setSystemId(source.getSystemId());
+        return is;
+    }
+    
     public OMXMLParserWrapper createOMBuilder(OMFactory omFactory, Source source) {
         if (source instanceof SAXSource) {
             return createOMBuilder(omFactory, (SAXSource)source, true);
         } else if (source instanceof DOMSource) {
             return createOMBuilder(omFactory, ((DOMSource)source).getNode(), true);
+        } else if (source instanceof StreamSource) {
+            return createOMBuilder(omFactory, StAXParserConfiguration.DEFAULT,
+                    toInputSource((StreamSource)source));
         } else {
             try {
                 return new StAXOMBuilder(omFactory, StAXUtils.getXMLInputFactory().createXMLStreamReader(source));
@@ -131,8 +175,9 @@ public abstract class AbstractOMMetaFact
 
     public OMXMLParserWrapper createOMBuilder(StAXParserConfiguration configuration,
             OMFactory omFactory, InputSource rootPart, MimePartProvider mimePartProvider) {
-        XOPAwareStAXOMBuilder builder = new XOPAwareStAXOMBuilder(omFactory, createXMLStreamReader(
-                configuration, rootPart), mimePartProvider);
+        SourceInfo sourceInfo = createXMLStreamReader(configuration, rootPart);
+        XOPAwareStAXOMBuilder builder = new XOPAwareStAXOMBuilder(omFactory, sourceInfo.getReader(),
+                mimePartProvider, sourceInfo.getCloseable());
         builder.setAutoClose(true);
         return builder;
     }
@@ -142,15 +187,18 @@ public abstract class AbstractOMMetaFact
     }
 
     public SOAPModelBuilder createSOAPModelBuilder(StAXParserConfiguration configuration, InputSource is) {
-        StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(this, createXMLStreamReader(configuration, is));
+        SourceInfo sourceInfo = createXMLStreamReader(configuration, is);
+        StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(this, sourceInfo.getReader(),
+                sourceInfo.getCloseable());
         builder.setAutoClose(true);
         return builder;
     }
 
     public SOAPModelBuilder createSOAPModelBuilder(StAXParserConfiguration configuration,
             SOAPFactory soapFactory, InputSource rootPart, MimePartProvider mimePartProvider) {
-        MTOMStAXSOAPModelBuilder builder = new MTOMStAXSOAPModelBuilder(soapFactory, createXMLStreamReader(
-                configuration, rootPart), mimePartProvider);
+        SourceInfo sourceInfo = createXMLStreamReader(configuration, rootPart);
+        MTOMStAXSOAPModelBuilder builder = new MTOMStAXSOAPModelBuilder(soapFactory,
+                sourceInfo.getReader(), mimePartProvider, sourceInfo.getCloseable());
         builder.setAutoClose(true);
         return builder;
     }

Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java?rev=1681352&r1=1681351&r2=1681352&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java Sat May 23 14:13:57 2015
@@ -45,6 +45,7 @@ import javax.xml.stream.XMLStreamConstan
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
+import java.io.Closeable;
 import java.io.InputStream;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -64,6 +65,8 @@ public abstract class StAXBuilder implem
 
     /** Field omfactory */
     protected OMFactoryEx omfactory;
+    
+    private final Closeable closeable;
 
     /** Field lastNode */
     protected OMContainerEx target;
@@ -126,35 +129,47 @@ public abstract class StAXBuilder implem
     private final Map/*<OMContainer,Throwable>*/ discardTracker = log.isDebugEnabled() ? new LinkedHashMap() : null;
     
     /**
+     * For internal use only.
+     */
+    protected StAXBuilder(OMFactory omFactory, XMLStreamReader parser, String encoding,
+            Closeable closeable) {
+        omfactory = (OMFactoryEx)omFactory;
+        this.closeable = closeable;
+        charEncoding = encoding;
+        initParser(parser);
+    }
+    
+    /**
+     * For internal use only.
+     */
+    protected StAXBuilder(OMFactory omFactory, XMLStreamReader parser, Closeable closeable) {
+        // The getEncoding information is only available at the START_DOCUMENT event.
+        this(omFactory, parser, parser.getEncoding(), closeable);
+    }
+    
+    /**
      * Constructor StAXBuilder.
      * This constructor is used if the parser is at the beginning (START_DOCUMENT).
      *
-     * @param ombuilderFactory
+     * @param omFactory
      * @param parser
      */
-    protected StAXBuilder(OMFactory ombuilderFactory, XMLStreamReader parser) {
-        omfactory = (OMFactoryEx)ombuilderFactory;
-        
-        // The getEncoding information is only available at the START_DOCUMENT event.
-        charEncoding = parser.getEncoding();
-
-        initParser(parser);
+    protected StAXBuilder(OMFactory omFactory, XMLStreamReader parser) {
+        this(omFactory, parser, (Closeable)null);
     }
     
     /**
      * Constructor StAXBuilder.
      * This constructor is used if the parser is not at the START_DOCUMENT.
      *
-     * @param ombuilderFactory
+     * @param omFactory
      * @param parser
-     * @param characterEncoding
+     * @param encoding
      */
-    protected StAXBuilder(OMFactory ombuilderFactory, 
+    protected StAXBuilder(OMFactory omFactory, 
                           XMLStreamReader parser, 
-                          String characterEncoding) {
-        omfactory = (OMFactoryEx)ombuilderFactory;
-        charEncoding = characterEncoding;
-        initParser(parser);
+                          String encoding) {
+        this(omFactory, parser, encoding, null);
     }
 
     private void initParser(XMLStreamReader parser) {
@@ -178,6 +193,7 @@ public abstract class StAXBuilder implem
      * @deprecated
      */
     protected StAXBuilder() {
+        closeable = null;
     }
 
     /**
@@ -738,6 +754,9 @@ public abstract class StAXBuilder implem
         try {
             if (!isClosed()) {
                 parser.close();
+                if (closeable != null) {
+                    closeable.close();
+                }
             }
         } catch (Throwable e) {
             // Can't see a reason why we would want to surface an exception

Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=1681352&r1=1681351&r2=1681352&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java Sat May 23 14:13:57 2015
@@ -41,6 +41,8 @@ import javax.xml.stream.XMLStreamConstan
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.Location;
+
+import java.io.Closeable;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
@@ -97,6 +99,13 @@ public class StAXOMBuilder extends StAXB
     private int lookAheadToken = -1;
     
     /**
+     * For internal use only.
+     */
+    public StAXOMBuilder(OMFactory ombuilderFactory, XMLStreamReader parser, Closeable closeable) {
+        super(ombuilderFactory, parser, closeable);
+    }
+    
+    /**
      * Constructor StAXOMBuilder.
      *
      * @param ombuilderFactory

Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java?rev=1681352&r1=1681351&r2=1681352&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/XOPAwareStAXOMBuilder.java Sat May 23 14:13:57 2015
@@ -31,6 +31,7 @@ import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
+import java.io.Closeable;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
@@ -103,9 +104,12 @@ public class XOPAwareStAXOMBuilder
         this.attachments = attachments;
     }
 
+    /**
+     * For internal use only.
+     */
     public XOPAwareStAXOMBuilder(OMFactory omFactory, XMLStreamReader reader,
-            MimePartProvider mimePartProvider) {
-        super(omFactory, new XOPDecodingStreamReader(reader, mimePartProvider));
+            MimePartProvider mimePartProvider, Closeable closeable) {
+        super(omFactory, new XOPDecodingStreamReader(reader, mimePartProvider), closeable);
         attachments = null;
     }
 

Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java?rev=1681352&r1=1681351&r2=1681352&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/MTOMStAXSOAPModelBuilder.java Sat May 23 14:13:57 2015
@@ -19,6 +19,8 @@
 
 package org.apache.axiom.soap.impl.builder;
 
+import java.io.Closeable;
+
 import org.apache.axiom.attachments.Attachments;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.impl.builder.OMAttachmentAccessorMimePartProvider;
@@ -61,9 +63,13 @@ public class MTOMStAXSOAPModelBuilder ex
         this.attachments = attachments;
     }
     
+    /**
+     * For internal use only.
+     */
     public MTOMStAXSOAPModelBuilder(SOAPFactory soapFactory, XMLStreamReader reader,
-            MimePartProvider mimePartProvider) {
-        super(new XOPDecodingStreamReader(reader, mimePartProvider), soapFactory, soapFactory.getSoapVersionURI());
+            MimePartProvider mimePartProvider, Closeable closeable) {
+        super(new XOPDecodingStreamReader(reader, mimePartProvider), soapFactory,
+                soapFactory.getSoapVersionURI(), closeable);
         attachments = null;
     }
 

Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/StAXSOAPModelBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/StAXSOAPModelBuilder.java?rev=1681352&r1=1681351&r2=1681352&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/StAXSOAPModelBuilder.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/impl/builder/StAXSOAPModelBuilder.java Sat May 23 14:13:57 2015
@@ -19,6 +19,8 @@
 
 package org.apache.axiom.soap.impl.builder;
 
+import java.io.Closeable;
+
 import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.OMContainer;
 import org.apache.axiom.om.OMDocument;
@@ -125,6 +127,15 @@ public class StAXSOAPModelBuilder extend
     }
     
     /**
+     * For internal use only.
+     */
+    public StAXSOAPModelBuilder(OMMetaFactory metaFactory, XMLStreamReader parser,
+            Closeable closeable) {
+        super(metaFactory.getOMFactory(), parser, closeable);
+        this.metaFactory = metaFactory;
+    }
+    
+    /**
      * Constructor.
      * 
      * @param metaFactory the meta factory used to get the appropriate {@link SOAPFactory}
@@ -132,11 +143,20 @@ public class StAXSOAPModelBuilder extend
      * @param parser the parser to read the SOAP message from
      */
     public StAXSOAPModelBuilder(OMMetaFactory metaFactory, XMLStreamReader parser) {
-        super(metaFactory.getOMFactory(), parser);
-        this.metaFactory = metaFactory;
+        this(metaFactory, parser, (Closeable)null);
     }
 
     /**
+     * For internal use only.
+     */
+    public StAXSOAPModelBuilder(XMLStreamReader parser, SOAPFactory factory, String soapVersion,
+            Closeable closeable) {
+        super(factory, parser, closeable);
+        soapFactory = (SOAPFactoryEx)factory;
+        identifySOAPVersion(soapVersion);
+    }
+    
+    /**
      * Constructor.
      * 
      * @param parser the parser to read the SOAP message from
@@ -145,9 +165,7 @@ public class StAXSOAPModelBuilder extend
      *                    of the message
      */
     public StAXSOAPModelBuilder(XMLStreamReader parser, SOAPFactory factory, String soapVersion) {
-        super(factory, parser);
-        soapFactory = (SOAPFactoryEx)factory;
-        identifySOAPVersion(soapVersion);
+        this(parser, factory, soapVersion, null);
     }
 
     /** @param soapVersionURIFromTransport  */

Modified: webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java?rev=1681352&r1=1681351&r2=1681352&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java (original)
+++ webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java Sat May 23 14:13:57 2015
@@ -18,9 +18,15 @@
  */
 package org.apache.axiom.ts.dom.builder;
 
+import static com.google.common.truth.Truth.assertThat;
+
+import javax.activation.URLDataSource;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 
+import org.apache.axiom.testutils.activation.InstrumentedDataSource;
+import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistration;
+import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistry;
 import org.apache.axiom.ts.dom.DOMTestCase;
 import org.apache.axiom.ts.xml.XMLSample;
 import org.w3c.dom.Document;
@@ -34,8 +40,16 @@ public class TestParseURI extends DOMTes
     }
 
     protected void runTest() throws Throwable {
-        DocumentBuilder builder = dbf.newDocumentBuilder();
-        Document document = builder.parse(XMLSample.SIMPLE.getUrl().toString());
-        assertEquals("root", document.getDocumentElement().getLocalName());
+        InstrumentedDataSource ds = new InstrumentedDataSource(new URLDataSource(
+                XMLSample.SIMPLE.getUrl()));
+        DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds);
+        try {
+            DocumentBuilder builder = dbf.newDocumentBuilder();
+            Document document = builder.parse(registration.getURL().toExternalForm());
+            assertThat(document.getDocumentElement().getLocalName()).isEqualTo("root");
+            assertThat(ds.getOpenStreamCount()).isEqualTo(0);
+        } finally {
+            registration.unregister();
+        }
     }
 }