KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > factory > DynProxyComponentFactory


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.factory;
8
9 import java.lang.reflect.InvocationHandler JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.lang.reflect.Proxy JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.jfox.ioc.Component;
17 import org.jfox.ioc.ComponentName;
18 import org.jfox.ioc.annotation.Interceptable;
19 import org.jfox.ioc.depend.Dependency;
20 import org.jfox.ioc.exception.ComponentException;
21 import org.jfox.ioc.ext.InterceptableComponent;
22 import org.jfox.ioc.util.Annotations;
23 import org.jfox.ioc.util.Classes;
24
25 /**
26  * 如果要使用{@link org.jfox.ioc.ext.InterceptableComponent}功能,
27  * 必须使用该ComponentFactory,使得在生成组件实例的时候,返回的是该实例的动态代理对象
28  *
29  * @see org.jfox.ioc.ext.InterceptableComponent
30  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
31  */

32
33 public class DynProxyComponentFactory extends ConstrComponentFactory {
34
35     public DynProxyComponentFactory(ComponentName name, Class JavaDoc implementation, Dependency[] params) throws ComponentException {
36         super(name,implementation,params);
37     }
38
39     /**
40      * 在生成组件实例的时候,返回的是该实例的动态代理对象
41      * 返回的代理对象可以造型成组件类实现的所有的接口
42      *
43      * @return 实例的动态代理对象
44      * @throws org.jfox.ioc.exception.ComponentException
45      */

46     public Component makeComponent() throws ComponentException {
47         Component comp = super.makeComponent();
48         return (Component) Proxy.newProxyInstance(getImplementation().getClassLoader(),
49                 getInterfaces(comp.getClass()),
50                 new DPInvocationHandler(comp));
51     }
52
53     /**
54      * 该InvocationHandler负责在执行组件的办法前后调用 {@link org.jfox.ioc.ext.InterceptableComponent}
55      * 的 {@link org.jfox.ioc.ext.InterceptableComponent#preInvoke(java.lang.reflect.Method, Object[])}
56      * 和 {@link org.jfox.ioc.ext.InterceptableComponent#postInvoke(java.lang.reflect.Method, Object)}
57      * 方法
58      */

59     private class DPInvocationHandler implements InvocationHandler JavaDoc {
60         private Component comp = null;
61         private boolean isInterceptable = false;
62
63         private Map JavaDoc<Method JavaDoc,Boolean JavaDoc> methods = new HashMap JavaDoc<Method JavaDoc, Boolean JavaDoc>();
64
65         public DPInvocationHandler(Component comp) {
66             this.comp = comp;
67             if(comp instanceof InterceptableComponent) {
68                 isInterceptable = true;
69             }
70         }
71
72         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
73             boolean isMethodInterceptable = isMethodInterceptable(method);
74             if(isInterceptable && isMethodInterceptable) {
75                 // 如果该方法被标注为一个 interceptable 的方法
76
if(Annotations.isAnnotated(method,Interceptable.class)) {
77                     ((InterceptableComponent) comp).preInvoke(method, args);
78                 }
79             }
80             Object JavaDoc result = method.invoke(comp, args);
81             if(isInterceptable && isMethodInterceptable) {
82                 // 如果该方法被标注为一个 interceptable 的方法
83
if(Annotations.isAnnotated(method, Interceptable.class)) {
84                     result = ((InterceptableComponent) comp).postInvoke(method, result);
85                 }
86             }
87             return result;
88         }
89
90         private boolean isMethodInterceptable(Method JavaDoc method) throws Exception JavaDoc {
91             if(methods.containsKey(method)) {
92                 return methods.get(method);
93             }
94             else {
95                 // 检查类方法有没有被 @Interceptable
96
Method JavaDoc concreteMethod = comp.getClass().getMethod(method.getName(),method.getParameterTypes());
97                 boolean b = Annotations.isAnnotated(concreteMethod,Interceptable.class);
98                 methods.put(method,b);
99                 return b;
100             }
101         }
102     }
103
104     /**
105      * 得到实现的所有的接口,包括所有超类实现的接口
106      *
107      * @param clazz clazz可以是接口,也可以是实际类
108      */

109     private static Class JavaDoc[] getInterfaces(Class JavaDoc clazz) {
110         List JavaDoc interfaces = Classes.getAllInterfaces(clazz);
111         return (Class JavaDoc[])interfaces.toArray(new Class JavaDoc[interfaces.size()]);
112     }
113
114 }
115
116
Popular Tags