aboutsummaryrefslogtreecommitdiffstats
path: root/src/java
diff options
context:
space:
mode:
authorPeter Hancock <phancock@apache.org>2013-04-16 10:48:44 +0000
committerPeter Hancock <phancock@apache.org>2013-04-16 10:48:44 +0000
commitda862fff7fc3219f3749a1a3c215b78f1cc7c165 (patch)
tree934017eed2d149705583d6a76d148ef5f8a5e6cb /src/java
parent641a8093e23bccfecd5d73bb85c469c4d84a68da (diff)
downloadxmlgraphics-fop-da862fff7fc3219f3749a1a3c215b78f1cc7c165.tar.gz
xmlgraphics-fop-da862fff7fc3219f3749a1a3c215b78f1cc7c165.zip
Fix & improve the handling of temporary files using the new URI resource resolvers
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
Diffstat (limited to 'src/java')
-rw-r--r--src/java/org/apache/fop/apps/io/ResourceResolverFactory.java56
1 files changed, 46 insertions, 10 deletions
diff --git a/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java b/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java
index cdc9438b9..28b8c8b12 100644
--- a/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java
+++ b/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java
@@ -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();
}
}
}