package org.jfox.ejb;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import javax.naming.Context;
import javax.transaction.TransactionManager;
import org.huihoo.jfox.system.ComponentSupport;
import org.huihoo.jfox.util.InitialContextHelper;
import org.jfox.ejb.plugin.Interceptor;
import org.jfox.ejb.meta.Protocol;
import org.jfox.tm.TxManager;
public class ContainerImpl extends ComponentSupport implements Container {
private static Container instance = null;
static Context INITIAL_CONTEXT = null;
private Map bulks = new HashMap();
private BulkFactory bulkFactory = BulkFactory.getInstance();
private List interceptors = new ArrayList();
protected String remoteProtocol = Protocol.JRMP;
protected String localProtocol = Protocol.LOCAL;
private ContainerImpl() {
}
public static synchronized Container getInstance(){
if(instance == null) {
instance = new ContainerImpl();
}
return instance;
}
public TransactionManager getTransactionManager() {
return TxManager.getInstance();
}
protected void doInit() throws Exception {
INITIAL_CONTEXT = InitialContextHelper.getInitialConext();
}
protected synchronized void doDestroy() throws Exception {
for(Iterator it= bulks.values().iterator(); it.hasNext();){
((BulkSupport)it.next()).destroy();
}
INITIAL_CONTEXT = null;
bulks = new HashMap();
}
public Iterator listBulks() {
return bulks.keySet().iterator();
}
public synchronized void createBulk(BulkMetaData meta) throws Exception{
Bulk bulk = bulkFactory.createBulk(meta);
bulk.init();
bulks.put(meta.getEJBMetaData().getHomeInterfaceClass().getName(),bulk);
}
public synchronized void dropBulk(String homeClassName) throws Exception {
bulks.remove(homeClassName);
((Bulk)bulks.get(homeClassName)).destroy();
}
public Object invoke(Invocation invocation) throws Exception {
logger.debug("invoke " + invocation.toString());
Bulk bulk = getBulk(invocation);
if(invocation.getObjectId().isHome()) { invocation.setMethod(bulk.getHomeMethod(invocation));
preInvokeHome(invocation);
Object result = bulk.invokeHome(invocation);
postInvokeHome(invocation);
return result;
}
else { invocation.setMethod(bulk.getBeanMethod(invocation));
preInvokeBean(invocation);
Object result = bulk.invokeBean(invocation);
postInvokeBean(invocation);
return result;
}
}
public void addInterceptor(Interceptor interceptor) throws Exception {
interceptor.setContainer(this);
synchronized(interceptors){
interceptors.add(interceptor);
}
}
public synchronized Iterator interceptors() {
return interceptors.iterator();
}
public String getRemoteProtocol() {
return remoteProtocol;
}
public void setRemoteProtocol(String remoteProtocol) {
this.remoteProtocol = remoteProtocol;
}
public String getLocalProtocol() {
return localProtocol;
}
public void setLocalProtocol(String localProtocol) {
this.localProtocol = localProtocol;
}
private void preInvokeHome(Invocation invocation) throws Exception {
for(Iterator it = this.interceptors(); it.hasNext();) {
Interceptor interceptor = (Interceptor)it.next();
interceptor.preInvokeHome(invocation);
}
}
private void preInvokeBean(Invocation invocation) throws Exception {
for(Iterator it = this.interceptors(); it.hasNext();) {
Interceptor interceptor = (Interceptor)it.next();
interceptor.preInvokeBean(invocation);
}
}
private void postInvokeHome(Invocation invocation) throws Exception {
for(Iterator it = this.interceptors(); it.hasNext();) {
Interceptor interceptor = (Interceptor)it.next();
interceptor.preInvokeHome(invocation);
}
}
private void postInvokeBean(Invocation invocation) throws Exception {
for(Iterator it = this.interceptors(); it.hasNext();) {
Interceptor interceptor = (Interceptor)it.next();
interceptor.preInvokeBean(invocation);
}
}
private Bulk getBulk(Invocation invocation){
return (Bulk)bulks.get(invocation.getObjectId().getHomeInterfName());
}
public static void main(String[] args) {
}
}