KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > cache > OrderedCache


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /**
66  * OrderedCache.java
67  *
68  * Copyright 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.core.cache;
71
72 import org.apache.log4j.Logger;
73
74 import java.util.Enumeration JavaDoc;
75 import java.util.Vector JavaDoc;
76
77
78 /**
79  * Insert the type's description here.
80  * Creation date: (9/7/00 11:42:43 AM)
81  *
82  * @author Administrator
83  */

84 public class OrderedCache
85         implements Cache {
86
87     private Vector JavaDoc cacheContents = new Vector JavaDoc();
88
89     private String JavaDoc cacheName = null;
90
91     private long accessCount = 0;
92
93     private int maxSize = 0;
94
95     private static final String JavaDoc thisClass = OrderedCache.class.getName();
96
97     private static final Logger log = Logger.getLogger(OrderedCache.class);
98
99     /**
100      * OrderedCache constructor comment.
101      */

102     public OrderedCache() {
103         super();
104     } /* OrderedCache() */
105
106     /**
107      * addItem method comment.
108      *
109      * @param newItem New item to add.
110      */

111     public void addItem(CacheEntry newItem) {
112
113         /* if we're now over our max size, we must find an item to drop */
114         /* from the cache */
115         if ((maxSize > 0) && (cacheContents.size() >= maxSize)) {
116             long minUsed = java.lang.Long.MAX_VALUE;
117             CacheEntry leastUsedItem = null;
118             CacheEntry oneItem = null;
119
120             synchronized (cacheContents) {
121                 for (Enumeration JavaDoc e = cacheContents.elements();
122                      e.hasMoreElements();) {
123                     oneItem = (CacheEntry) e.nextElement();
124
125                     /* if we hit an "expired" item, we remove it & now have */
126                     /* room to insert */
127                     if (oneItem.isExpired()) {
128                         break;
129                     }
130                     if (oneItem.getUsedCount() < minUsed) {
131                         leastUsedItem = oneItem;
132                         minUsed = oneItem.getUsedCount();
133                     }
134                 }
135
136                 removeItem(oneItem.getContents());
137             }
138         }
139
140         cacheContents.addElement(newItem);
141         newItem.clearUsedCount();
142     } /* addItem(CacheEntry) */
143
144     /**
145      * clear method comment.
146      */

147     public void clear() {
148         synchronized (cacheContents) {
149             cacheContents = new Vector JavaDoc();
150         }
151     } /* clear() */
152
153
154     /**
155      * Retrieve the low-level CacheEntry for the cache. This is particularly
156      * useful for debugging or management.
157      *
158      * @param itemKey the cache key to retrieve
159      * @return the CacheEntry for the cache or null.
160      */

161     public CacheEntry getCacheEntry(String JavaDoc itemKey) {
162         CacheEntry oneItem = null;
163         synchronized (cacheContents) {
164             for (Enumeration JavaDoc allItems = cacheContents.elements();
165                  allItems.hasMoreElements();) {
166                 oneItem = (CacheEntry) allItems.nextElement();
167
168                 if (oneItem.getKey().equals(itemKey)) {
169                     if (oneItem.isExpired()) {
170                         removeItem(oneItem.getContents());
171
172                         return null;
173                     }
174                     oneItem.incrementUsedCount();
175
176                     return oneItem;
177                 }
178             }
179         }
180
181         return null;
182     }
183
184     /**
185      * removeItem method comment.
186      *
187      * @param itemKey The item key
188      * @return the Cacheable entry
189      */

190     public Cacheable getItem(String JavaDoc itemKey) {
191         accessCount++;
192
193         CacheEntry oneItem = this.getCacheEntry(itemKey);
194
195         if (oneItem == null) {
196             return null;
197         } else {
198             return oneItem.getContents();
199         }
200     } /* getItem(String) */
201
202     /**
203      * getItemCount method comment.
204      *
205      * @return the item count
206      */

207     public int getItemCount() {
208         return cacheContents.size();
209     } /* getItemCount() */
210
211     /**
212      * Retrieves all the items in this cache May return an empty Vector
213      * if the cache contents have expired.
214      *
215      * @return the actual items or null if no items exist in the cache.
216      */

217     public Vector JavaDoc getItems() {
218         accessCount++;
219
220         Vector JavaDoc aClone = null;
221         CacheEntry oneEntry = null;
222
223         synchronized (cacheContents) {
224             if (cacheContents.size() > 0) {
225                 oneEntry = (CacheEntry) cacheContents.get(0);
226                 if (oneEntry.isExpired()) {
227                     this.clear();
228                     return new Vector JavaDoc(1);
229                 } else {
230                     aClone = (Vector JavaDoc) cacheContents.clone();
231                 }
232             } else {
233                 return new Vector JavaDoc(1);
234             }
235         }
236
237         Vector JavaDoc v = new Vector JavaDoc(aClone.size());
238         for (Enumeration JavaDoc e = aClone.elements(); e.hasMoreElements();) {
239             oneEntry = (CacheEntry) e.nextElement();
240             v.addElement(oneEntry.getContents());
241         }
242
243         return v;
244     } /* getItems() */
245
246     /**
247      * getName method comment.
248      *
249      * @return the cache name
250      */

251     public String JavaDoc getName() {
252         return cacheName;
253     } /* getName() */
254
255     /**
256      * getUsedCount method comment.
257      *
258      * @return the useage count
259      */

260     public long getUsedCount() {
261         return accessCount;
262     } /* getUsedCount() */
263
264
265     /**
266      * Retrieve whether the cache instance is an ordered cache [list based]
267      * or unordered cache. [map based]
268      *
269      * @return true if the cache is an ordered cache.
270      */

271     public boolean isOrdered() {
272         return true;
273     }
274
275
276     /**
277      * removeItem method comment.
278      *
279      * @param oldItem the old item to remove
280      */

281     public void removeItem(Cacheable oldItem) {
282         CacheEntry oneItem = null;
283         for (int i = 0; i < cacheContents.size(); i++) {
284
285             try {
286                 oneItem = (CacheEntry) cacheContents.elementAt(i);
287                 if (oneItem != null) {
288                     if (oneItem.getKey().equals(oldItem.getKey())) {
289                         oneItem.clearUsedCount();
290
291                         synchronized (cacheContents) {
292                             cacheContents.removeElement(oneItem);
293                         }
294                     }
295                 }
296             } catch (Exception JavaDoc ex) {
297                 //Ignore this one! We stepped out of bounds due to
298
//another thread.
299
if (log.isDebugEnabled()) {
300                     log.debug("Ignored Exception", ex);
301                 }
302             }
303         }
304
305     } /* removeItem(Cacheable) */
306
307
308     /**
309      * Eventually the new way to set items
310      *
311      * @param newItems the new items to add
312      * @throws CacheException upon error
313      */

314     public void setItems(java.util.List JavaDoc newItems) throws CacheException {
315         if (newItems instanceof java.util.Vector JavaDoc) {
316             setItems((Vector JavaDoc) newItems);
317         } else {
318             accessCount++;
319             clear();
320
321             Object JavaDoc oneObject = null;
322
323             for (java.util.Iterator JavaDoc e = newItems.iterator(); e.hasNext();) {
324                 oneObject = e.next();
325
326                 if (oneObject instanceof Cacheable) {
327                     CacheEntry ce = new CacheEntry((Cacheable) oneObject, -1);
328                     addItem(ce);
329                 } else {
330                     String JavaDoc myName = (thisClass + "setItems(Vector)");
331                     throw new CacheException(myName +
332                             ":Item in vector is of class " +
333                             oneObject.getClass().getName() +
334                             ", which is not Cacheable - cannot set items");
335                 }
336             }
337             return;
338         }
339     }
340
341     /**
342      * getItemCount method comment.
343      *
344      * @param newItems the new items to add
345      */

346     public void setItems(Vector JavaDoc newItems)
347             throws CacheException {
348         accessCount++;
349         clear();
350
351         Object JavaDoc oneObject = null;
352
353         for (Enumeration JavaDoc e = newItems.elements(); e.hasMoreElements();) {
354             oneObject = e.nextElement();
355
356             if (oneObject instanceof Cacheable) {
357                 CacheEntry ce = new CacheEntry((Cacheable) oneObject, -1);
358                 addItem(ce);
359             } else {
360                 String JavaDoc myName = (thisClass + "setItems(Vector)");
361                 throw new CacheException(myName +
362                         ":Item in vector is of class " +
363                         oneObject.getClass().getName() +
364                         ", which is not Cacheable - cannot set items");
365             }
366         }
367     } /* setItems(Vector) */
368
369
370     /**
371      * setName method comment.
372      *
373      * @param newMaxSize the new maximum size for the cache
374      */

375     public synchronized void setMaxSize(int newMaxSize) {
376         maxSize = newMaxSize;
377     } /* setMaxSize(int) */
378
379     /**
380      * setName method comment.
381      *
382      * @param newName The new cache name
383      */

384     public synchronized void setName(String JavaDoc newName) {
385         cacheName = newName;
386     } /* setName(String) */
387
388 } /* OrderedCache */
389
Popular Tags