KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > cache > treecache > JahiaTreeCache


1 package org.jahia.services.cache.treecache;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import javax.transaction.SystemException JavaDoc;
11 import javax.transaction.Transaction JavaDoc;
12 import javax.transaction.TransactionManager JavaDoc;
13
14 import org.apache.log4j.Logger;
15 import org.jahia.exceptions.JahiaInitializationException;
16 import org.jahia.services.cache.Cache;
17 import org.jahia.services.cache.CacheEntry;
18 import org.jahia.services.cache.CacheListener;
19 import org.jboss.cache.CacheException;
20 import org.jboss.cache.ConfigureException;
21 import org.jboss.cache.Fqn;
22 import org.jboss.cache.Node;
23 import org.jboss.cache.PropertyConfigurator;
24 import org.jboss.cache.TreeCache;
25 import org.jboss.cache.TreeCacheMBean;
26 import org.jboss.mx.util.MBeanProxyExt;
27 import org.jboss.mx.util.MBeanServerLocator;
28
29 public class JahiaTreeCache implements Cache {
30     /** logging. */
31     private static final Logger logger = Logger.getLogger(JahiaTreeCache.class);
32
33     private static final String JavaDoc TX_CACHE = "jboss.cache:service=jahiaTxTreeCache";
34     private static final String JavaDoc NON_TX_CACHE = "jboss.cache:service=jahiaNonTxTreeCache";
35     
36     private static final String JavaDoc[] clusteredCacheNames = {TX_CACHE, NON_TX_CACHE};
37     
38     private static TreeCache globalNonTxTreeCache = null;
39     private static TreeCache globalTxTreeCache = null;
40     
41     private TreeCache treeCache = null;
42
43     private Fqn fqn;
44
45     private ArrayList JavaDoc listeners = null;
46
47     private static final Set JavaDoc nonTxCaches = new HashSet JavaDoc(Arrays
48             .asList(new String JavaDoc[] { "LockPrerequisitesResultMap", "LockCache" }));
49
50     /**
51      * <p>
52      * Creates a new <code>Cache</code> instance.
53      * </p>
54      *
55      * @param aName
56      * the cache name
57      * @param propagatedMode
58      * <code>false</code> defines a local cache behaviour, without
59      * synchronization.
60      */

61     protected JahiaTreeCache(final String JavaDoc aName, boolean propagatedMode)
62             throws JahiaInitializationException {
63         try {
64             init(aName, propagatedMode);
65         } catch (Exception JavaDoc e) {
66             throw new JahiaInitializationException("Cannot initialize cache: "
67                     + aName, e);
68         }
69     }
70
71     /**
72      * <p>
73      * Initialize the cache.
74      * </p>
75      *
76      * @param aName
77      * the cache name
78      * @param propagatedMode
79      * <code>false</code> defines a local cache behaviour, without
80      * synchronization.
81      */

82     private void init(String JavaDoc aName, boolean propagatedMode) throws Exception JavaDoc {
83         setTreeCache(getTreeCache(aName));
84         setFqn(new Fqn(aName));
85         TreeCache cache = getTreeCache();
86         if (!cache.exists(aName)){
87           cache.put(aName, new HashMap JavaDoc());
88         }
89     }
90
91     /**
92      * @return Returns the treeCache.
93      */

94     protected TreeCache getTreeCache() {
95         return treeCache;
96     }
97
98     /**
99      * @param aTreeCache
100      * The treeCache to set.
101      */

102     private void setTreeCache(TreeCache aTreeCache) {
103         this.treeCache = aTreeCache;
104     }
105
106     public boolean containsKey(Object JavaDoc entryKey) {
107         boolean containsKey = false;
108         Node node = getTreeNode();
109         if (node != null) {
110             containsKey = node.childExists(entryKey);
111         }
112         return containsKey;
113     }
114
115     public void flush() {
116         flush(true);
117     }
118
119     public void flush(boolean propagate) {
120         try {
121             if (!propagate) {
122                 getTreeCache().evict(getFqn());
123             } else {
124                 getTreeCache().remove(getFqn());
125             }
126
127             getTreeCache().put(getName(), new HashMap JavaDoc());
128
129             if (propagate) {
130                 // call the listeners if present
131
if ((listeners != null) && (listeners.size() > 0)) {
132                     for (int i = 0; i < listeners.size(); i++) {
133                         CacheListener listener = (CacheListener) listeners
134                                 .get(i);
135                         if (listener != null)
136                             listener.onCacheFlush(getName());
137                     }
138                 }
139             }
140         } catch (Exception JavaDoc e) {
141             logger.warn(getName(), e);
142         }
143     }
144
145     public Object JavaDoc get(Object JavaDoc entryKey) {
146         Object JavaDoc result = null;
147
148         CacheEntry entry = getCacheEntry(entryKey);
149         if (entry != null) {
150             result = entry.getObject();
151         }
152
153         return result;
154     }
155
156     protected Fqn getFqn() {
157         return fqn;
158     }
159
160     public String JavaDoc getName() {
161         return (String JavaDoc) fqn.get(0);
162     }
163
164     public Object JavaDoc[] keys() {
165         Object JavaDoc[] keys = null;
166         try {
167             Set JavaDoc treeKeys = getTreeCache().getChildrenNames(getFqn());
168
169             if (treeKeys != null) {
170                 keys = treeKeys.toArray();
171             }
172         } catch (CacheException e) {
173             logger.warn(getName(), e);
174         }
175         if (keys == null) {
176             keys = new Object JavaDoc[] {};
177         }
178
179         return keys;
180     }
181
182     public void put(Object JavaDoc entryKey, Object JavaDoc entryObj) {
183         CacheEntry entry = new CacheEntry(entryObj);
184         putCacheEntry(entryKey, entry, true);
185     }
186
187     /**
188      * <p>
189      * Register a new cache listener.
190      * </p>
191      * <p>
192      * When the specified <code>listener</code> is <code>null</code> or
193      * already present within the listeners list, the registration process is
194      * ignored.
195      * </p>
196      *
197      * @param listener
198      * the reference of the cache listener to register.
199      * @since Jahia 4.0.2
200      */

201     public synchronized void registerListener(CacheListener listener) {
202         if (listener == null)
203             return;
204
205         if (listeners == null) {
206             listeners = new ArrayList JavaDoc();
207
208         } else if (listeners.contains(listener)) {
209             return;
210         }
211
212         listeners.add(listener);
213         getTreeCache().addTreeCacheListener(
214                 new TreeCacheListenerWrapper(listener));
215     }
216
217     /**
218      * <p>
219      * Unregister a cache listener.
220      * </p>
221      * <p>
222      * When there is not cache listener registered, or the specified
223      * <code>listener</code> is <code>null</code>, the unregistration
224      * process is ignored.
225      * </p>
226      *
227      * @param listener
228      * the reference of the cache listener to register
229      * @since Jahia 4.0.2
230      */

231     public synchronized void unregisterListener(CacheListener listener) {
232         if ((listeners == null) || (listener == null))
233             return;
234
235         listeners.remove(listener);
236         getTreeCache().removeTreeCacheListener(
237                 new TreeCacheListenerWrapper(listener));
238     }
239
240     public void remove(Object JavaDoc entryKey) {
241         try {
242             Fqn localFqn = null;
243             if (entryKey instanceof List JavaDoc) {
244                 List JavaDoc entryList = (List JavaDoc) entryKey;
245                 localFqn = new Fqn(getFqn(), entryList);
246             } else {
247                 localFqn = new Fqn(getFqn(), entryKey);
248             }
249
250             TreeCache localTreeCache = getTreeCache();
251             // if (localTreeCache.exists(localFqn)) {
252
localTreeCache.remove(localFqn);
253             // }
254
} catch (CacheException e) {
255             logger.warn(getName() + entryKey, e);
256         }
257     }
258
259     public int size() {
260         return getTreeCache().getNumberOfAttributes(getFqn());
261     }
262     
263     /**
264      * <p>
265      * Return true if cache is empty.
266      * </p>
267      *
268      * @return true if cache is empty
269      */

270     final public boolean isEmpty() {
271         return !getTreeCache().hasChild(getFqn());
272     }
273
274     public CacheEntry getCacheEntry(Object JavaDoc entryKey) {
275         CacheEntry entry = null;
276         try {
277             if (entryKey instanceof List JavaDoc) {
278                 List JavaDoc entryList = (List JavaDoc) entryKey;
279                 entry = (CacheEntry) getTreeCache().get(
280                         new Fqn(getFqn(), entryList), "entry");
281             } else {
282                 entry = (CacheEntry) getTreeCache().get(
283                         new Fqn(getFqn(), entryKey), "entry");
284             }
285         } catch (CacheException e) {
286             logger.warn(getName() + entryKey, e);
287         }
288         return entry;
289     }
290
291     public void putCacheEntry(Object JavaDoc entryKey, CacheEntry entry,
292             boolean propagate) {
293         try {
294             if (entryKey instanceof List JavaDoc) {
295                 List JavaDoc entryList = (List JavaDoc) entryKey;
296                 getTreeCache()
297                         .put(new Fqn(getFqn(), entryList), "entry", entry);
298             } else {
299                 getTreeCache().put(new Fqn(getFqn(), entryKey), "entry", entry);
300             }
301         } catch (Exception JavaDoc e) {
302             logger.warn(getName() + entryKey, e);
303         }
304     }
305
306     public int getCacheLimit() {
307         int cacheLimit = -1;
308         /*
309          * Element evictionPolicyConfig = tree.getEvictionPolicyConfig(); if
310          * (evictionPolicyConfig != null) { String maxNodes =
311          * evictionPolicyConfig.getAttribute("maxNodes"); if (maxNodes != null &&
312          * maxNodes.length() > 0) cacheLimit = Integer.parseInt(maxNodes); }
313          */

314         return cacheLimit;
315     }
316
317     public void setCacheLimit(int limit) {
318         // tree.setEvictionPolicyConfig(new DOMElementImpl());
319

320     }
321
322     public double getCacheEfficiency() {
323         // TODO Auto-generated method stub
324
return 0;
325     }
326
327     public long getSuccessHits() {
328         // TODO Auto-generated method stub
329
return 0;
330     }
331
332     public long getTotalHits() {
333         // TODO Auto-generated method stub
334
return 0;
335     }
336
337     protected TreeCache getTreeCache(String JavaDoc aName) throws Exception JavaDoc {
338         boolean isTxCache = isTxCache(aName);
339
340         treeCache = isTxCache ? getTxTreeCache() : getNonTxTreeCache();
341
342         return treeCache;
343     }
344
345     /**
346      * @param aFqn
347      * The fqn to set.
348      */

349     private void setFqn(Fqn aFqn) {
350         this.fqn = aFqn;
351     }
352
353     private Node getTreeNode() {
354         Node treeNode = null;
355         try {
356             treeNode = getTreeCache().get(getFqn());
357         } catch (CacheException e) {
358             logger.warn(getName(), e);
359         }
360         return treeNode;
361     }
362     
363     public static void activateService() {
364         globalNonTxTreeCache = createAndActivateCache("jahia-cache-service.xml");
365         globalTxTreeCache = createAndActivateCache("jahia-tx-cache-service.xml");
366     }
367     
368     private static TreeCache createAndActivateCache (String JavaDoc cacheConfigFileName) {
369         TreeCache currentCache = null;
370         try {
371             currentCache = new TreeCache();
372             PropertyConfigurator config = new PropertyConfigurator();
373             config.configure(currentCache, cacheConfigFileName);
374             currentCache.start();
375             currentCache.registerClassLoader("", Thread.currentThread()
376                     .getContextClassLoader());
377             currentCache.activateRegion("");
378         } catch (ConfigureException e) {
379             logger.error("Jahia Tree-Cache could not be started due to configuration errors", e);
380         } catch (Exception JavaDoc e) {
381             logger.error("Jahia Tree-Cache could not be started", e);
382         }
383         return currentCache;
384     }
385     
386     public static void inactivateService(){
387             try {
388                 globalNonTxTreeCache.unregisterClassLoader("");
389                 globalNonTxTreeCache.inactivateRegion("");
390                 globalTxTreeCache.unregisterClassLoader("");
391                 globalTxTreeCache.inactivateRegion("");
392             } catch (Exception JavaDoc e) {
393                 logger.warn("Cannot inactivate cache service!", e);
394             }
395        
396     }
397
398     public Transaction JavaDoc getCurrentTransaction()
399     {
400       Transaction JavaDoc trx = null;
401       TransactionManager JavaDoc trxManager = getTreeCache().getTransactionManager();
402       if (trxManager != null)
403       {
404         try
405         {
406           trx = trxManager.getTransaction();
407         }
408         catch (SystemException JavaDoc ex)
409         {
410           logger.warn("Unable to retrieve current transaction", ex);
411         }
412       }
413   
414       return trx;
415     }
416     
417     private boolean isTxCache (String JavaDoc aName) {
418         boolean isTxCache = !nonTxCaches.contains(aName);
419
420         return isTxCache;
421     }
422     /**
423      * @return Returns the txTreeCache.
424      */

425     private TreeCache getNonTxTreeCache() {
426         return globalNonTxTreeCache;
427     }
428     
429     /**
430      * @return Returns the txTreeCache.
431      */

432     private TreeCache getTxTreeCache() {
433         return globalTxTreeCache;
434     }
435 }
436
Popular Tags