Commit 214d8519 authored by Jerome Velociter's avatar Jerome Velociter
Browse files

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!)
parent 12fcf4e7
......@@ -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 @@
@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 @@ public String getURL(AttachmentReference reference, Map<String, Object> extraPar
} catch (CacheException e) {
this.logger.error("Error initializing the property document cache.", e);
}
this.getPropertiesValueCache();
}
return propertiesValueCache;
}
......@@ -336,6 +348,7 @@ public String getURL(AttachmentReference reference, Map<String, Object> extraPar
} catch (CacheException e) {
this.logger.error("Error initializing the thumbnails url cache.", e);
}
}
return this.thumbnailURLCache;
}
......@@ -343,47 +356,62 @@ public String getURL(AttachmentReference reference, Map<String, Object> extraPar
/**
* {@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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment