1 4 package org.jfox.ioc.connector; 5 6 import java.lang.reflect.Method ; 7 import java.util.HashMap ; 8 import java.util.List ; 9 import java.util.Map ; 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 23 24 public abstract class AbstractHandler extends AbstractComponent implements Handler, ActiveComponent, SingletonComponent { 25 26 protected Container container; 27 28 protected Map <String , Method > methodHashers = new HashMap <String , Method >(); 29 30 public AbstractHandler(Container container) { 31 this.container = container; 32 if(!container.getHandlerClass().isAssignableFrom(this.getClass())){ 33 throw new RuntimeException ("container " + container + " 's handler must be " + container.getHandlerClass().getName() + ", can not be " + this.getClass().getName()); 34 } 35 methodHashers.putAll(introspectorMethods()); 36 } 37 38 41 public AbstractHandler(){ 42 if(!(this instanceof Container)){ 43 throw new IllegalArgumentException ("Handler " + this.getClass().getName() + " not implement " + Container.class.getName()); 44 } 45 container = (Container)this; 46 } 47 48 public Object execute(Invocation invocation) throws Throwable { 49 if(!isInitialized()) { 50 throw new Exception ("Handler not started."); 51 } 52 String hash = invocation.getMethodHash(); 53 Method method = getMethodByHash(hash); 54 invocation.setMethod(method); 55 if(method == null) { 56 throw new NoSuchMethodException (hash); 57 } 58 59 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 60 64 ClassLoader 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 80 protected Object doExecute(Invocation invocation) throws Throwable { 81 logger.debug(invocation); 82 return invocation.getMethod().invoke(container, invocation.getArgs()); 83 84 } 85 86 protected void doInit() throws Exception { 87 HandlerManager.getInstance().registerHandler(getInvocationClass(), this); 88 } 89 90 protected void doDestroy() throws Exception { 91 HandlerManager.getInstance().removeHandler(getInvocationClass()); 92 } 93 94 public Container getContainer() { 95 return container; 96 } 97 98 101 protected Map <String , Method > introspectorMethods() { 102 Map <String , Method > methodHashers = new HashMap <String , Method >(); 103 List <Class > clzes = Classes.getAllInterfaces(getContainer().getClass()); 105 for(Class interf : clzes) { 106 List <Method > methods = Classes.introspectMethods(interf); 107 for(Method method : methods) { 108 methodHashers.put(String.valueOf(Methods.getMethodHash(method)), method); 109 } 110 } 111 return methodHashers; 112 } 113 114 public Method getMethodByHash(String hashId) { 115 return methodHashers.get(hashId); 116 } 117 118 } 119 120 | Popular Tags |