KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > server > session > AbstractSessionManager


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.server.session;
20
21 import java.io.Serializable JavaDoc;
22
23 import java.util.Collection JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import java.util.logging.Level JavaDoc;
26
27 import sync4j.framework.logging.Sync4jLogger;
28 import sync4j.framework.server.session.SessionHandler;
29 import sync4j.framework.server.session.SessionExpiredException;
30
31 /**
32  * This is a basic implementation of a session manager. Subclasses have to
33  * extend it in order to get and create sessions.
34  *
35  * @author Stefano Fornari @ Funambol.com
36  * @version $Id: AbstractSessionManager.java,v 1.4 2005/03/02 20:57:38 harrie Exp $
37  */

38 public abstract class AbstractSessionManager implements Serializable JavaDoc {
39     
40     // -------------------------------------------------------------- Properties
41

42     /**
43      * The lifetime of a session in minutes
44      */

45     private int lifeTime = 5;
46     
47     /** Getter for property lifeTime.
48      * @return Value of property lifeTime.
49      */

50     public int getLifeTime() {
51         return lifeTime;
52     }
53     
54     /** Setter for property lifeTime.
55      * @param lifeTime New value of property lifeTime.
56      */

57     public void setLifeTime(int lifeTime) {
58         this.lifeTime = lifeTime;
59     }
60
61     
62     // ------------------------------------------------------------ Private data
63

64     /**
65      * The logger
66      */

67     private static final Logger JavaDoc log = Sync4jLogger.getLogger();
68         
69     // ---------------------------------------------------------- Public methods
70

71     public SessionHandler getSessionHandler(String JavaDoc sessionId)
72     throws SessionExpiredException {
73         if (log.isLoggable(Level.FINEST)) {
74         log.finest("Looking for session " + sessionId);
75         }
76         
77         SessionHandler handler = getSessionFromBag(sessionId);
78         
79         if (handler == null) {
80             handler = createNewSession(sessionId);
81             putSessionInBag(sessionId, handler);
82         } else {
83             if (handler.isNew()) {
84                 handler.setNew(false);
85                 putSessionInBag(sessionId, handler);
86             }
87         }
88         
89         return handler;
90     }
91     
92     public void removeSession(String JavaDoc sessionId) {
93         if (log.isLoggable(Level.FINEST)) {
94             log.finest("Removing session " + sessionId);
95         }
96         
97         try {
98             SessionHandler oldSession = getSessionFromBag(sessionId);
99             oldSession.expire();
100             removeSessionFromBag(sessionId);
101         } catch (SessionExpiredException e) {
102             if (log.isLoggable(Level.FINEST)) {
103                 log.finest("Session already expired");
104             }
105         }
106         
107         if (log.isLoggable(Level.FINEST)) {
108             log.finest("Remaining sessions: " + getSessionBag());
109         }
110     }
111     
112     public void removeSession(SessionHandler handler) {
113         if (log.isLoggable(Level.FINEST)) {
114             log.finest("Removing session " + ((handler!=null) ? handler.getSessionId() : "null"));
115         }
116         
117         handler.expire();
118         removeSessionFromBag(handler.getSessionId());
119         
120         if (log.isLoggable(Level.FINEST)) {
121             log.finest("Remaining sessions: " + getSessionBag());
122         }
123     }
124     
125     public void storeSessionHandler(SessionHandler handler) {
126         log.fine("Storing session " + handler);
127
128
129         
130         putSessionInBag(handler.getSessionId(), handler);
131     }
132     
133     
134     // -------------------------------------------------------- Abstract methods
135

136     abstract protected SessionHandler createNewSession(String JavaDoc sessionId);
137     abstract protected SessionHandler getSessionFromBag(String JavaDoc sessionId) throws SessionExpiredException;
138     abstract protected void putSessionInBag(String JavaDoc sessionId, SessionHandler handler);
139     abstract protected void removeSessionFromBag(String JavaDoc sessionId);
140     abstract protected Collection JavaDoc getSessionBag();
141 }
142
143
Popular Tags