KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ejb > connector > EJBContainerImpl


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4
5 package org.jfox.ejb.connector;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.transaction.TransactionManager JavaDoc;
14
15 import org.jfox.ejb.Bucket;
16 import org.jfox.ejb.BucketMeta;
17 import org.jfox.ejb.BucketSupport;
18 import org.jfox.ejb.EJBObjectId;
19 import org.jfox.ejb.MDBBucket;
20 import org.jfox.ejb.StatefulBucket;
21 import org.jfox.ejb.StatelessBucket;
22 import org.jfox.ejb.handler.Interceptor;
23 import org.jfox.ejb.meta.MDBDescriptor;
24 import org.jfox.ioc.ComponentName;
25 import org.jfox.ioc.connector.AbstractContainer;
26 import org.jfox.ioc.depend.DependencyPack;
27 import org.jfox.ioc.depend.ObjectDependency;
28 import org.jfox.ioc.ext.ActiveComponent;
29 import org.jfox.jndi.InitialContextHelper;
30 import org.jfox.tm.TxManager;
31
32 /**
33  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
34  */

35
36 public class EJBContainerImpl extends AbstractContainer implements EJBContainer, ActiveComponent {
37
38     // jndi instance
39
static Context JavaDoc INITIAL_CONTEXT = null;
40
41     // loaded bean buckets
42
private Map JavaDoc buckets = new HashMap JavaDoc();
43
44 // private BucketFactory bucketFactory = BucketFactory.getInstance();
45

46     private List JavaDoc interceptors = new ArrayList JavaDoc();
47
48     public EJBContainerImpl() {
49
50     }
51
52     public Class JavaDoc getHandlerClass() {
53         return EJBHandler.class;
54     }
55
56     public TransactionManager JavaDoc getTransactionManager() {
57         return TxManager.getInstance();
58     }
59
60     public Context JavaDoc getInitialContext() {
61         return INITIAL_CONTEXT;
62     }
63
64     protected void doInit() throws Exception JavaDoc {
65         INITIAL_CONTEXT = InitialContextHelper.getInitialContext();
66     }
67
68     protected synchronized void doDestroy() throws Exception JavaDoc {
69         for(Iterator JavaDoc it = buckets.values().iterator(); it.hasNext();) {
70             ((BucketSupport) it.next()).destroy();
71         }
72         INITIAL_CONTEXT = null;
73         buckets = new HashMap JavaDoc();
74     }
75
76     public Iterator JavaDoc listBuckets() {
77         return buckets.keySet().iterator();
78     }
79
80     /**
81      * create a bean bucket and bind it's home to jndi
82      *
83      * @param meta
84      */

85     public synchronized void createBucket(BucketMeta meta) throws Exception JavaDoc {
86         Bucket bucket = null;
87         if(meta.getEJBMetaData().isSession()) {
88             if(meta.getEJBMetaData().isStatelessSession()) {
89                 bucket = (StatelessBucket)context.getRegistry().getComponentInstance(ComponentName.newInstance(Bucket.class,"Stateless"),
90                                                                                      new DependencyPack[]{
91                                                                                          new DependencyPack("bucketMeta",new ObjectDependency(meta))
92                                                                                      });
93             }
94             else {
95                 bucket = (StatefulBucket) context.getRegistry().getComponentInstance(ComponentName.newInstance(Bucket.class, "Stateful"),
96                                                                                      new DependencyPack[]{
97                                                                                         new DependencyPack("bucketMeta", new ObjectDependency(meta))
98                                                                                      });
99             }
100         }
101         else if(meta.getEJBDescriptor() instanceof MDBDescriptor){
102             bucket = (MDBBucket) context.getRegistry().getComponentInstance(ComponentName.newInstance(Bucket.class,"MDB"),
103                                                                             new DependencyPack[]{
104                                                                                 new DependencyPack("bucketMeta",new ObjectDependency(meta))
105                                                                             });
106         }
107         else {
108             throw new RuntimeException JavaDoc("not support EntityBean now!");
109         }
110 // Bucket bucket = bucketFactory.createBucket(meta);
111
// bucket.init();
112
buckets.put(meta.getEJBDescriptor().getEjbName(), bucket);
113     }
114
115     /**
116      * remove the bucket from container , 发生在 undeploy 一个 bean 的时候
117      */

118     public synchronized void dropBucket(String JavaDoc ejbName) throws Exception JavaDoc {
119         buckets.remove(ejbName);
120         ((Bucket) buckets.get(ejbName)).destroy();
121     }
122
123     /**
124      * container 可能对该方法调用进行一些统一的调控,如:安全验证等,然后 forward 给 bucket 执行
125      *
126      * @return
127      * @throws Exception
128      */

129     public Object JavaDoc invoke(EJBInvocation invocation) throws Exception JavaDoc {
130         logger.debug("invoke " + invocation.toString());
131         EJBObjectId ejbObjectId = (EJBObjectId)invocation.getObjectId();
132         String JavaDoc ejbName = ejbObjectId.getEjbName();
133         Bucket bucket = getBucket(ejbName);
134
135         if(((EJBObjectId)invocation.getObjectId()).isHome()) { // EJBHome 方法
136
invocation.setMethod(bucket.getHomeMethod(invocation));
137             preInvokeHome(invocation);
138             Object JavaDoc result = bucket.invokeHome(invocation);
139             postInvokeHome(invocation);
140             return result;
141         }
142         else { // Bean 方法
143
invocation.setMethod(bucket.getBeanMethod(invocation));
144             preInvokeBean(invocation);
145             Object JavaDoc result = bucket.invokeBean(invocation);
146             postInvokeBean(invocation);
147             return result;
148         }
149
150     }
151
152     public void addInterceptor(Interceptor interceptor) throws Exception JavaDoc {
153         interceptor.setContainer(this);
154         synchronized(interceptors) {
155             interceptors.add(interceptor);
156         }
157     }
158
159     public synchronized Iterator JavaDoc interceptors() {
160         return interceptors.iterator();
161     }
162
163     public Bucket getBucket(String JavaDoc ejbName) {
164         return (Bucket) buckets.get(ejbName);
165     }
166
167     private void preInvokeHome(EJBInvocation invocation) throws Exception JavaDoc {
168         for(Iterator JavaDoc it = this.interceptors(); it.hasNext();) {
169             Interceptor interceptor = (Interceptor) it.next();
170             interceptor.preInvokeHome(invocation);
171         }
172     }
173
174     private void preInvokeBean(EJBInvocation invocation) throws Exception JavaDoc {
175         for(Iterator JavaDoc it = this.interceptors(); it.hasNext();) {
176             Interceptor interceptor = (Interceptor) it.next();
177             interceptor.preInvokeBean(invocation);
178         }
179     }
180
181     private void postInvokeHome(EJBInvocation invocation) throws Exception JavaDoc {
182         for(Iterator JavaDoc it = this.interceptors(); it.hasNext();) {
183             Interceptor interceptor = (Interceptor) it.next();
184             interceptor.preInvokeHome(invocation);
185         }
186     }
187
188     private void postInvokeBean(EJBInvocation invocation) throws Exception JavaDoc {
189         for(Iterator JavaDoc it = this.interceptors(); it.hasNext();) {
190             Interceptor interceptor = (Interceptor) it.next();
191             interceptor.preInvokeBean(invocation);
192         }
193     }
194
195     public static void main(String JavaDoc[] args) {
196
197     }
198 }
199
Popular Tags