1 6 7 package org.jfox.ejb.handler; 8 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import javax.ejb.EJBException ; 14 15 import org.jfox.ejb.Bucket; 16 import org.jfox.ejb.connector.EJBInvocation; 17 import org.jfox.ioc.common.AbstractComponent; 18 import org.jfox.ioc.ext.ManagableComponent; 19 20 25 26 public class HandlerChain extends AbstractComponent implements ManagableComponent { 27 protected List invokers = new ArrayList (); 28 29 public HandlerChain(String [] handlers) { 30 for(int i = 0; i < handlers.length; i++) { 31 addHandler(handlers[i]); 32 } 33 } 34 35 public static Object nextInvokeHome(Bucket bucket, EJBInvocation invocation, Iterator iter) throws Exception { 36 if(iter.hasNext()) { 37 return ((Handler) iter.next()).invokeHome(bucket, invocation, iter); 38 } 39 else { 40 throw new EJBException ("No Invoker"); 41 } 42 } 43 44 public static Object nextInvokeBean(Bucket bucket, EJBInvocation invocation, Iterator iter) throws Exception { 45 if(iter.hasNext()) { 46 return ((Handler) iter.next()).invokeBean(bucket, invocation, iter); 47 } 48 else { 49 throw new EJBException ("No Invoker"); 50 } 51 } 52 53 public synchronized void addHandler(Handler invoker) { 54 for(int i = 0; i < invokers.size(); i++) { 55 if(invokers.get(i).getClass().getName().equals(invoker.getClass().getName())) { 57 return; 58 } 59 } 60 invokers.add(invoker); 61 Collections.sort(invokers); 62 } 63 64 public synchronized void addHandler(String handler) { 65 try { 66 Object obj = this.getClass().getClassLoader().loadClass(handler).newInstance(); 67 if(obj instanceof Handler) { 68 addHandler((Handler) obj); 69 } 70 else { 71 logger.warn(handler + " not implements " + Handler.class.getName() + ", ignore it."); 72 } 73 } 74 catch(ClassNotFoundException e) { 75 logger.warn("can not load class " + handler, e); 76 } 77 catch(InstantiationException e) { 78 logger.warn("instantate class " + handler + " error", e); 79 } 80 catch(IllegalAccessException e) { 81 logger.warn(e); 82 } 83 } 84 85 public synchronized void removeHandler(String invokerClassName) { 86 for(int i = 0; i < invokers.size(); i++) { 87 if(invokers.get(i).getClass().getName().equals(invokerClassName)) { 88 invokers.remove(i); 89 return; 90 } 91 } 92 } 93 94 public String [] listInvokers() { 95 List invokerStringArray = new ArrayList (); 96 for(int i = 0; i < invokers.size(); i++) { 97 invokerStringArray.add(invokers.get(i).getClass().getName()); 98 } 99 return (String []) invokerStringArray.toArray(new String []{}); 100 } 101 102 protected void doInit() throws Exception { 103 } 104 105 protected void doDestroy() throws Exception { 106 invokers.clear(); 107 } 108 109 public Object invokeHome(Bucket bucket, EJBInvocation invocation) throws Exception { 110 Iterator iter = invokers.iterator(); 111 if(iter.hasNext()) { 112 return ((Handler) iter.next()).invokeHome(bucket, invocation, iter); 113 } 114 else { 115 throw new EJBException ("No Invoker"); 116 } 117 } 118 119 public Object invokeBean(Bucket bucket, EJBInvocation invocation) throws Exception { 120 Iterator iter = invokers.iterator(); 121 if(iter.hasNext()) { 122 return ((org.jfox.ejb.handler.Handler) iter.next()).invokeBean(bucket, invocation, iter); 123 } 124 else { 125 throw new EJBException ("No Invoker"); 126 } 127 } 128 } | Popular Tags |