Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/ghettojedi/aop/aspect/Delegator.java


1   package com.ghettojedi.aop.aspect;
2   
3   import com.ghettojedi.common.ExceptionAdapter;
4   
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  public class Delegator {
11      private static Delegator INSTANCE = new Delegator();
12  
13      public static Delegator get() {
14          return INSTANCE;
15      }
16  
17      public static String getHiddenMethodName(String methodName) {
18          return "___" + methodName;
19      }
20  
21      private Map interceptorTypesToInterceptors = new HashMap();
22      private Map classesToInterceptorStacks = new HashMap();
23  
24      private Delegator() {
25      }
26  
27      public Object doInvoke(Object target, String name, Class[] parameterTypes, Object[] arguments) throws Throwable {
28          List interceptors = getInterceptorStack(target.getClass());
29          Invocation invocation = new Invocation(interceptors, target, name, parameterTypes, arguments);
30          return invocation.proceed();
31      }
32  
33      public void addInterceptor(Class type, Class interceptorType) {
34          if (!Interceptor.class.isAssignableFrom(interceptorType)) {
35              throw new IllegalArgumentException("Supplied interceptor type is not an Interceptor: " + interceptorType.getName());
36          }
37          Interceptor interceptor = getInterceptor(interceptorType);
38          getInterceptorStack(type).add(interceptor);
39      }
40  
41      public void removeInterceptor(Class type, Class interceptorType) {
42          getInterceptorStack(type).remove(interceptorTypesToInterceptors.get(interceptorType.getName()));
43      }
44  
45      private List getInterceptorStack(Class type) {
46          List interceptors = (List) classesToInterceptorStacks.get(type);
47          if (interceptors == null) {
48              interceptors = new ArrayList();
49              classesToInterceptorStacks.put(type, interceptors);
50          }
51          return interceptors;
52      }
53  
54      private Interceptor getInterceptor(Class interceptorType) {
55          try {
56              Interceptor interceptor = (Interceptor) interceptorTypesToInterceptors.get(interceptorType);
57              if (interceptor == null) {
58                  interceptor = (Interceptor) interceptorType.newInstance();
59                  interceptorTypesToInterceptors.put(interceptorType, interceptor);
60              }
61              return interceptor;
62          } catch (Exception e) {
63              throw new ExceptionAdapter(e);
64          }
65      }
66  }