001: //========================================================================
002: //$Id: StateAdaptor.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.rmi.RemoteException;
021: import java.util.Enumeration;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.http.HttpSessionContext;
025:
026: import org.jboss.logging.Logger;
027:
028: //----------------------------------------
029: //this class is responsible for presenting a State container to a
030: //servlet as a HttpSession - since this interface is not an ideal one
031: //to use for the container interceptors. It constrains all the inputs
032: //to this API as specified...
033:
034: //Since this is the front end of the Container, maybe it should be so
035: //called ? or maybe I should just call it [Http]Session?
036:
037: //we should cache our id locally...
038:
039: public class StateAdaptor implements Manager.Session {
040: protected static final Logger _log = Logger
041: .getLogger(StateAdaptor.class);
042:
043: Manager _manager;
044:
045: State _state = null;
046:
047: boolean _new = true;
048:
049: // we cache these for speed...
050: final String _id;
051:
052: StateAdaptor(String id, Manager manager, int maxInactiveInterval,
053: long lastAccessedTime) {
054: _id = id;
055: _manager = manager;
056: }
057:
058: void setState(State state) {
059: _state = state;
060: }
061:
062: State getState() {
063: return _state;
064: }
065:
066: // hmmmm...
067: // why does Greg call this?
068: // could it be compressed into a subsequent call ?
069: public boolean isValid() {
070: if (_state == null)
071: return false;
072:
073: StateInterceptor si = (StateInterceptor) _state;
074: si.setManager(_manager);
075: si.setSession(this );
076:
077: try {
078: _state.getLastAccessedTime(); // this should cause an interceptor/the session to check
079: return true;
080: } catch (IllegalStateException ignore) {
081: return false;
082: } catch (Exception e) {
083: _log.error("problem contacting HttpSession", e);
084: return false;
085: }
086: }
087:
088: // HttpSession API
089:
090: public long getCreationTime() throws IllegalStateException {
091: checkState();
092:
093: try {
094: return _state.getCreationTime();
095: } catch (RemoteException e) {
096: _log.error("could not get CreationTime", e);
097: throw new IllegalStateException(
098: "problem with distribution layer");
099: }
100: }
101:
102: public String getId() throws IllegalStateException {
103: checkState();
104:
105: // locally cached and invariant
106: return _id;
107: }
108:
109: public long getLastAccessedTime() throws IllegalStateException {
110: checkState();
111:
112: try {
113: return _state.getLastAccessedTime();
114: } catch (RemoteException e) {
115: _log.error("could not get LastAccessedTime", e);
116: throw new IllegalStateException(
117: "problem with distribution layer");
118: }
119: }
120:
121: // clarify with Tomcat, whether this is on a per Session or SessionManager basis...
122: public void setMaxInactiveInterval(int interval) {
123: checkState();
124:
125: try {
126: _state.setMaxInactiveInterval(interval);
127: } catch (RemoteException e) {
128: _log.error("could not set MaxInactiveInterval", e);
129: }
130: }
131:
132: public int getMaxInactiveInterval() {
133: checkState();
134:
135: try {
136: return _state.getMaxInactiveInterval();
137: } catch (RemoteException e) {
138: // Can I throw an exception of some type here - instead of
139: // returning rubbish ? - TODO
140: _log.error("could not get MaxInactiveInterval", e);
141: return 0;
142: }
143: }
144:
145: public Object getAttribute(String name)
146: throws IllegalStateException {
147: checkState();
148:
149: try {
150: return _state.getAttribute(name);
151: } catch (RemoteException e) {
152: _log.error("could not get Attribute", e);
153: throw new IllegalStateException(
154: "problem with distribution layer");
155: }
156: }
157:
158: public Object getValue(String name) throws IllegalStateException {
159: checkState();
160:
161: try {
162: return _state.getAttribute(name);
163: } catch (RemoteException e) {
164: _log.error("could not get Value", e);
165: throw new IllegalStateException(
166: "problem with distribution layer");
167: }
168: }
169:
170: public Enumeration getAttributeNames() throws IllegalStateException {
171: checkState();
172:
173: try {
174: return _state.getAttributeNameEnumeration();
175: } catch (RemoteException e) {
176: _log.error("could not get AttributeNames", e);
177: throw new IllegalStateException(
178: "problem with distribution layer");
179: }
180: }
181:
182: public String[] getValueNames() throws IllegalStateException {
183: checkState();
184:
185: try {
186: return _state.getAttributeNameStringArray();
187: } catch (RemoteException e) {
188: _log.error("could not get ValueNames", e);
189: throw new IllegalStateException(
190: "problem with distribution layer");
191: }
192: }
193:
194: public void setAttribute(String name, Object value)
195: throws IllegalStateException {
196: checkState();
197:
198: try {
199: if (value == null)
200: _state.removeAttribute(name, false);
201: else {
202: if (name == null)
203: throw new IllegalArgumentException(
204: "invalid attribute name: " + name);
205:
206: _state.setAttribute(name, value, false);
207: }
208: } catch (RemoteException e) {
209: _log.error("could not set Attribute", e);
210: throw new IllegalStateException(
211: "problem with distribution layer");
212: }
213: }
214:
215: public void putValue(String name, Object value)
216: throws IllegalStateException {
217: checkState();
218:
219: if (name == null)
220: throw new IllegalArgumentException(
221: "invalid attribute name: " + name);
222:
223: if (value == null)
224: throw new IllegalArgumentException(
225: "invalid attribute value: " + value);
226:
227: try {
228: _state.setAttribute(name, value, false);
229: } catch (RemoteException e) {
230: _log.error("could not put Value", e);
231: throw new IllegalStateException(
232: "problem with distribution layer");
233: }
234: }
235:
236: public void removeAttribute(String name)
237: throws IllegalStateException {
238: checkState();
239:
240: try {
241: _state.removeAttribute(name, false);
242: } catch (RemoteException e) {
243: _log.error("could not remove Attribute", e);
244: throw new IllegalStateException(
245: "problem with distribution layer");
246: }
247: }
248:
249: public void removeValue(String name) throws IllegalStateException {
250: checkState();
251:
252: try {
253: _state.removeAttribute(name, false);
254: } catch (RemoteException e) {
255: _log.error("could not remove Value", e);
256: throw new IllegalStateException(
257: "problem with distribution layer");
258: }
259: }
260:
261: public void invalidate() throws IllegalStateException {
262: if (_log.isTraceEnabled())
263: _log.trace("user invalidated session: " + getId());
264: _manager.destroySession(this );
265: }
266:
267: /**
268: *
269: * Returns <code>true</code> if the client does not yet know about the
270: * session or if the client chooses not to join the session. For
271: * example, if the server used only cookie-based sessions, and
272: * the client had disabled the use of cookies, then a session would
273: * be new on each request.
274: *
275: * @return <code>true</code> if the
276: * server has created a session,
277: * but the client has not yet joined
278: *
279: * @exception IllegalStateException if this method is called on an
280: * already invalidated session
281: *
282: */
283:
284: public boolean isNew() throws IllegalStateException {
285: return _new;
286: }
287:
288: public ServletContext getServletContext() {
289: return _manager.getServletContext();
290: }
291:
292: public HttpSessionContext getSessionContext() {
293: return _manager.getSessionContext();
294: }
295:
296: // this one's for Greg...
297: public void access() {
298: long time = System.currentTimeMillis(); // we could get this from Manager - less accurate
299: setLastAccessedTime(time);
300:
301: _new = false; // synchronise - TODO
302: }
303:
304: public void setLastAccessedTime(long time)
305: throws IllegalStateException {
306: checkState();
307:
308: try {
309: _state.setLastAccessedTime(time);
310: } catch (RemoteException e) {
311: _log.error("could not set LastAccessedTime", e);
312: throw new IllegalStateException(
313: "problem with distribution layer");
314: }
315: }
316:
317: protected void checkState() throws IllegalStateException {
318: if (_state == null)
319: throw new IllegalStateException("invalid session");
320: else {
321:
322: // this is a hack to get new interceptor stack to work... - TODO
323: // StateInterceptor si=(StateInterceptor)_state;
324: // si.setManager(_manager);
325: // si.setSession(this);
326: StateInterceptor si = null;
327: if (_state instanceof StateInterceptor) {
328: si = (StateInterceptor) _state;
329: si.setManager(_manager);
330: si.setSession(this );
331: } else {
332: si = new StateInterceptor();
333: si.setManager(_manager);
334: si.setSession(this );
335: si.setState(_state);
336: _state = si;
337: }
338: }
339: }
340:
341: public String toString() {
342: return "<" + getClass() + "->" + _state + ">";
343: }
344:
345: // I'm still not convinced that this is the correct place for this
346: // method- but I can;t think of a better way - maybe in the next
347: // iteration...
348:
349: // MigrationInterceptor _mi=null;
350:
351: // public void
352: // registerMigrationListener(MigrationInterceptor mi)
353: // {
354: // _mi=mi;
355: // }
356:
357: public void migrate() {
358: // if (_mi!=null)
359: // _mi.migrate(); // yeugh - TODO
360: }
361: }
|