1
2
3
4
5 package net.sf.crispy.proxy;
6
7 import java.lang.reflect.Method;
8
9 import net.sf.crispy.Executor;
10 import net.sf.crispy.InterceptorContext;
11 import net.sf.crispy.InterceptorHandler;
12
13 /**
14 *
15 * Handel Invocation from the DynamicProxy-implementation.
16 * Intern call the InvocationHandler all registered Interceptors before and after the doInvoke-Method.
17 *
18 * @author Linke
19 *
20 */
21 public final class InvocationHandler {
22
23 /**
24 *
25 * This method must call from the DynamicProy-implementation.
26 * The method invoke the <code>invokeIfExecutorIsNull</code> method from DynamicProxy-implementation.
27 *
28 * @param pvDynamicProxy Implementation of DynamicProxy
29 * @param pvProxy the Proxy-Object
30 * @param pvMethod the called Method
31 * @param pvArgs the Args from called Method
32 * @param pvElseObject implementation specific Object.
33 * For example the Param-Object: MethodProxy bei @see net.sf.crispy.impl.DynamicCglibProxy Implementation.
34 * Or <code>null</code>
35 *
36 * @return Result from the method-call
37 *
38 * @throws Throwable Error by the method-call
39 *
40 */
41 public static Object doInvoke(DynamicProxy pvDynamicProxy, Object pvProxy, Method pvMethod, Object[] pvArgs,
42 Object pvElseObject, final InterceptorHandler pvInterceptorHandler) throws Exception {
43 Object ret = null;
44 InterceptorContext lvInterceptorContext = new InterceptorContext(pvDynamicProxy, pvMethod, pvArgs, pvElseObject);
45
46 if (pvInterceptorHandler == null) {
47 throw new IllegalArgumentException ("The InvocationHandler has no InterceptorHandler (is null)!");
48 }
49
50
51 InterceptorContext lvInterceptorContextModify = pvInterceptorHandler.fireBeforeModifyInvocation(lvInterceptorContext);
52 boolean lvInterruptInvocationModifier = lvInterceptorContextModify.getInterruptInvocation();
53
54 Method lvNewMethod = lvInterceptorContextModify.getMethod();
55 Object lvNewArgs[] = lvInterceptorContextModify.getArgs();
56 Object lvNewElseObject = lvInterceptorContextModify.getElseObject();
57 DynamicProxy lvNewDynamicProxy = lvInterceptorContextModify.getDynamicProxy();
58
59
60 boolean lvInterruptInvocationInterceptor = pvInterceptorHandler.fireBeforeMethodInvocation(lvInterceptorContext);
61 try {
62 boolean lvInterruptInvocation = false;
63 if ((lvInterruptInvocationModifier == true) || (lvInterruptInvocationInterceptor == true)) {
64 lvInterruptInvocation = true;
65 }
66
67
68 if (lvInterruptInvocation == true) {
69 ret = lvInterceptorContext.getResult();
70 if (ret instanceof Exception) {
71 throw (Exception) ret;
72 }
73 } else {
74
75 Executor lvExecutor = lvNewDynamicProxy.getExecutor();
76 if (lvExecutor == null) {
77 ret = lvNewDynamicProxy.invokeIfExecutorIsNull(pvProxy, lvNewMethod, lvNewArgs, lvNewElseObject);
78 } else {
79 ret = lvExecutor.execute(lvNewDynamicProxy.getProxyClass(), lvNewDynamicProxy.getProxyObject(), lvNewMethod, lvNewArgs);
80 }
81
82 }
83
84 } catch (Exception e) {
85 pvInterceptorHandler.fireOnError(e);
86 throw e;
87 }
88 finally {
89 lvInterceptorContext.setResult(ret);
90
91 InterceptorContext lvInterceptorContext2 = pvInterceptorHandler.fireAfterModifyInvocation(lvInterceptorContext);
92 ret = lvInterceptorContext2.getResult();
93
94 pvInterceptorHandler.fireAfterMethodInvocation(lvInterceptorContext);
95 }
96 return ret;
97 }
98
99 public static Object doInvoke(DynamicProxy pvDynamicProxy, Object pvProxy, Method pvMethod, Object[] pvArgs, Object pvElseObject) throws Exception {
100 return doInvoke(pvDynamicProxy, pvProxy, pvMethod, pvArgs, pvElseObject, new InterceptorHandler());
101 }
102
103 public static Object doInvoke(DynamicProxy pvDynamicProxy, Object pvProxy, Method pvMethod, Object[] pvArgs, InterceptorHandler pvInterceptorHandler) throws Exception {
104 return doInvoke(pvDynamicProxy, pvProxy, pvMethod, pvArgs, null, pvInterceptorHandler);
105 }
106
107 public static Object doInvoke(DynamicProxy pvDynamicProxy, Object pvProxy, Method pvMethod, Object[] pvArgs) throws Exception {
108 return doInvoke(pvDynamicProxy, pvProxy, pvMethod, pvArgs, null, new InterceptorHandler());
109 }
110
111 }