001: // ========================================================================
002: // $Id: LocalState.java,v 1.4 2004/05/09 20:30:47 gregwilkins Exp $
003: // Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.j2ee.session;
017:
018: //----------------------------------------
019:
020: import java.util.Collections;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.jboss.logging.Logger;
026:
027: //----------------------------------------
028:
029: /**
030: * Hold the state of an HttpSession
031: *
032: * @author <a href="mailto:jules@mortbay.com">Jules Gosnell</a>
033: * @version 1.0
034: */
035: public class LocalState implements State, java.io.Serializable {
036: protected static final Logger _log = Logger
037: .getLogger(LocalState.class);
038:
039: protected String _id;
040:
041: protected int _maxInactiveInterval;
042:
043: protected int _actualMaxInactiveInterval;
044:
045: protected long _creationTime;
046:
047: protected long _lastAccessedTime;
048:
049: protected Map _attributes; // allocated lazily
050:
051: public LocalState(String id, long creationTime,
052: int maxInactiveInterval, int actualMaxInactiveInterval) {
053: _id = id;
054: _creationTime = creationTime;
055: _lastAccessedTime = _creationTime;
056: _maxInactiveInterval = maxInactiveInterval;
057: _actualMaxInactiveInterval = actualMaxInactiveInterval;
058: }
059:
060: public LocalState(String id, int maxInactiveInterval,
061: int actualMaxInactiveInterval) {
062: _id = id;
063: _creationTime = System.currentTimeMillis();
064: _lastAccessedTime = _creationTime;
065: _maxInactiveInterval = maxInactiveInterval;
066: _actualMaxInactiveInterval = actualMaxInactiveInterval;
067: }
068:
069: protected LocalState() {
070: // for deserialisation only
071: }
072:
073: public String getId() {
074: return _id;
075: }
076:
077: public long getCreationTime() {
078: return _creationTime;
079: }
080:
081: public int getActualMaxInactiveInterval() {
082: return _actualMaxInactiveInterval;
083: }
084:
085: public long getLastAccessedTime() {
086: return _lastAccessedTime;
087: }
088:
089: public void setLastAccessedTime(long time) {
090: _lastAccessedTime = time;
091: }
092:
093: public int getMaxInactiveInterval() {
094: return _maxInactiveInterval;
095: }
096:
097: public void setMaxInactiveInterval(int interval) {
098: _log.info("_maxInactiveInterval=" + interval);
099: _maxInactiveInterval = interval;
100: }
101:
102: // allocate attribute map lazily. This is more complex, but JSPs
103: // seem to force allocation of sessions and then never put anything
104: // in them! - so it is a worthwhile saving in speed and footprint...
105:
106: protected static Map _emptyMap = Collections.EMPTY_MAP;
107:
108: protected static Enumeration _emptyEnumeration = Collections
109: .enumeration(Collections.EMPTY_LIST);
110:
111: protected static String[] _emptyStringArray = new String[0]; // could
112:
113: // this be
114: // changed
115: // by user ?
116:
117: protected void ensureAttributes() {
118: if (_attributes == null)
119: _attributes = new HashMap();
120: }
121:
122: public Object getAttribute(String name) {
123: // _log.info("getAttribute("+name+")");
124: return _attributes == null ? null : _attributes.get(name);
125: }
126:
127: public Map getAttributes() {
128: return (_attributes == null || _attributes.size() == 0) ? _emptyMap
129: : Collections.unmodifiableMap(_attributes);
130: }
131:
132: public Enumeration getAttributeNameEnumeration() {
133: return (_attributes == null || _attributes.size() == 0) ? _emptyEnumeration
134: : Collections.enumeration(_attributes.keySet());
135: }
136:
137: public String[] getAttributeNameStringArray() {
138: return (_attributes == null || _attributes.size() == 0) ? _emptyStringArray
139: : (String[]) _attributes.keySet().toArray(
140: new String[_attributes.size()]);
141: }
142:
143: public Object setAttribute(String name, Object value,
144: boolean returnValue) {
145: // we can be sure that name is non-null, because this will have
146: // been checked in our adaptor...
147:
148: // _log.info("setAttribute("+name+", "+value+", "+returnValue+")");
149:
150: ensureAttributes();
151: Object tmp = _attributes.put(name, value);
152: return returnValue ? tmp : null;
153: }
154:
155: public void setAttributes(Map attributes) {
156: if (_attributes != null)
157: _attributes.clear();
158:
159: if (attributes.size() > 0) {
160: ensureAttributes();
161: _attributes.putAll(attributes);
162: }
163: }
164:
165: public Object removeAttribute(String name, boolean returnValue) {
166: if (_attributes == null)
167: return null;
168: else {
169: Object tmp = _attributes.remove(name);
170: return returnValue ? tmp : null;
171: }
172: }
173:
174: protected long remainingTime() {
175: long maxInactiveInterval = _maxInactiveInterval < 1 ? _actualMaxInactiveInterval
176: : _maxInactiveInterval;
177: return (_lastAccessedTime + (maxInactiveInterval * 1000L))
178: - System.currentTimeMillis();
179: }
180:
181: public boolean isValid(int extraTime) {
182: long remainingTime = remainingTime();
183: long etime = (extraTime * 1000L);
184: boolean valid = remainingTime + etime > 0;
185: return valid;
186: }
187:
188: public boolean isValid() {
189: return isValid(0);
190: }
191: }
|