KickJava   Java API By Example, From Geeks To Geeks.

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


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.connector;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.jfox.ioc.Registry;
16 import org.jfox.ioc.common.AbstractComponent;
17 import org.jfox.ioc.ext.ActiveComponent;
18 import org.jfox.ioc.ext.ManagableComponent;
19 import org.jfox.ioc.ext.SingletonComponent;
20
21 /**
22  * 总的Handler,各个协议的Handler注册到这里,ConnectorRemote只调用这个Handler
23  *
24  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
25  */

26
27 public class HandlerManager extends AbstractComponent implements ActiveComponent,ManagableComponent, SingletonComponent {
28     /**
29      * invocation class => Handler
30      */

31     private Map JavaDoc<Class JavaDoc, Handler> handlers = new HashMap JavaDoc<Class JavaDoc, Handler>();
32
33     protected Registry registry;
34
35     private final static HandlerManager ME = new HandlerManager();
36
37     private HandlerManager() {
38     }
39
40     public static HandlerManager getInstance(){
41         return ME;
42     }
43
44     /**
45      * 注册一个应用协议的响应器
46      * ConnectorService会根据不同的应用协议调用不同的响应器,
47      * 这样一个Connector可以支持多个应用协议的调用
48      * 比如:既可以响应EJB请求,也可以响应 JMS 请求
49      * @param invocationType must be a Invocation class
50      * @param handler
51      */

52     public synchronized void registerHandler(Class JavaDoc invocationType, Handler handler) {
53         if(Invocation.class.isAssignableFrom(invocationType)){
54             handlers.put(invocationType, handler);
55         }
56     }
57
58     public synchronized void removeHandler(Class JavaDoc invocationType) {
59         handlers.remove(invocationType);
60     }
61
62     public synchronized Handler getHandler(Class JavaDoc invocationType) {
63         return handlers.get(invocationType);
64     }
65
66     public List JavaDoc<Handler> listHandlers() {
67         return Collections.unmodifiableList(new ArrayList JavaDoc<Handler>(handlers.values()));
68     }
69
70     public Object JavaDoc execute(Invocation invocation) throws Throwable JavaDoc {
71         logger.debug(invocation);
72         Class JavaDoc invocationClass = invocation.getClass();
73         Handler handler = getHandler(invocationClass);
74         if(handler == null) {
75             throw new Exception JavaDoc("no handler response for invocation " + invocation);
76         }
77         Object JavaDoc result = handler.execute(invocation);
78
79         try {
80             //如果不是ClusterInvocation,才进行同步,因为ClusterInvocation本身就是用来同步的调用
81
if(!(invocation instanceof ClusterInvocation) && invocation.isClustable()){
82                 try {
83                     // 通过 IoC 获得的ConnectorServer
84
// ConnectorServer connectorServer = (ConnectorServer) context.getRegistry().getComponentInstance(ConnectorServer.class);
85
ClusterServer clusterServer = ConnectorServer.getClusterServer();
86                     if(clusterServer != null){ // clusterServer 存在
87
ClusterInvocation clusterInvocation = new ClusterInvocation(invocation,ServerNode.THE_NODE);
88                         clusterServer.cast(clusterInvocation);
89                     }
90                 }
91                 catch(Exception JavaDoc e) {
92                     logger.warn("cluster cast error!",e);
93                 }
94             }
95         }
96         finally{
97             return result;
98         }
99
100     }
101
102     protected void doInit() throws Exception JavaDoc {
103         registry = context.getRegistry();
104     }
105
106     protected void doDestroy() throws Exception JavaDoc {
107     }
108 }
109
Popular Tags