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: 2008-10-10 17:12:49 -0700 (Fri, 10 Oct 2008) $'
10
 * '$Revision: 4442 $'
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() {
48
		sessionHash = new Hashtable<String, SessionData>();
49
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
50
	}
51
	
52
	/**
53
	 * Get the single instance of SessionService.
54
	 * 
55
	 * @return the single instance of SessionService
56
	 */
57
	public static SessionService getInstance() {
58
		if (sessionService == null) {
59
			sessionService = new SessionService();
60
		}
61
		return sessionService;
62
	}
63
	
64
	public boolean refreshable() {
65
		return false;
66
	}
67
	
68
	protected void doRefresh() {
69
		return;
70
	}
71
	
72
	/**
73
	 * Register a session in the session hash table.  This uses
74
	 * the parameters passed in to create a SessionData object.
75
	 * 
76
	 * @param sessionId
77
	 *            the ID of the session to register
78
	 * @param userName
79
	 *            the user name of the session
80
	 * @param groupNames
81
	 *            the group names for the session
82
	 * @param password
83
	 *            the password for the session
84
	 */
85
	public static void registerSession(String sessionId, String userName,
86
			String[] groupNames, String password) {
87
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
88
				password);
89
		sessionHash.put(sessionId, sessionData);
90
	}
91
	
92
	/**
93
	 * Register a session in the session hash table.
94
	 * 
95
	 * @param sessionData
96
	 *            the session data object to add to the session hash
97
	 */
98
	public static void registerSession(SessionData sessionData) {
99
		sessionHash.put(sessionData.getId(), sessionData);
100
	}
101
	
102
	/**
103
	 * Unregister a session from the session hash table.
104
	 * 
105
	 * @param sessionId
106
	 *            the id of the session to remove.
107
	 */
108
	public static void unRegisterSession(String sessionId) {
109
		sessionHash.remove(sessionId);
110
	}
111
	
112
	/**
113
	 * Check if a session is registered in the session hash table. 
114
	 * TODO MCD need to time sessions out
115
	 * 
116
	 * @param sessionId
117
	 *            the id of the session to look for.
118
	 */
119
	public static boolean isSessionRegistered(String sessionId) {
120
		return sessionHash.containsKey(sessionId);
121
	}
122
	
123
	/**
124
	 * Get a registered session from the session hash table. 
125
	 * TODO MCD need to time sessions out
126
	 * 
127
	 * @param sessionId
128
	 *            the id of the session to retrieve.
129
	 */
130
	public static SessionData getRegisteredSession(String sessionId) {
131
		return sessionHash.get(sessionId);
132
	}
133
	
134
	/**
135
	 * Get the public session from the session hash table. 
136
	 */
137
	public static SessionData getPublicSession() {
138
		return sessionHash.get(PUBLIC_SESSION_ID);
139
	}
140
	
141
	/**
142
	 * Keep a session active by updating its last accessed time. 
143
	 * 
144
	 * @param sessionId
145
	 *            the id of the session to update.
146
	 */
147
	public static synchronized void touchSession(String sessionId) {
148
		if (isSessionRegistered(sessionId)) {
149
			SessionData sessionData = getRegisteredSession(sessionId);
150
			sessionData.setLastAccessedTime();
151
		}
152
	}
153

    
154
}
(6-6/9)