001: package org.sakaibrary.xserver.session;
002:
003: import net.sf.ehcache.Cache;
004: import net.sf.ehcache.CacheException;
005: import net.sf.ehcache.CacheManager;
006: import net.sf.ehcache.Element;
007:
008: /**
009: * MetasearchSessionManager is a Singleton class designed for session
010: * management in metasearching applications. It makes use of ehcache
011: * and MetasearchSession objects indexed by globally unique identifiers
012: * to hold all session state for individual sessions.
013: *
014: * @author gbhatnag
015: */
016: public class MetasearchSessionManager implements java.io.Serializable {
017: /* constants */
018: private static final String CACHE_NAME = "org.sakaibrary.xserver.session.MetasearchSession";
019: private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
020: .getLog("org.sakaibrary.osid.repository.xserver.session.MetasearchSessionManager");
021:
022: /* private static variables */
023: private static MetasearchSessionManager metasearchSessionManager;
024: private static CacheManager cacheManager;
025: private static Cache cache;
026:
027: /**
028: * Private constructor to ensure only one MetasearchSessionManager
029: * is instantiated. Initializes ehcache components CacheManager
030: * and Cache.
031: */
032: private MetasearchSessionManager() {
033: try {
034: cacheManager = CacheManager.create();
035:
036: // add the cache to the CacheManager if it doesn't already exist
037: if (!cacheManager.cacheExists(CACHE_NAME)) {
038: // create a cache using ehcache 1.2.4 constructor
039: Cache temp = new Cache(
040: CACHE_NAME, // cache name
041: 50, // maxElementsInMemory
042: net.sf.ehcache.store.MemoryStoreEvictionPolicy.LRU,
043: true, // overflowToDisk
044: "ignored", // disk store path - ignored, CacheManager sets it using setter injection
045: false, // eternal
046: 0L, // time to live (seconds)
047: 900L, // time to idle (seconds)
048: false, // diskPersistent
049: 120L, // diskExpiryThreadIntervalSeconds
050: null, // registeredEventListeners
051: null // bootstrapCacheLoader
052: );
053: cacheManager.addCache(temp);
054: }
055:
056: // get cache for use
057: cache = cacheManager.getCache(CACHE_NAME);
058: } catch (CacheException ce) {
059: LOG
060: .warn(
061: "MetasearchSessionManager() failed to create CacheManager or Cache",
062: ce);
063: }
064:
065: LOG.info("ehcache session initiated properly.");
066: }
067:
068: /**
069: * Gets the Singleton instance of MetasearchSessionManager
070: *
071: * @return an instance of MetasearchSessionManager
072: */
073: public static synchronized MetasearchSessionManager getInstance() {
074: if (metasearchSessionManager == null) {
075: metasearchSessionManager = new MetasearchSessionManager();
076: }
077:
078: return metasearchSessionManager;
079: }
080:
081: /**
082: * Puts the MetasearchSession object into the MetasearchSessionManager
083: * cache indexed by the guid. If the guid already exists, the
084: * MetasearchSession object is updated with the given object.
085: *
086: * @param guid a globally unique identifier String
087: * @param ms the MetasearchSession object to be put/updated in the
088: * MetasearchSessionManager cache.
089: */
090: public void putMetasearchSession(String guid, MetasearchSession ms) {
091: // given guid and ms.getGuid() should match -- TODO new Exception Type?
092: if (!ms.getGuid().equals(guid)) {
093: LOG
094: .warn("putMetasearchSession(): putting MetasearchSession into "
095: + "ehcache with mismatched guids...");
096: }
097:
098: // the following puts if guid is new, updates if guid is old
099: cache.put(new Element(guid, ms));
100: }
101:
102: /**
103: * Gets the MetasearchSession object out of the MetasearchSessionManager
104: * cache indexed by the guid.
105: *
106: * @param guid a globally unique identifier String
107: * @return the MetasearchSession object if it exists and has not expired,
108: * otherwise, null
109: */
110: public MetasearchSession getMetasearchSession(String guid) {
111: Element element = null;
112: try {
113: element = cache.get(guid);
114: } catch (CacheException ce) {
115: LOG.warn("MetasearchSessionManager.getMetasearchSession()"
116: + " cannot get cache with guid: " + guid, ce);
117: }
118:
119: // element could have expired
120: boolean isExpired = (element == null) ? true : cache
121: .isExpired(element);
122:
123: return isExpired ? null : (MetasearchSession) element
124: .getValue();
125: }
126: }
|