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-26 14:20:43 -0700 (Wed, 26 Aug 2009) $'
10
 * '$Revision: 5041 $'
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.io.PrintWriter;
30
import java.util.Calendar;
31
import java.util.Hashtable;
32

    
33
import javax.servlet.http.HttpServletResponse;
34

    
35
import org.apache.log4j.Logger;
36

    
37
import edu.ucsb.nceas.metacat.properties.PropertyService;
38
import edu.ucsb.nceas.metacat.shared.BaseService;
39
import edu.ucsb.nceas.metacat.shared.ServiceException;
40
import edu.ucsb.nceas.metacat.util.SessionData;
41
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
42

    
43
public class SessionService extends BaseService {
44
	
45
	private static SessionService sessionService = null;
46
	private static int sessionTimeoutMinutes;
47
	
48
	private static Logger logMetacat = Logger.getLogger(SessionService.class);
49
	private static Hashtable<String, SessionData> sessionHash = null;
50
	
51
	private static final String PUBLIC_SESSION_ID = "0";
52

    
53
	/**
54
	 * private constructor since this is a singleton
55
	 */
56
	private SessionService() throws ServiceException {		
57
		_serviceName = "SessionService";
58
		
59
		String sessionTimeoutStr = null;
60
		try {
61
			sessionHash = new Hashtable<String, SessionData>();
62
			sessionTimeoutStr = PropertyService.getProperty("session.timeoutMinutes");
63
			sessionTimeoutMinutes = Integer.parseInt(sessionTimeoutStr);
64
		
65
			logMetacat.debug("SessionService() - Registering public session id: " + 
66
					PUBLIC_SESSION_ID);
67
			registerSession(PUBLIC_SESSION_ID, "public", null, null);
68
		} catch (PropertyNotFoundException pnfe) {
69
			throw new ServiceException("SessionService() - Error getting property: " + 
70
					pnfe.getMessage());
71
		} catch (NumberFormatException nfe) {
72
			throw new ServiceException("SessionService() - Error parsing session timeout minutes: " + 
73
					sessionTimeoutStr);
74
		}
75
	}
76
	
77
	/**
78
	 * Get the single instance of SessionService.
79
	 * 
80
	 * @return the single instance of SessionService
81
	 */
82
	public static SessionService getInstance() throws ServiceException {
83
		if (sessionService == null) {
84
			sessionService = new SessionService();
85
		}
86
		return sessionService;
87
	}
88
	
89
	public boolean refreshable() {
90
		return false;
91
	}
92
	
93
	public void doRefresh() throws ServiceException {
94
		return;
95
	}
96
	
97
	public void stop() throws ServiceException {
98
		return;
99
	}
100
	
101
	/**
102
	 * Register a session in the session hash table.  This uses
103
	 * the parameters passed in to create a SessionData object.
104
	 * 
105
	 * @param sessionId
106
	 *            the ID of the session to register
107
	 * @param userName
108
	 *            the user name of the session
109
	 * @param groupNames
110
	 *            the group names for the session
111
	 * @param password
112
	 *            the password for the session
113
	 */
114
	public static void registerSession(String sessionId, String userName,
115
			String[] groupNames, String password) throws ServiceException {
116
		if (sessionId == null) {
117
			throw new ServiceException("SessionService.registerSession - " + 
118
					"Cannot register a null session id");
119
		}
120
		logMetacat.debug("SessionService.registerSession - Registering session id: " + sessionId);
121
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
122
				password);
123
		sessionHash.put(sessionId, sessionData);
124
	}
125
	
126
	/**
127
	 * Register a session in the session hash table.
128
	 * 
129
	 * @param sessionData
130
	 *            the session data object to add to the session hash
131
	 */
132
	public static void registerSession(SessionData sessionData) throws ServiceException {
133
		if (sessionData == null) {
134
			throw new ServiceException("SessionService.registerSession - " + 
135
					"Cannot register null session data");
136
		}
137
		logMetacat.debug("SessionService.registerSession - Registering session " + 
138
				"data with id: " + sessionData.getId());
139
		sessionHash.put(sessionData.getId(), sessionData);
140
	}
