You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by re...@apache.org on 2019/09/18 14:45:08 UTC

svn commit: r1867128 - /jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/WorkspaceContentHandler.java

Author: reschke
Date: Wed Sep 18 14:45:08 2019
New Revision: 1867128

URL: http://svn.apache.org/viewvc?rev=1867128&view=rev
Log:
JCR-4482: jcr2spi: WorkspaceContentHandler leaks temp files

Modified:
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/WorkspaceContentHandler.java

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/WorkspaceContentHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/WorkspaceContentHandler.java?rev=1867128&r1=1867127&r2=1867128&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/WorkspaceContentHandler.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/WorkspaceContentHandler.java Wed Sep 18 14:45:08 2019
@@ -20,6 +20,8 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 
 import javax.jcr.RepositoryException;
 import javax.jcr.Workspace;
@@ -46,6 +48,7 @@ public class WorkspaceContentHandler ext
     private final Workspace workspace;
 
     private final File tmpFile;
+    private final OutputStream tmpStream;
     private final ContentHandler delegatee;
 
     public WorkspaceContentHandler(Workspace workspace, String parentAbsPath, int uuidBehavior) throws RepositoryException {
@@ -56,8 +59,8 @@ public class WorkspaceContentHandler ext
         try {
             String tmpName = Text.md5(parentAbsPath);
             this.tmpFile = File.createTempFile("___" + tmpName, ".xml");
-            this.delegatee = SerializingContentHandler.getSerializer(
-                    new FileOutputStream(tmpFile));
+            this.tmpStream = new FileOutputStream(tmpFile);
+            this.delegatee = SerializingContentHandler.getSerializer(tmpStream);
         } catch (FileNotFoundException e) {
             throw new RepositoryException(e);
         } catch (IOException e) {
@@ -70,13 +73,19 @@ public class WorkspaceContentHandler ext
     @Override
     public void endDocument() throws SAXException {
         delegatee.endDocument();
-        try {
-            workspace.importXML(parentAbsPath, new FileInputStream(tmpFile), uuidBehavior);
+        try (InputStream is = new FileInputStream(tmpFile)) {
+            workspace.importXML(parentAbsPath, is, uuidBehavior);
         } catch (IOException e) {
             throw new SAXException(e);
         } catch (RepositoryException e) {
             throw new SAXException(e);
         } finally {
+            try {
+                tmpStream.close();
+            } catch (IOException ex) {
+                // best effort
+                log.error("closing temp file stream", ex);
+            }
             tmpFile.delete();
         }
     }
@@ -130,4 +139,4 @@ public class WorkspaceContentHandler ext
     public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
         delegatee.startElement(namespaceURI, localName, qName, atts);
     }
-}
\ No newline at end of file
+}