cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r964818 - in /cxf/trunk/tools/validator/src: main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java test/java/org/apache/cxf/tools/validator/internal/Stax2DOMTest.java
Date Fri, 16 Jul 2010 15:04:40 GMT
Author: dkulp
Date: Fri Jul 16 15:04:40 2010
New Revision: 964818

URL: http://svn.apache.org/viewvc?rev=964818&view=rev
Log:
[CXF-2900] WSDLValidator through Stax2DOM leaves input streams open
which causes input files to be locked.

Patch from Gary Gregory applied (with mods to not require woodstox)

Modified:
    cxf/trunk/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java
    cxf/trunk/tools/validator/src/test/java/org/apache/cxf/tools/validator/internal/Stax2DOMTest.java

Modified: cxf/trunk/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java?rev=964818&r1=964817&r2=964818&view=diff
==============================================================================
--- cxf/trunk/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java
(original)
+++ cxf/trunk/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java
Fri Jul 16 15:04:40 2010
@@ -20,8 +20,13 @@
 package org.apache.cxf.tools.validator.internal;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.stream.StreamSource;
 
 import org.w3c.dom.Document;
@@ -36,8 +41,6 @@ public class Stax2DOM {
     public Stax2DOM() {
     }
 
-
-
     public Document getDocument(String wsdl) throws ToolException {
         try {
             URI wsdlURI = new URI(URIParserUtil.getAbsoluteURI(wsdl));
@@ -53,22 +56,49 @@ public class Stax2DOM {
     }
 
     public Document getDocument(URL url) throws ToolException {
+        InputStream input = null;
         try {
-            StreamSource src = new StreamSource(url.openStream(), url.toExternalForm());
+            input = url.openStream();
+            StreamSource src = new StreamSource(input, url.toExternalForm());
             return StaxUtils.read(StaxUtils.createXMLStreamReader(src), true);
         } catch (Exception e) {
             throw new ToolException(e);
+        } finally {
+            if (input != null) {
+                try {
+                    input.close();
+                } catch (IOException e) {
+                    // throw or change do nothing.
+                    throw new ToolException(e);
+                }
+            }
         }
     }
 
     public Document getDocument(File wsdl) throws ToolException {
+        XMLStreamReader reader = null;
         try {
-            StreamSource src = new StreamSource(wsdl);
-            return StaxUtils.read(StaxUtils.createXMLStreamReader(src), true);
+            StreamSource source = new StreamSource(wsdl);
+            reader = StaxUtils.createXMLStreamReader(source);
+            return StaxUtils.read(reader, true);
         } catch (Exception e) {
             throw new ToolException(e);
+        } finally {
+            try {
+                try {
+                    //on woodstox, calling closeCompletely will allow any 
+                    //cached things like dtds and such to be completely
+                    //closed and cleaned up.
+                    reader.getClass().getMethod("closeCompletely").invoke(reader);
+                } catch (Throwable t) {
+                    //ignore
+                }
+                reader.close();
+            } catch (XMLStreamException e) {
+                // throw or change do nothing.
+                throw new ToolException(e);
+            }
         }
     }
 
-
 }

Modified: cxf/trunk/tools/validator/src/test/java/org/apache/cxf/tools/validator/internal/Stax2DOMTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/validator/src/test/java/org/apache/cxf/tools/validator/internal/Stax2DOMTest.java?rev=964818&r1=964817&r2=964818&view=diff
==============================================================================
--- cxf/trunk/tools/validator/src/test/java/org/apache/cxf/tools/validator/internal/Stax2DOMTest.java
(original)
+++ cxf/trunk/tools/validator/src/test/java/org/apache/cxf/tools/validator/internal/Stax2DOMTest.java
Fri Jul 16 15:04:40 2010
@@ -20,9 +20,14 @@
 package org.apache.cxf.tools.validator.internal;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URISyntaxException;
 
 import org.w3c.dom.Document;
 
+import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.helpers.XMLUtils;
 import org.junit.Assert;
 import org.junit.Test;
@@ -31,11 +36,63 @@ public class Stax2DOMTest extends Assert
 
     @Test
     public void testGetDocument() throws Exception {
-        File wsdl = new File(getClass().getResource("/validator_wsdl/jms_test.wsdl").toURI());
-        Document doc = new Stax2DOM().getDocument(wsdl);
+        File wsdlFile = new File(getClass().getResource(
+                "/validator_wsdl/jms_test.wsdl").toURI());
+        Document doc = new Stax2DOM().getDocument(wsdlFile);
+        assertDocumentContent(doc);
+    }
+
+    public void assertDocumentContent(Document doc) {
         String content = XMLUtils.toString(doc);
-        assertTrue(content, content.indexOf("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"")
!= -1);
-        assertTrue(content, content.indexOf("xmlns:x1=\"http://cxf.apache.org/hello_world_jms/types\"")
-                                           != -1);
+        assertTrue(
+                content,
+                content.indexOf("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"") != -1);
+        assertTrue(
+                content,
+                content.indexOf("xmlns:x1=\"http://cxf.apache.org/hello_world_jms/types\"")
!= -1);
+    }
+
+    @Test
+    public void testGetDocumentURL() throws Exception {
+        File tempFile = createTempWsdlFile();
+        Document doc = new Stax2DOM().getDocument(tempFile.toURI().toURL());
+        assertDocumentContent(doc);
+        assertDelete(tempFile);
+    }
+
+    @Test
+    public void testGetDocumentString() throws Exception {
+        File tempFile = createTempWsdlFile();
+        Document doc = new Stax2DOM().getDocument(tempFile.toURI().toString());
+        assertDocumentContent(doc);
+        assertDelete(tempFile);
+    }
+
+    public void assertDelete(File tempFile) {
+        boolean deleted = tempFile.delete();
+        if (!deleted) {
+            tempFile.deleteOnExit();
+        }
+        Assert.assertTrue(
+                "Stax2DOM left the input stream open, file cannot be deleted: "
+                        + tempFile, deleted);
+    }
+
+    public File createTempWsdlFile() throws URISyntaxException, IOException {
+        File wsdlFile = new File(getClass().getResource(
+                "/validator_wsdl/jms_test.wsdl").toURI());
+        File tempFile = File.createTempFile("Stax2DOMTest", ".wsdl");
+        FileOutputStream output = new FileOutputStream(tempFile);
+        IOUtils.copyAndCloseInput(new FileInputStream(wsdlFile), output);
+        output.close();
+        return tempFile;
+    }
+
+    @Test
+    public void testGetDocumentFile() throws Exception {
+        File tempFile = createTempWsdlFile();
+        Document doc = new Stax2DOM().getDocument(tempFile);
+        assertDocumentContent(doc);
+        assertDelete(tempFile);
     }
 }



Mime
View raw message