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-03-24 10:17:11 -0700 (Tue, 24 Mar 2009) $'
10
 * '$Revision: 4856 $'
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
	public static final String PUBLIC_USER_NAME = "public";
44
	
45
	private static final String REPLICATION_SESSION_ID = "1";
46
	public static final String REPLICATION_USER_NAME = "replication";
47

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

    
188
}
(6-6/9)