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:
020: package org.openharmonise.rm.sessions;
021:
022: import org.openharmonise.commons.cache.*;
023: import org.openharmonise.commons.dsi.*;
024:
025: /**
026: * This class provides a cache for <code>Session</code> objects</p>.
027: *
028: * @author Michael Bell
029: *
030: */
031: public final class SessionCache extends AbstractCache {
032: private static SessionCache m_instance = null;
033: private static String CACHE_NAME = "SESSION";
034:
035: protected AbstractDataStoreInterface m_dbinterf = null;
036:
037: /**
038: * Creates an instance of this cache.
039: *
040: * @param dbinterf
041: * @throws CacheException
042: */
043: private SessionCache(AbstractDataStoreInterface dbinterf)
044: throws CacheException {
045: super (CACHE_NAME);
046:
047: m_dbinterf = dbinterf;
048: }
049:
050: /**
051: * Returns the singleton instance of this cache.
052: *
053: * @param dbinterf
054: * @return
055: * @throws CacheException
056: */
057: public synchronized static SessionCache getInstance(
058: AbstractDataStoreInterface dbinterf) throws CacheException {
059: if (m_instance == null) {
060: m_instance = new SessionCache(dbinterf);
061: }
062:
063: return m_instance;
064: }
065:
066: /**
067: * Returns Session object corresponding to the given session id.
068: *
069: * @param session_id
070: * @return
071: * @throws CacheException
072: */
073: public Session getSession(String session_id) throws CacheException {
074: final Session cached_session = (Session) getObject(session_id);
075:
076: return cached_session;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.openharmonise.commons.cache.AbstractCache#getObject(java.lang.Object)
081: */
082: public Object getObject(final Object key) throws CacheException {
083: // check hashmap
084: Object cached_object = super .getObject(key);
085: if (cached_object == null) {
086: try {
087: cached_object = new Session(m_dbinterf, (String) key);
088: addToCache(key, cached_object);
089: } catch (SessionException e) {
090: cached_object = null;
091: }
092: }
093: return cached_object;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.openharmonise.commons.cache.AbstractCache#getCacheableObject(java.lang.Object)
098: */
099: protected Object getCacheableObject(Object session_id)
100: throws Exception {
101: if (session_id == null) {
102: throw new IllegalArgumentException(
103: "Session id must be passed "
104: + " to GetCacheableObject user_id:"
105: + session_id);
106: }
107:
108: Session session = null;
109:
110: try {
111: session = new Session(m_dbinterf, (String) session_id);
112: } catch (InvalidSessionIdException e) {
113: session = null;
114: }
115:
116: return session;
117: }
118: }
|