From 214d8519fc6cea5ad7d48229f247b05af513b066 Mon Sep 17 00:00:00 2001
From: Jerome Velociter <jerome.velociter@gmail.com>
Date: Tue, 23 Aug 2011 23:55:38 +0200
Subject: [PATCH] XWIKI-6855 New xwiki-thumbnails module to manipulate
 thumbnails and provide URL for thumbnail images

Fix cache flushing mechanism by implementing just one component role and
having the event listener manually registered against the OM (thanks
Vincent!)
---
 .../internal/DefaultThumbnailURLProvider.java | 94 ++++++++++++-------
 1 file changed, 61 insertions(+), 33 deletions(-)

diff --git a/xwiki-platform-core/xwiki-platform-thumbnails/src/main/java/org/xwiki/thumbnails/internal/DefaultThumbnailURLProvider.java b/xwiki-platform-core/xwiki-platform-thumbnails/src/main/java/org/xwiki/thumbnails/internal/DefaultThumbnailURLProvider.java
index 8b608d554ca..f9eb7d523fd 100644
--- a/xwiki-platform-core/xwiki-platform-thumbnails/src/main/java/org/xwiki/thumbnails/internal/DefaultThumbnailURLProvider.java
+++ b/xwiki-platform-core/xwiki-platform-thumbnails/src/main/java/org/xwiki/thumbnails/internal/DefaultThumbnailURLProvider.java
@@ -27,6 +27,7 @@
 
 import javax.inject.Inject;
 import javax.inject.Named;
+import javax.inject.Singleton;
 
 import org.slf4j.Logger;
 import org.xwiki.bridge.DocumentAccessBridge;
@@ -38,12 +39,15 @@
 import org.xwiki.cache.config.CacheConfiguration;
 import org.xwiki.cache.eviction.LRUEvictionConfiguration;
 import org.xwiki.component.annotation.Component;
+import org.xwiki.component.phase.Initializable;
+import org.xwiki.component.phase.InitializationException;
 import org.xwiki.model.EntityType;
 import org.xwiki.model.reference.AttachmentReference;
 import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.model.reference.EntityReferenceSerializer;
 import org.xwiki.model.reference.ObjectPropertyReference;
 import org.xwiki.observation.EventListener;
+import org.xwiki.observation.ObservationManager;
 import org.xwiki.observation.event.Event;
 import org.xwiki.thumbnails.NoSuchImageException;
 import org.xwiki.thumbnails.ThumbnailURLProvider;
@@ -57,7 +61,8 @@
  * @since 2.3-M2
  */
 @Component
-public class DefaultThumbnailURLProvider implements ThumbnailURLProvider, EventListener
+@Singleton
+public class DefaultThumbnailURLProvider implements ThumbnailURLProvider, Initializable
 {
     /**
      * The question mark sign (indicates the beginning of query string parameters in a URL).
@@ -95,6 +100,12 @@ public class DefaultThumbnailURLProvider implements ThumbnailURLProvider, EventL
     @Inject
     private ThumbnailsConfiguration thumbnailsConfiguration;
     
+    /**
+     * Observation manager, used to add the listener that will flush cache entries when document update occurs.
+     */
+    @Inject
+    private ObservationManager observationManager;
+    
     /**
      * Cache for documents that holds a property pointing to an image.
      * 
@@ -131,12 +142,11 @@ public String getURL(ObjectPropertyReference reference, Map<String, Object> extr
         if (reference == null) {
             throw new NoSuchImageException();
         }
-
+        
         DocumentReference documentReference = (DocumentReference) reference.extractReference(EntityType.DOCUMENT);
         String serializedDocumentReference = (String) this.serializer.serialize(documentReference);
         String serializedReference = (String) this.serializer.serialize(reference);
         String imageName;
-
         if (this.getPropertiesValueCache().get(serializedDocumentReference) != null
             && this.getPropertiesValueCache().get(serializedDocumentReference).containsKey(serializedReference)) {
             // Cache already contains this property
@@ -310,6 +320,8 @@ private Cache<Map<String, String>> getPropertiesValueCache()
             } catch (CacheException e) {
                 this.logger.error("Error initializing the property document cache.", e);
             }
+            
+            this.getPropertiesValueCache();
         }
         return propertiesValueCache;
     }
@@ -336,6 +348,7 @@ private Cache<Map<String, String>> getThumbnailURLCache()
             } catch (CacheException e) {
                 this.logger.error("Error initializing the thumbnails url cache.", e);
             }
+
         }
         return this.thumbnailURLCache;
     }
@@ -343,47 +356,62 @@ private Cache<Map<String, String>> getThumbnailURLCache()
     /**
      * {@inheritDoc}
      */
-    public List<Event> getEvents()
+    public void initialize() throws InitializationException
     {
-        return Arrays.<Event> asList(new DocumentUpdatedEvent());
+        this.observationManager.addListener(new UpdateCachesEventListener());
+        
     }
-
+    
     /**
-     * {@inheritDoc}
+     * Event listener that flush cache entries when document update occurs.
      */
-    public String getName()
+    private class UpdateCachesEventListener implements EventListener
     {
-        return "thumbnailURLProvider";
-    }
+        /**
+         * {@inheritDoc}
+         */
+        public List<Event> getEvents()
+        {
+            return Arrays.<Event> asList(new DocumentUpdatedEvent());
+        }
 
-    /**
-     * {@inheritDoc}
-     */
-    public void onEvent(Event event, Object data, Object arg2)
-    {
-        try {
-            DocumentModelBridge document = (DocumentModelBridge) data;
-            if (this.getThumbnailURLCache().get(
-                (String) this.serializer.serialize(document.getDocumentReference())) != null) {
+        /**
+         * {@inheritDoc}
+         */
+        public String getName()
+        {
+            return "thumbnailURLProvider";
+        }
 
-                // Pessimistic cache flushing : if a document with an image with a thumbnail URL
-                // has been saved ; just remove this document from the cache.
+        /**
+         * {@inheritDoc}
+         */
+        public void onEvent(Event event, Object data, Object arg2)
+        {
 
-                this.getThumbnailURLCache().remove((String) this.serializer.serialize(document.getDocumentReference()));
-            }
+            try {
+                DocumentModelBridge document = (DocumentModelBridge) data;
+                String serializedReference = (String) serializer.serialize(document.getDocumentReference());
+                if (getThumbnailURLCache().get(serializedReference) != null) {
 
-            if (this.getPropertiesValueCache().get(
-                (String) this.serializer.serialize(document.getDocumentReference())) != null) {
+                    // Pessimistic cache flushing : if a document with an image with a thumbnail URL
+                    // has been saved ; just remove this document from the cache.
+
+                    getThumbnailURLCache().remove(serializedReference);
+                }
 
-                // Pessimistic cache flushing : if a document with a property pointing to an image with
-                // thumbnail has been saved ; just remove this document from the cache.
+                if (getPropertiesValueCache().get(serializedReference) != null) {
 
-                this.getPropertiesValueCache().remove(
-                    (String) this.serializer.serialize(document.getDocumentReference()));
+                    // Pessimistic cache flushing : if a document with a property pointing to an image with
+                    // thumbnail has been saved ; just remove this document from the cache.
+
+                    getPropertiesValueCache().remove(serializedReference);
+                }
+
+            } catch (ClassCastException e) {
+                // Should not happen, but if it does, silently do nothing !
             }
-            
-        } catch (ClassCastException e) {
-            // Should not happen, but if it does, silently do nothing !
         }
-    }   
+    }
+
 }
\ No newline at end of file
-- 
GitLab