Project

General

Profile

1
/**
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: daigle $'
9
 *     '$Date: 2009-08-04 14:32:58 -0700 (Tue, 04 Aug 2009) $'
10
 * '$Revision: 5015 $'
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.shared.BaseService;
34
import edu.ucsb.nceas.metacat.shared.ServiceException;
35
import edu.ucsb.nceas.metacat.util.SessionData;
36

    
37
public class SessionService extends BaseService {
38
	
39
	private static SessionService sessionService = null;
40
	
41
	private static Logger logMetacat = Logger.getLogger(SessionService.class);
42
	private static Hashtable<String, SessionData> sessionHash = null;
43
	
44
	private static final String PUBLIC_SESSION_ID = "0";
45

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

    
184
}
(3-3/6)