KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > aop > AopClient


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;
17
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.aopalliance.intercept.MethodInvocation;
22
23 import com.jdon.aop.joinpoint.AdvisorChainFactory;
24 import com.jdon.aop.reflection.MethodConstructor;
25 import com.jdon.aop.reflection.ProxyMethodInvocation;
26 import com.jdon.bussinessproxy.meta.MethodMetaArgs;
27 import com.jdon.bussinessproxy.target.TargetServiceFactory;
28 import com.jdon.container.access.TargetMetaRequest;
29 import com.jdon.util.Debug;
30
31 /**
32  * Aop Client
33  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
34  *
35  */

36 public class AopClient {
37
38   private final static String JavaDoc module = AopClient.class.getName();
39
40   private AdvisorChainFactory advisorChainFactory;
41
42   private TargetServiceFactory targetServiceFactory;
43   
44   private MethodConstructor methodConstructor;
45
46   public AopClient(AdvisorChainFactory advisorChainFactory,
47                   
48                    TargetServiceFactory targetServiceFactory) {
49     this.advisorChainFactory = advisorChainFactory;
50     this.targetServiceFactory = targetServiceFactory;
51     this.methodConstructor = new MethodConstructor();
52   }
53
54   /**
55    *
56    * different target service has its Interceptor instance
57    * and MethodInvocation instance
58    *
59    */

60   public Object JavaDoc invoke(TargetMetaRequest targetMetaRequest) throws
61       Throwable JavaDoc {
62     Debug.logVerbose("[JdonFramework] enter AOP invoker for:" +targetMetaRequest.getTargetMetaDef().getClassName()
63             +" method:" + targetMetaRequest.getMethodMetaArgs().getMethodName(), module);
64     
65     Object JavaDoc result = null;
66     MethodInvocation methodInvocation = null;
67     try {
68       List JavaDoc chain = advisorChainFactory.create(targetMetaRequest.getTargetMetaDef());
69       Object JavaDoc[] args = targetMetaRequest.getMethodMetaArgs().getArgs();
70       Method JavaDoc method = methodConstructor.createMethod(targetServiceFactory, targetMetaRequest);
71       methodInvocation = new ProxyMethodInvocation(chain, targetMetaRequest, targetServiceFactory, method, args);
72       Debug.logVerbose("[JdonFramework] MethodInvocation will proceed ... ", module);
73       result = methodInvocation.proceed();
74     } catch (Exception JavaDoc ex) {
75       Debug.logError(ex, module);
76       throw new Exception JavaDoc(ex);
77     } catch (Throwable JavaDoc ex) {
78       throw new Throwable JavaDoc(ex);
79     }
80     return result;
81   }
82   
83   public Object JavaDoc invoke(TargetMetaRequest targetMetaRequest,
84           Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
85       Debug.logVerbose("[JdonFramework] enter AOP invoker2 for:" +targetMetaRequest.getTargetMetaDef().getClassName()
86               +" method:" + method.getName(), module);
87
88       
89       Object JavaDoc result = null;
90       MethodInvocation methodInvocation = null;
91       try {
92         List JavaDoc chain = advisorChainFactory.create(targetMetaRequest.getTargetMetaDef());
93         methodInvocation = new ProxyMethodInvocation(chain, targetMetaRequest, targetServiceFactory, method, args);
94         Debug.logVerbose("[JdonFramework] MethodInvocation will proceed ... ", module);
95         result = methodInvocation.proceed();
96       } catch (Exception JavaDoc ex) {
97         Debug.logError(ex, module);
98         throw new Exception JavaDoc(ex);
99       } catch (Throwable JavaDoc ex) {
100         throw new Throwable JavaDoc(ex);
101       }
102       return result;
103       
104   }
105
106 }
107
Popular Tags