KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > connector > AbstractHandler


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4 package org.jfox.ioc.connector;
5
6 import java.lang.reflect.Method JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.jfox.ioc.common.AbstractComponent;
12 import org.jfox.ioc.ext.ActiveComponent;
13 import org.jfox.ioc.ext.SingletonComponent;
14 import org.jfox.ioc.util.Classes;
15 import org.jfox.ioc.util.Methods;
16
17 /**
18  * 每个Module必须在自己的Module内实现一个 HandlerSupport 的子类,
19  * 因为需要设置当前线程上下文类装载起为该Handler的装载器
20  *
21  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
22  */

23
24 public abstract class AbstractHandler extends AbstractComponent implements Handler, ActiveComponent, SingletonComponent {
25
26     protected Container container;
27
28     protected Map JavaDoc<String JavaDoc, Method JavaDoc> methodHashers = new HashMap JavaDoc<String JavaDoc, Method JavaDoc>();
29
30     public AbstractHandler(Container container) {
31         this.container = container;
32         if(!container.getHandlerClass().isAssignableFrom(this.getClass())){
33             throw new RuntimeException JavaDoc("container " + container + " 's handler must be " + container.getHandlerClass().getName() + ", can not be " + this.getClass().getName());
34         }
35         methodHashers.putAll(introspectorMethods());
36     }
37
38     /**
39      * Handler 本身就是 Container
40      */

41     public AbstractHandler(){
42         if(!(this instanceof Container)){
43             throw new IllegalArgumentException JavaDoc("Handler " + this.getClass().getName() + " not implement " + Container.class.getName());
44         }
45         container = (Container)this;
46     }
47
48     public Object JavaDoc execute(Invocation invocation) throws Throwable JavaDoc {
49         if(!isInitialized()) {
50             throw new Exception JavaDoc("Handler not started.");
51         }
52         String JavaDoc hash = invocation.getMethodHash();
53         Method JavaDoc method = getMethodByHash(hash);
54         invocation.setMethod(method);
55         if(method == null) {
56             throw new NoSuchMethodException JavaDoc(hash);
57         }
58
59         ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
60         /**
61          * 需要设置成 Module 的 LocalClassLoader,
62          * 否则 Module 里面没有预加载的类会找不到
63          */

64         ClassLoader JavaDoc cl = this.getClass().getClassLoader();
65         Thread.currentThread().setContextClassLoader(cl);
66         try {
67             return doExecute(invocation);
68         }
69         finally {
70             Thread.currentThread().setContextClassLoader(oldCl);
71         }
72     }
73
74     /**
75      * 反射调用其Container的方法
76      * @param invocation
77      * @return
78      * @throws Throwable
79      */

80     protected Object JavaDoc doExecute(Invocation invocation) throws Throwable JavaDoc {
81         logger.debug(invocation);
82         return invocation.getMethod().invoke(container, invocation.getArgs());
83
84     }
85
86     protected void doInit() throws Exception JavaDoc {
87         HandlerManager.getInstance().registerHandler(getInvocationClass(), this);
88     }
89
90     protected void doDestroy() throws Exception JavaDoc {
91         HandlerManager.getInstance().removeHandler(getInvocationClass());
92     }
93
94     public Container getContainer() {
95         return container;
96     }
97
98     /**
99      * 获得Container所有接口的方法
100      */

101     protected Map JavaDoc<String JavaDoc, Method JavaDoc> introspectorMethods() {
102         Map JavaDoc<String JavaDoc, Method JavaDoc> methodHashers = new HashMap JavaDoc<String JavaDoc, Method JavaDoc>();
103         // cache 所有接口的方法
104
List JavaDoc<Class JavaDoc> clzes = Classes.getAllInterfaces(getContainer().getClass());
105         for(Class JavaDoc interf : clzes) {
106             List JavaDoc<Method JavaDoc> methods = Classes.introspectMethods(interf);
107             for(Method JavaDoc method : methods) {
108                 methodHashers.put(String.valueOf(Methods.getMethodHash(method)), method);
109             }
110         }
111         return methodHashers;
112     }
113
114     public Method JavaDoc getMethodByHash(String JavaDoc hashId) {
115         return methodHashers.get(hashId);
116     }
117
118 }
119
120
Popular Tags