01: package com.meterware.servletunit;
02:
03: /********************************************************************************************************************
04: * $Id: ServletUnitContext.java,v 1.9 2004/09/23 22:48:19 russgold Exp $
05: *
06: * Copyright (c) 2000-2004, Russell Gold
07: *
08: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
09: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
10: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
11: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
14: * of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20: * DEALINGS IN THE SOFTWARE.
21: *
22: *******************************************************************************************************************/
23: import javax.servlet.ServletContext;
24: import java.util.*;
25: import java.util.Hashtable;
26:
27: class ServletUnitContext {
28:
29: private SessionListenerDispatcher _listenerDispatcher;
30: private ServletContext _servletContext;
31:
32: ServletUnitContext(String contextPath,
33: ServletContext servletContext,
34: SessionListenerDispatcher dispatcher) {
35: _servletContext = servletContext;
36: _contextPath = (contextPath != null ? contextPath : "");
37: _listenerDispatcher = dispatcher;
38: }
39:
40: Set getSessionIDs() {
41: return _sessions.keySet();
42: }
43:
44: /**
45: * Returns an appropriate session for a request. If no cached session is
46: * @param sessionId
47: * @param session the session cached by previous requests. May be null.
48: * @param create
49: * @return
50: */
51: ServletUnitHttpSession getValidSession(String sessionId,
52: ServletUnitHttpSession session, boolean create) {
53: if (session == null && sessionId != null) {
54: session = getSession(sessionId);
55: }
56:
57: if (session != null && session.isInvalid()) {
58: session = null;
59: }
60:
61: if (session == null && create) {
62: session = newSession();
63: }
64: return session;
65: }
66:
67: /**
68: * Returns the session with the specified ID, if any.
69: **/
70: ServletUnitHttpSession getSession(String id) {
71: return (ServletUnitHttpSession) _sessions.get(id);
72: }
73:
74: /**
75: * Creates a new session with a unique ID.
76: **/
77: ServletUnitHttpSession newSession() {
78: ServletUnitHttpSession result = new ServletUnitHttpSession(
79: _servletContext, _listenerDispatcher);
80: _sessions.put(result.getId(), result);
81: _listenerDispatcher.sendSessionCreated(result);
82: return result;
83: }
84:
85: /**
86: * Returns the contextPath
87: */
88: String getContextPath() {
89: return _contextPath;
90: }
91:
92: //------------------------------- private members ---------------------------
93:
94: private Hashtable _sessions = new Hashtable();
95:
96: private String _contextPath = null;
97:
98: }
|