KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > session > LocalState


1 // ========================================================================
2
// $Id: LocalState.java,v 1.4 2004/05/09 20:30:47 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.j2ee.session;
17
18 //----------------------------------------
19

20 import java.util.Collections JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.jfox.ioc.logger.Logger;
26
27 //----------------------------------------
28

29 /**
30  * Hold the state of an HttpSession
31  *
32  * @author <a HREF="mailto:jules@mortbay.com">Jules Gosnell</a>
33  * @version 1.0
34  */

35 public class
36   LocalState
37   implements State, java.io.Serializable JavaDoc
38 {
39   protected static final Logger _log=Logger.getLogger(LocalState.class);
40
41   protected String JavaDoc _id;
42   protected int _maxInactiveInterval;
43   protected int _actualMaxInactiveInterval;
44
45   protected long _creationTime;
46   protected long _lastAccessedTime;
47   protected Map JavaDoc _attributes; // allocated lazily
48

49
50   public
51     LocalState(String JavaDoc id, long creationTime, int maxInactiveInterval, int actualMaxInactiveInterval)
52     {
53       _id=id;
54       _creationTime=creationTime;
55       _lastAccessedTime=_creationTime;
56       _maxInactiveInterval=maxInactiveInterval;
57       _actualMaxInactiveInterval=actualMaxInactiveInterval;
58     }
59
60   public
61     LocalState(String JavaDoc id, int maxInactiveInterval, int actualMaxInactiveInterval)
62     {
63       _id=id;
64       _creationTime=System.currentTimeMillis();
65       _lastAccessedTime=_creationTime;
66       _maxInactiveInterval=maxInactiveInterval;
67       _actualMaxInactiveInterval=actualMaxInactiveInterval;
68     }
69
70   protected
71     LocalState()
72     {
73       // for deserialisation only
74
}
75
76   public String JavaDoc getId() {return _id;}
77   public long getCreationTime() {return _creationTime;}
78   public int getActualMaxInactiveInterval() {return _actualMaxInactiveInterval;}
79   public long getLastAccessedTime() {return _lastAccessedTime;}
80   public void setLastAccessedTime(long time) {_lastAccessedTime=time;}
81   public int getMaxInactiveInterval() {return _maxInactiveInterval;}
82   public void setMaxInactiveInterval(int interval) {_log.info("_maxInactiveInterval="+interval);_maxInactiveInterval=interval;}
83
84   // allocate attribute map lazily. This is more complex, but JSPs
85
// seem to force allocation of sessions and then never put anything
86
// in them! - so it is a worthwhile saving in speed and footprint...
87

88   protected static Map JavaDoc _emptyMap =Collections.EMPTY_MAP;
89   protected static Enumeration JavaDoc _emptyEnumeration =Collections.enumeration(Collections.EMPTY_LIST);
90   protected static String JavaDoc[] _emptyStringArray =new String JavaDoc[0]; // could this be changed by user ?
91

92   protected void ensureAttributes() { if (_attributes==null) _attributes=new HashMap JavaDoc();}
93
94   public Object JavaDoc
95     getAttribute(String JavaDoc name)
96     {
97       // _log.info("getAttribute("+name+")");
98
return _attributes==null?null:_attributes.get(name);
99     }
100
101   public Map JavaDoc
102     getAttributes()
103     {
104       return (_attributes==null || _attributes.size()==0)?_emptyMap:Collections.unmodifiableMap(_attributes);
105     }
106
107   public Enumeration JavaDoc
108     getAttributeNameEnumeration()
109     {
110       return (_attributes==null || _attributes.size()==0)?_emptyEnumeration:Collections.enumeration(_attributes.keySet());
111     }
112
113   public String JavaDoc[]
114     getAttributeNameStringArray()
115     {
116       return (_attributes==null || _attributes.size()==0)?_emptyStringArray:(String JavaDoc[])_attributes.keySet().toArray(new String JavaDoc[_attributes.size()]);
117     }
118
119   public Object JavaDoc
120     setAttribute(String JavaDoc name, Object JavaDoc value, boolean returnValue)
121     {
122       // we can be sure that name is non-null, because this will have
123
// been checked in our adaptor...
124

125       // _log.info("setAttribute("+name+", "+value+", "+returnValue+")");
126

127       ensureAttributes();
128       Object JavaDoc tmp=_attributes.put(name, value);
129       return returnValue?tmp:null;
130     }
131
132   public void
133     setAttributes(Map JavaDoc attributes)
134     {
135       if (_attributes!=null)
136     _attributes.clear();
137
138       if (attributes.size()>0)
139       {
140     ensureAttributes();
141     _attributes.putAll(attributes);
142       }
143     }
144
145   public Object JavaDoc
146     removeAttribute(String JavaDoc name, boolean returnValue)
147     {
148       if (_attributes==null)
149     return null;
150       else
151       {
152     Object JavaDoc tmp=_attributes.remove(name);
153     return returnValue?tmp:null;
154       }
155     }
156
157   protected long
158     remainingTime()
159     {
160       long maxInactiveInterval=_maxInactiveInterval<1?_actualMaxInactiveInterval:_maxInactiveInterval;
161       return (_lastAccessedTime+(maxInactiveInterval*1000L))-System.currentTimeMillis();
162     }
163
164   public boolean
165     isValid(int extraTime)
166     {
167       long remainingTime=remainingTime();
168       long etime=(extraTime*1000L);
169       boolean valid=remainingTime+etime>0;
170       return valid;
171     }
172
173   public boolean
174     isValid()
175     {
176       return isValid(0);
177     }
178 }
179
Popular Tags