KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > container > visitor > HttpSessionProxyVisitor


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7  http://www.apache.org/licenses/LICENSE-2.0
8
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 com.jdon.container.visitor;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
22 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
23
24 import com.jdon.container.ContainerWrapper;
25 import com.jdon.container.access.TargetMetaRequest;
26 import com.jdon.container.finder.ComponentKeys;
27 import com.jdon.container.visitor.data.SessionContext;
28 import com.jdon.util.Debug;
29
30 /**
31  * using HttpSession as those components that need be cached
32  *
33  * now there are three kinds type: ComponentKeys.PROXYINSTANCE_FACTORY
34  * ComponentKeys.TARGETSERVICE_FACTORY; ComponentKeys.SESSIONCONTEXT_FACTORY;
35  *
36  * PROXYINSTANCE_FACTORY and TARGETSERVICE_FACTORY are the factorys that create
37  * components that need be optimized, if every time create these components, it
38  * will cost performance.
39  *
40  * ComponentKeys.SESSIONCONTEXT_FACTORY is the factory of the state data from the
41  * web container.
42  *
43  * Decorator patterns. Decoratee is ComponentOriginVisitor Decorator is this
44  * class;
45  *
46  * @see ComponentKeys container.xml
47  * @author banq
48  */

49 public class HttpSessionProxyVisitor implements ComponentVisitor, HttpSessionBindingListener JavaDoc {
50     private final static String JavaDoc module = HttpSessionProxyVisitor.class.getName();
51
52     /**
53      * in this box there are the result objects of the components running that
54      * need be optimized. note" not the components itself.
55      * the key = components name + VisitableName(such as :ComponentKeys.PROXYINSTANCE_FACTORY);
56      * the value is the result of components factory creating.
57      *
58      * samples:
59      * key: NewsManagerproxyInstanceFactoryVisitable
60      * value: the dynamic proxy instance for NewsManager object
61      *
62      * key:NewsManagertargetServiceFactoryVisitable
63      * value: the NewsManager object
64      *
65      */

66     private Map JavaDoc componentsboxs = new HashMap JavaDoc();
67
68     private ComponentVisitor componentVisitor;
69
70     public HttpSessionProxyVisitor(ComponentVisitor componentVisitor) {
71         this.componentVisitor = componentVisitor;
72     }
73
74     public ContainerWrapper getContainerWrapper() {
75         return componentVisitor.getContainerWrapper();
76     }
77
78     public void valueBound(HttpSessionBindingEvent JavaDoc event) {
79         Debug.logVerbose("[JdonFramework] valueBound active, sessionId :" + event.getSession().getId(), module);
80         componentsboxs.clear();
81
82     }
83
84     /**
85      * session destroyed. remove all references;
86      */

87     public void valueUnbound(HttpSessionBindingEvent JavaDoc event) {
88         String JavaDoc sessionId = event.getSession().getId();
89         Debug.logVerbose("[JdonFramework] unvalueBound active, sessionId :" + sessionId, module);
90         Debug.logVerbose("[JdonFramework] unvalueUnbound active, componentsboxs size" + componentsboxs.size(), module);
91         //removeObjects();
92
componentsboxs.clear();
93     }
94
95     /**
96      * the object type saved in componentsboxs is decided by the method"
97      * visitableFactory.createVisitable. only ejb service need cached, pojo
98      * service not need.
99      *
100      * @param targetMetaDef
101      * TargetMetaDef
102      * @return Object
103      */

104     public Object JavaDoc visit(TargetMetaRequest targetMetaRequest) {
105         Object JavaDoc o = null;
106         try {
107             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(targetMetaRequest.getTargetMetaDef().getCacheKey());
108             sb.append(targetMetaRequest.getVisitableName());
109             Debug.logVerbose("[JdonFramework] get the optimized instance for the key " + sb.toString(), module);
110             o = componentsboxs.get(sb.toString());
111             if (o == null) {
112                 Debug.logVerbose("[JdonFramework] first time visit: " + targetMetaRequest.getTargetMetaDef().getClassName(), module);
113                 o = componentVisitor.visit(targetMetaRequest);
114                 componentsboxs.put(sb.toString(), o);
115             }
116         } catch (Exception JavaDoc e) {
117             Debug.logError("[JdonFramework]visit error: " + e);
118         }
119         return o;
120     }
121     
122     public SessionContext visitSessionContext(TargetMetaRequest targetMetaRequest){
123         Object JavaDoc sessionContext = componentsboxs.get(SessionContext.NAME);
124         if (sessionContext == null) {
125             Debug.logVerbose("[JdonFramework] first time visit sessionContext: " + targetMetaRequest.getVisitableName(), module);
126             sessionContext = componentVisitor.visit(targetMetaRequest);
127             componentsboxs.put(SessionContext.NAME, sessionContext);
128         }
129         return (SessionContext)sessionContext;
130     }
131
132     /**
133      * remove all ejb references
134      */

135     public void removeObjects() {
136         /**
137          *
138          * try { Iterator iter = componentsboxs.keySet().iterator(); while
139          * (iter.hasNext()) { String key = (String) iter.next(); Object ccEjb =
140          * (Object) componentsboxs.get(key); removeEJBObject(ccEjb); } } catch
141          * (Exception ex) { Debug.logWarning(ex, module); } finally {
142          * componentsboxs.clear(); }
143          */

144
145     }
146
147     /**
148      * destroy the EJB.
149      *
150      * private void removeEJBObject(Object ccEjb) { if (ccEjb == null) return;
151      * try { //这个EJBçš„remove方法需è¦?被任何角色访问,因为此时principle为空 if (ccEjb instanceof
152      * EJBLocalObject) { EJBLocalObject eo = ( (EJBLocalObject) ccEjb);
153      * eo.remove(); } else if (ccEjb instanceof EJBObject) { EJBObject eo = (
154      * (EJBObject) ccEjb); eo.remove(); } } catch (Exception re) {
155      * } }
156      */

157 }
158
Popular Tags