Revision 5311
Added by daigle about 14 years ago
SessionService.java | ||
---|---|---|
28 | 28 |
|
29 | 29 |
import java.io.PrintWriter; |
30 | 30 |
import java.util.Calendar; |
31 |
import java.util.Enumeration; |
|
31 | 32 |
import java.util.Hashtable; |
32 | 33 |
|
33 | 34 |
import javax.servlet.http.HttpServletResponse; |
... | ... | |
49 | 50 |
private static Hashtable<String, SessionData> sessionHash = null; |
50 | 51 |
|
51 | 52 |
private static final String PUBLIC_SESSION_ID = "0"; |
53 |
|
|
54 |
private static Object lockObj = new Object(); |
|
52 | 55 |
|
53 | 56 |
/** |
54 | 57 |
* private constructor since this is a singleton |
... | ... | |
113 | 116 |
*/ |
114 | 117 |
public static void registerSession(String sessionId, String userName, |
115 | 118 |
String[] groupNames, String password, String name) throws ServiceException { |
116 |
if (sessionId == null) { |
|
117 |
throw new ServiceException("SessionService.registerSession - " + |
|
118 |
"Cannot register a null session id"); |
|
119 |
synchronized(lockObj) { |
|
120 |
if (sessionId == null) { |
|
121 |
throw new ServiceException("SessionService.registerSession - " + |
|
122 |
"Cannot register a null session id"); |
|
123 |
} |
|
124 |
logMetacat.debug("SessionService.registerSession - Registering session id: " + sessionId); |
|
125 |
SessionData sessionData = new SessionData(sessionId, userName, groupNames, |
|
126 |
password, name); |
|
127 |
sessionHash.put(sessionId, sessionData); |
|
119 | 128 |
} |
120 |
logMetacat.debug("SessionService.registerSession - Registering session id: " + sessionId); |
|
121 |
SessionData sessionData = new SessionData(sessionId, userName, groupNames, |
|
122 |
password, name); |
|
123 |
sessionHash.put(sessionId, sessionData); |
|
124 | 129 |
} |
125 | 130 |
|
126 | 131 |
/** |
... | ... | |
130 | 135 |
* the session data object to add to the session hash |
131 | 136 |
*/ |
132 | 137 |
public static void registerSession(SessionData sessionData) throws ServiceException { |
133 |
if (sessionData == null) { |
|
134 |
throw new ServiceException("SessionService.registerSession - " + |
|
135 |
"Cannot register null session data"); |
|
138 |
synchronized(lockObj) { |
|
139 |
if (sessionData == null) { |
|
140 |
throw new ServiceException("SessionService.registerSession - " + |
|
141 |
"Cannot register null session data"); |
|
142 |
} |
|
143 |
logMetacat.debug("SessionService.registerSession - Registering session " + |
|
144 |
"data with id: " + sessionData.getId()); |
|
145 |
sessionHash.put(sessionData.getId(), sessionData); |
|
136 | 146 |
} |
137 |
logMetacat.debug("SessionService.registerSession - Registering session " + |
|
138 |
"data with id: " + sessionData.getId()); |
|
139 |
sessionHash.put(sessionData.getId(), sessionData); |
|
140 | 147 |
} |
141 | 148 |
|
142 | 149 |
/** |
... | ... | |
146 | 153 |
* the id of the session to remove. |
147 | 154 |
*/ |
148 | 155 |
public static void unRegisterSession(String sessionId) { |
149 |
if (sessionId == null) { |
|
150 |
logMetacat.error("SessionService.unRegisterSession - trying to " + |
|
156 |
synchronized(lockObj) { |
|
157 |
if (sessionId == null) { |
|
158 |
logMetacat.error("SessionService.unRegisterSession - trying to " + |
|
151 | 159 |
"unregister a session with null id"); |
160 |
} |
|
161 |
|
|
162 |
logMetacat.info("SessionService.unRegisterSession - unRegistering session: " + sessionId); |
|
163 |
sessionHash.remove(sessionId); |
|
152 | 164 |
} |
153 |
|
|
154 |
logMetacat.info("SessionService.unRegisterSession - unRegistering session: " + sessionId); |
|
155 |
sessionHash.remove(sessionId); |
|
156 | 165 |
} |
157 | 166 |
|
158 | 167 |
/** |
168 |
* Unregister all sessions from the session hash table except the public session. |
|
169 |
* |
|
170 |
* @param sessionId |
|
171 |
* the id of the session to remove. |
|
172 |
*/ |
|
173 |
public static void unRegisterAllSessions() { |
|
174 |
synchronized(lockObj) { |
|
175 |
Enumeration<String> keyEnum = sessionHash.keys(); |
|
176 |
while (keyEnum.hasMoreElements()) { |
|
177 |
String sessionId = keyEnum.nextElement(); |
|
178 |
if (!sessionId.equals(PUBLIC_SESSION_ID)) { |
|
179 |
logMetacat.info("SessionService.unRegisterAllSessions - unRegistering session: " + sessionId); |
|
180 |
sessionHash.remove(sessionId); |
|
181 |
} |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
|
|
186 |
/** |
|
159 | 187 |
* Check if a session is registered in the session hash table. |
160 | 188 |
* |
161 | 189 |
* @param sessionId |
... | ... | |
246 | 274 |
if (sessionId == null) { |
247 | 275 |
logMetacat.error("SessionService.touchSession - trying to touch a session with null id"); |
248 | 276 |
} else if (isSessionRegistered(sessionId)) { |
249 |
SessionData sessionData = getRegisteredSession(sessionId); |
|
250 |
sessionData.setLastAccessedTime(); |
|
277 |
synchronized(lockObj) { |
|
278 |
SessionData sessionData = getRegisteredSession(sessionId); |
|
279 |
sessionData.setLastAccessedTime(); |
|
280 |
} |
|
251 | 281 |
} |
252 | 282 |
} |
253 | 283 |
|
... | ... | |
260 | 290 |
if(lastAccessedTime.compareTo(expireTime) < 0 ) { |
261 | 291 |
unRegisterSession(sessionId); |
262 | 292 |
} |
263 |
} |
|
264 |
|
|
265 |
|
|
293 |
} |
|
266 | 294 |
} |
267 | 295 |
|
268 | 296 |
} |
Also available in: Unified diff
Merge 1.9.2 changes back into the trunk