KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > aop > interceptor > SessionContextInterceptor


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.aop.interceptor;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23
24 import com.jdon.aop.reflection.ProxyMethodInvocation;
25 import com.jdon.bussinessproxy.TargetMetaDef;
26 import com.jdon.container.ContainerWrapper;
27 import com.jdon.container.access.TargetMetaRequest;
28 import com.jdon.container.finder.ComponentKeys;
29 import com.jdon.container.finder.ContainerCallback;
30 import com.jdon.container.visitor.data.SessionContext;
31 import com.jdon.container.visitor.data.SessionContextAcceptable;
32 import com.jdon.util.Debug;
33
34 /**
35  * interceptor the SessionContextAcceptable concrete services,
36  * and inject the SessionContext object into them, so these application
37  * service can got the datas from session that saved by framework.
38  *
39  * this interceptor must be after the targetservice creating interceptors
40  * such as StatefulInterceptor or PoolInterceptor,
41  *
42  * because this interceptor will inject SessionContext object into the existed
43  * targetservice object.
44  *
45  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
46  *
47  */

48
49 public class SessionContextInterceptor implements MethodInterceptor {
50     private final static String JavaDoc module = SessionContextInterceptor.class.getName();
51     
52     private List JavaDoc isSessionContextAcceptables = new ArrayList JavaDoc();
53     
54     private List JavaDoc unSessionContextAcceptables = new ArrayList JavaDoc();
55     
56     private ContainerCallback containerCallback;
57     
58     public SessionContextInterceptor(ContainerCallback containerCallback) {
59         this.containerCallback = containerCallback;
60     }
61     
62     /*
63      * inject SessionContext into targetObject.
64      * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
65      */

66     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
67         ProxyMethodInvocation pmi = (ProxyMethodInvocation)invocation;
68         TargetMetaRequest targetMetaRequest = pmi.getTargetMetaRequest();
69         TargetMetaDef targetMetaDef = targetMetaRequest.getTargetMetaDef();
70         if (targetMetaDef.isEJB())
71             return invocation.proceed();
72
73         if (!isSessionContextAcceptable(targetMetaDef)){
74            //Debug.logVerbose("[JdonFramework] target service is not SessionContextAcceptable: "
75
// + targetMetaDef.getClassName() + " SessionContextInterceptor unactiive", module);
76
return invocation.proceed();
77         }
78         Debug.logVerbose("[JdonFramework] enter SessionContextInterceptor", module);
79         
80         Object JavaDoc result = null;
81         try {
82             Object JavaDoc targetObject = pmi.getThis();
83             Debug.logVerbose("[JdonFramework] targetObject should be SessionContextAcceptable: " + targetObject.getClass().getName(), module);
84             SessionContextAcceptable myResult = (SessionContextAcceptable)targetObject;
85             targetMetaRequest.setVisitableName(ComponentKeys.SESSIONCONTEXT_FACTORY);
86             SessionContext sessionContext = targetMetaRequest.getComponentVisitor().visitSessionContext(targetMetaRequest);
87             myResult.setSessionContext(sessionContext);
88             
89             result = invocation.proceed();
90             
91         }catch(Exception JavaDoc ex){
92             Debug.logError("[JdonFramework]SessionContextInterceptor error: " + ex, module);
93         }
94         return result;
95     }
96
97     public boolean isSessionContextAcceptable(TargetMetaDef targetMetaDef) {
98         boolean found = false;
99         if (isSessionContextAcceptables.contains(targetMetaDef.getName())) {
100             found = true;
101         }else if(!unSessionContextAcceptables.contains(targetMetaDef.getName())){
102             Debug.logVerbose("[JdonFramework] check if it is a isSessionContextAcceptable", module);
103             ContainerWrapper containerWrapper = containerCallback.getContainerWrapper();
104             Class JavaDoc thisCLass = containerWrapper.getComponentClass(targetMetaDef.getName());
105             if (SessionContextAcceptable.class.isAssignableFrom(thisCLass)) {
106                 found = true;
107                 isSessionContextAcceptables.add(targetMetaDef.getName());
108             }else{
109                 unSessionContextAcceptables.add(targetMetaDef.getName());
110             }
111         }
112         return found;
113     }
114     
115
116 }
117
Popular Tags