Project

General

Profile

Revision 5041

Added by daigle almost 12 years ago

Add session Validation action and session timeout functionality.

View differences:

lib/workflowscheduler/workflowscheduler.properties
134 134
ldap.templates.mainServerFailure=ldapMainServerFailure.tmpl
135 135
ldap.templates.searchResults=searchResults.tmpl
136 136

  
137
############### Session Values ###############
138
session.timeoutMinutes=360
139

  
137 140
######## XML / EML  #########################################
138 141

  
139 142
xml.saxparser=org.apache.xerces.parsers.SAXParser
lib/metacat.properties
157 157
ldap.templates.mainServerFailure=ldapMainServerFailure.tmpl
158 158
ldap.templates.searchResults=searchResults.tmpl
159 159

  
160
############### Session Values ###############
161
session.timeoutMinutes=360
162

  
160 163
############### Organization Values ###############
161 164
organization.configured.NCEAS=false
162 165
organization.name.NCEAS=National Center for Ecological Analysis and Synthesis
......
403 406
test.printdebug=true
404 407
test.metacatUrl=http://indus.msi.ucsb.edu/knb/metacat
405 408
test.contextUrl=http://indus.msi.ucsb.edu/knb
409
test.workflowSchedulerUrl=http://indus.msi.ucsb.edu/workflowscheduler/scheduler
406 410
test.metacatDeployDir=/usr/local/tomcat/webapps/knb
407 411
test.mcUser=uid=kepler,o=unaffiliated,dc=ecoinformatics,dc=org
408 412
test.mcPassword=kepler
src/edu/ucsb/nceas/workflowscheduler/WorkflowSchedulerServlet.java
47 47
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
48 48
import edu.ucsb.nceas.metacat.properties.PropertyService;
49 49
import edu.ucsb.nceas.metacat.scheduler.SchedulerService;
50
import edu.ucsb.nceas.metacat.service.ServiceService;
51 50
import edu.ucsb.nceas.metacat.service.SessionService;
52 51
import edu.ucsb.nceas.metacat.shared.BaseException;
53 52
import edu.ucsb.nceas.metacat.shared.ServiceException;
......
148 147
            
149 148
			// initialize DBConnection pool
150 149
			DBConnectionPool connPool = DBConnectionPool.getInstance();
151
			logMetacat.debug("DBConnection pool initialized: " + connPool.toString());
150
			logMetacat.debug("WorkflowSchedulerServlet.init - DBConnection pool initialized: " + connPool.toString());
152 151
            
