KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > cache > simplecache > PersistenceCache


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  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Developer of the Shared Modifications is Jahia Solution Sarl.
30  * Portions created by the Initial Developer are Copyright (C) 2002 by the
31  * Initial Developer. All Rights Reserved.
32  *
33  * Contributor(s):
34  * 11-SEP-2003, Jahia Solutions Sarl, Serge Huber : Initial version
35  * 12-SEP-2003, Jahia Solutions Sarl, Fulco Houkes
36  *
37  * ----- END LICENSE BLOCK -----
38  */

39
40 package org.jahia.services.cache.simplecache;
41
42 import java.util.Hashtable JavaDoc;
43
44 import org.jahia.exceptions.JahiaInitializationException;
45 import org.jahia.exceptions.JahiaPersistenceException;
46 import org.jahia.services.cache.CacheEntry;
47 import org.jahia.services.cache.JMSHub;
48 import org.jahia.services.cache.PersistenceCacheable;
49
50 /**
51  * <p>Persistence aware cache.</p>
52  * <p>This object is actually quite powerful, it uses an
53  * object that implements that the PersistenceCacheable interface to take
54  * care of the loading of the objects from the persistence store. If the
55  * object is not available through in the store, it will mark the cache
56  * entry as unavailalble, making future requests much faster since they will
57  * not have to constantly go to the store to get the information of non-
58  * existence of that entry.</p>
59  * <p>Note that in order for this to work correctly, all PUT operation MUST
60  * go through this cache object, instead of through ad-hoc methods, otherwise
61  * the entries marked as non-existent will not be properly updated !</p>
62  *
63  * @author Serge Huber, Copyright (c) 2003 by Jahia Ltd.
64  * @version 1.0
65  * @since Jahia 4.0
66  */

67 public class PersistenceCache extends SimpleCache {
68
69     /** Object stored with the black listed keys, to avoid object creation and to
70      * save memory space.
71      */

72     final private static Integer JavaDoc NULL_OBJECT = new Integer JavaDoc(0);
73
74     /** logging. */
75     final private static org.apache.log4j.Logger logger =
76             org.apache.log4j.Logger.getLogger (PersistenceCache.class);
77
78     /** The persistence layer to call when adding or removing cache entries. */
79     private PersistenceCacheable persistenceCacheable;
80
81     /** The keys black list. */
82     final private Hashtable JavaDoc blackList = new Hashtable JavaDoc();
83
84
85     /** Creates a new <code>PersistenceCache</code> instance with the specified
86      * parameters.
87      *
88      * @param name the cache name
89      * @param description the cahce description
90      * @param hub the JMS Hub to use for cache synchronization
91      *
92      * @param persistenceCacheable the persistence layer to use to store and fetch objects
93      *
94      * @throws JahiaInitializationException
95      * when the cache could not be properly initialized
96      */

97     public PersistenceCache (String JavaDoc name, JMSHub hub,
98                                 PersistenceCacheable persistenceCacheable)
99             throws JahiaInitializationException
100     {
101         super (name, hub);
102         if (persistenceCacheable == null) {
103             throw new JahiaInitializationException (
104                     "In order to use the PersistenceCacheAdapter, you must pass an object that implements the PersistenceCacheable interface");
105         }
106         this.persistenceCacheable = persistenceCacheable;
107     }
108
109     /**
110      * <p>Fetchs the cache entry associated to the <code>entryKey</code> and returns the
111      * object stored in fetched cache entry.</p>
112      *
113      * <p><code>null</code> is returned when the <code>entryKey</code> is
114      * <code>null</code>, or when no cache entry could be found for the specified
115      * <code>entryKey</code>.</p>
116      *
117      * <p>When the <code>entryKey</code> couldn't be neither fetch out of the cache nor
118      * found in the persistent layer, the entry key is black listed in order to prevent
119      * further persistent layer accesses to this key.</p>
120      *
121      * @param entryKey the key associated to the requested object
122      *
123      * @return the object associated to the code <code>entryKey</code> cache entry.
124      */

125     public Object JavaDoc get (Object JavaDoc entryKey) {
126
127         if (entryKey == null) {
128             logger.debug ("Cannot fetch with an null entry key!!!");
129             return null;
130         }
131
132         // get the object from the cache
133
Object JavaDoc obj = super.get (entryKey);
134         if (obj != null) {
135             blackList.remove (entryKey);
136             return obj;
137         }
138
139         // the object could not be found in the cache, check if the entryKey has been
140
// black listed previously
141
if (blackList.containsKey (entryKey))
142             return null;
143
144         // the key is not black listed, try to load the object from the persistence layer
145
try {
146             Object JavaDoc loadedObject = persistenceCacheable.getFromPersistence (entryKey);
147             if (loadedObject != null) {
148                 // add the object in the cache
149
super.put (entryKey, loadedObject);
150                 return loadedObject;
151             }
152
153         } catch (JahiaPersistenceException ex) {
154             logger.warn ("Could not get the object ["+ entryKey +
155                     "] from the persistence layer!", ex);
156             return null;
157         }
158
159         // the object associated to the specified entry key could neither be found in
160
// the cache nor in the persistence layer: black list the entry key to avoid
161
// further unnecessary accesses to this object in the persistence layer.
162
blackList.put (entryKey, NULL_OBJECT);
163         logger.debug ("could not find the associated CacheEntry instance");
164         return null;
165     }
166
167     /**
168      * <p>Add a new object into the cache.</p>
169      *
170      * <p>This method encapsulates automatically the specified <code>entryObj</code>
171      * object into a new {@link org.jahia.services.cache.CacheEntry CacheEntry}
172      * instance and associated the new cache entry to the <code>entryKey</code> key.</p>
173      *
174      * <p>If the <code>entryKey</code> was black listed, it will be removed
175      * from that list.</p>
176      *
177      * @param entryKey the object's associated key, <code>null</code> is not allowed
178      * @param entryObj the reference to the object to be cached.
179      */

180     public void put (Object JavaDoc entryKey, Object JavaDoc entryObj) {
181         if (entryKey == null) {
182             logger.debug ("Cannot add an object with a null key!!");
183             return;
184         }
185
186         if (entryObj == null) {
187             logger.debug ("Cannot add a null object!!");
188             return;
189         }
190
191         // add the new object in the persistence layer
192
boolean newKey = !containsKey (entryKey);
193         try {
194             persistenceCacheable.setToPersistence (entryKey, entryObj, newKey);
195
196         } catch (JahiaPersistenceException ex) {
197             logger.warn ("Could not add the cached object ["+ entryKey +
198                     "] into the persistence layer!", ex);
199             return;
200
201         }
202
203         // add the object in the cache
204
super.put (entryKey, entryObj);
205
206         // in case the entryKey was previously black listed, invalidate it.
207
blackList.remove (entryKey);
208     }
209
210
211     /** <p>Invalidates the specified <code>entryKey</code> in the keys black list.</p>
212      *
213      * <p>CAUTION: <b>This method should NOT be used. It has been implemented only for
214      * the code refactoring time of all the services to use the <code>PersistenceCache</code>.
215      * This method will disappear as soon as the refactoring is done!</b></p>
216      *
217      * @param entryKey the entry key to be invalidate
218      */

219     public void invalidateKey (final Object JavaDoc entryKey) {
220         if (entryKey == null) {
221             return;
222         }
223
224         blackList.remove (entryKey);
225     }
226
227 }
228
Popular Tags