1 4 5 package org.jfox.ejb.connector; 6 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 import javax.naming.Context ; 13 import javax.transaction.TransactionManager ; 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 35 36 public class EJBContainerImpl extends AbstractContainer implements EJBContainer, ActiveComponent { 37 38 static Context INITIAL_CONTEXT = null; 40 41 private Map buckets = new HashMap (); 43 44 46 private List interceptors = new ArrayList (); 47 48 public EJBContainerImpl() { 49 50 } 51 52 public Class getHandlerClass() { 53 return EJBHandler.class; 54 } 55 56 public TransactionManager getTransactionManager() { 57 return TxManager.getInstance(); 58 } 59 60 public Context getInitialContext() { 61 return INITIAL_CONTEXT; 62 } 63 64 protected void doInit() throws Exception { 65 INITIAL_CONTEXT = InitialContextHelper.getInitialContext(); 66 } 67 68 protected synchronized void doDestroy() throws Exception { 69 for(Iterator it = buckets.values().iterator(); it.hasNext();) { 70 ((BucketSupport) it.next()).destroy(); 71 } 72 INITIAL_CONTEXT = null; 73 buckets = new HashMap (); 74 } 75 76 public Iterator listBuckets() { 77 return buckets.keySet().iterator(); 78 } 79 80 85 public synchronized void createBucket(BucketMeta meta) throws Exception { 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 ("not support EntityBean now!"); 109 } 110 buckets.put(meta.getEJBDescriptor().getEjbName(), bucket); 113 } 114 115 118 public synchronized void dropBucket(String ejbName) throws Exception { 119 buckets.remove(ejbName); 120 ((Bucket) buckets.get(ejbName)).destroy(); 121 } 122 123 129 public Object invoke(EJBInvocation invocation) throws Exception { 130 logger.debug("invoke " + invocation.toString()); 131 EJBObjectId ejbObjectId = (EJBObjectId)invocation.getObjectId(); 132 String ejbName = ejbObjectId.getEjbName(); 133 Bucket bucket = getBucket(ejbName); 134 135 if(((EJBObjectId)invocation.getObjectId()).isHome()) { invocation.setMethod(bucket.getHomeMethod(invocation)); 137 preInvokeHome(invocation); 138 Object result = bucket.invokeHome(invocation); 139 postInvokeHome(invocation); 140 return result; 141 } 142 else { invocation.setMethod(bucket.getBeanMethod(invocation)); 144 preInvokeBean(invocation); 145 Object result = bucket.invokeBean(invocation); 146 postInvokeBean(invocation); 147 return result; 148 } 149 150 } 151 152 public void addInterceptor(Interceptor interceptor) throws Exception { 153 interceptor.setContainer(this); 154 synchronized(interceptors) { 155 interceptors.add(interceptor); 156 } 157 } 158 159 public synchronized Iterator interceptors() { 160 return interceptors.iterator(); 161 } 162 163 public Bucket getBucket(String ejbName) { 164 return (Bucket) buckets.get(ejbName); 165 } 166 167 private void preInvokeHome(EJBInvocation invocation) throws Exception { 168 for(Iterator 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 { 175 for(Iterator 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 { 182 for(Iterator 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 { 189 for(Iterator it = this.interceptors(); it.hasNext();) { 190 Interceptor interceptor = (Interceptor) it.next(); 191 interceptor.preInvokeBean(invocation); 192 } 193 } 194 195 public static void main(String [] args) { 196 197 } 198 } 199 | Popular Tags |