/* 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.plugin;

import java.util.Iterator;

import javax.ejb.EnterpriseBean;

import org.jfox.ejb.Bulk;
import org.jfox.ejb.Invocation;
import org.jfox.ejb.SessionBulk;
import org.jfox.ejb.EJBPoolableObject;
import org.jfox.ejb.EJBObjectMethod;

/**
 * 从实例池取得 Bean 实例,执行后返回
 *
 * A5 用于排序
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class A5_PoolInvoker extends AbstractInvoker {

  public Object invokeBean(Bulk bulk, Invocation invocation, Iterator iter) throws Exception {
    EJBPoolableObject pobj  = ((SessionBulk)bulk).retrieveBean(invocation.getObjectId());
    EnterpriseBean bean = pobj.getBeanInstance();
    invocation.setBean(bean);

    /**
     * 拦截 remove 方法调用,因为这个调用将不再把 Bean 返回给 ObjectPool 
     */
    if(invocation.getMethod().equals(EJBObjectMethod.Remove)){
      bulk.remove(bean);
      pobj = null;
      return null;
    }

    try {
      return super.invokeBean(bulk, invocation, iter);
    }
    finally {
      ((SessionBulk)bulk).restoreBean(invocation.getObjectId(), pobj);
    }

  }

  public static void main(String[] args) {

  }
}