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