001: // ========================================================================
002: // $Id: AroundInterceptor.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: import java.util.ArrayList;
019: import java.util.ListIterator;
020:
021: import javax.servlet.http.HttpSession;
022:
023: import org.jboss.logging.Logger;
024:
025: /**
026: * Container
027: *
028: * Container is Object at front of stack at beginning of call, sets up
029: * threadlocals Interceptors divided into Stateful/less Interceptors
030: * Interceptors implement cloneable
031: */
032: public class Container extends ArrayList implements Cloneable {
033: protected static final Logger _log = Logger
034: .getLogger(Container.class);
035:
036: // this will come into service when I figure out how to remove the
037: // next interceptor from each interceptor's state...
038:
039: // public Object
040: // clone()
041: // {
042: // Container c=new Container();
043: //
044: // for (Iterator i=iterator(); i.hasNext();)
045: // c.add(((StateInterceptor)i.next()).clone());
046: //
047: // return c;
048: // }
049:
050: public Object clone() {
051: Container c = new Container();
052:
053: try {
054: State state = null;
055:
056: for (ListIterator i = listIterator(size()); i.hasPrevious();) {
057: State lastState = state;
058: StateInterceptor si = (StateInterceptor) i.previous();
059: si = (StateInterceptor) si.getClass().newInstance();
060: si.setState(lastState);
061: state = si;
062: c.add(0, state);
063: }
064: } catch (Exception e) {
065: _log.error("could not clone Container", e);
066: }
067:
068: return c;
069: }
070:
071: // newContainer(this, id, state, getMaxInactiveInterval(), currentSecond()
072:
073: public static HttpSession newContainer(Manager manager, String id,
074: State state, int maxInactiveInterval, long currentSecond,
075: StateInterceptor[] interceptors) {
076: // put together the make-believe container and HttpSession state
077:
078: StateAdaptor adp = new StateAdaptor(id, manager,
079: maxInactiveInterval, currentSecond);
080:
081: State last = state;
082: try {
083: Class[] ctorParams = {};
084: // for (int i=interceptors.length; i>0; i--)
085: // {
086: // StateInterceptor si=interceptors[i-1];
087: // // if (_log.isDebugEnabled()) _log.debug("adding interceptor
088: // instance: "+name);
089: // StateInterceptor interceptor=(StateInterceptor)si.clone();
090: // si.setManager(manager); // overkill - but safe
091: // si.setSession(adp); // overkill - but safe
092: // interceptor.setState(last); // this is also passed into ctor -
093: // make up your mind - TODO
094: // interceptor.start();
095: // last=interceptor;
096: // }
097: } catch (Exception e) {
098: _log
099: .error(
100: "could not build distributed HttpSession container",
101: e);
102: }
103:
104: adp.setState(last);
105:
106: return adp;
107: }
108:
109: public static State destroyContainer(HttpSession session,
110: StateInterceptor[] interceptors) {
111: // dissasemble the container here to aid GC
112:
113: StateAdaptor sa = (StateAdaptor) session;
114: State last = sa.getState();
115: sa.setState(null);
116:
117: for (int i = interceptors.length; i > 0; i--) {
118: StateInterceptor si = (StateInterceptor) last;
119: si.stop();
120: State s = si.getState();
121: si.setState(null);
122: last = s;
123: }
124:
125: return last;
126: }
127: }
|