KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > SessionManager


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import org.hsqldb.lib.IntKeyHashMap;
35 import org.hsqldb.lib.Iterator;
36 import org.hsqldb.SchemaManager.Schema;
37
38 /**
39  * Container that maintains a map of session id's to Session objects.
40  * Responsible for managing opening and closing of sessions.
41  *
42  * @author fredt@users
43  * @version 1.8.0
44  * @since 1.7.2
45  */

46 public class SessionManager {
47
48     int sessionIdCount = 1;
49     private IntKeyHashMap sessionMap = new IntKeyHashMap();
50     private Session sysSession;
51
52 // TODO:
53
//
54
// Eliminate the Database-centric nature of SessionManager.
55
// e.g. Sessions should be able to migrate from one Database instance
56
// to another using session control language moderated by
57
// SessionManager
58

59     /**
60      * Constructs an new SessionManager handling the specified Database.
61      * Creates a SYS User.
62      */

63     public SessionManager(Database db) {
64
65         User sysUser = db.getUserManager().getSysUser();
66
67         sysSession = new Session(db, sysUser, false, false, 0);
68     }
69
70 // TODO:
71
//
72
// It should be possible to create an initially 'disconnected' Session that
73
// can execute general commands using a SessionCommandInterpreter.
74
//
75
// EXAMPLES: Open a Session to start a Server, add/remove
76
// databases hosted by an existing Server, connect to a
77
// Database...
78
//
79
// REQUIRES: auth scheme independent of any particular Database instance
80
// e.g. provide service to use /etc/passwd and /etc/groups,
81
// JAAS-plugin, etc.
82

83     /**
84      * Binds the specified Session object into this SessionManager's active
85      * Session registry. This method is typically called internally from
86      * {@link
87      * Database#connect(String,String) Database.connect(username,password)}
88      * as the final step, when a successful connection has been made.
89      *
90      * @param db the database to which the new Session is initially connected
91      * @param user the initial Session User
92      * @param readonly the initial ReadOnly attribute for the new Session
93      */

94     public synchronized Session newSession(Database db, User user,
95                                            boolean readonly, boolean forlog) {
96
97         Session s = new Session(db, user, true, readonly, sessionIdCount);
98
99         s.isProcessingLog = forlog;
100
101         sessionMap.put(sessionIdCount, s);
102
103         sessionIdCount++;
104
105         return s;
106     }
107
108     /**
109      * Retrieves the special SYS Session.
110      *
111      * @return the special SYS Session
112      */

113     public Session getSysSession(String JavaDoc schema,
114                                  boolean forScript) throws HsqlException {
115
116         sysSession.currentSchema =
117             sysSession.database.schemaManager.getSchemaHsqlName(schema);
118         sysSession.isProcessingScript = forScript;
119         sysSession.isProcessingLog = false;
120
121         sysSession.setUser(sysSession.database.getUserManager().getSysUser());
122
123         return sysSession;
124     }
125
126     /**
127      * Retrieves the special SYS Session.
128      *
129      * @return the special SYS Session
130      */

131     public Session getSysSession() {
132
133         sysSession.currentSchema =
134             sysSession.database.schemaManager.defaultSchemaHsqlName;
135         sysSession.isProcessingScript = false;
136         sysSession.isProcessingLog = false;
137
138         sysSession.setUser(sysSession.database.getUserManager().getSysUser());
139
140         return sysSession;
141     }
142
143     /**
144      * Retrieves the special SYS Session.
145      *
146      * @return the special SYS Session
147      */

148     public Session getSysSession(String JavaDoc schema,
149                                  User user) throws HsqlException {
150
151         sysSession.currentSchema =
152             sysSession.database.schemaManager.getSchemaHsqlName(schema);
153         sysSession.isProcessingScript = false;
154         sysSession.isProcessingLog = false;
155
156         sysSession.setUser(user);
157
158         return sysSession;
159     }
160
161 /** @todo sig change should be either: closeAllSessions(Database) or closeAllSessions(dbID) */
162
163     /**
164      * Closes all Sessions registered with this SessionManager.
165      */

166     public synchronized void closeAllSessions() {
167
168         // don't disconnect system user; need it to save database
169
Session[] sessions = getAllSessions();
170
171         for (int i = 0; i < sessions.length; i++) {
172             sessions[i].close();
173         }
174     }
175
176     /**
177      * Removes the session from management and disconnects.
178      *
179      * @param session to disconnect
180      */

181     synchronized void removeSession(Session session) {
182         sessionMap.remove(session.getId());
183     }
184
185     /**
186      * Removes all Sessions registered with this SessionManager.
187      */

188     synchronized void clearAll() {
189         sessionMap.clear();
190     }
191
192     /**
193      * Returns true if no session exists beyond the sys session.
194      */

195     synchronized boolean isEmpty() {
196         return sessionMap.isEmpty();
197     }
198
199     /**
200      * Retrieves a list of the Sessions in this container that
201      * are visible to the specified Session, given the access rights of
202      * the Session User.
203      *
204      * @param session The Session determining visibility
205      * @return the Sessions visible to the specified Session
206      */

207     synchronized Session[] getVisibleSessions(Session session) {
208         return session.isAdmin() ? getAllSessions()
209                                  : new Session[]{ session };
210     }
211
212     /**
213      * Retrieves the Session with the specified Session identifier or null
214      * if no such Session is registered with this SessionManager.
215      */

216     synchronized Session getSession(int id) {
217         return (Session) sessionMap.get(id);
218     }
219
220     public synchronized Session[] getAllSessions() {
221
222         Session[] sessions = new Session[sessionMap.size()];
223         Iterator it = sessionMap.values().iterator();
224
225         for (int i = 0; it.hasNext(); i++) {
226             sessions[i] = (Session) it.next();
227         }
228
229         return sessions;
230     }
231
232     public synchronized boolean isUserActive(String JavaDoc userName) {
233
234         Iterator it = sessionMap.values().iterator();
235
236         for (int i = 0; it.hasNext(); i++) {
237             Session session = (Session) it.next();
238
239             if (userName.equals(session.getUser().getName())) {
240                 return true;
241             }
242         }
243
244         return false;
245     }
246
247     public synchronized void removeSchemaReference(Schema schema) {
248
249         Iterator it = sessionMap.values().iterator();
250
251         for (int i = 0; it.hasNext(); i++) {
252             Session session = (Session) it.next();
253
254             if (session.currentSchema == schema.name) {
255                 session.resetSchema();
256             }
257         }
258     }
259 }
260
Popular Tags