]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Fix & improve the handling of temporary files using the new URI resource resolvers
authorPeter Hancock <phancock@apache.org>
Tue, 16 Apr 2013 10:48:44 +0000 (10:48 +0000)
committerPeter Hancock <phancock@apache.org>
Tue, 16 Apr 2013 10:48:44 +0000 (10:48 +0000)
Thanks to Alexios Giotis for valuable contribution

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1468361 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/apps/io/ResourceResolverFactory.java
status.xml

index cdc9438b94d0f4cf3248889dd56bbeaa28a4ffb4..28b8c8b1228ef7b7b6f597be4fa68722fc1de6c2 100644 (file)
@@ -21,12 +21,15 @@ package org.apache.fop.apps.io;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.xmlgraphics.io.Resource;
 import org.apache.xmlgraphics.io.ResourceResolver;
@@ -161,24 +164,57 @@ public final class ResourceResolverFactory {
     }
 
     private static class DefaultTempResourceResolver implements TempResourceResolver {
-        private static File getTempFile(String path) throws IOException {
-            File file = new File(System.getProperty("java.io.tmpdir"), path);
-            file.deleteOnExit();
-            return file;
+
+        private final ConcurrentHashMap<String, File> tempFiles = new ConcurrentHashMap<String, File>();
+
+        private File getTempFile(String uri) throws IllegalStateException {
+            File tempFile = tempFiles.remove(uri);
+            if (tempFile == null) {
+                throw new IllegalStateException(uri + " was never created or has been deleted");
+            }
+            return tempFile;
+        }
+
+        private File createTempFile(String path) throws IOException {
+            File tempFile = File.createTempFile(path, ".fop.tmp");
+            File oldFile = tempFiles.put(path, tempFile);
+            if (oldFile != null) {
+                String errorMsg = oldFile.getAbsolutePath() + " has been already created for " + path;
+                boolean newTempDeleted = tempFile.delete();
+                if (!newTempDeleted) {
+                    errorMsg += ". " + tempFile.getAbsolutePath() + " was not deleted.";
+                }
+                throw new IOException(errorMsg);
+            }
+            return tempFile;
         }
 
         /** {@inheritDoc} */
         public Resource getResource(String id) throws IOException {
-            return new Resource(getTempFile(id).toURI().toURL().openStream());
+            return new Resource(new FileDeletingInputStream(getTempFile(id)));
         }
 
         /** {@inheritDoc} */
         public OutputStream getOutputStream(String id) throws IOException {
-            File file = getTempFile(id);
-            if (file.createNewFile()) {
-                return new FileOutputStream(file);
-            } else {
-                throw new IOException("Filed to create temporary file: " + id);
+            return new FileOutputStream(createTempFile(id));
+        }
+    }
+
+    private static class FileDeletingInputStream extends FilterInputStream {
+
+        private final File file;
+
+        protected FileDeletingInputStream(File file) throws MalformedURLException, IOException {
+            super(file.toURI().toURL().openStream());
+            this.file = file;
+        }
+
+        @Override
+        public void close() throws IOException {
+            try {
+                super.close();
+            } finally {
+                file.delete();
             }
         }
     }
index f19fd5963d499b2ba07b1f96e70cc85c523ed9c4..d17168936ab745005fa1cf403a60ad486c2a888a 100644 (file)
@@ -59,6 +59,9 @@
       documents. Example: the fix of marks layering will be such a case when it's done.
     -->
     <release version="FOP Trunk" date="TBD">
+      <action context="Code" dev="PH" type="fix" fixes-bug="FOP-2211" due-to="Alexios Giotis, PH">
+          Fix and improve the handling of temporary files using the new URI resource resolvers
+      </action>
       <action context="Layout" dev="CB" type="fix" fixes-bug="FOP-2217" due-to="Robert Meyer">
         Image scaling change was adversely affecting other image types 
       </action>