From 6012c2eaa647bc650c73b273a2f2dffd8eaafd35 Mon Sep 17 00:00:00 2001 From: Adrian Cumiskey Date: Mon, 21 Jul 2008 12:49:59 +0000 Subject: [PATCH] * Invalidate/remove entry for the cache properly on AFPDataStream.write(). git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@678403 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/render/afp/DataObjectCache.java | 35 ++++++++++++------- .../fop/render/afp/modca/AFPDataStream.java | 2 +- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/java/org/apache/fop/render/afp/DataObjectCache.java b/src/java/org/apache/fop/render/afp/DataObjectCache.java index 1c917a75b..f2631f0cd 100644 --- a/src/java/org/apache/fop/render/afp/DataObjectCache.java +++ b/src/java/org/apache/fop/render/afp/DataObjectCache.java @@ -66,6 +66,9 @@ public final class DataObjectCache { /** The next file pointer position in the cache file */ private long nextPos; + + /** Our assigned cache id */ + private Integer id; /** * Returns an instance of the cache @@ -74,13 +77,12 @@ public final class DataObjectCache { */ public static DataObjectCache getInstance() { synchronized (cacheMap) { - int id = System.identityHashCode(Thread.currentThread()); - Integer cacheKey = new Integer(id); - DataObjectCache cache = (DataObjectCache)cacheMap.get(cacheKey); + Integer cacheId = new Integer(System.identityHashCode(Thread.currentThread())); + DataObjectCache cache = (DataObjectCache)cacheMap.get(cacheId); if (cache == null) { try { - cache = new DataObjectCache(id); - cacheMap.put(cacheKey, cache); + cache = new DataObjectCache(cacheId); + cacheMap.put(cacheId, cache); } catch (IOException e) { log.error("Failed to create cache"); } @@ -92,10 +94,11 @@ public final class DataObjectCache { /** * Default constructor * - * @param id the cache id + * @param cacheId the cache id */ - private DataObjectCache(int id) throws IOException { - this.tempFile = File.createTempFile(CACHE_FILENAME_PREFIX + id, null); + private DataObjectCache(Integer cacheId) throws IOException { + this.id = cacheId; + this.tempFile = File.createTempFile(CACHE_FILENAME_PREFIX + cacheId, null); this.raFile = new RandomAccessFile(tempFile, "rw"); this.channel = raFile.getChannel(); } @@ -107,6 +110,7 @@ public final class DataObjectCache { try { raFile.close(); tempFile.delete(); + cacheMap.remove(id); // remove ourselves from the cache map } catch (IOException e) { log.error("Failed to close temporary file"); } @@ -129,8 +133,7 @@ public final class DataObjectCache { record.size = os.size(); MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, record.position, record.size); - byte[] data = os.toByteArray(); - byteBuffer.put(data); + byteBuffer.put(os.toByteArray()); channel.write(byteBuffer); nextPos += record.size + 1; } catch (IOException e) { @@ -152,12 +155,20 @@ public final class DataObjectCache { Record record = null; if (!objectType.canBeIncluded()) { AbstractNamedAFPObject dataObj = factory.createObject(dataObjectInfo); + if (dataObj == null) { + log.error("Failed to create object: " + dataObjectInfo); + return null; + } record = store(dataObj); } else { ResourceInfo resourceInfo = dataObjectInfo.getResourceInfo(); record = (Record)includableMap.get(resourceInfo); if (record == null) { AbstractNamedAFPObject dataObj = factory.createObject(dataObjectInfo); + if (dataObj == null) { + log.error("Failed to create object: " + dataObjectInfo); + return null; + } record = store(dataObj); includableMap.put(resourceInfo, record); } @@ -166,10 +177,10 @@ public final class DataObjectCache { } /** - * Returns the written binary data of the AbstractDataObject from the cache file + * Returns the written binary data of the AbstractNamedDataObject from the cache file * * @param record the cache record - * @return the binary data of the AbstractDataObject or null if failed. + * @return the binary data of the AbstractNamedDataObject or null if failed. */ public byte[] retrieve(Record record) { if (record == null) { diff --git a/src/java/org/apache/fop/render/afp/modca/AFPDataStream.java b/src/java/org/apache/fop/render/afp/modca/AFPDataStream.java index 1d85f60be..c55653214 100644 --- a/src/java/org/apache/fop/render/afp/modca/AFPDataStream.java +++ b/src/java/org/apache/fop/render/afp/modca/AFPDataStream.java @@ -209,7 +209,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer { this.outputStream = null; - DataObjectCache.getInstance().clear(); + cache.clear(); } /** -- 2.39.5