1 17 18 19 package org.jahia.data.containers; 20 21 import org.apache.log4j.Logger; 22 import org.jahia.data.events.JahiaEvent; 23 import org.jahia.data.events.JahiaEventListener; 24 import org.jahia.exceptions.JahiaException; 25 import org.jahia.registries.JahiaListenersRegistry; 26 import org.jahia.services.cache.Cache; 27 import org.jahia.services.cache.CacheFactory; 28 import org.jahia.services.cache.CacheListener; 29 import org.jahia.services.containers.ContentContainer; 30 31 39 public class ContainersChangeEventListener extends JahiaEventListener implements CacheListener 40 { 41 42 private static Logger logger = Logger 43 .getLogger(ContainersChangeEventListener.class); 44 45 public static final String CONTAINER_ADDED = "containerAdded"; 46 public static final String CONTAINER_UPDATED = "containerUpdated"; 47 public static final String CONTAINER_DELETED = "containerDeleted"; 48 public static final String CONTAINER_ACTIVATED = "containerActivated"; 49 public static final String CONTAINER_MARKED_FOR_DELETION = "containerMarkedForDeletion"; 50 51 public static final String CONTAINER_UPDATE_DATE_CACHE = "ContainerUpdateDateCache"; 54 public static final String CONTAINERLIST_UPDATE_DATE_CACHE = "ContainerListUpdateDateCache"; 56 private static Cache containerDateCache = null; 57 private static Cache containerListDateCache = null; 58 59 private long lastContainerUpdateTime = -1; 60 61 public ContainersChangeEventListener() throws JahiaException { 62 containerDateCache = CacheFactory.createCache(CONTAINER_UPDATE_DATE_CACHE); 66 containerDateCache.registerListener(this); 67 containerListDateCache = CacheFactory.createCache(CONTAINERLIST_UPDATE_DATE_CACHE); 68 containerListDateCache.registerListener(this); 69 } 70 71 77 public void containerAdded( JahiaEvent je ) { 78 logger.debug("Started"); 79 JahiaContainer theObject = (JahiaContainer) je.getObject(); 80 try { 81 ContentContainer contentContainer = 82 ContentContainer.getContainer(theObject.getID()); 83 notifyChange(contentContainer, CONTAINER_ADDED); 84 } catch ( Throwable t ){ 85 logger.error("Error notify added container ",t); 86 } 87 } 88 89 95 public void containerUpdated( JahiaEvent je ) { 96 logger.debug("Started"); 97 JahiaContainer theObject = (JahiaContainer) je.getObject(); 98 try { 99 ContentContainer contentContainer = 100 ContentContainer.getContainer(theObject.getID()); 101 notifyChange(contentContainer, CONTAINER_UPDATED); 102 } catch ( Throwable t ){ 103 logger.error("Error notify updated container ",t); 104 } 105 } 106 107 113 public void containerDeleted( JahiaEvent je ) { 114 logger.debug("Started"); 115 JahiaContainer theObject = (JahiaContainer) je.getObject(); 116 try { 117 ContentContainer contentContainer = 118 ContentContainer.getContainer(theObject.getID()); 119 notifyChange(contentContainer, CONTAINER_DELETED); 120 } catch ( Throwable t ){ 121 logger.error("Error notify deleted container ",t); 122 } 123 } 124 125 133 public synchronized void notifyChange( ContentContainer container, String operation ) { 134 135 if ( container == null ){ 136 return; 137 } 138 139 long now = System.currentTimeMillis(); 141 142 lastContainerUpdateTime = now; 143 144 putCtnLastChangeInMap(container.getID(),now); 145 putCtnListLastChangeInMap(container.getParentContainerListID(),now); 146 } 147 148 155 public long getCtnListLastChangeTime( int ctnListID ) { 156 157 Long L = (Long )containerListDateCache.get(new Integer (ctnListID)); 158 if ( L == null ){ 159 return -1; 160 } 161 try { 162 return L.longValue(); 163 } catch ( Throwable t ){ 164 } 165 return -1; 166 } 167 168 175 public long getContainerLastChangeTime( int ctnID ) { 176 177 Long L = (Long )containerDateCache.get(new Integer (ctnID)); 178 if ( L == null ){ 179 return -1; 180 } 181 try { 182 return L.longValue(); 183 } catch ( Throwable t ){ 184 } 185 return -1; 186 } 187 188 194 public long getContainerLastChangeTime() { 195 return lastContainerUpdateTime; 196 } 197 198 205 public void onCacheFlush (String cacheName) { 206 } 208 209 216 public void onCachePut (String cacheName, Object entryKey) { 217 218 long now = System.currentTimeMillis(); 219 220 if (CONTAINER_UPDATE_DATE_CACHE.equals(cacheName)) { 221 lastContainerUpdateTime = now; 222 } else if (CONTAINERLIST_UPDATE_DATE_CACHE.equals(cacheName)) { 223 lastContainerUpdateTime = now; 224 } 225 } 226 227 private synchronized void putCtnLastChangeInMap(int containerId, long date){ 228 containerDateCache.put(new Integer (containerId),new Long (date)); 229 } 230 231 private synchronized void putCtnListLastChangeInMap(int containerListId, 232 long date){ 233 containerListDateCache.put(new Integer (containerListId),new Long (date)); 234 } 235 236 } | Popular Tags |