/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.annotation.security.SecurityDomain;
import org.jboss.aop.Advisor;
import org.jboss.aop.metadata.SimpleMetaData;
import org.jboss.logging.Logger;
import org.jboss.security.RealmMapping;

/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.1.2.4 $
 */
public abstract class BaseContext implements BeanContext
{
   protected static Logger log = Logger.getLogger(BaseSessionContext.class);
   protected Container container;
   protected Object bean;
   protected RealmMapping rm;
   protected SimpleMetaData metadata;

   public Object getInstance()
   {
      return bean;
   }

   public void setInstance(Object instance)
   {
      bean = instance;
   }

   public Container getContainer()
   {
      return container;
   }

   public SimpleMetaData getMetaData()
   {
      if (metadata == null) metadata = new SimpleMetaData();
      return metadata;
   }

   public void setContainer(Container container)
   {
      this.container = container;
      try
      {
         InitialContext ctx = container.getInitialContext();
         SecurityDomain securityAnnotation = (SecurityDomain) ((Advisor) container).resolveAnnotation(SecurityDomain.class);
         if (securityAnnotation == null) return;
         Object domain = ctx.lookup("java:/jaas/" + securityAnnotation.value());
         rm = (RealmMapping) domain;
      }
      catch (NamingException e)
      {
         throw new RuntimeException(e);
      }
   }

   protected RealmMapping getRm()
   {
      return rm;
   }

}
