KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Container is Object at front of stack
2
// at beginning of call, sets up threadlocals
3
// Interceptors divided into Stateful/less Interceptors
4
//// Interceptos implement cloneable
5

6 package org.mortbay.j2ee.session;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.ListIterator JavaDoc;
10
11 import javax.servlet.http.HttpSession JavaDoc;
12
13 import org.jfox.ioc.logger.Logger;
14
15 public class
16     Container
17     extends ArrayList JavaDoc
18     implements Cloneable JavaDoc
19 {
20   protected static final Logger _log= Logger.getLogger(Container.class);
21
22 // this will come into service when I figure out how to remove the
23
// next interceptor from each interceptor's state...
24

25 // public Object
26
// clone()
27
// {
28
// Container c=new Container();
29
//
30
// for (Iterator i=iterator(); i.hasNext();)
31
// c.add(((StateInterceptor)i.next()).clone());
32
//
33
// return c;
34
// }
35

36   public Object JavaDoc
37     clone()
38     {
39       Container c=new Container();
40
41       try
42       {
43     State state=null;
44
45     for (ListIterator JavaDoc i=listIterator(size()); i.hasPrevious();)
46     {
47       State lastState=state;
48       StateInterceptor si=(StateInterceptor)i.previous();
49       si=(StateInterceptor)si.getClass().newInstance();
50       si.setState(lastState);
51       state=si;
52       c.add(0,state);
53     }
54       }
55       catch (Exception JavaDoc e)
56       {
57     _log.error("could not clone Container", e);
58       }
59
60       return c;
61     }
62
63   // newContainer(this, id, state, getMaxInactiveInterval(), currentSecond()
64

65   public static HttpSession JavaDoc
66     newContainer(Manager manager, String JavaDoc id, State state, int maxInactiveInterval, long currentSecond, StateInterceptor[] interceptors)
67     {
68       // put together the make-believe container and HttpSession state
69

70       StateAdaptor adp=new StateAdaptor(id, manager, maxInactiveInterval, currentSecond);
71
72       State last=state;
73       try
74       {
75     Class JavaDoc[] ctorParams={};
76     for (int i=interceptors.length; i>0; i--)
77     {
78       StateInterceptor si=interceptors[i-1];
79 // if (_log.isDebugEnabled()) _log.debug("adding interceptor instance: "+name);
80
StateInterceptor interceptor=(StateInterceptor)si.clone();
81       si.setManager(manager); // overkill - but safe
82
si.setSession(adp); // overkill - but safe
83
interceptor.setState(last); // this is also passed into ctor - make up your mind - TODO
84
interceptor.start();
85       last=interceptor;
86     }
87       }
88       catch (Exception JavaDoc e)
89       {
90         _log.error("could not build distributed HttpSession container", e);
91       }
92
93       adp.setState(last);
94
95       return adp;
96     }
97
98   public static State
99     destroyContainer(HttpSession JavaDoc session, StateInterceptor[] interceptors)
100   {
101     // dissasemble the container here to aid GC
102

103     StateAdaptor sa=(StateAdaptor)session;
104     State last=sa.getState(); sa.setState(null);
105
106     for (int i=interceptors.length; i>0; i--)
107     {
108       StateInterceptor si=(StateInterceptor)last;
109       si.stop();
110       State s=si.getState();
111       si.setState(null);
112       last=s;
113     }
114
115     return last;
116   }
117 }
118
Popular Tags