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-01-26 16:38:42 -0800 (Mon, 26 Jan 2009) $'
10
 * '$Revision: 4780 $'
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
	protected void doRefresh() throws ServiceException {
70
		return;
71
	}
72
	
73
	/**
74
	 * Register a session in the session hash table.  This uses
75
	 * the parameters passed in to create a SessionData object.
76
	 * 
77
	 * @param sessionId
78
	 *            the ID of the session to register
79
	 * @param userName
80
	 *            the user name of the session
81
	 * @param groupNames
82
	 *            the group names for the session
83
	 * @param password
84
	 *            the password for the session
85
	 */
86
	public static void registerSession(String sessionId, String userName,
87
			String[] groupNames, String password) throws ServiceException {
88
		if (sessionId == null) {
89
			throw new ServiceException("Cannot register a null session id");
90
		}
91
		logMetacat.debug("Registering session id: " + sessionId);
92
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
93
				password);
94
		sessionHash.put(sessionId, sessionData);
95
	}
96
	
97
	/**
98
	 * Register a session in the session hash table.
99
	 * 
100
	 * @param sessionData
101
	 *            the session data object to add to the session hash
102
	 */
103
	public static void registerSession(SessionData sessionData) throws ServiceException {
104
		if (sessionData == null) {
105
			throw new ServiceException("Cannot register null session data");
106
		}
107
		logMetacat.debug("Registering session date with id: " + sessionData.getId());
108
		sessionHash.put(sessionData.getId(), sessionData);
109
	}
110
	
111
	/**
112
	 * Unregister a session from the session hash table.
113
	 * 
114
	 * @param sessionId
115
	 *            the id of the session to remove.
116
	 */
117
	public static void unRegisterSession(String sessionId) {
118
		if (sessionId != null) {
119
			logMetacat.error("trying to unregister a session with null id");
120
			sessionHash.remove(sessionId);
121
		}
122
	}
123
	
124
	/**
125
	 * Check if a session is registered in the session hash table. 
126
	 * TODO MCD need to time sessions out
127
	 * 
128
	 * @param sessionId
129
	 *            the id of the session to look for.
130
	 */
131
	public static boolean isSessionRegistered(String sessionId) {
132
		if (sessionId == null) {
133
			logMetacat.error("trying to check if a session with null id is registered");
134
			return false;
135
		}
136
		return sessionHash.containsKey(sessionId);
137
	}
138
	
139
	/**
140
	 * Get a registered session from the session hash table. 
141
	 * TODO MCD need to time sessions out
142
	 * 
143
	 * @param sessionId
144
	 *            the id of the session to retrieve.
145
	 */
146
	public static SessionData getRegisteredSession(String sessionId) {
147
		if (sessionId == null) {
148
			logMetacat.error("trying to get a session with null id");
149
			return null;
150
		}
151
		return sessionHash.get(sessionId);
152
	}
153
	
154
	/**
155
	 * Get the public session from the session hash table. 
156
	 */
157
	public static SessionData getPublicSession() {
158
		return sessionHash.get(PUBLIC_SESSION_ID);
159
	}
160
	
161
	/**
162
	 * Keep a session active by updating its last accessed time. 
163
	 * 
164
	 * @param sessionId
165
	 *            the id of the session to update.
166
	 */
167
	public static synchronized void touchSession(String sessionId) {
168
		if (sessionId == null) {
169
			logMetacat.error("trying to touch a session with null id");
170
		} else if (isSessionRegistered(sessionId)) {
171
			SessionData sessionData = getRegisteredSession(sessionId);
172
			sessionData.setLastAccessedTime();
173
		}
174
	}
175

    
176
}
(6-6/9)