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-07-11 07:16:20 -0700 (Sat, 11 Jul 2009) $'
10
 * '$Revision: 4981 $'
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
public class SessionService extends BaseService {
36
	
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
	private static final String PUBLIC_SESSION_ID = "0";
43

    
44
	/**
45
	 * private constructor since this is a singleton
46
	 */
47
	private SessionService() throws ServiceException {
48
		sessionHash = new Hashtable<String, SessionData>();
49
		logMetacat.debug("Registering public session id: " + PUBLIC_SESSION_ID);
50
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
51
	}
52
	
53
	/**
54
	 * Get the single instance of SessionService.
55
	 * 
56
	 * @return the single instance of SessionService
57
	 */
58
	public static SessionService getInstance() throws ServiceException {
59
		if (sessionService == null) {
60
			sessionService = new SessionService();
61
		}
62
		return sessionService;
63
	}
64
	
65
	public boolean refreshable() {
66
		return false;
67
	}
68
	
69
	public void doRefresh() throws ServiceException {
70
		return;
71
	}
72
	
73
	public void stop() throws ServiceException {
74
		return;
75
	}
76
	
77
	/**
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
	public static void registerSession(String sessionId, String userName,
91
			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
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
97
				password);
98
		sessionHash.put(sessionId, sessionData);
99
	}
100
	
101
	/**
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
	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
		sessionHash.put(sessionData.getId(), sessionData);
113
	}
114
	
115
	/**
116
	 * Unregister a session from the session hash table.
117
	 * 
118
	 * @param sessionId
119
	 *            the id of the session to remove.
120
	 */
121
	public static void unRegisterSession(String sessionId) {
122
		if (sessionId != null) {
123
			logMetacat.error("trying to unregister a session with null id");
124
			sessionHash.remove(sessionId);
125
		}
126
	}
127
	
128
	/**
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
	public static boolean isSessionRegistered(String sessionId) {
136
		if (sessionId == null) {
137
			logMetacat.error("trying to check if a session with null id is registered");
138
			return false;
139
		}
140
		return sessionHash.containsKey(sessionId);
141
	}
142
	
143
	/**
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
	public static SessionData getRegisteredSession(String sessionId) {
151
		if (sessionId == null) {
152
			logMetacat.error("trying to get a session with null id");
153
			return null;
154
		}
155
		return sessionHash.get(sessionId);
156
	}
157
	
158
	/**
159
	 * Get the public session from the session hash table. 
160
	 */
161
	public static SessionData getPublicSession() {
162
		return sessionHash.get(PUBLIC_SESSION_ID);
163
	}
164
	
165
	/**
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
	public static synchronized void touchSession(String sessionId) {
172
		if (sessionId == null) {
173
			logMetacat.error("trying to touch a session with null id");
174
		} else if (isSessionRegistered(sessionId)) {
175
			SessionData sessionData = getRegisteredSession(sessionId);
176
			sessionData.setLastAccessedTime();
177
		}
178
	}
179

    
180
}
(6-6/9)