KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > syncbean > session > SessionManager


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.server.syncbean.session;
20
21 import java.io.File JavaDoc;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 import sync4j.framework.tools.beans.BeanFactory;
29 import sync4j.framework.tools.beans.BeanException;
30 import sync4j.framework.logging.Sync4jLogger;
31 import sync4j.framework.server.session.AbstractSessionManager;
32 import sync4j.framework.server.session.SessionHandler;
33 import sync4j.framework.server.session.SessionExpiredException;
34 import sync4j.server.session.SyncSessionHandler;
35
36 /**
37  * This implementation of a <i>AbstractSessionManager</i> stores and retrieves
38  * sessions to/from the file system.
39  * For each session is serialized under the directory pointed by <i>sessionsPath</i>
40  * in a file called {session_id}.session . This is a very simple implementation
41  * that allows many servers to access the same session repository. In fact, in
42  * a real deployment, we can have SyncML servers replicated for reliability and
43  * scalability purposes. In that case they must share the directory specified
44  * by <i>sessionsPath</i>.
45  * <p>
46  * <b>This class is not thread safe. It is supposed that an instance is not
47  * shared by multiple threads</b>.
48  *
49  * @author Stefano Fornari @ Funambol.com
50  * @version $Id: SessionManager.java,v 1.11 2005/03/02 20:57:40 harrie Exp $
51  */

52 public class SessionManager extends AbstractSessionManager implements java.io.Serializable JavaDoc {
53     
54     // ------------------------------------------------------------ Private data
55

56     /**
57      * The logger
58      */

59     private static final Logger JavaDoc log = Sync4jLogger.getLogger();
60     
61     // -------------------------------------------------------------- Properties
62

63     private String JavaDoc sessionsPath = null;
64     
65     /** Getter for property sessionsPath.
66      * @return Value of property sessionsPath.
67      */

68     public String JavaDoc getSessionsPath() {
69         return sessionsPath;
70     }
71     
72     /** Setter for property sessionsPath.
73      * @param sessionsPath New value of property sessionsPath.
74      */

75     public void setSessionsPath(String JavaDoc sessionsPath) {
76         this.sessionsPath = sessionsPath;
77     }
78     
79     // ---------------------------------------------------------- Public methods
80

81     
82     // --------------------------------------------------------- Private methods
83
protected Collection JavaDoc getSessionBag() {
84         ArrayList JavaDoc sessions = new ArrayList JavaDoc();
85         
86         //
87
// Get a list of the content of the sessionsPath directory. Than for
88
// each file deserialized it and add it to sessions.
89
//
90
File JavaDoc sessionsPathFile = new File JavaDoc(sessionsPath);
91         
92         if (!sessionsPathFile.exists()) {
93             return sessions; // returns an empty collection
94
}
95         
96         File JavaDoc[] files = sessionsPathFile.listFiles();
97         
98         SessionHandler handler = null;
99         for (int i=0; (files != null) && (i<files.length); ++i) {
100             try {
101                 handler = (SessionHandler)BeanFactory.getBeanInstance(files[i]);
102                 sessions.add(handler);
103             } catch (BeanException e) {
104                 log.throwing(getClass().getName(), "getSessionBag", e.getCause());
105             }
106         }
107         
108         return sessions;
109     }
110     
111     protected SessionHandler getSessionFromBag(String JavaDoc sessionId) throws SessionExpiredException {
112         File JavaDoc sessionFile = new File JavaDoc(sessionsPath, sessionId + ".session");
113         
114         if (!sessionFile.exists()) {
115             return null;
116         }
117         
118         SessionHandler handler = null;
119         try {
120             handler = (SessionHandler)BeanFactory.getBeanInstance(sessionFile);
121         } catch (BeanException e) {
122             log.throwing(getClass().getName(), "getSessionFromBag", e.getCause());
123         }
124         
125         return handler;
126     }
127     
128     protected void putSessionInBag(String JavaDoc sessionId, SessionHandler handler) {
129         File JavaDoc sessionFile = new File JavaDoc(sessionsPath, sessionId + ".session");
130         
131         try {
132             BeanFactory.saveBeanInstance(handler, sessionFile);
133         } catch (BeanException e) {
134             log.severe("Error in serializing " + sessionFile);
135             log.throwing(getClass().getName(), "putSessionInBag", e.getCause());
136         }
137     }
138     
139     protected void removeSessionFromBag(String JavaDoc sessionId) {
140         File JavaDoc sessionFile = new File JavaDoc(sessionsPath, sessionId + ".session");
141         
142         if (sessionFile.exists()) {
143             sessionFile.delete();
144         }
145     }
146
147     protected SessionHandler createNewSession(String JavaDoc sessionId) {
148         if (log.isLoggable(Level.FINEST)) {
149             log.finest("Creating a new session with id " + sessionId);
150         }
151         
152         SyncSessionHandler sessionHandler = new SyncSessionHandler();
153         
154         putSessionInBag(sessionId, sessionHandler);
155         
156         return sessionHandler;
157     }
158 }
159
160
Popular Tags