View Javadoc

1   /*************************************************
2    * Copyright (c) Shen Li. All rights reserved.  *
3    * http://joyaop.sourceforge.net                *
4    * -------------------------------------------- *
5    * Distributable under LGPL license.            *
6    * See terms of license at gnu.org.             *
7    ************************************************/
8   package net.sf.joyaop.impl;
9   
10  import net.sf.cglib.proxy.MethodInterceptor;
11  import net.sf.cglib.proxy.MethodProxy;
12  import net.sf.joyaop.AspectRuntimeException;
13  
14  import java.io.Serializable;
15  import java.lang.reflect.Method;
16  import java.util.List;
17  
18  /***
19   * @see net.sf.cglib.proxy.MethodInterceptor
20   *
21   * @author Shen Li
22   */
23  public class InvocationHandler implements MethodInterceptor, Serializable {
24      private static final long serialVersionUID = 0;
25      private List interceptorAspectInstances;
26      private RuntimeInstance targetInstance;
27  
28      public InvocationHandler(List interceptorInstances) {
29          this.interceptorAspectInstances = interceptorInstances;
30      }
31  
32      public InvocationHandler(List interceptorInstances, RuntimeInstance targetInstance) {
33          this.interceptorAspectInstances = interceptorInstances;
34          this.targetInstance = targetInstance;
35      }
36  
37      public Object intercept(Object proxy, Method method, Object[] arguments, MethodProxy methodProxy) throws Throwable {
38          InvocationImpl invocation = InvocationImpl.getCurrentInvocation();
39          if (invocation == null) {
40              invocation = new InvocationImpl();
41              InvocationImpl.setCurrentInvocation(invocation);
42          } else if (invocation.isDirty()) {
43              if (method == invocation.getMethod()) {
44                  if (!invocation.allowRecursion()) {
45                      throw new AspectRuntimeException("You have recursively invoked the method "
46                              + method.getName() + ", it will cause stack to be overflow.");
47                  }
48                  if (invocation.isOnInterceptor()) {
49                      return invocation.proceed();
50                  }
51              }
52              Object[] oldArguments = invocation.getArguments();
53              short oldInterceptorIndex = invocation.getCurrentInterceptorIndex();
54              Method oldMethod = invocation.getMethod();
55              MethodProxy oldMethodProxy = invocation.getMethodProxy();
56              Object oldProxy = invocation.getProxy();
57              List oldInterceptorAspectInstances = invocation.getInterceptorAspectInstances();
58              RuntimeInstance oldTargetAspectInstance = invocation.getTargetInstance();
59              invocation.setArguments(arguments);
60              invocation.resetCurrentInterceptorIndex();
61              invocation.setInterceptorAspectInstances(interceptorAspectInstances);
62              invocation.setMethod(method);
63              invocation.setMethodProxy(methodProxy);
64              invocation.setTargetInstance(targetInstance);
65              invocation.setProxy(proxy);
66              
67              try {
68                  return invocation.proceed();
69              } finally {
70                  invocation.setArguments(oldArguments);
71                  invocation.setCurrentInterceptorIndex(oldInterceptorIndex);
72                  invocation.setInterceptorAspectInstances(oldInterceptorAspectInstances);
73                  invocation.setMethod(oldMethod);
74                  invocation.setMethodProxy(oldMethodProxy);
75                  invocation.setTargetInstance(oldTargetAspectInstance);
76                  invocation.setProxy(oldProxy);
77              }
78          }
79  
80          invocation.setArguments(arguments);
81          invocation.resetCurrentInterceptorIndex();
82          invocation.setInterceptorAspectInstances(interceptorAspectInstances);
83          invocation.setMethod(method);
84          invocation.setMethodProxy(methodProxy);
85          invocation.setTargetInstance(targetInstance);
86          invocation.setProxy(proxy);
87          return invocation.proceed();
88      }
89  }