package org.jfox.ejb;
import java.rmi.RemoteException;
import java.lang.reflect.Proxy;
import javax.ejb.RemoveException;
import javax.ejb.Handle;
import javax.ejb.EnterpriseBean;
import javax.ejb.SessionBean;
import javax.ejb.EJBObject;
import org.jfox.ejb.invoker.ContainerInvokerSupport;
public class StatefulSessionBulk extends SessionBulk {
private StatefulSessionObjectPool instancePool = null;
public StatefulSessionBulk(BulkMetaData meta) {
super(meta);
}
public Object invokeHome(Invocation invocation) throws Exception {
logger.debug("invokeHome " + invocation.getMethod().getName());
return StatefulInvokerChain.getInstance().invokeHome(this,invocation);
}
public Object invokeBean(Invocation invocation) throws Exception {
logger.debug("invokeBean " + invocation.getMethod().getName());
return StatefulInvokerChain.getInstance().invokeBean(this,invocation);
}
public void remove(EnterpriseBean bean) throws RemoteException, RemoveException {
ejbRemove((SessionBean)bean);
}
public void remove(Handle handle) throws RemoteException, RemoveException {
EJBObject ejbObject = handle.getEJBObject();
ObjectId objectId = ((ContainerInvokerSupport)Proxy.getInvocationHandler(ejbObject)).getObjectId();
try {
EJBPoolableObject pobj = (EJBPoolableObject)instancePool.retrieveObject(objectId);
remove(pobj.getBeanInstance());
}
catch(Exception e) {
throw new RemoveException(e.getMessage());
}
}
public boolean isIdentical(ObjectId thisObjectId, ObjectId thatObjectId) throws RemoteException {
return thisObjectId.equals(thatObjectId);
}
public EJBPoolableObject retrieveBean(Object key) {
try {
return (EJBPoolableObject)instancePool.retrieveObject(key);
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
public void restoreBean(Object key, EJBPoolableObject pobj) {
instancePool.restoreObject(key,pobj);
}
public ObjectId createBean(Invocation invocation) throws Exception {
ObjectId beanObjectId = oidGenerator.nextBeanObjectId();
EJBPoolableObject pobj = (EJBPoolableObject)instancePool.createObject();
SessionBean bean = (SessionBean)pobj.getBeanInstance();
setSessionContext(bean, new SessionContextImpl(beanObjectId,bulkMeta));
ejbCreate(bean,invocation.getMethod().getName(),invocation.getArgs());
instancePool.restoreObject(beanObjectId,pobj);
return beanObjectId;
}
protected void doInit() throws Exception {
instancePool = new StatefulSessionObjectPool(this,new EJBObjectFactory(EJBPoolableObject.class,bulkMeta.getBeanClass()));
instancePool.init();
super.doInit();
}
protected void doDestroy() throws Exception {
super.doDestroy();
instancePool.destroy();
}
public static void main(String[] args) {
}
}