Project

General

Profile

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 4080 daigle
44
	/**
45
	 * private constructor since this is a singleton
46
	 */
47 4780 daigle
	private SessionService() throws ServiceException {
48 4080 daigle
		sessionHash = new Hashtable<String, SessionData>();
49 4780 daigle
		logMetacat.debug("Registering public session id: " + PUBLIC_SESSION_ID);
50 4861 daigle
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
51 4080 daigle
	}
52
53
	/**
54 4429 daigle
	 * Get the single instance of SessionService.
55 4080 daigle
	 *
56 4429 daigle
	 * @return the single instance of SessionService
57 4080 daigle
	 */
58 4780 daigle
	public static SessionService getInstance() throws ServiceException {
59 4080 daigle
		if (sessionService == null) {
60
			sessionService = new SessionService();
61
		}
62
		return sessionService;
63
	}
64 4297 daigle
65 4442 daigle
	public boolean refreshable() {
66
		return false;
67
	}
68
69 4981 daigle
	public void doRefresh() throws ServiceException {
70 4442 daigle
		return;
71
	}
72
73 4981 daigle
	public void stop() throws ServiceException {
74
		return;
75
	}
76
77 4297 daigle
	/**
78
	 * Register a session in the session hash table.  This uses
79
	 * the parameters passed in to create a SessionData object.
80
	 *
81
	 * @param sessionId
82
	 *            the ID of the session to register
83
	 * @param userName
84
	 *            the user name of the session
85
	 * @param groupNames
86
	 *            the group names for the session
87
	 * @param password
88
	 *            the password for the session
89
	 */
90 4080 daigle
	public static void registerSession(String sessionId, String userName,
91 4780 daigle
			String[] groupNames, String password) throws ServiceException {
92
		if (sessionId == null) {
93
			throw new ServiceException("Cannot register a null session id");
94
		}
95
		logMetacat.debug("Registering session id: " + sessionId);
96 4080 daigle
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
97
				password);
98
		sessionHash.put(sessionId, sessionData);
99
	}
100
101 4297 daigle
	/**
102
	 * Register a session in the session hash table.
103
	 *
104
	 * @param sessionData
105
	 *            the session data object to add to the session hash
106
	 */
107 4780 daigle
	public static void registerSession(SessionData sessionData) throws ServiceException {
108
		if (sessionData == null) {
109
			throw new ServiceException("Cannot register null session data");
110
		}
111
		logMetacat.debug("Registering session date with id: " + sessionData.getId());
112 4080 daigle
		sessionHash.put(sessionData.getId(), sessionData);
113
	}
114
115 4297 daigle
	/**
116
	 * Unregister a session from the session hash table.
117
	 *
118
	 * @param sessionId
119
	 *            the id of the session to remove.
120
	 */
121 4080 daigle
	public static void unRegisterSession(String sessionId) {
122 4780 daigle
		if (sessionId != null) {
123
			logMetacat.error("trying to unregister a session with null id");
124
			sessionHash.remove(sessionId);
125
		}
126 4080 daigle
	}
127
128 4297 daigle
	/**
129
	 * Check if a session is registered in the session hash table.
130
	 * TODO MCD need to time sessions out
131
	 *
132
	 * @param sessionId
133
	 *            the id of the session to look for.
134
	 */
135 4080 daigle
	public static boolean isSessionRegistered(String sessionId) {
136 4780 daigle
		if (sessionId == null) {
137
			logMetacat.error("trying to check if a session with null id is registered");
138
			return false;
139
		}
140 4080 daigle
		return sessionHash.containsKey(sessionId);
141
	}
142
143 4297 daigle
	/**
144
	 * Get a registered session from the session hash table.
145
	 * TODO MCD need to time sessions out
146
	 *
147
	 * @param sessionId
148
	 *            the id of the session to retrieve.
149
	 */
150 4080 daigle
	public static SessionData getRegisteredSession(String sessionId) {
151 4780 daigle
		if (sessionId == null) {
152
			logMetacat.error("trying to get a session with null id");
153
			return null;
154
		}
155 4080 daigle
		return sessionHash.get(sessionId);
156
	}
157
158 4297 daigle
	/**
159
	 * Get the public session from the session hash table.
160
	 */
161
	public static SessionData getPublicSession() {
162
		return sessionHash.get(PUBLIC_SESSION_ID);
163 4080 daigle
	}
164
165 4297 daigle
	/**
166
	 * Keep a session active by updating its last accessed time.
167
	 *
168
	 * @param sessionId
169
	 *            the id of the session to update.
170
	 */
171 4080 daigle
	public static synchronized void touchSession(String sessionId) {
172 4780 daigle
		if (sessionId == null) {
173
			logMetacat.error("trying to touch a session with null id");
174
		} else if (isSessionRegistered(sessionId)) {
175 4080 daigle
			SessionData sessionData = getRegisteredSession(sessionId);
176
			sessionData.setLastAccessedTime();
177
		}
178
	}
179
180
}