1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.caleigo.security;
20
21 import java.util.*;
22 import java.lang.reflect.*;
23
24 import org.caleigo.toolkit.log.*;
25
26 /*** <Description for SessionHandler>
27 *
28 * @author Mattias Hagstrand
29 * @version 1.00
30 *
31 *//*
32 *
33 * WHEN WHO WHY & WHAT
34 * -----------------------------------------------------------------------------
35 * 2002-10-20 Mattias Hagstrand Creation
36 */
37 public class SessionHandler
38 {
39
40 private static SessionHandler sInstance;
41 private static Class mSessionClass;
42
43
44 public static SessionHandler getInstance()
45 {
46 if (sInstance == null)
47 sInstance = new SessionHandler();
48 return sInstance;
49 }
50
51
52 private int mCurrentSessionID;
53 protected HashMap mSessions;
54
55
56 private SessionHandler()
57 {
58 mSessions = new HashMap(20);
59 setSessionClass(ISession.DefaultSession.class);
60 }
61
62
63
64 /*** Creates a new session and associates a session objetc with it.
65 */
66 public synchronized void createSession(UserInfo userInfo)
67 {
68 int sessionID = this.generateSessionID();
69 mSessions.put(new Integer(sessionID), createSession(userInfo, sessionID));
70 userInfo.setSessionID(sessionID);
71 }
72
73 public synchronized void destroySession(int sessionID)
74 {
75 mSessions.remove(new Integer(sessionID));
76 }
77
78 /*** Returns an ISessesion object for the provided session id, or <code>null</code>
79 * if no valid session exists. Note that invoker of this method should not
80 * cache the session object since it may be invalidated at any time.
81 */
82 public synchronized ISession getSession(int sessionID)
83 {
84 ISession session = (ISession) mSessions.get(new Integer(sessionID));
85 if (session != null && session.isValid())
86 return session;
87
88 return null;
89 }
90
91 public synchronized boolean isValidSession(UserInfo userInfo, int sessionID)
92 {
93 ISession session = (ISession) mSessions.get(new Integer(sessionID));
94 if (session != null &&
95 session.isValid() &&
96 session.getUserInfo().getUserID().compareTo(userInfo.getUserID()) == 0 &&
97 session.getUserInfo().getSessionID() == sessionID)
98 return true;
99 return false;
100 }
101
102 public synchronized void setSessionClass(Class sessionClass)
103 {
104 if ( !ISession.class.isAssignableFrom(sessionClass) )
105 throw new IllegalArgumentException("The class " + sessionClass +
106 " does not implement ISession");
107 mSessionClass = sessionClass;
108 }
109
110
111 protected synchronized int generateSessionID()
112 {
113 return ++mCurrentSessionID;
114 }
115
116 protected ISession createSession(UserInfo userInfo, int sessionID)
117 {
118 try
119 {
120 Constructor constructor = mSessionClass.getConstructor(new Class[] {UserInfo.class, Integer.TYPE});
121 ISession session = (ISession) constructor.newInstance(new Object[] {userInfo, new Integer(sessionID)});
122 return session;
123 }
124 catch (Exception e)
125 {
126 Log.printError(this, "Could not create session", e);
127 throw new RuntimeException("Error while creating a session object", e);
128 }
129 }
130 }