KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > sessions > SessionCache


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.rm.sessions;
21
22 import org.openharmonise.commons.cache.*;
23 import org.openharmonise.commons.dsi.*;
24
25
26
27 /**
28  * This class provides a cache for <code>Session</code> objects</p>.
29  *
30  * @author Michael Bell
31  *
32  */

33 public final class SessionCache extends AbstractCache {
34     private static SessionCache m_instance = null;
35     private static String JavaDoc CACHE_NAME = "SESSION";
36
37     protected AbstractDataStoreInterface m_dbinterf = null;
38
39     /**
40      * Creates an instance of this cache.
41      *
42      * @param dbinterf
43      * @throws CacheException
44      */

45     private SessionCache(AbstractDataStoreInterface dbinterf) throws CacheException {
46         super(CACHE_NAME);
47
48         m_dbinterf = dbinterf;
49     }
50
51     /**
52      * Returns the singleton instance of this cache.
53      *
54      * @param dbinterf
55      * @return
56      * @throws CacheException
57      */

58     public synchronized static SessionCache getInstance(AbstractDataStoreInterface dbinterf)
59                                                      throws CacheException {
60         if (m_instance == null) {
61             m_instance = new SessionCache(dbinterf);
62         }
63
64         return m_instance;
65     }
66
67     /**
68      * Returns Session object corresponding to the given session id.
69      *
70      * @param session_id
71      * @return
72      * @throws CacheException
73      */

74     public Session getSession(String JavaDoc session_id) throws CacheException {
75         final Session cached_session = (Session) getObject(session_id);
76
77         return cached_session;
78     }
79
80
81     /* (non-Javadoc)
82      * @see org.openharmonise.commons.cache.AbstractCache#getObject(java.lang.Object)
83      */

84     public Object JavaDoc getObject(final Object JavaDoc key) throws CacheException {
85         // check hashmap
86
Object JavaDoc cached_object = super.getObject(key);
87         if (cached_object == null) {
88             try {
89                 cached_object = new Session(m_dbinterf,(String JavaDoc)key);
90                 addToCache(key, cached_object);
91             } catch (SessionException e) {
92                 cached_object = null;
93             }
94         }
95         return cached_object;
96     }
97
98     /* (non-Javadoc)
99      * @see org.openharmonise.commons.cache.AbstractCache#getCacheableObject(java.lang.Object)
100      */

101     protected Object JavaDoc getCacheableObject(Object JavaDoc session_id)
102                                  throws Exception JavaDoc {
103         if (session_id == null) {
104             throw new IllegalArgumentException JavaDoc("Session id must be passed " +
105                                                " to GetCacheableObject user_id:" +
106                                                session_id);
107         }
108
109         Session session = null;
110         
111         try {
112             session = new Session(m_dbinterf, (String JavaDoc) session_id);
113         } catch (InvalidSessionIdException e) {
114             session = null;
115         }
116         
117         return session;
118     }
119 }
Popular Tags