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 javax.servlet.http.Cookie;
32
import javax.servlet.http.HttpServletRequest;
33
34
import org.apache.log4j.Logger;
35
36
import edu.ucsb.nceas.metacat.util.SessionData;
37
38
public class SessionService implements ServiceInterface {
39
40
	private static SessionService sessionService = null;
41
42
	private static Logger logMetacat = Logger.getLogger(SessionService.class);
43
	private static Hashtable<String, SessionData> sessionHash = null;
44
45 4297 daigle
	private static final String PUBLIC_SESSION_ID = "0";
46 4080 daigle
47
	/**
48
	 * private constructor since this is a singleton
49
	 */
50
	private SessionService() {
51
		sessionHash = new Hashtable<String, SessionData>();
52
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
53
	}
54
55
	/**
56
	 * Get the single instance of AuthAdmin.
57
	 *
58
	 * @return the single instance of AuthAdmin
59
	 */
60
	public static SessionService getInstance() {
61
		if (sessionService == null) {
62
			sessionService = new SessionService();
63
		}
64
		return sessionService;
65
	}
66 4297 daigle
67
	/**
68
	 * Register a session in the session hash table.  This uses
69
	 * the parameters passed in to create a SessionData object.
70
	 *
71
	 * @param sessionId
72
	 *            the ID of the session to register
73
	 * @param userName
74
	 *            the user name of the session
75
	 * @param groupNames
76
	 *            the group names for the session
77
	 * @param password
78
	 *            the password for the session
79
	 */
80 4080 daigle
	public static void registerSession(String sessionId, String userName,
81
			String[] groupNames, String password) {
82
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
83
				password);
84
		sessionHash.put(sessionId, sessionData);
85
	}
86
87 4297 daigle
	/**
88
	 * Register a session in the session hash table.
89
	 *
90
	 * @param sessionData
91
	 *            the session data object to add to the session hash
92
	 */
93 4080 daigle
	public static void registerSession(SessionData sessionData) {
94
		sessionHash.put(sessionData.getId(), sessionData);
95
	}
96
97 4297 daigle
	/**
98
	 * Unregister a session from the session hash table.
99
	 *
100
	 * @param sessionId
101
	 *            the id of the session to remove.
102
	 */
103 4080 daigle
	public static void unRegisterSession(String sessionId) {
104
		sessionHash.remove(sessionId);
105
	}
106
107 4297 daigle
	/**
108
	 * Check if a session is registered in the session hash table.
109
	 * TODO MCD need to time sessions out
110
	 *
111
	 * @param sessionId
112
	 *            the id of the session to look for.
113
	 */
114 4080 daigle
	public static boolean isSessionRegistered(String sessionId) {
115
		return sessionHash.containsKey(sessionId);
116
	}
117
118 4297 daigle
	/**
119
	 * Get a registered session from the session hash table.
120
	 * TODO MCD need to time sessions out
121
	 *
122
	 * @param sessionId
123
	 *            the id of the session to retrieve.
124
	 */
125 4080 daigle
	public static SessionData getRegisteredSession(String sessionId) {
126
		return sessionHash.get(sessionId);
127
	}
128
129 4297 daigle
	/**
130
	 * Get the public session from the session hash table.
131
	 */
132
	public static SessionData getPublicSession() {
133
		return sessionHash.get(PUBLIC_SESSION_ID);
134 4080 daigle
	}
135
136 4297 daigle
	/**
137
	 * Keep a session active by updating its last accessed time.
138
	 *
139
	 * @param sessionId
140
	 *            the id of the session to update.
141
	 */
142 4080 daigle
	public static synchronized void touchSession(String sessionId) {
143
		if (isSessionRegistered(sessionId)) {
144
			SessionData sessionData = getRegisteredSession(sessionId);
145
			sessionData.setLastAccessedTime();
146
		}
147
	}
148
149
}