KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ejb > handler > HandlerChain


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.ejb.handler;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import javax.ejb.EJBException JavaDoc;
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 /**
21  * 对于一个 Bean 的调用链,Bucket 利用调用链来完成具体的操作
22  *
23  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
24  */

25
26 public class HandlerChain extends AbstractComponent implements ManagableComponent {
27     protected List JavaDoc invokers = new ArrayList JavaDoc();
28
29     public HandlerChain(String JavaDoc[] handlers) {
30         for(int i = 0; i < handlers.length; i++) {
31             addHandler(handlers[i]);
32         }
33     }
34
35     public static Object JavaDoc nextInvokeHome(Bucket bucket, EJBInvocation invocation, Iterator JavaDoc iter) throws Exception JavaDoc {
36         if(iter.hasNext()) {
37             return ((Handler) iter.next()).invokeHome(bucket, invocation, iter);
38         }
39         else {
40             throw new EJBException JavaDoc("No Invoker");
41         }
42     }
43
44     public static Object JavaDoc nextInvokeBean(Bucket bucket, EJBInvocation invocation, Iterator JavaDoc iter) throws Exception JavaDoc {
45         if(iter.hasNext()) {
46             return ((Handler) iter.next()).invokeBean(bucket, invocation, iter);
47         }
48         else {
49             throw new EJBException JavaDoc("No Invoker");
50         }
51     }
52
53     public synchronized void addHandler(Handler invoker) {
54         for(int i = 0; i < invokers.size(); i++) {
55             // already have
56
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 JavaDoc handler) {
65         try {
66             Object JavaDoc 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 JavaDoc e) {
75             logger.warn("can not load class " + handler, e);
76         }
77         catch(InstantiationException JavaDoc e) {
78             logger.warn("instantate class " + handler + " error", e);
79         }
80         catch(IllegalAccessException JavaDoc e) {
81             logger.warn(e);
82         }
83     }
84
85     public synchronized void removeHandler(String JavaDoc 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 JavaDoc[] listInvokers() {
95         List JavaDoc invokerStringArray = new ArrayList JavaDoc();
96         for(int i = 0; i < invokers.size(); i++) {
97             invokerStringArray.add(invokers.get(i).getClass().getName());
98         }
99         return (String JavaDoc[]) invokerStringArray.toArray(new String JavaDoc[]{});
100     }
101
102     protected void doInit() throws Exception JavaDoc {
103     }
104
105     protected void doDestroy() throws Exception JavaDoc {
106         invokers.clear();
107     }
108
109     public Object JavaDoc invokeHome(Bucket bucket, EJBInvocation invocation) throws Exception JavaDoc {
110         Iterator JavaDoc iter = invokers.iterator();
111         if(iter.hasNext()) {
112             return ((Handler) iter.next()).invokeHome(bucket, invocation, iter);
113         }
114         else {
115             throw new EJBException JavaDoc("No Invoker");
116         }
117     }
118
119     public Object JavaDoc invokeBean(Bucket bucket, EJBInvocation invocation) throws Exception JavaDoc {
120         Iterator JavaDoc 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 JavaDoc("No Invoker");
126         }
127     }
128  }
Popular Tags