1 6 7 package org.jfox.ioc.connector; 8 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.Map ; 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 26 27 public class HandlerManager extends AbstractComponent implements ActiveComponent,ManagableComponent, SingletonComponent { 28 31 private Map <Class , Handler> handlers = new HashMap <Class , 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 52 public synchronized void registerHandler(Class invocationType, Handler handler) { 53 if(Invocation.class.isAssignableFrom(invocationType)){ 54 handlers.put(invocationType, handler); 55 } 56 } 57 58 public synchronized void removeHandler(Class invocationType) { 59 handlers.remove(invocationType); 60 } 61 62 public synchronized Handler getHandler(Class invocationType) { 63 return handlers.get(invocationType); 64 } 65 66 public List <Handler> listHandlers() { 67 return Collections.unmodifiableList(new ArrayList <Handler>(handlers.values())); 68 } 69 70 public Object execute(Invocation invocation) throws Throwable { 71 logger.debug(invocation); 72 Class invocationClass = invocation.getClass(); 73 Handler handler = getHandler(invocationClass); 74 if(handler == null) { 75 throw new Exception ("no handler response for invocation " + invocation); 76 } 77 Object result = handler.execute(invocation); 78 79 try { 80 if(!(invocation instanceof ClusterInvocation) && invocation.isClustable()){ 82 try { 83 ClusterServer clusterServer = ConnectorServer.getClusterServer(); 86 if(clusterServer != null){ ClusterInvocation clusterInvocation = new ClusterInvocation(invocation,ServerNode.THE_NODE); 88 clusterServer.cast(clusterInvocation); 89 } 90 } 91 catch(Exception 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 { 103 registry = context.getRegistry(); 104 } 105 106 protected void doDestroy() throws Exception { 107 } 108 } 109 | Popular Tags |