001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.providers.jsp;
006:
007: import java.util.Hashtable;
008: import java.util.Enumeration;
009: import java.util.Vector;
010:
011: import javax.servlet.ServletContext;
012: import javax.servlet.http.HttpSession;
013: import javax.servlet.http.HttpSessionBindingListener;
014: import javax.servlet.http.HttpSessionBindingEvent;
015:
016: /*
017: * The JspSession is an implementation of the HttpSession interface
018: * that is used by the JSP servlet for maintaining session state. One
019: * JspSession object is created for each JSPProvider object, allowing
020: * the JSPs in a channel to share session information. Most of this
021: * code was copied from the Tomcat ApplicationSession class.
022: **/
023: class JspSession implements HttpSession {
024: private Hashtable values = new Hashtable();
025: private ServletContext sContext = null;
026: private String id;
027: private long creationTime = System.currentTimeMillis();;
028: private long this AccessTime = creationTime;
029: private long lastAccessed = creationTime;
030: private int inactiveInterval = -1;
031: private boolean valid = true;
032:
033: JspSession() {
034: id = "" + hashCode();
035: }
036:
037: void setServletContext(ServletContext sc) {
038: sContext = sc;
039: }
040:
041: /**
042: * Called by context when request comes in so that accesses and
043: * inactivities can be dealt with accordingly.
044: */
045:
046: void accessed() {
047: // set last accessed to thisAccessTime as it will be left over
048: // from the previous access
049: lastAccessed = this AccessTime;
050: this AccessTime = System.currentTimeMillis();
051:
052: validate();
053: }
054:
055: void validate() {
056: // if we have an inactive interval, check to see if we've exceeded it
057: if (inactiveInterval != -1) {
058: int this Interval = (int) (System.currentTimeMillis() - lastAccessed) / 1000;
059:
060: if (this Interval > inactiveInterval) {
061: invalidate();
062: }
063: }
064: }
065:
066: // HTTP SESSION IMPLEMENTATION METHODS
067:
068: public String getId() {
069: if (valid) {
070: return id;
071: } else {
072: throw new IllegalStateException("invalid session");
073: }
074: }
075:
076: public long getCreationTime() {
077: if (valid) {
078: return creationTime;
079: } else {
080: throw new IllegalStateException("invalid session");
081: }
082: }
083:
084: public long getLastAccessedTime() {
085: if (valid) {
086: return lastAccessed;
087: } else {
088: throw new IllegalStateException("invalid session");
089: }
090: }
091:
092: public void invalidate() {
093: // remove everything in the session
094:
095: Enumeration enum = values.keys();
096: while (enum.hasMoreElements()) {
097: String name = (String)enum.nextElement();
098: removeValue(name);
099: }
100:
101: valid = false;
102: }
103:
104: public boolean isNew() {
105: if (!valid) {
106: throw new IllegalStateException("invalid session");
107: }
108:
109: if (this AccessTime == creationTime) {
110: return true;
111: } else {
112: return false;
113: }
114: }
115:
116: /**
117: * @deprecated
118: */
119:
120: public void putValue(String name, Object value) {
121: setAttribute(name, value);
122: }
123:
124: public void setAttribute(String name, Object value) {
125: if (!valid) {
126: throw new IllegalStateException("invalid session");
127: }
128:
129: if (name == null) {
130: throw new IllegalArgumentException("null name");
131: }
132:
133: removeValue(name); // remove any existing binding
134:
135: if (value != null
136: && value instanceof HttpSessionBindingListener) {
137: HttpSessionBindingEvent e = new HttpSessionBindingEvent(
138: this , name);
139:
140: ((HttpSessionBindingListener) value).valueBound(e);
141: }
142:
143: values.put(name, value);
144: }
145:
146: /**
147: * @deprecated
148: */
149: public Object getValue(String name) {
150: return getAttribute(name);
151: }
152:
153: public Object getAttribute(String name) {
154: if (!valid) {
155: throw new IllegalStateException("invalid session");
156: }
157:
158: if (name == null) {
159: throw new IllegalArgumentException("null name");
160: }
161:
162: return values.get(name);
163: }
164:
165: /**
166: * @deprecated
167: */
168: public String[] getValueNames() {
169: Enumeration e = getAttributeNames();
170: Vector names = new Vector();
171:
172: while (e.hasMoreElements()) {
173: names.addElement(e.nextElement());
174: }
175:
176: String[] valueNames = new String[names.size()];
177:
178: names.copyInto(valueNames);
179:
180: return valueNames;
181:
182: }
183:
184: public Enumeration getAttributeNames() {
185: if (!valid) {
186: throw new IllegalStateException("invalid session");
187: }
188:
189: Hashtable valuesClone = (Hashtable) values.clone();
190:
191: return (Enumeration) valuesClone.keys();
192: }
193:
194: /**
195: * @deprecated
196: */
197:
198: public void removeValue(String name) {
199: removeAttribute(name);
200: }
201:
202: public void removeAttribute(String name) {
203: if (!valid) {
204: throw new IllegalStateException("invalid session");
205: }
206:
207: if (name == null) {
208: throw new IllegalArgumentException("null name");
209: }
210:
211: Object o = values.get(name);
212:
213: if (o instanceof HttpSessionBindingListener) {
214: HttpSessionBindingEvent e = new HttpSessionBindingEvent(
215: this , name);
216:
217: ((HttpSessionBindingListener) o).valueUnbound(e);
218: }
219:
220: values.remove(name);
221: }
222:
223: public void setMaxInactiveInterval(int interval) {
224: if (!valid) {
225: throw new IllegalStateException("invalid session");
226: }
227:
228: inactiveInterval = interval;
229: }
230:
231: public int getMaxInactiveInterval() {
232: if (!valid) {
233: throw new IllegalStateException("invalid session");
234: }
235:
236: return inactiveInterval;
237: }
238:
239: public ServletContext getServletContext() {
240: return sContext;
241: }
242:
243: /**
244: *
245: * @deprecated
246: */
247: public javax.servlet.http.HttpSessionContext getSessionContext() {
248: return new javax.servlet.http.HttpSessionContext() {
249: /**
250: *
251: * @deprecated
252: */
253:
254: public HttpSession getSession(String sessionId) {
255: return null;
256: }
257:
258: /**
259: *
260: * @deprecated
261: */
262:
263: public Enumeration getIds() {
264: // cheap hack to get an empty enum
265: Vector v = new Vector();
266:
267: return v.elements();
268: }
269: };
270: }
271:
272: }
|