1 |
4080
|
daigle
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that implements session utility methods
|
4 |
|
|
* Copyright: 2008 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Michael Daigle
|
7 |
|
|
*
|
8 |
|
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
|
|
*
|
12 |
|
|
* This program is free software; you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
*
|
22 |
|
|
* You should have received a copy of the GNU General Public License
|
23 |
|
|
* along with this program; if not, write to the Free Software
|
24 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package edu.ucsb.nceas.metacat.service;
|
28 |
|
|
|
29 |
|
|
import java.util.Hashtable;
|
30 |
|
|
|
31 |
|
|
import org.apache.log4j.Logger;
|
32 |
|
|
|
33 |
|
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
34 |
|
|
|
35 |
4442
|
daigle
|
public class SessionService extends BaseService {
|
36 |
4080
|
daigle
|
|
37 |
|
|
private static SessionService sessionService = null;
|
38 |
|
|
|
39 |
|
|
private static Logger logMetacat = Logger.getLogger(SessionService.class);
|
40 |
|
|
private static Hashtable<String, SessionData> sessionHash = null;
|
41 |
|
|
|
42 |
4297
|
daigle
|
private static final String PUBLIC_SESSION_ID = "0";
|
43 |
4856
|
daigle
|
public static final String PUBLIC_USER_NAME = "public";
|
44 |
|
|
|
45 |
|
|
private static final String REPLICATION_SESSION_ID = "1";
|
46 |
|
|
public static final String REPLICATION_USER_NAME = "replication";
|
47 |
4080
|
daigle
|
|
48 |
|
|
/**
|
49 |
|
|
* private constructor since this is a singleton
|
50 |
|
|
*/
|
51 |
4780
|
daigle
|
private SessionService() throws ServiceException {
|
52 |
4080
|
daigle
|
sessionHash = new Hashtable<String, SessionData>();
|
53 |
4780
|
daigle
|
logMetacat.debug("Registering public session id: " + PUBLIC_SESSION_ID);
|
54 |
4856
|
daigle
|
registerSession(PUBLIC_SESSION_ID, PUBLIC_USER_NAME, null, null);
|
55 |
|
|
registerSession(REPLICATION_SESSION_ID, REPLICATION_USER_NAME, null, null);
|
56 |
4080
|
daigle
|
}
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
4429
|
daigle
|
* Get the single instance of SessionService.
|
60 |
4080
|
daigle
|
*
|
61 |
4429
|
daigle
|
* @return the single instance of SessionService
|
62 |
4080
|
daigle
|
*/
|
63 |
4780
|
daigle
|
public static SessionService getInstance() throws ServiceException {
|
64 |
4080
|
daigle
|
if (sessionService == null) {
|
65 |
|
|
sessionService = new SessionService();
|
66 |
|
|
}
|
67 |
|
|
return sessionService;
|
68 |
|
|
}
|
69 |
4297
|
daigle
|
|
70 |
4442
|
daigle
|
public boolean refreshable() {
|
71 |
|
|
return false;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
4709
|
daigle
|
protected void doRefresh() throws ServiceException {
|
75 |
4442
|
daigle
|
return;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
4297
|
daigle
|
/**
|
79 |
|
|
* Register a session in the session hash table. This uses
|
80 |
|
|
* the parameters passed in to create a SessionData object.
|
81 |
|
|
*
|
82 |
|
|
* @param sessionId
|
83 |
|
|
* the ID of the session to register
|
84 |
|
|
* @param userName
|
85 |
|
|
* the user name of the session
|
86 |
|
|
* @param groupNames
|
87 |
|
|
* the group names for the session
|
88 |
|
|
* @param password
|
89 |
|
|
* the password for the session
|
90 |
|
|
*/
|
91 |
4080
|
daigle
|
public static void registerSession(String sessionId, String userName,
|
92 |
4780
|
daigle
|
String[] groupNames, String password) throws ServiceException {
|
93 |
|
|
if (sessionId == null) {
|
94 |
|
|
throw new ServiceException("Cannot register a null session id");
|
95 |
|
|
}
|
96 |
|
|
logMetacat.debug("Registering session id: " + sessionId);
|
97 |
4080
|
daigle
|
SessionData sessionData = new SessionData(sessionId, userName, groupNames,
|
98 |
|
|
password);
|
99 |
|
|
sessionHash.put(sessionId, sessionData);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
4297
|
daigle
|
/**
|
103 |
|
|
* Register a session in the session hash table.
|
104 |
|
|
*
|
105 |
|
|
* @param sessionData
|
106 |
|
|
* the session data object to add to the session hash
|
107 |
|
|
*/
|
108 |
4780
|
daigle
|
public static void registerSession(SessionData sessionData) throws ServiceException {
|
109 |
|
|
if (sessionData == null) {
|
110 |
|
|
throw new ServiceException("Cannot register null session data");
|
111 |
|
|
}
|
112 |
|
|
logMetacat.debug("Registering session date with id: " + sessionData.getId());
|
113 |
4080
|
daigle
|
sessionHash.put(sessionData.getId(), sessionData);
|
114 |
|
|
}
|
115 |
|
|
|
116 |
4297
|
daigle
|
/**
|
117 |
|
|
* Unregister a session from the session hash table.
|
118 |
|
|
*
|
119 |
|
|
* @param sessionId
|
120 |
|
|
* the id of the session to remove.
|
121 |
|
|
*/
|
122 |
4080
|
daigle
|
public static void unRegisterSession(String sessionId) {
|
123 |
4780
|
daigle
|
if (sessionId != null) {
|
124 |
|
|
logMetacat.error("trying to unregister a session with null id");
|
125 |
|
|
sessionHash.remove(sessionId);
|
126 |
|
|
}
|
127 |
4080
|
daigle
|
}
|
128 |
|
|
|
129 |
4297
|
daigle
|
/**
|
130 |
|
|
* Check if a session is registered in the session hash table.
|
131 |
|
|
* TODO MCD need to time sessions out
|
132 |
|
|
*
|
133 |
|
|
* @param sessionId
|
134 |
|
|
* the id of the session to look for.
|
135 |
|
|
*/
|
136 |
4080
|
daigle
|
public static boolean isSessionRegistered(String sessionId) {
|
137 |
4780
|
daigle
|
if (sessionId == null) {
|
138 |
|
|
logMetacat.error("trying to check if a session with null id is registered");
|
139 |
|
|
return false;
|
140 |
|
|
}
|
141 |
4080
|
daigle
|
return sessionHash.containsKey(sessionId);
|
142 |
|
|
}
|
143 |
|
|
|
144 |
4297
|
daigle
|
/**
|
145 |
|
|
* Get a registered session from the session hash table.
|
146 |
|
|
* TODO MCD need to time sessions out
|
147 |
|
|
*
|
148 |
|
|
* @param sessionId
|
149 |
|
|
* the id of the session to retrieve.
|
150 |
|
|
*/
|
151 |
4080
|
daigle
|
public static SessionData getRegisteredSession(String sessionId) {
|
152 |
4780
|
daigle
|
if (sessionId == null) {
|
153 |
|
|
logMetacat.error("trying to get a session with null id");
|
154 |
|
|
return null;
|
155 |
|
|
}
|
156 |
4080
|
daigle
|
return sessionHash.get(sessionId);
|
157 |
|
|
}
|
158 |
|
|
|
159 |
4297
|
daigle
|
/**
|
160 |
|
|
* Get the public session from the session hash table.
|
161 |
|
|
*/
|
162 |
|
|
public static SessionData getPublicSession() {
|
163 |
|
|
return sessionHash.get(PUBLIC_SESSION_ID);
|
164 |
4080
|
daigle
|
}
|
165 |
|
|
|
166 |
4297
|
daigle
|
/**
|
167 |
4856
|
daigle
|
* Get the replication session from the session hash table.
|
168 |
|
|
*/
|
169 |
|
|
public static SessionData getReplicationSession() {
|
170 |
|
|
return sessionHash.get(REPLICATION_SESSION_ID);
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
/**
|
174 |
4297
|
daigle
|
* Keep a session active by updating its last accessed time.
|
175 |
|
|
*
|
176 |
|
|
* @param sessionId
|
177 |
|
|
* the id of the session to update.
|
178 |
|
|
*/
|
179 |
4080
|
daigle
|
public static synchronized void touchSession(String sessionId) {
|
180 |
4780
|
daigle
|
if (sessionId == null) {
|
181 |
|
|
logMetacat.error("trying to touch a session with null id");
|
182 |
|
|
} else if (isSessionRegistered(sessionId)) {
|
183 |
4080
|
daigle
|
SessionData sessionData = getRegisteredSession(sessionId);
|
184 |
|
|
sessionData.setLastAccessedTime();
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
}
|