001: package org.mortbay.jetty.servlet;
002:
003: import java.util.Random;
004:
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpSession;
007:
008: import net.sf.ehcache.Cache;
009: import net.sf.ehcache.CacheManager;
010: import net.sf.ehcache.config.CacheConfiguration;
011: import net.sf.ehcache.config.Configuration;
012: import net.sf.ehcache.config.DiskStoreConfiguration;
013: import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
014:
015: import org.mortbay.component.AbstractLifeCycle;
016: import org.mortbay.jetty.SessionIdManager;
017:
018: public class CacheSessionIdManager implements SessionIdManager {
019: protected CacheManager _cacheManager;
020: protected String _cacheDir = System.getProperty("java.io.tmpdir",
021: ".")
022: + "/org.mortbay.jetty.session.cache";
023:
024: private HashSessionIdManager _temporaryHack = new HashSessionIdManager();
025:
026: public CacheSessionIdManager() {
027: }
028:
029: public CacheSessionIdManager(String cacheDir) {
030: _cacheDir = cacheDir;
031: }
032:
033: /* ------------------------------------------------------------ */
034: /**
035: * @param session
036: * @see org.mortbay.jetty.servlet.HashSessionIdManager#addSession(javax.servlet.http.HttpSession)
037: */
038: public void addSession(HttpSession session) {
039: _temporaryHack.addSession(session);
040: }
041:
042: /* ------------------------------------------------------------ */
043: /**
044: * @param obj
045: * @return
046: * @see java.lang.Object#equals(java.lang.Object)
047: */
048: public boolean equals(Object obj) {
049: return _temporaryHack.equals(obj);
050: }
051:
052: /* ------------------------------------------------------------ */
053: /**
054: * @return
055: * @see org.mortbay.jetty.servlet.HashSessionIdManager#getRandom()
056: */
057: public Random getRandom() {
058: return _temporaryHack.getRandom();
059: }
060:
061: /* ------------------------------------------------------------ */
062: /**
063: * @return
064: * @see org.mortbay.jetty.servlet.HashSessionIdManager#getWorkerName()
065: */
066: public String getWorkerName() {
067: return _temporaryHack.getWorkerName();
068: }
069:
070: /* ------------------------------------------------------------ */
071: /**
072: * @return
073: * @see java.lang.Object#hashCode()
074: */
075: public int hashCode() {
076: return _temporaryHack.hashCode();
077: }
078:
079: /* ------------------------------------------------------------ */
080: /**
081: * @param id
082: * @return
083: * @see org.mortbay.jetty.servlet.HashSessionIdManager#idInUse(java.lang.String)
084: */
085: public boolean idInUse(String id) {
086: return _temporaryHack.idInUse(id);
087: }
088:
089: /* ------------------------------------------------------------ */
090: /**
091: * @param id
092: * @see org.mortbay.jetty.servlet.HashSessionIdManager#invalidateAll(java.lang.String)
093: */
094: public void invalidateAll(String id) {
095: _temporaryHack.invalidateAll(id);
096: }
097:
098: /* ------------------------------------------------------------ */
099: /**
100: * @return
101: * @see org.mortbay.component.AbstractLifeCycle#isFailed()
102: */
103: public boolean isFailed() {
104: return _temporaryHack.isFailed();
105: }
106:
107: /* ------------------------------------------------------------ */
108: /**
109: * @return
110: * @see org.mortbay.component.AbstractLifeCycle#isRunning()
111: */
112: public boolean isRunning() {
113: return _temporaryHack.isRunning();
114: }
115:
116: /* ------------------------------------------------------------ */
117: /**
118: * @return
119: * @see org.mortbay.component.AbstractLifeCycle#isStarted()
120: */
121: public boolean isStarted() {
122: return _temporaryHack.isStarted();
123: }
124:
125: /* ------------------------------------------------------------ */
126: /**
127: * @return
128: * @see org.mortbay.component.AbstractLifeCycle#isStarting()
129: */
130: public boolean isStarting() {
131: return _temporaryHack.isStarting();
132: }
133:
134: /* ------------------------------------------------------------ */
135: /**
136: * @return
137: * @see org.mortbay.component.AbstractLifeCycle#isStopped()
138: */
139: public boolean isStopped() {
140: return _temporaryHack.isStopped();
141: }
142:
143: /* ------------------------------------------------------------ */
144: /**
145: * @return
146: * @see org.mortbay.component.AbstractLifeCycle#isStopping()
147: */
148: public boolean isStopping() {
149: return _temporaryHack.isStopping();
150: }
151:
152: /* ------------------------------------------------------------ */
153: /**
154: * @param request
155: * @param created
156: * @return
157: * @see org.mortbay.jetty.servlet.HashSessionIdManager#newSessionId(javax.servlet.http.HttpServletRequest, long)
158: */
159: public String newSessionId(HttpServletRequest request, long created) {
160: return _temporaryHack.newSessionId(request, created);
161: }
162:
163: /* ------------------------------------------------------------ */
164: /**
165: * @param session
166: * @see org.mortbay.jetty.servlet.HashSessionIdManager#removeSession(javax.servlet.http.HttpSession)
167: */
168: public void removeSession(HttpSession session) {
169: _temporaryHack.removeSession(session);
170: }
171:
172: /* ------------------------------------------------------------ */
173: /**
174: * @param random
175: * @see org.mortbay.jetty.servlet.HashSessionIdManager#setRandom(java.util.Random)
176: */
177: public void setRandom(Random random) {
178: _temporaryHack.setRandom(random);
179: }
180:
181: /* ------------------------------------------------------------ */
182: /**
183: * @param workerName
184: * @see org.mortbay.jetty.servlet.HashSessionIdManager#setWorkerName(java.lang.String)
185: */
186: public void setWorkerName(String workerName) {
187: _temporaryHack.setWorkerName(workerName);
188: }
189:
190: /* ------------------------------------------------------------ */
191: /**
192: * @throws Exception
193: * @see org.mortbay.component.LifeCycle#start()
194: */
195: public void start() throws Exception {
196: _temporaryHack.start();
197:
198: //configure cacheManager - all values are hardcoded for now but there will be mutators for the values
199: Configuration conf = new Configuration();
200:
201: DiskStoreConfiguration diskStoreConf = new DiskStoreConfiguration();
202: diskStoreConf.setPath(_cacheDir);
203: conf.addDiskStore(diskStoreConf);
204:
205: CacheConfiguration cacheConf = new CacheConfiguration();
206: cacheConf.setDiskPersistent(true);
207: cacheConf.setMaxElementsInMemory(1000);
208: cacheConf
209: .setMemoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU
210: .toString());
211: cacheConf.setName("Default");
212: cacheConf.setOverflowToDisk(true);
213: cacheConf.setTimeToIdleSeconds(600);
214: cacheConf.setTimeToLiveSeconds(600);
215: cacheConf.setDiskExpiryThreadIntervalSeconds(200);
216:
217: conf.addDefaultCache(cacheConf);
218:
219: _cacheManager = new CacheManager(conf);
220: }
221:
222: /* ------------------------------------------------------------ */
223: /**
224: * @throws Exception
225: * @see org.mortbay.component.LifeCycle#stop()
226: */
227: public void stop() throws Exception {
228: _temporaryHack.stop();
229: }
230:
231: /* ------------------------------------------------------------ */
232: /**
233: * @return
234: * @see java.lang.Object#toString()
235: */
236: public String toString() {
237: return _temporaryHack.toString();
238: }
239:
240: public void setCacheDirectory(String cacheDir) {
241: _cacheDir = cacheDir;
242: }
243:
244: }
|