141
	
142
	/**
143
	 * Unregister a session from the session hash table.
144
	 * 
145
	 * @param sessionId
146
	 *            the id of the session to remove.
147
	 */
148
	public static void unRegisterSession(String sessionId) {
149
		if (sessionId != null) {
150
			logMetacat.error("SessionService.unRegisterSession - trying to " + 
151
					"unregister a session with null id");
152
			sessionHash.remove(sessionId);
153
		}
154
	}
155
	
156
	/**
157
	 * Check if a session is registered in the session hash table. 
158
	 * 
159
	 * @param sessionId
160
	 *            the id of the session to look for.
161
	 */
162
	public static boolean isSessionRegistered(String sessionId) {		
163
		if (sessionId == null) {
164
			logMetacat.error("SessionService.isSessionRegistered - trying to check if a " + 
165
					"session with null id is registered");
166
			return false;
167
		}
168
		
169
		checkTimeout(sessionId);
170
		
171
		return sessionHash.containsKey(sessionId);
172
	}
173
	
174
	/**
175
	 * Check if a session is registered in the session hash table. Write results
176
	 * in XML format to output.
177
	 * 
178
	 * @param out
179
	 *            the output stream to write to.
180
	 * @param sessionId
181
	 *            the id of the session to look for.
182
	 */
183
	public static void validateSession(PrintWriter out, HttpServletResponse response, 
184
			String sessionId) {		
185
		
186
		response.setContentType("text/xml");
187
		out.println("<?xml version=\"1.0\"?>");
188
		out.write("<validateSession><status>");
189
		if (sessionId != null && isSessionRegistered(sessionId)) {
190
			out.write("valid");
191
		} else {
192
			out.write("invalid");
193
		}
194
		out.write("</status>");
195
		out.write("<sessionId>" + sessionId + "</sessionId></validateSession>");				
196
	}
197
	
198
	/**
199
	 * Get a registered session from the session hash table. 
200
	 * TODO MCD need to time sessions out
201
	 * 
202
	 * @param sessionId
203
	 *            the id of the session to retrieve.
204
	 */
205
	public static SessionData getRegisteredSession(String sessionId) {
206
		if (sessionId == null) {
207
			logMetacat.error("SessionService.getRegisteredSession - trying to get a session with null id");
208
			return null;
209
		}
210
		checkTimeout(sessionId);
211
		
212
		return sessionHash.get(sessionId);
213
	}
214
	
215
	/**
216
	 * Get the public session from the session hash table. 
217
	 */
218
	public static SessionData getPublicSession() {
219
		return sessionHash.get(PUBLIC_SESSION_ID);
220
	}
221
	
222
	/**
223
	 * Keep a session active by updating its last accessed time. 
224
	 * 
225
	 * @param sessionId
226
	 *            the id of the session to update.
227
	 */
228
	public static synchronized void touchSession(String sessionId) {
229
		if (sessionId == null) {
230
			logMetacat.error("SessionService.touchSession - trying to touch a session with null id");
231
		} else if (isSessionRegistered(sessionId)) {
232
			SessionData sessionData = getRegisteredSession(sessionId);
233
			sessionData.setLastAccessedTime();
234
		}
235
	}
236
	
237
	private static void checkTimeout (String sessionId) {
238
		SessionData sessionData = null;
239
		if ((sessionData = sessionHash.get(sessionId)) != null) {
240
			Calendar expireTime = Calendar.getInstance();
241
			Calendar lastAccessedTime = sessionData.getLastAccessedTime();
242
			expireTime.add(Calendar.MINUTE, 0 - sessionTimeoutMinutes);
243
			if(lastAccessedTime.compareTo(expireTime) < 0 ) {
244
				unRegisterSession(sessionId);
245
			}
246
		}
247
		
248
		
249
	}
250

    
251
}
(2-2/4)