153 152
    	} catch (ServiceException se) {
154 153
        	String errorMessage = 
155
        		"Service problem while intializing WorkflowScheduler Servlet: " + se.getMessage();
154
        		"WorkflowSchedulerServlet.init - Service problem while intializing WorkflowScheduler Servlet: " + se.getMessage();
156 155
            logMetacat.error(errorMessage);
157 156
            throw new ServletException(errorMessage);
158 157
        } catch (SQLException e) {
159
			String errorMessage = "SQL problem while intializing MetaCat Servlet: "
158
			String errorMessage = "WorkflowSchedulerServlet.init - SQL problem while intializing WorkflowScheduler Servlet: "
160 159
					+ e.getMessage();
161 160
			logMetacat.error(errorMessage);
162 161
			throw new ServletException(errorMessage);
......
169 168
    public void destroy() {
170 169
    	Logger logMetacat = Logger.getLogger(WorkflowSchedulerServlet.class);
171 170
    	
172
    	ServiceService.stopAllServices();
171
    	try {
172
    		SchedulerService.getInstance().stop();
173
    	} catch (ServiceException se) {
174
    		logMetacat.warn("WorkflowSchedulerServlet.destroy - Could not stop scheduler service: " + se.getMessage());
175
    	}
173 176
    	
174 177
        // Close all db connection
175
        logMetacat.warn("Destroying WorkflowSchedulerServlet");
178
        logMetacat.warn("WorkflowSchedulerServlet.destroy - Destroying WorkflowSchedulerServlet");
176 179
        timer.cancel();
177 180
//        IndexingQueue.getInstance().setMetacatRunning(false);
178 181
        DBConnectionPool.release();
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
806 806
				out.close();
807 807

  
808 808
				// handle shrink DBConnection request
809
			} else if (action.equals("validatesession")) {
810
				PrintWriter out = response.getWriter();
811
				String idToValidate = null;
812
				String idsToValidate[] = params.get("sessionid");
813
				if (idsToValidate != null) {
814
					idToValidate = idsToValidate[0];
815
				}
816
				SessionService.validateSession(out, response, idToValidate);
817
				out.close();
818

  
819
				// handle shrink DBConnection request
809 820
			} else if (action.equals("shrink")) {
810 821
				PrintWriter out = response.getWriter();
811 822
				boolean success = false;
......
1011 1022
				 */
1012 1023
			} else if (action.equals("refreshServices")) {
1013 1024
				// TODO MCD this interface is for testing. It should go through
1014
				// a
1015
				// ServiceService class and only work for an admin user. Move to
1016
				// the
1017
				// MetacatAdminServlet
1025
				// a ServiceService class and only work for an admin user. Move 
1026
				// to the MetacatAdminServlet
1018 1027
				ServiceService.refreshService("XMLSchemaService");
1019 1028
				return;
1020 1029
			} else if (action.equals("scheduleWorkflow")) {
......
1077 1086
				out.close();
1078 1087
			}
1079 1088

  
1080
			// util.closeConnections();
1081
			// Close the stream to the client
1082
			// out.close();
1083

  
1084 1089
			// Schedule the sitemap generator to run periodically
1085 1090
			scheduleSitemapGeneration(request);
1086 1091

  
src/edu/ucsb/nceas/metacat/service/SessionService.java
26 26

  
27 27
package edu.ucsb.nceas.metacat.service;
28 28

  
29
import java.io.PrintWriter;
30
import java.util.Calendar;
29 31
import java.util.Hashtable;
30 32

  
33
import javax.servlet.http.HttpServletResponse;
34

  
31 35
import org.apache.log4j.Logger;
32 36

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

  
37 43
public class SessionService extends BaseService {
38 44
	
39 45
	private static SessionService sessionService = null;
46
	private static int sessionTimeoutMinutes;
40 47
	
41 48
	private static Logger logMetacat = Logger.getLogger(SessionService.class);
42 49
	private static Hashtable<String, SessionData> sessionHash = null;
......
49 56
	private SessionService() throws ServiceException {		
50 57
		_serviceName = "SessionService";
51 58
		
52
		sessionHash = new Hashtable<String, SessionData>();
53
		logMetacat.debug("Registering public session id: " + PUBLIC_SESSION_ID);
54
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
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
		}
55 75
	}
56 76
	
57 77
	/**
......
94 114
	public static void registerSession(String sessionId, String userName,
95 115
			String[] groupNames, String password) throws ServiceException {
96 116
		if (sessionId == null) {
97
			throw new ServiceException("Cannot register a null session id");
117
			throw new ServiceException("SessionService.registerSession - " + 
118
					"Cannot register a null session id");
98 119
		}
99
		logMetacat.debug("Registering session id: " + sessionId);
120
		logMetacat.debug("SessionService.registerSession - Registering session id: " + sessionId);
100 121
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
101 122
				password);
102 123
		sessionHash.put(sessionId, sessionData);
......
110 131
	 */
111 132
	public static void registerSession(SessionData sessionData) throws ServiceException {
112 133
		if (sessionData == null) {
113
			throw new ServiceException("Cannot register null session data");
134
			throw new ServiceException("SessionService.registerSession - " + 
135
					"Cannot register null session data");
114 136
		}
115
		logMetacat.debug("Registering session date with id: " + sessionData.getId());
137
		logMetacat.debug("SessionService.registerSession - Registering session " + 
138
				"data with id: " + sessionData.getId());
116 139
		sessionHash.put(sessionData.getId(), sessionData);
117 140
	}
118 141
	
......
124 147
	 */
125 148
	public static void unRegisterSession(String sessionId) {
126 149
		if (sessionId != null) {
127
			logMetacat.error("trying to unregister a session with null id");
150
			logMetacat.error("SessionService.unRegisterSession - trying to " + 
151
					"unregister a session with null id");
128 152
			sessionHash.remove(sessionId);
129 153
		}
130 154
	}
131 155
	
132 156
	/**
133 157
	 * Check if a session is registered in the session hash table. 
134
	 * TODO MCD need to time sessions out
135 158
	 * 
136 159
	 * @param sessionId
137 160
	 *            the id of the session to look for.
138 161
	 */
139
	public static boolean isSessionRegistered(String sessionId) {
162
	public static boolean isSessionRegistered(String sessionId) {		
140 163
		if (sessionId == null) {
141
			logMetacat.error("trying to check if a session with null id is registered");
164
			logMetacat.error("SessionService.isSessionRegistered - trying to check if a " + 
165
					"session with null id is registered");
142 166
			return false;
143 167
		}
168
		
169
		checkTimeout(sessionId);
170
		
144 171
		return sessionHash.containsKey(sessionId);
145 172
	}
146 173
	
147 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
	/**
148 199
	 * Get a registered session from the session hash table. 
149 200
	 * TODO MCD need to time sessions out
150 201
	 * 
......
153 204
	 */
154 205
	public static SessionData getRegisteredSession(String sessionId) {
155 206
		if (sessionId == null) {
156
			logMetacat.error("trying to get a session with null id");
207
			logMetacat.error("SessionService.getRegisteredSession - trying to get a session with null id");
157 208
			return null;
158 209
		}
210
		checkTimeout(sessionId);
211
		
159 212
		return sessionHash.get(sessionId);
160 213
	}
161 214
	
......
174 227
	 */
175 228
	public static synchronized void touchSession(String sessionId) {
176 229
		if (sessionId == null) {
177
			logMetacat.error("trying to touch a session with null id");
230
			logMetacat.error("SessionService.touchSession - trying to touch a session with null id");
178 231
		} else if (isSessionRegistered(sessionId)) {
179 232
			SessionData sessionData = getRegisteredSession(sessionId);
180 233
			sessionData.setLastAccessedTime();
181 234
		}
182 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
	}
183 250

  
184 251
}

Also available in: Unified diff