001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.cache;
020:
021: import java.util.*;
022: import java.util.logging.*;
023: import java.util.logging.Logger;
024:
025: import org.apache.commons.collections.map.*;
026:
027: /**
028: * An extension of the Apache commons <code>LRUMap</code> which handles
029: * the removal of cached objects such that dependencies are kept and
030: * listeners get notified on object removal.
031: *
032: * @author Michael Bell
033: * @version $Revision: 1.1 $
034: *
035: */
036: public class LRUCacheMap extends LRUMap {
037:
038: private boolean m_bIsDependantAware = false;
039:
040: /**
041: * Logger for this class.
042: */
043: private static final Logger m_logger = Logger
044: .getLogger(LRUCacheMap.class.getName());
045:
046: /**
047: * Construct new <code>LRUCacheMap</code> with size 100.
048: */
049: public LRUCacheMap() {
050: super (100, true);
051: }
052:
053: /**
054: * Construct new <code>LRUCacheMap</code> with specified size.
055: *
056: * @param size size for map
057: */
058: public LRUCacheMap(int size) {
059: super (size, true);
060: }
061:
062: /* (non-Javadoc)
063: * @see org.apache.commons.collections.map.LRUMap#removeLRU(org.apache.commons.collections.map.AbstractLinkedMap.LinkEntry)
064: */
065: protected boolean removeLRU(AbstractLinkedMap.LinkEntry entry) {
066:
067: boolean bRemove = true;
068:
069: Object obj = entry.getValue();
070:
071: if (m_logger.isLoggable(Level.FINEST)) {
072: m_logger.logp(Level.FINEST, this .getClass().getName(),
073: "removeLRU", "Removing " + obj);
074: }
075:
076: if (obj instanceof CacheableObject) {
077: ((CacheableObject) obj).notifyCacheListeners();
078: }
079:
080: if (m_bIsDependantAware == true
081: && obj instanceof DependableCacheObject
082: && ((DependableCacheObject) obj).hasDependants()) {
083: bRemove = false;
084: }
085:
086: return bRemove;
087: }
088:
089: /* (non-Javadoc)
090: * @see java.util.Map#clear()
091: */
092: public void clear() {
093:
094: for (Iterator iter = keySet().iterator(); iter.hasNext();) {
095: String sKey = (String) iter.next();
096: Object obj = get(sKey);
097:
098: if (obj instanceof CacheableObject) {
099: ((CacheableObject) obj).notifyCacheListeners();
100: }
101: }
102: super .clear();
103: }
104:
105: /**
106: * Sets whether this cache checks objects for dependancies before
107: * paging the object out. If, when checking for dependancies, the object is
108: * found to have dependances the object will not be paged out.
109: *
110: *
111: * @param bIsDepAware <code>true</code> if the cache should check dependancies
112: * of an object before paging it out of the cache.
113: */
114: public void setDependancyAware(boolean bIsDepAware) {
115: m_bIsDependantAware = bIsDepAware;
116: }
117:
118: /**
119: * Returns <code>true</code> if this cache will check an object for
120: * dependancies before paging the object out.
121: *
122: * @return<code>true</code> if this cache will check an object for
123: * dependancies before paging the object out.
124: */
125: public boolean isDependancyAware() {
126: return m_bIsDependantAware;
127: }
128: }
|