KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > aop > reflection > ProxyMethodInvocation


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.reflection;
17
18 import java.lang.reflect.AccessibleObject JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.aopalliance.intercept.MethodInterceptor;
23 import org.aopalliance.intercept.MethodInvocation;
24
25 import com.jdon.bussinessproxy.TargetMetaDef;
26 import com.jdon.bussinessproxy.target.TargetServiceFactory;
27 import com.jdon.container.access.TargetMetaRequest;
28 import com.jdon.util.Debug;
29
30 /**
31  * MethodInvocation Implemention by this class, Interceptor will action.
32  *
33  * @author banq
34  */

35 public class ProxyMethodInvocation implements MethodInvocation,
36         java.io.Serializable JavaDoc {
37     private final static String JavaDoc module = ProxyMethodInvocation.class.getName();
38
39     protected TargetServiceFactory targetServiceFactory;
40
41     private TargetMetaRequest targetMetaRequest;
42
43     private Method JavaDoc method;
44
45     private Object JavaDoc[] args;
46
47     private Object JavaDoc target;
48
49     protected List JavaDoc interceptors;
50
51     protected MethodInvokerUtil mUtil;
52     
53     protected int currentInterceptorInt = -1;
54  
55     public ProxyMethodInvocation(List JavaDoc interceptors,
56             TargetMetaRequest targetMetaRequest,
57             TargetServiceFactory targetServiceFactory, Method JavaDoc method,
58             Object JavaDoc[] args) {
59         Debug.logVerbose("[JdonFramework] method.getName() :" + method.getName(), module);
60         this.targetMetaRequest = targetMetaRequest;
61         this.interceptors = interceptors;
62         this.targetServiceFactory = targetServiceFactory;
63         this.mUtil = new MethodInvokerUtil();
64         this.method = method;
65         this.args = args;
66     }
67
68     /**
69      * Invokes next interceptor/proxy target. now there is no mixin
70      */

71     public Object JavaDoc proceed() throws Throwable JavaDoc {
72         //Debug.logVerbose("[JdonFramework] <-----> enter ProxyMethodInvocation proceed() for "
73
// + currentInterceptorInt, module);
74
if (currentInterceptorInt == interceptors.size() - 1) {
75             Debug.logVerbose("[JdonFramework] finish call all inteceptors", module);
76             return methodInvoke();
77         }
78
79         Object JavaDoc interceptor = interceptors.get(++currentInterceptorInt);
80         if (interceptor != null) {
81             MethodInterceptor methodInterceptor = (MethodInterceptor) interceptor;
82             //Debug.logVerbose("[JdonFramework] now call inteceptor : "
83
// + methodInterceptor.getClass().getName(), module);
84
return methodInterceptor.invoke(this);
85         } else {
86             Debug.logVerbose("[JdonFramework] null finish call all inteceptors", module);
87             return methodInvoke();
88         }
89     }
90
91     private Object JavaDoc methodInvoke() throws Throwable JavaDoc {
92         Debug.logVerbose("[JdonFramework]enter method reflection ", module);
93         Object JavaDoc result = null;
94         try {
95             if (target == null){//interceptor not set target
96
Debug.logVerbose("[JdonFramework] all interceptors not set this target object, now create it", module);
97                 target = mUtil.createTargetObject(targetServiceFactory, targetMetaRequest);
98             }
99
100             Debug.logVerbose("[JdonFramework] target:"+ target.getClass().getName() +" service's method:"
101                         + method.getName()+ " running.. ", module);
102
103             if (targetMetaRequest.getTargetMetaDef().isEJB()) {
104                 Debug.logVerbose("[JdonFramework] it is ejb target service", module);
105                 result = mUtil.execute(method, target, mUtil.narrowArgs(args));
106             } else {
107                 Debug.logVerbose("[JdonFramework] it is pojo target service", module);
108                 result = mUtil.execute(method, target, args);
109             }
110         } catch (Exception JavaDoc ex) {
111             Debug.logError("[JdonFramework]run error: " + ex, module);
112             throw new Throwable JavaDoc(ex);
113         } catch (Throwable JavaDoc tex) {
114             throw new Throwable JavaDoc(tex);
115         }
116         return result;
117     }
118
119     public TargetMetaDef getTargetMetaDef() {
120         return targetMetaRequest.getTargetMetaDef();
121     }
122
123
124     public Object JavaDoc[] getArguments() {
125         return this.args;
126     }
127
128     public Object JavaDoc getThis() {
129         /**
130         try {
131             if (target == null)
132                 target = mUtil.createTargetObject(targetServiceFactory, targetMetaRequest);
133         } catch (Exception e) {
134             Debug.logError("[JdonFramework]createTargetObject error :" + e, module);
135         }
136         **/

137         return target;
138     }
139
140     public void setThis(Object JavaDoc target) {
141         this.target = target;
142     }
143
144     public AccessibleObject JavaDoc getStaticPart() {
145         return null;
146     }
147
148     public Method JavaDoc getMethod() {
149         return method;
150     }
151
152     
153     /**
154      * @return Returns the targetMetaRequest.
155      */

156     public TargetMetaRequest getTargetMetaRequest() {
157         return targetMetaRequest;
158     }
159     /**
160      * @param targetMetaRequest The targetMetaRequest to set.
161      */

162     public void setTargetMetaRequest(TargetMetaRequest targetMetaRequest) {
163         this.targetMetaRequest = targetMetaRequest;
164     }
165 }
166
Popular Tags