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
	private SessionService() {
48
		sessionHash = new Hashtable<String, SessionData>();
49
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
50
	}
51
52
	/**
53 4429 daigle
	 * Get the single instance of SessionService.
54 4080 daigle
	 *
55 4429 daigle
	 * @return the single instance of SessionService
56 4080 daigle
	 */
57
	public static SessionService getInstance() {
58
		if (sessionService == null) {
59
			sessionService = new SessionService();
60
		}
61
		return sessionService;
62
	}
63 4297 daigle
64 4442 daigle
	public boolean refreshable() {
65
		return false;
66
	}
67
68 4709 daigle
	protected void doRefresh() throws ServiceException {
69 4442 daigle
		return;
70
	}
71
72 4297 daigle
	/**
73
	 * Register a session in the session hash table.  This uses
74
	 * the parameters passed in to create a SessionData object.
75
	 *
76
	 * @param sessionId
77
	 *            the ID of the session to register
78
	 * @param userName
79
	 *            the user name of the session
80
	 * @param groupNames
81
	 *            the group names for the session
82
	 * @param password
83
	 *            the password for the session
84
	 */
85 4080 daigle
	public static void registerSession(String sessionId, String userName,
86
			String[] groupNames, String password) {
87
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
88
				password);
89
		sessionHash.put(sessionId, sessionData);
90
	}
91
92 4297 daigle
	/**
93
	 * Register a session in the session hash table.
94
	 *
95
	 * @param sessionData
96
	 *            the session data object to add to the session hash
97
	 */
98 4080 daigle
	public static void registerSession(SessionData sessionData) {
99
		sessionHash.put(sessionData.getId(), sessionData);
100
	}
101
102 4297 daigle
	/**
103
	 * Unregister a session from the session hash table.
104
	 *
105
	 * @param sessionId
106
	 *            the id of the session to remove.
107
	 */
108 4080 daigle
	public static void unRegisterSession(String sessionId) {
109
		sessionHash.remove(sessionId);
110
	}
111
112 4297 daigle
	/**
113
	 * Check if a session is registered in the session hash table.
114
	 * TODO MCD need to time sessions out
115
	 *
116
	 * @param sessionId
117
	 *            the id of the session to look for.
118
	 */
119 4080 daigle
	public static boolean isSessionRegistered(String sessionId) {
120
		return sessionHash.containsKey(sessionId);
121
	}
122
123 4297 daigle
	/**
124
	 * Get a registered session from the session hash table.
125
	 * TODO MCD need to time sessions out
126
	 *
127
	 * @param sessionId
128
	 *            the id of the session to retrieve.
129
	 */
130 4080 daigle
	public static SessionData getRegisteredSession(String sessionId) {
131
		return sessionHash.get(sessionId);
132
	}
133
134 4297 daigle
	/**
135
	 * Get the public session from the session hash table.
136
	 */
137
	public static SessionData getPublicSession() {
138
		return sessionHash.get(PUBLIC_SESSION_ID);
139 4080 daigle
	}
140
141 4297 daigle
	/**
142
	 * Keep a session active by updating its last accessed time.
143
	 *
144
	 * @param sessionId
145
	 *            the id of the session to update.
146
	 */
147 4080 daigle
	public static synchronized void touchSession(String sessionId) {
148 4778 daigle
		if (sessionId != null && isSessionRegistered(sessionId)) {
149 4080 daigle
			SessionData sessionData = getRegisteredSession(sessionId);
150
			sessionData.setLastAccessedTime();
151
		}
152
	}
153
154
}