You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2007/10/02 23:11:00 UTC

svn commit: r581385 - in /incubator/cxf/trunk: common/common/src/main/java/org/apache/cxf/helpers/ common/common/src/main/java/org/apache/cxf/resource/ rt/core/src/main/java/org/apache/cxf/catalog/ rt/core/src/main/java/org/apache/cxf/wsdl11/

Author: dkulp
Date: Tue Oct  2 14:10:58 2007
New Revision: 581385

URL: http://svn.apache.org/viewvc?rev=581385&view=rev
Log:
Fix a bunch of issues with InputStreams not being closed from wsdl/schema processing.

Modified:
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogWSDLLocator.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogXmlSchemaURIResolver.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/ResourceManagerWSDLLocator.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLLocatorImpl.java

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java?rev=581385&r1=581384&r2=581385&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java Tue Oct  2 14:10:58 2007
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.helpers;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -115,17 +116,48 @@
 
         return sb.toString();
     }
+    
+    /**
+     * Load the InputStream into memory and return a ByteArrayInputStream that 
+     * represents it.  Closes the in stream.
+     * @param in
+     * @return
+     * @throws IOException
+     */
+    public static ByteArrayInputStream loadIntoBAIS(InputStream in) throws IOException {
+        int i = in.available();
+        if (i < DEFAULT_BUFFER_SIZE) {
+            i = DEFAULT_BUFFER_SIZE;
+        }
+        LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream(i);
+        copy(in, bout);
+        in.close();
+        return bout.createInputStream();
+    }
 
     public static byte[] readBytesFromStream(InputStream in) throws IOException {
-
-        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
-
-        for (int i = in.read(); i != -1; i = in.read()) {
-            bos.write(i);
+        int i = in.available();
+        if (i < DEFAULT_BUFFER_SIZE) {
+            i = DEFAULT_BUFFER_SIZE;
         }
-
+        ByteArrayOutputStream bos = new ByteArrayOutputStream(i);
+        copy(in, bos);
         in.close();
-
         return bos.toByteArray();
+    }
+    
+    //class to create a BAIS from a BAOS but without the copies of the byte[] into
+    //one of the exact size.
+    static class LoadingByteArrayOutputStream extends ByteArrayOutputStream {
+        public LoadingByteArrayOutputStream() {
+            super(1024);
+        }
+        public LoadingByteArrayOutputStream(int i) {
+            super(i);
+        }
+        
+        public ByteArrayInputStream createInputStream() {
+            return new ByteArrayInputStream(buf, 0, count);
+        }
     }
 }

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java?rev=581385&r1=581384&r2=581385&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java Tue Oct  2 14:10:58 2007
@@ -66,13 +66,13 @@
     }
     
     public void close() {
-        try {
-            while (!resourceOpened.isEmpty()) {
+        while (!resourceOpened.isEmpty()) {
+            try {
                 InputStream in = resourceOpened.pop();
                 in.close();
+            } catch (IOException ioe) {
+                // move on...
             }
-        } catch (IOException ioe) {
-            // move on...
         }
     }
     

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogWSDLLocator.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogWSDLLocator.java?rev=581385&r1=581384&r2=581385&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogWSDLLocator.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogWSDLLocator.java Tue Oct  2 14:10:58 2007
@@ -39,14 +39,13 @@
     private String baseUri;
     
     public CatalogWSDLLocator(String wsdlUrl, OASISCatalogManager catalogManager) {
-        this.wsdlUrl = wsdlUrl;
-        this.baseUri = this.wsdlUrl;
+        this.baseUri = wsdlUrl;
         this.catalogResolver = catalogManager.getCatalog();
         this.resolver = new ExtendedURIResolver();
     }
 
     public InputSource getBaseInputSource() {
-        InputSource result =  resolver.resolve(baseUri, null);
+        InputSource result = resolver.resolve(baseUri, null);
         if (result == null) {
             try {
                 String s = catalogResolver.resolveSystem(baseUri);
@@ -59,12 +58,26 @@
                 //ignore
             }
         }
+        if (wsdlUrl == null
+            && result != null) {
+            wsdlUrl = result.getSystemId();
+        }
         baseUri = resolver.getURI();
         return result;
     }
 
     public String getBaseURI() {
-        return getBaseInputSource().getSystemId();
+        if (wsdlUrl == null) {
+            InputSource is = getBaseInputSource();
+            if (is.getByteStream() != null) {
+                try {
+                    is.getByteStream().close();
+                } catch (IOException e) {
+                    //ignore
+                }
+            }
+        }
+        return wsdlUrl;
     }
 
     public String getLatestImportURI() {
@@ -92,7 +105,7 @@
         if (resolvedImportLocation == null) {
             in = this.resolver.resolve(importLocation, this.baseUri);
         } else {
-            in =  this.resolver.resolve(resolvedImportLocation, null);
+            in = this.resolver.resolve(resolvedImportLocation, null);
         }
 
         // XXX: If we return null (as per javadoc), a NPE is raised in WSDL4J code.

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogXmlSchemaURIResolver.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogXmlSchemaURIResolver.java?rev=581385&r1=581384&r2=581385&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogXmlSchemaURIResolver.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/catalog/CatalogXmlSchemaURIResolver.java Tue Oct  2 14:10:58 2007
@@ -19,9 +19,11 @@
 package org.apache.cxf.catalog;
 
 import java.io.IOException;
+import java.io.InputStream;
 
 import org.xml.sax.InputSource;
 
+import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.resource.ExtendedURIResolver;
 import org.apache.ws.commons.schema.XmlSchemaException;
 import org.apache.ws.commons.schema.resolver.URIResolver;
@@ -71,6 +73,22 @@
                                          + (baseUri == null
                                             ? "."
                                             : ", relative to '" + baseUri + "'."));
+        } else if (in.getByteStream() != null) {
+            //workaround bug in XmlSchema - XmlSchema is not closing the InputStreams
+            //that are returned for imports.  Thus, with a lot of services starting up 
+            //or a lot of schemas imported or similar, it's easy to run out of
+            //file handles.  We'll just load the file into a byte[] and return that.
+            try {
+                InputStream ins = IOUtils.loadIntoBAIS(in.getByteStream());
+                in.setByteStream(ins);
+            } catch (IOException e) {
+                throw new XmlSchemaException("Unable to load imported document "
+                                             + "at '" + schemaLocation + "'"
+                                             + (baseUri == null
+                                                ? "."
+                                                : ", relative to '" + baseUri + "'."),
+                                                e);
+            }
         }
 
         return in;

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/ResourceManagerWSDLLocator.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/ResourceManagerWSDLLocator.java?rev=581385&r1=581384&r2=581385&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/ResourceManagerWSDLLocator.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/ResourceManagerWSDLLocator.java Tue Oct  2 14:10:58 2007
@@ -18,6 +18,7 @@
  */
 package org.apache.cxf.wsdl11;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 
@@ -35,6 +36,8 @@
     Bus bus;
     String wsdlUrl;
     InputSource last;
+    String baseUri;
+    boolean fromParent;
     
     public ResourceManagerWSDLLocator(String wsdlUrl,
                                       WSDLLocator parent,
@@ -53,13 +56,21 @@
 
 
     public void close() {
-        if (last != null) {
-            parent.close();
+        if (!fromParent) {
+            try {
+                if (last.getByteStream() != null) {
+                    last.getByteStream().close();
+                }
+            } catch (IOException e) {
+                //ignore
+            }
         }
+        parent.close();
     }
 
     public InputSource getBaseInputSource() {
         InputSource is = parent.getBaseInputSource();
+        fromParent = true;
         if (is == null) {
             InputStream ins = bus.getExtension(ResourceManager.class).getResourceAsStream(wsdlUrl);
             is = new InputSource(ins);
@@ -71,29 +82,35 @@
                 is.setSystemId(url.toString());
                 is.setPublicId(url.toString());
             }
-            last = is;
+            fromParent = false;
+            baseUri = is.getPublicId();
         } else {
-            last = null;
+            baseUri = is.getSystemId();
         }
+        last = is;
         
         return is;
     }
 
     public String getBaseURI() {
-        getBaseInputSource();
         if (last == null) {
-            return parent.getBaseURI();
+            getBaseInputSource();
+            try {
+                if (last.getByteStream() != null) {
+                    last.getByteStream().close();
+                }
+            } catch (IOException e) {
+                //ignore
+            }
         }
-        return last.getPublicId();
+        return baseUri;
     }
 
     public InputSource getImportInputSource(String parentLocation, String importLocation) {
-        // TODO Auto-generated method stub
         return parent.getImportInputSource(parentLocation, importLocation);
     }
 
     public String getLatestImportURI() {
-        // TODO Auto-generated method stub
         return parent.getLatestImportURI();
     }
 

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLLocatorImpl.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLLocatorImpl.java?rev=581385&r1=581384&r2=581385&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLLocatorImpl.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLLocatorImpl.java Tue Oct  2 14:10:58 2007
@@ -70,7 +70,7 @@
     }
 
     public InputSource getBaseInputSource() {
-        InputSource result =  resolve(baseUri, null);
+        InputSource result = resolve(baseUri, null);
         baseUri = resolver.getURI();
         return result;
     }