View Javadoc

1   package net.sf.dobo;
2   
3   import net.sf.cglib.proxy.Enhancer;
4   import net.sf.cglib.proxy.MethodInterceptor;
5   import net.sf.cglib.proxy.MethodProxy;
6   
7   import java.lang.annotation.Annotation;
8   import java.lang.reflect.InvocationTargetException;
9   import java.lang.reflect.Method;
10  
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  
15  /***
16   * Context Implementation Object Proxy.
17   *
18   * @author arif
19   * @version 1.0
20   */
21  public class ContextImplementationObjectProxyCglib implements MethodInterceptor {
22      /***
23       * Method buffer will buffer the binding between context interface and
24       * context implementation object
25       */
26      private Map<Method, Method> contextInterfaceToContextImplementationObject = new HashMap<Method, Method>();
27  
28      /***
29       * Context object
30       */
31      private Class<?extends Annotation> context;
32  
33      /***
34       * Context Implementation of target object
35       */
36      private Object contextImplementationObject;
37  
38      /***
39       * Factory method creating instance
40       *
41       * @param contextImplementationObject
42       * @param context
43       *
44       * @return proxy instance
45       */
46      public static Object newInstance(final Object contextImplementationObject,
47          final Class<?extends Annotation> context) {
48          try {
49              Dobo.check(contextImplementationObject.getClass());
50  
51              ContextImplementationObjectProxyCglib interceptor = new ContextImplementationObjectProxyCglib();
52              interceptor.context = context;
53              interceptor.contextImplementationObject = contextImplementationObject;
54  
55              Class<?> contextInterface = context.getAnnotation(Context.class).value();
56              Enhancer e = new Enhancer();
57              e.setSuperclass(contextInterface);
58              e.setCallback(interceptor);
59  
60              Object bean = e.create();
61  
62              return bean;
63          } catch (final Throwable e) {
64              e.printStackTrace();
65              throw new Error(e.getMessage());
66          }
67      }
68  
69      /***
70       * @see net.sf.dobo.cglib.proxy.MethodInterceptor#intercept(java.lang.Object,
71       *      java.lang.reflect.Method, java.lang.Object[],
72       *      net.sf.dobo.cglib.proxy.MethodProxy)
73       */
74      public Object intercept(final Object obj, final Method m, final Object[] args,
75          final MethodProxy proxy) throws Throwable {
76          Object retValFromSuper = null;
77  
78          try {
79              if (!contextInterfaceToContextImplementationObject.containsKey(m)) {
80                  Class<?extends Annotation> markedAnnotation = Dobo.getContextMemberMatchWith(context,
81                          m);
82  
83                  if (markedAnnotation != null) {
84                      Method method = Dobo.getMethodAnnotatedWith(markedAnnotation,
85                              contextImplementationObject.getClass());
86                      contextInterfaceToContextImplementationObject.put(m, method);
87                  } else {
88                      contextInterfaceToContextImplementationObject.put(m, m);
89                  }
90              }
91  
92              Method targetMethod = contextInterfaceToContextImplementationObject.get(m);
93              retValFromSuper = (targetMethod != m)
94                  ? targetMethod.invoke(contextImplementationObject, args)
95                  : proxy.invokeSuper(obj, args);
96          } catch (final InvocationTargetException e) {
97              throw e.getTargetException();
98          } catch (final Exception e) {
99              e.printStackTrace();
100         }
101 
102         return retValFromSuper;
103     }
104 }