KickJava   Java API By Example, From Geeks To Geeks.

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


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.ComponentVisitor;
31 import com.jdon.controller.service.Stateful;
32 import com.jdon.util.Debug;
33
34 /**
35  * StatefulInterceptor is a Interceptor of
36  * creating target object.
37  * must be the last in Interceptors.
38  *
39  * it active for all pojoServices that implements
40  * com.jdon.controller.service.StatefulPOJOService
41  *
42  * this class stateful function is using ComponentVisitor's
43  * cache, such as HttpSession.
44  *
45  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
46  *
47  */

48 public class StatefulInterceptor implements MethodInterceptor {
49     private final static String JavaDoc module = StatefulInterceptor.class.getName();
50     
51     private List JavaDoc isStatefulCache = new ArrayList JavaDoc();
52     
53     private List JavaDoc unStatefulCache = new ArrayList JavaDoc();
54     
55     private ContainerCallback containerCallback;
56     
57     
58     /**
59      * @param containerCallback
60      */

61     public StatefulInterceptor(ContainerCallback containerCallback) {
62         super();
63         this.containerCallback = containerCallback;
64     }
65     
66     /*
67      * check the pojoService if implements StatefulPOJOService
68      * using ComponentVisitor, we can get targetObjRef from httpsession.
69      *
70      * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
71      */

72     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
73         ProxyMethodInvocation pmi = (ProxyMethodInvocation)invocation;
74         TargetMetaRequest targetMetaRequest = pmi.getTargetMetaRequest();
75         TargetMetaDef targetMetaDef = targetMetaRequest.getTargetMetaDef();
76         if (targetMetaDef.isEJB())
77             return invocation.proceed();
78
79         if (!isStateful(targetMetaDef)){
80             //Debug.logVerbose("[JdonFramework] target service is not Stateful: "
81
// + targetMetaDef.getClassName() + " StatefulInterceptor unactiive", module);
82
return invocation.proceed();
83         }
84         Debug.logVerbose("[JdonFramework] enter StatefulInterceptor", module);
85         Object JavaDoc result = null;
86         try {
87             ComponentVisitor cm = targetMetaRequest.getComponentVisitor();
88             targetMetaRequest.setVisitableName(ComponentKeys.TARGETSERVICE_FACTORY);
89             Debug.logVerbose(ComponentKeys.TARGETSERVICE_FACTORY
90                     + " in action (Stateful)", module);
91             Object JavaDoc targetObjRef = cm.visit(targetMetaRequest);
92             pmi.setThis(targetObjRef);
93             result = invocation.proceed();
94         }catch(Exception JavaDoc ex){
95             Debug.logError("[JdonFramework]StatefulInterceptor error: " + ex, module);
96         }
97         return result;
98     }
99
100     public boolean isStateful(TargetMetaDef targetMetaDef) {
101         boolean found = false;
102         if (isStatefulCache.contains(targetMetaDef.getName())) {
103             found = true;
104         }else if(!unStatefulCache.contains(targetMetaDef.getName())){
105             Debug.logVerbose("[JdonFramework] check if it is a isStateful", module);
106             ContainerWrapper containerWrapper = containerCallback.getContainerWrapper();
107             Class JavaDoc thisCLass = containerWrapper.getComponentClass(targetMetaDef.getName());
108             if (Stateful.class.isAssignableFrom(thisCLass)) {
109                 found = true;
110                 isStatefulCache.add(targetMetaDef.getName());
111             }else{
112                 unStatefulCache.add(targetMetaDef.getName());
113             }
114         }
115         return found;
116     }
117     
118     
119 }
120
Popular Tags