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: import net.sf.jportlet.service.PortletServiceAdapter;
013: import net.sf.jportlet.service.PortletServiceConfig;
014: import net.sf.jportlet.service.PortletServiceException;
015:
016: /**
017: * Implementation of {@link net.sf.jportlet.service.cache.CacheService}
018: *
019: * @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
020: */
021: public class CacheServiceImpl extends PortletServiceAdapter implements
022: CacheService {
023: //~ Instance fields --------------------------------------------------------
024:
025: private HashMap _regions = new HashMap();
026: private Expirer _expirer = new Expirer(_regions);
027:
028: //~ Methods ----------------------------------------------------------------
029:
030: /**
031: * @see net.sf.jportlet.service.PortletService#destroy()
032: */
033: public void destroy() {
034: super .destroy();
035:
036: _expirer.stop();
037: _regions.clear();
038: }
039:
040: /**
041: * @see net.sf.jportlet.service.cache.CacheService#getRegion(java.lang.Object, boolean)
042: */
043: public CacheRegion getRegion(Object id, boolean create) {
044: CacheRegion region = (CacheRegion) _regions.get(id);
045:
046: if (region == null) {
047: synchronized (this ) {
048: region = new CacheRegionImpl(id);
049: _regions.put(id, region);
050: }
051: }
052:
053: return region;
054: }
055:
056: /**
057: * @see net.sf.jportlet.service.PortletService#getServiceName()
058: */
059: public String getServiceName() {
060: return CacheService.NAME;
061: }
062:
063: /**
064: * @see net.sf.jportlet.service.PortletService#init(net.sf.jportlet.service.PortletServiceConfig)
065: */
066: public void init(PortletServiceConfig serviceConfig)
067: throws PortletServiceException {
068: super .init(serviceConfig);
069:
070: /* Expirer interval */
071: String interval = getInitParameter("expirer.interval", "60");
072: _expirer.setInterval(Integer.parseInt(interval) * 1000);
073:
074: /* Start the expirer thread */
075: Thread thr = new Thread(_expirer);
076: thr.setPriority(Thread.MIN_PRIORITY);
077: thr.start();
078: }
079:
080: //~ Inner Classes ----------------------------------------------------------
081:
082: private static class Expirer implements Runnable {
083: //~ Static fields/initializers -----------------------------------------
084:
085: private static final Log __log = LogFactory
086: .getLog(Expirer.class);
087:
088: //~ Instance fields ----------------------------------------------------
089:
090: private long _interval;
091: private boolean _stopped;
092: private HashMap _regions;
093:
094: //~ Constructors -------------------------------------------------------
095:
096: public Expirer(HashMap regions) {
097: _interval = 60000;
098: _regions = regions;
099: }
100:
101: //~ Methods ------------------------------------------------------------
102:
103: public void stop() {
104: if (__log.isDebugEnabled()) {
105: __log.debug("Stopping");
106: }
107:
108: _stopped = true;
109: }
110:
111: /**
112: * @see java.lang.Runnable#run()
113: */
114: public void run() {
115: if (__log.isDebugEnabled()) {
116: __log.debug("Running");
117: }
118:
119: try {
120: while (!_stopped) {
121: Thread.sleep(_interval);
122:
123: Iterator it = _regions.values().iterator();
124:
125: while (it.hasNext()) {
126: ((CacheRegion) it.next()).removeExpiredObject();
127: }
128: }
129: } catch (InterruptedException i) {
130: }
131: }
132:
133: public long getInterval() {
134: return _interval;
135: }
136:
137: public void setInterval(long interval) {
138: _interval = interval;
139: }
140: }
141: }
|