KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > aop > CollectionInterceptorUtil


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.aop;
8
9 import org.jboss.aop.*;
10 import org.jboss.aop.advice.Interceptor;
11 import org.jboss.aop.joinpoint.MethodInvocation;
12 import org.jboss.aop.joinpoint.Invocation;
13 import org.jboss.aop.proxy.ClassProxy;
14 import org.jboss.aop.proxy.ClassProxyFactory;
15 import org.jboss.logging.Logger;
16 import org.jboss.aop.util.MethodHashing;
17 import org.jboss.util.NestedRuntimeException;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * CollectionInterceptorUtil contains helper methods for the interceptors of
27  * the different collection types.
28  *
29  * @author <a HREF="mailto:harald@gliebe.de">Harald Gliebe</a>
30  * @authro Ben Wang
31  */

32 public class CollectionInterceptorUtil
33 {
34    static Logger logger_ = Logger.getLogger(CollectionInterceptorUtil.class.getName());
35
36    public static ClassProxy createProxy(Class JavaDoc clazz, Interceptor interceptor)
37          throws Exception JavaDoc
38    {
39       ClassProxy result = ClassProxyFactory.newInstance(clazz);
40       InstanceAdvisor advisor = result._getInstanceAdvisor();
41       advisor.appendInterceptor(interceptor);
42       return result;
43    }
44
45    public static Map JavaDoc getMethodMap(Class JavaDoc clazz)
46    {
47       Map JavaDoc result = ClassProxyFactory.getMethodMap(clazz.getName());
48       if (result == null) {
49          try {
50             ClassProxyFactory.newInstance(clazz);
51          } catch (Exception JavaDoc e) {
52             throw new NestedRuntimeException(e);
53          }
54          result = ClassProxyFactory.getMethodMap(clazz.getName());
55       }
56       return result;
57    }
58
59    public static Map JavaDoc getManagedMethods(Class JavaDoc clazz)
60    {
61       Method JavaDoc tostring = null;
62       try {
63          tostring = Object JavaDoc.class.getDeclaredMethod("toString", new Class JavaDoc[0]);
64       } catch (NoSuchMethodException JavaDoc e) {
65          e.printStackTrace();
66          throw new NestedRuntimeException("getManagedMathods: " +e);
67       }
68
69       Map JavaDoc managedMethods = new HashMap JavaDoc();
70       try {
71          Method JavaDoc[] methods = clazz.getDeclaredMethods();
72          for (int i = 0; i < methods.length; i++) {
73             long hash = MethodHashing.methodHash(methods[i]);
74             managedMethods.put(new Long JavaDoc(hash), methods[i]);
75          }
76          // Add toString to ManagedMethod
77
long hash = MethodHashing.methodHash(tostring);
78          managedMethods.put(new Long JavaDoc(hash), tostring);
79       } catch (Exception JavaDoc ignored) {
80          ignored.printStackTrace();
81       }
82       return managedMethods;
83    }
84
85    public static Object JavaDoc invoke(Invocation invocation,
86                                Object JavaDoc interceptor,
87                                Map JavaDoc methodMap,
88                                Map JavaDoc managedMethods)
89          throws Throwable JavaDoc
90    {
91
92       if (invocation instanceof MethodInvocation) {
93          MethodInvocation methodInvocation = (MethodInvocation) invocation;
94          Long JavaDoc methodHash = new Long JavaDoc(methodInvocation.getMethodHash());
95          Method JavaDoc method = (Method JavaDoc) managedMethods.get(methodHash);
96          if (logger_.isDebugEnabled() && method != null) {
97             logger_.trace("invoke(): method intercepted " + method.getName());
98          }
99          Object JavaDoc[] args = methodInvocation.getArguments();
100          if (method != null) {
101             return method.invoke(interceptor, args);
102          } else {
103             method = methodInvocation.getMethod();
104             if (method == null) {
105                method = (Method JavaDoc) methodMap.get(methodHash);
106             }
107
108             logger_.trace("invke(): invoke non-managed method: " +method.toString());
109             Object JavaDoc target = methodInvocation.getTargetObject();
110             if(target == null) {
111                throw new NestedRuntimeException("CollectionInterceptorUtil.invoke(): targetObject is null." +
112                      " Can't invoke " +method.toString());
113             }
114             return method.invoke(target, args);
115 // return method.invoke(interceptor, args);
116
}
117       }
118       return invocation.invokeNext();
119    }
120
121 }
122
Popular Tags