KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
10  * 用来解析集群间通信,负责解析 ClusterInvocation 的动作
11  *
12  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
13  */

14
15 public class ClusterHandler extends AbstractHandler implements ClusterContainer {
16     /**
17      * ConnectorHandler 控制中心
18      */

19     private transient HandlerManager handlerManager = HandlerManager.getInstance();
20
21     public ClusterHandler() {
22
23     }
24
25     public Class JavaDoc getInvocationClass() {
26         return ClusterInvocation.class;
27     }
28
29     public Object JavaDoc execute(Invocation invocation) throws Throwable JavaDoc {
30         ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
31         try {
32             sync(invocation);
33         }
34         finally{
35             Thread.currentThread().setContextClassLoader(oldCl);
36         }
37         return null;
38     }
39
40     public void sync(Invocation invocation) {
41
42         // 不用判断自己节点的情况,因为ClusterServer已经过滤了
43
ClusterInvocation clusterInvocation = (ClusterInvocation)invocation;
44         Invocation innerInvocation = clusterInvocation.getInvocation();
45         Handler handler = handlerManager.getHandler(innerInvocation.getClass());
46
47         Container container = handler.getContainer();
48         if(!(container instanceof ClusterableContainer)){
49             logger.warn("Invocation " + innerInvocation.getClass().getName() + " 's Container " + container.getClass().getName() + " is not a ClusterableContainer.");
50             return;
51         }
52         innerInvocation.setMethod(handler.getMethodByHash(innerInvocation.getMethodHash()));
53
54
55         /**
56          * 应该使用其Handler的ClassLoader
57          */

58         ClassLoader JavaDoc oldCL = Thread.currentThread().getContextClassLoader();
59         ClassLoader JavaDoc cl = handler.getClass().getClassLoader();
60         try {
61             Thread.currentThread().setContextClassLoader(cl);
62             ((ClusterableContainer) container).syncInvocation(innerInvocation);
63         }
64         finally{
65             Thread.currentThread().setContextClassLoader(oldCL);
66         }
67
68     }
69
70     public Class JavaDoc getHandlerClass() {
71         return this.getClass();
72     }
73
74 }
75
Popular Tags