1 21 package oracle.toplink.essentials.tools.sessionmanagement; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.*; 27 import oracle.toplink.essentials.logging.*; 28 import oracle.toplink.essentials.sessions.*; 29 import oracle.toplink.essentials.exceptions.*; 30 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 31 import oracle.toplink.essentials.internal.security.PrivilegedGetClassLoaderForClass; 32 import oracle.toplink.essentials.internal.sessions.AbstractSession; 33 34 53 public class SessionManager { 54 protected static SessionManager manager; 55 protected AbstractSession defaultSession; 56 protected Hashtable sessions = new Hashtable(); 57 58 62 public SessionManager() { 63 sessions = new Hashtable(5); 64 } 65 66 71 public void addSession(Session session) { 72 getSessions().put(session.getName(), session); 73 } 74 75 79 public void addSession(String sessionName, Session session) { 80 session.setName(sessionName); 81 getSessions().put(sessionName, session); 82 } 83 84 88 public Session getDefaultSession() { 89 if (defaultSession == null) { 90 defaultSession = getSession("default"); 91 } 92 return defaultSession; 93 } 94 95 99 public void destroySession(String sessionName) { 100 DatabaseSession session = (DatabaseSession)getSessions().get(sessionName); 101 102 if (session != null) { 103 destroy(session); 104 } else { 105 logAndThrowException(SessionLog.WARNING, ValidationException.noSessionRegisteredForName(sessionName)); 106 } 107 } 108 109 private void destroy(DatabaseSession session) { 110 if (session.isConnected()) { 111 session.logout(); 112 } 113 114 sessions.remove(session.getName()); 115 session = null; 116 } 117 118 122 public void destroyAllSessions() { 123 Enumeration toRemoveSessions = getSessions().elements(); 124 125 while (toRemoveSessions.hasMoreElements()) { 126 destroy((DatabaseSession)toRemoveSessions.nextElement()); 127 } 128 } 129 130 private ClassLoader getMyClassLoader(){ 131 ClassLoader classLoader = null; 132 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 133 try{ 134 return (ClassLoader )AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(this.getClass())); 135 } catch (PrivilegedActionException exc){ 136 return null; 137 } 138 } else { 139 return PrivilegedAccessHelper.getClassLoaderForClass(this.getClass()); 140 } 141 } 142 143 148 public synchronized static SessionManager getManager() { 149 if (manager == null) { 150 manager = new SessionManager(); 151 } 152 153 return manager; 154 } 155 156 161 public AbstractSession getSession(String sessionName) { 162 return getSession(sessionName, getMyClassLoader(), true, false, false); 163 } 164 165 171 public AbstractSession getSession(String sessionName, boolean shouldLoginSession) { 172 return getSession(sessionName, getMyClassLoader(), shouldLoginSession, false, false); 173 } 174 175 182 public AbstractSession getSession(String sessionName, boolean shouldLoginSession, boolean shouldRefreshSession) { 183 return getSession(sessionName, getMyClassLoader(), shouldLoginSession, shouldRefreshSession, false); 184 } 185 186 196 public AbstractSession getSession(String sessionName, ClassLoader objectClassLoader) { 197 return getSession(sessionName, objectClassLoader, true, false, false); 198 } 199 200 214 public synchronized AbstractSession getSession(String sessionName, ClassLoader objectClassLoader, boolean shouldLoginSession, boolean shouldRefreshSession, boolean shouldCheckClassLoader) { 215 AbstractSession session = (AbstractSession)getSessions().get(sessionName); 216 if (shouldCheckClassLoader && (session != null) && !session.getDatasourcePlatform().getConversionManager().getLoader().equals(objectClassLoader)) { 217 shouldRefreshSession = true; 220 } 221 if ((session == null) || shouldRefreshSession) { 222 if (session != null) { 223 if (session.isDatabaseSession() && session.isConnected()) { 224 ((DatabaseSession)session).logout(); 225 } 226 227 getSessions().remove(sessionName); 228 } 229 } 230 231 if (session == null) { 232 logAndThrowException(SessionLog.WARNING, ValidationException.noSessionFound(sessionName, "")); 233 } else if (shouldLoginSession && !session.isConnected()) { 234 ((DatabaseSession)session).login(); 235 } 236 237 return session; 238 } 239 240 244 private void logAndThrowException(int level, RuntimeException exception) throws RuntimeException { 245 AbstractSessionLog.getLog().logThrowable(level, exception); 246 throw exception; 247 } 248 249 253 public void setSessions(Hashtable sessions) { 254 this.sessions = sessions; 255 } 256 257 261 public Hashtable getSessions() { 262 return sessions; 263 } 264 265 270 public void setDefaultSession(Session defaultSession) { 271 this.defaultSession = (AbstractSession)defaultSession; 272 addSession("default", defaultSession); 273 } 274 275 280 public static void setManager(SessionManager theManager) { 281 manager = theManager; 282 } 283 } 284 | Popular Tags |