KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.services.cache.treecache;
2
3 import java.util.Iterator JavaDoc;
4
5 import org.apache.log4j.Logger;
6 import org.jahia.exceptions.JahiaException;
7 import org.jahia.exceptions.JahiaInitializationException;
8 import org.jahia.mbeans.JahiaMBeanServer;
9 import org.jahia.services.cache.Cache;
10 import org.jahia.services.cache.CacheFactory;
11 import org.jahia.services.cache.HtmlCache;
12 import org.jahia.services.cache.simplecache.SimpleHtmlCache;
13 import org.jahia.settings.SettingsBean;
14
15 public class TreeCacheFactory extends CacheFactory {
16     /** logging. */
17     private static final Logger logger = Logger.getLogger(TreeCacheFactory.class);
18
19     private boolean propagated = false;
20
21     /**
22      * Initializes the Cache factory.
23      *
24      * @param jSettings
25      * the jahia private settings
26      *
27      * @exception JahiaInitializationException
28      * when the Cache Factory could not be initialized
29      */

30     public void init(SettingsBean jSettings) throws JahiaInitializationException {
31         // do nothing when settings are missing
32
if (jSettings == null)
33             return;
34
35         // PAP: activate the treeCache-propagation here as all services are up !!
36
JahiaTreeCache.activateService();
37         
38 // if (jSettings.lookupBoolean(SettingsBean.JMS_CACHE_ACTIVATION)) {
39
propagated = true;
40             keyHierarchyEnabled = true;
41 // }
42
}
43
44     // Javadoc inherited from parent
45
public synchronized void shutdown() throws JahiaException {
46         super.shutdown();
47
48         // flush the caches
49
// we deactivated this because on a cluster we don't want to flush
50
// on all nodes.
51
// flushAllCaches();
52
caches.clear();
53         
54         JahiaTreeCache.inactivateService();
55     }
56
57     /**
58      * Return the unique instance of this class.
59      *
60      * @return the class' unique instance.
61      */

62     public static synchronized CacheFactory getInstance() {
63         if (instance == null) {
64             instance = new TreeCacheFactory();
65         }
66         return instance;
67     }
68     
69     /**
70      * <p>
71      * Creates a new instance of type <code>Cache</code>.
72      * </p>
73      * <p>
74      * When the region is <code>null</code> the cache creation is canceled and
75      * a <code>null</code> instance will be returned.
76      * </p>
77      *
78      * @param name
79      * the cache region
80      *
81      * @return the new cache instance
82      *
83      * @exception JahiaInitializationException
84      * when the cache could not be initialized
85      */

86     public synchronized Cache createCacheInstance(String JavaDoc name) throws JahiaInitializationException {
87         // validity check
88
if (name == null)
89             return null;
90
91         // When the cache already exists in the factory, return the instance.
92
Cache cache = getCache(name);
93         if (cache != null) {
94             return cache;
95         }
96
97         // Get the factory instance
98
TreeCacheFactory factory = this;
99         if (factory == null) {
100             logger.warn("Could not get the CacheFactory instance! ");
101             return null;
102         }
103
104         // instanciate the new cache, can throw an JahiaInitialization exception
105
cache = new JahiaTreeCache(name, propagated);
106
107         logger.debug("Created cache instance [" + name + "]");
108
109         if (registerCache(cache)) {
110             return cache;
111         }
112
113         cache = null;
114         return null;
115     }
116
117     private boolean registerCache(Cache cache) {
118         // Add the cache to the table
119
caches.put(cache.getName(), cache);
120
121         if (isJMXEnabled()) {
122             // register the cache for JMX monitoring
123
JahiaMBeanServer.getInstance().registerManagedInstance(cache, "Cache", cache.getName());
124         }
125         return true;
126     }
127
128     public Cache getCache(String JavaDoc name) {
129         if (name == null) {
130             return null;
131         }
132         return (Cache) caches.get(name);
133     }
134
135     /**
136      * <p>
137      * Retrieves the HTML cach instance.
138      * </p>
139      *
140      * <p>
141      * When the HTML cache is not present, a new instance is created and
142      * inserted into the factory.
143      * <p>
144      *
145      * @return the HTML cache instance
146      *
147      * @exception JahiaInitializationException
148      * when the HTML cache could not be instanciated and properly
149      * initialized
150      */

151     public HtmlCache createHtmlCacheInstance() throws JahiaInitializationException {
152
153         // At this point, the HTML cache does not exist, create it
154
JahiaTreeHtmlCache cache = new JahiaTreeHtmlCache(this.propagated);
155
156         this.caches.put(cache.getName(), cache);
157
158         return cache;
159     }
160
161     /**
162      * <p>
163      * Flush all the cache entries of all the registered caches.
164      * </p>
165      *
166      * <p>
167      * Use this method with caution as it may take a lot of CPU time, because
168      * the method is synchronized and each accessed cache has to be synchronized
169      * too.
170      * </p>
171      */

172     public synchronized void flushAllCaches() {
173
174         Iterator JavaDoc cacheNames = getNames().iterator();
175         while (cacheNames.hasNext()) {
176             String JavaDoc curCacheName = (String JavaDoc) cacheNames.next();
177             Cache cache = (Cache) caches.get(curCacheName);
178
179             synchronized (cache) {
180                 cache.flush();
181             }
182         }
183
184         logger.info("Flushed all caches.");
185     }
186
187     /**
188      * <p>
189      * Checks if the cache synchronization is enabled.
190      * </p>
191      *
192      * @return <code>true</code> when the cache synchronization is enabled
193      */

194     public boolean isJMSEnabled() {
195         return propagated;
196     }
197
198     /**
199      * <p>
200      * Enables the cache synchronization by initiating a connection to the JMS
201      * Server.
202      * </p>
203      *
204      * @throws JahiaInitializationException
205      * When the cache synchronization could not be succuessfully be
206      * initialized. The most probable cause is a wrong value for a
207      * configuration parameter.
208      */

209     public void enableJMSSynchronization() throws JahiaInitializationException {
210         propagated = true;
211     }
212
213     /**
214      * <p>
215      * Disables the cache synchronization by closing all existing connections to
216      * the JMS Server.
217      * </p>
218      */

219     public void disableJMSSynchronization() {
220         propagated = false;
221     }
222 }
223
Popular Tags