1 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 46 public class SessionManager { 47 48 int sessionIdCount = 1; 49 private IntKeyHashMap sessionMap = new IntKeyHashMap(); 50 private Session sysSession; 51 52 59 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 83 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 113 public Session getSysSession(String 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 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 148 public Session getSysSession(String 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 162 163 166 public synchronized void closeAllSessions() { 167 168 Session[] sessions = getAllSessions(); 170 171 for (int i = 0; i < sessions.length; i++) { 172 sessions[i].close(); 173 } 174 } 175 176 181 synchronized void removeSession(Session session) { 182 sessionMap.remove(session.getId()); 183 } 184 185 188 synchronized void clearAll() { 189 sessionMap.clear(); 190 } 191 192 195 synchronized boolean isEmpty() { 196 return sessionMap.isEmpty(); 197 } 198 199 207 synchronized Session[] getVisibleSessions(Session session) { 208 return session.isAdmin() ? getAllSessions() 209 : new Session[]{ session }; 210 } 211 212 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 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 |