001: /*
002: * Created on 24-Mar-2003
003: */
004: package net.sf.jportlet.service.cache;
005:
006: import java.util.HashMap;
007: import java.util.Iterator;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: /**
013: * Implementation of {@link net.sf.jportlet.service.cache.CacheRegion}
014: *
015: * @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
016: */
017: public class CacheRegionImpl implements CacheRegion {
018: //~ Static fields/initializers ---------------------------------------------
019:
020: private static final Log __log = LogFactory
021: .getLog(CacheRegionImpl.class);
022:
023: //~ Instance fields --------------------------------------------------------
024:
025: private HashMap _data = new HashMap();
026: private Object _id;
027:
028: //~ Constructors -----------------------------------------------------------
029:
030: public CacheRegionImpl(Object id) {
031: _id = id;
032: }
033:
034: //~ Methods ----------------------------------------------------------------
035:
036: /**
037: * @see net.sf.jportlet.service.cache.CacheRegion#get(java.lang.Object)
038: */
039: public Cacheable get(Object id) {
040: Cacheable obj = (Cacheable) _data.get(id);
041:
042: if (obj == null) {
043: return null;
044: }
045:
046: if (obj.isExpired()) {
047: if (__log.isDebugEnabled()) {
048: __log.debug("expired: " + id);
049: }
050:
051: return null;
052: } else {
053: return obj;
054: }
055: }
056:
057: /**
058: * @see net.sf.jportlet.service.cache.CacheRegion#getId()
059: */
060: public Object getId() {
061: return _id;
062: }
063:
064: /**
065: * @see net.sf.jportlet.service.cache.CacheRegion#put(net.sf.jportlet.service.cache.Cacheable)
066: */
067: public void put(Cacheable obj) {
068: _data.put(obj.getId(), obj);
069: }
070:
071: /**
072: * @see net.sf.jportlet.service.cache.CacheRegion#removeExpiredObject()
073: */
074: public void removeExpiredObject() {
075: boolean debug = __log.isDebugEnabled();
076:
077: if (debug) {
078: __log.debug("Removing expired data from: " + _id);
079: }
080:
081: /* Get all the object to expire */
082: synchronized (_data) {
083: Iterator it = _data.values().iterator();
084:
085: while (it.hasNext()) {
086: Cacheable obj = (Cacheable) it.next();
087:
088: if (obj.isExpired()) {
089: Object id = obj.getId();
090:
091: if (debug) {
092: __log.debug("Removing: " + id);
093: }
094:
095: _data.remove(id);
096: }
097: }
098: }
099: }
100: }
|