1 6 7 package org.jfox.ejb; 8 9 import java.lang.reflect.Proxy ; 10 import java.rmi.RemoteException ; 11 import javax.ejb.EJBObject ; 12 import javax.ejb.EnterpriseBean ; 13 import javax.ejb.Handle ; 14 import javax.ejb.RemoveException ; 15 import javax.ejb.SessionBean ; 16 17 import org.jfox.ejb.connector.EJBInvocation; 18 import org.jfox.ioc.ComponentName; 19 import org.jfox.ioc.connector.ConnectorInvoker; 20 import org.jfox.ioc.depend.DependencyPack; 21 import org.jfox.ioc.depend.ObjectDependency; 22 23 26 27 public class StatefulBucket extends SessionBucket { 28 29 private StatefulObjectPool instancePool = null; 30 31 public StatefulBucket() { 32 33 } 34 35 public void remove(EnterpriseBean bean) throws RemoteException , RemoveException { 36 ejbRemove((SessionBean ) bean); 37 } 38 39 public void remove(Handle handle) throws RemoteException , RemoveException { 40 EJBObject ejbObject = handle.getEJBObject(); 41 EJBObjectId objectId = (EJBObjectId)(((ConnectorInvoker) Proxy.getInvocationHandler(ejbObject)).getClientId()); 43 try { 44 EJBPoolableObject pobj = (EJBPoolableObject) instancePool.retrieveObject(objectId); 45 remove(pobj.getBeanInstance()); 46 } 47 catch(Exception e) { 48 throw new RemoveException (e.getMessage()); 49 } 50 } 51 52 public boolean isIdentical(EJBObjectId thisObjectId, EJBObjectId thatObjectId) throws RemoteException { 53 return thisObjectId.equals(thatObjectId); 54 } 55 56 public EJBPoolableObject retrieveBean(Object key) { 57 try { 58 return (EJBPoolableObject) instancePool.retrieveObject(key); 59 } 60 catch(Exception e) { 61 e.printStackTrace(); 62 return null; 63 } 64 } 65 66 public void restoreBean(Object key, EJBPoolableObject pobj) { 67 instancePool.restoreObject(key, pobj); 68 } 69 70 public EJBObjectId createBean(EJBInvocation invocation) throws Exception { 71 EJBObjectId beanObjectId = nextObjectId(); 72 EJBPoolableObject pobj = (EJBPoolableObject) instancePool.createObject(); 73 SessionBean bean = (SessionBean ) pobj.getBeanInstance(); 74 setContext(bean, new SessionContextImpl(beanObjectId, bucketMeta)); 75 ejbCreate(bean, invocation.getMethod().getName(), invocation.getArgs()); 76 instancePool.restoreObject(beanObjectId, pobj); 77 return beanObjectId; 78 } 79 80 public void syncBean(EJBInvocation invocation) { 81 ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 82 ClassLoader cl = this.getEJBClassLoader(); 83 try { 84 Thread.currentThread().setContextClassLoader(cl); 85 EnterpriseBean bean = (EnterpriseBean ) invocation.getTargetObject(); 87 EJBPoolableObject pobj = (EJBPoolableObject) instancePool.createObject(); 88 pobj.setBeanInstance(bean); 89 this.restoreBean(invocation.getObjectId(), pobj); 91 } 92 catch(Exception e) { 93 logger.warn("pool.createObject failed.", e); 94 } 95 finally { 96 Thread.currentThread().setContextClassLoader(oldCL); 97 } 98 } 99 100 protected void doInit() throws Exception { 101 super.doInit(); 104 instancePool = (StatefulObjectPool) context.getRegistry().getComponentInstance( 105 ComponentName.newInstance(StatefulObjectPool.class), 106 new DependencyPack[]{new DependencyPack("EJBObjectFactory", 107 new ObjectDependency(new StatefulObjectFactory(StatefulPoolableObject.class, this))), 108 }); 109 110 } 111 112 protected void doDestroy() throws Exception { 113 super.doDestroy(); 114 instancePool.destroy(); 116 } 117 118 public static void main(String [] args) { 119 120 } 121 } | Popular Tags |