/* 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.Properties;
import java.security.Identity;
import java.security.Principal;
import java.lang.reflect.Proxy;
import javax.ejb.EJBContext;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.ejb.EJBException;

import org.jfox.ejb.invoker.local.LOCALContainerInvoker;
import org.jfox.ejb.invoker.local.LOCALContainerService;
import org.huihoo.jfox.logging.Logger;

/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public abstract class EJBContextSupport implements EJBContext {
  protected Logger logger = Logger.getLogger(getClass().getName());
  protected ObjectId objectId = null;
  protected BulkMetaData bulkMeta = null;

  public EJBContextSupport(ObjectId objectId, BulkMetaData bulkMeta) {
    this.objectId = objectId;
    this.bulkMeta = bulkMeta;
  }

  // use LOCALContainerInvoker & LOCALContainerService
  public EJBHome getEJBHome() {
    ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
    // get the EJBObject class name
    String ejbHomeInterfaceName = objectId.getHomeInterfName();
    try {
      Class remoteInterface = ctxLoader.loadClass(ejbHomeInterfaceName);
      return (EJBHome)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
              new Class[]{remoteInterface},
              new LOCALContainerInvoker(objectId,LOCALContainerService.getInstance()));
    }
    catch(Exception e){
      throw new EJBException(e);
    }

  }

  public EJBLocalHome getEJBLocalHome() {
    throw new IllegalStateException("current not support local ejb");
  }

  public Properties getEnvironment() {
    throw new EJBException("Deprecated");
  }

  public Identity getCallerIdentity() {
    throw new EJBException("Deprecated");
  }

  public Principal getCallerPrincipal() {
    throw new IllegalStateException("current not support security context");
  }

  public boolean isCallerInRole(Identity identity) {
    throw new EJBException("Deprecated");
  }

  public boolean isCallerInRole(String string) {
    throw new IllegalStateException("current not support security context");
  }

}