]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
* Invalidate/remove entry for the cache properly on AFPDataStream.write().
authorAdrian Cumiskey <acumiskey@apache.org>
Mon, 21 Jul 2008 12:49:59 +0000 (12:49 +0000)
committerAdrian Cumiskey <acumiskey@apache.org>
Mon, 21 Jul 2008 12:49:59 +0000 (12:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@678403 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/render/afp/DataObjectCache.java
src/java/org/apache/fop/render/afp/modca/AFPDataStream.java

index 1c917a75b85c76d582e00bc4ba56d4d48688d2f7..f2631f0cd9e676c4e4a51c75d48af64b2f8c4a0d 100644 (file)
@@ -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) {
index 1d85f60be36168c05e9e6c0c73485b573ace0c6c..c556532146c2273080315e96119666ae8d08f44d 100644 (file)
@@ -209,7 +209,7 @@ public class AFPDataStream extends AbstractResourceGroupContainer {
 
         this.outputStream = null;
 
-        DataObjectCache.getInstance().clear();
+        cache.clear();
     }
 
     /**