KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > containers > ContainersChangeEventListener


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
//
15
// 28.05.2002 NK Creation
16

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 /**
32  * Listener for containers and container list change events.
33  * Created for container search caching purpose.
34  * You must access this Singleton through JahiaListenersRegistry
35  *
36  * @see JahiaListenersRegistry
37  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
38  */

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 JavaDoc CONTAINER_ADDED = "containerAdded";
46     public static final String JavaDoc CONTAINER_UPDATED = "containerUpdated";
47     public static final String JavaDoc CONTAINER_DELETED = "containerDeleted";
48     public static final String JavaDoc CONTAINER_ACTIVATED = "containerActivated";
49     public static final String JavaDoc CONTAINER_MARKED_FOR_DELETION = "containerMarkedForDeletion";
50
51     // private static Cache cache = null;
52
// Cache that holds containers update status ( last modif time etc... )
53
public static final String JavaDoc CONTAINER_UPDATE_DATE_CACHE = "ContainerUpdateDateCache";
54     // Cache that holds containers update status ( last modif time etc... )
55
public static final String JavaDoc 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         // cache = CacheFactory.createCache(CacheFactory.CONTAINER_UPDATE_STATUS_CACHE,
63
// "Cache of container update status");
64
// cache.registerListener(this);
65
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     //--------------------------------------------------------------------------
72
/**
73      * triggered when Jahia adds a container
74      *
75      * @param je the associated JahiaEvent
76      */

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 JavaDoc t ){
85             logger.error("Error notify added container ",t);
86         }
87     }
88
89     //--------------------------------------------------------------------------
90
/**
91      * triggered when Jahia updates a container
92      *
93      * @param je the associated JahiaEvent
94      */

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 JavaDoc t ){
103             logger.error("Error notify updated container ",t);
104         }
105     }
106
107     //--------------------------------------------------------------------------
108
/**
109      * triggered when Jahia adds a container
110      *
111      * @param je the associated JahiaEvent
112      */

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 JavaDoc t ){
121             logger.error("Error notify deleted container ",t);
122         }
123     }
124
125     //--------------------------------------------------------------------------
126
/**
127      * set the last modifying time for the given container,
128      * the container list containing the container is updated too.
129      * Notify registered observers
130      *
131      * @param JahiaContainer the container.
132      */

133     public synchronized void notifyChange( ContentContainer container, String JavaDoc operation ) {
134
135         if ( container == null ){
136             return;
137         }
138
139         // Set search time
140
long now = System.currentTimeMillis();
141
142         lastContainerUpdateTime = now;
143
144         putCtnLastChangeInMap(container.getID(),now);
145         putCtnListLastChangeInMap(container.getParentContainerListID(),now);
146     }
147
148     //--------------------------------------------------------------------------
149
/**
150      * Returns the last modifying time for the given container list.
151      *
152      * @param ctnListID the container list id.
153      * @return long time, the last change time. -1 if information not available.
154      */

155     public long getCtnListLastChangeTime( int ctnListID ) {
156
157         Long JavaDoc L = (Long JavaDoc)containerListDateCache.get(new Integer JavaDoc(ctnListID));
158         if ( L == null ){
159             return -1;
160         }
161         try {
162             return L.longValue();
163         } catch ( Throwable JavaDoc t ){
164         }
165         return -1;
166     }
167
168     //--------------------------------------------------------------------------
169
/**
170      * Returns the last modifying time for the given container.
171      *
172      * @param ctnID the container id.
173      * @return long time, the last change time. -1 if information not available.
174      */

175     public long getContainerLastChangeTime( int ctnID ) {
176
177         Long JavaDoc L = (Long JavaDoc)containerDateCache.get(new Integer JavaDoc(ctnID));
178         if ( L == null ){
179             return -1;
180         }
181         try {
182             return L.longValue();
183         } catch ( Throwable JavaDoc t ){
184         }
185         return -1;
186     }
187
188     //--------------------------------------------------------------------------
189
/**
190      * Returns the last modifying time for any container.
191      *
192      * @return long time, the last change time. -1 if information not available.
193      */

194     public long getContainerLastChangeTime() {
195         return lastContainerUpdateTime;
196     }
197
198     /**
199      * This method is called each time the cache flushes its items.
200      * Warning : no calls to flush should be done in this method or this will
201      * result in recursive calls !
202      *
203      * @param cacheName the name of the cache which flushed its items.
204      */

205     public void onCacheFlush (String JavaDoc cacheName) {
206         // do nothing
207
}
208
209     /**
210      * This method is called each time the cache flushes its items.
211      * Warning : no calls to put with propagate activated should be done in
212      * this method or this will result in recursive calls !
213      *
214      * @param cacheName the name of the cache which flushed its items.
215      */

216     public void onCachePut (String JavaDoc cacheName, Object JavaDoc 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 JavaDoc(containerId),new Long JavaDoc(date));
229         }
230
231     private synchronized void putCtnListLastChangeInMap(int containerListId,
232         long date){
233         containerListDateCache.put(new Integer JavaDoc(containerListId),new Long JavaDoc(date));
234     }
235
236 } // end ContainersChangeEventListener
237
Popular Tags