/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

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;

/**
 * EJB Container 实现,Container 是一个 singleton 类,一个 Container 可以布署多种 ejb ,
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ContainerImpl extends ComponentSupport implements Container {
  // singleton instance
  private static Container instance = null;

  // jndi instance
  static Context INITIAL_CONTEXT = null;

  // loaded bean bulks
  private Map bulks = new HashMap();

  private BulkFactory bulkFactory = BulkFactory.getInstance();

  private List interceptors = new ArrayList();

  // 指定用于绑定在 Home 上的默认的协议
  protected String remoteProtocol = Protocol.JRMP;
  protected String localProtocol = Protocol.LOCAL;


  private ContainerImpl() {
//    bulkFactory = BulkFactory.getInstance();
  }

  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();
  }

  /**
   * create a bean bulk and bind it's home to jndi
   * @param meta
   */
  public synchronized void createBulk(BulkMetaData meta) throws Exception{
    Bulk bulk = bulkFactory.createBulk(meta);
    bulk.init();
    bulks.put(meta.getEJBMetaData().getHomeInterfaceClass().getName(),bulk);
  }

  /**
   * remove the bulk from container , 发生在 undeploy 一个 bean 的时候
   */
  public synchronized void dropBulk(String homeClassName) throws Exception {
    bulks.remove(homeClassName);
    ((Bulk)bulks.get(homeClassName)).destroy();
  }

  /**
   * container 可能对该方法调用进行一些统一的调控,如:安全验证等,然后 forward 给 bulk 执行
   * @return
   * @throws Exception
   */
  public Object invoke(Invocation invocation) throws Exception {
    logger.debug("invoke " + invocation.toString());
    Bulk bulk = getBulk(invocation);

    if(invocation.getObjectId().isHome()) { // EJBHome 方法
      invocation.setMethod(bulk.getHomeMethod(invocation));
      preInvokeHome(invocation);
      Object result = bulk.invokeHome(invocation);
      postInvokeHome(invocation);
      return result;
    }
    else { // Bean 方法
      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) {

  }
}