FileDocCategorySizeDatePackage
BaseSessionContext.javaAPI DocJBoss 4.2.111913Fri Jul 13 20:53:58 BST 2007org.jboss.ejb3

BaseSessionContext

public class BaseSessionContext extends Object implements javax.ejb.MessageDrivenContext, javax.ejb.SessionContext, Externalizable
Comment
author
Bill Burke
version
$Revision: 63839 $

Fields Summary
private static final long
serialVersionUID
private static final Logger
log
protected transient Container
container
protected transient org.jboss.security.RealmMapping
rm
protected BaseContext
baseContext
Constructors Summary
public BaseSessionContext()


    
   
   
Methods Summary
public java.lang.ObjectgetBusinessObject(java.lang.Class businessInterface)

      return ((EJBContainer)container).getBusinessObject(baseContext, businessInterface); 
   
public java.security.IdentitygetCallerIdentity()

      throw new IllegalStateException("deprecated");
   
public java.security.PrincipalgetCallerPrincipal()

      Principal principal = SecurityAssociation.getCallerPrincipal();
      if (getRm() != null)
      {
         principal = getRm().getPrincipal(principal);
      }

      // This method never returns null.
      if (principal == null)
         throw new java.lang.IllegalStateException("No valid security context for the caller identity");

      return principal;
   
public ContainergetContainer()

      return container;
   
public javax.ejb.EJBHomegetEJBHome()

      throw new EJBException("EJB 3.0 does not have a home type.");
   
public javax.ejb.EJBLocalHomegetEJBLocalHome()

      throw new EJBException("EJB 3.0 does not have a home type.");
   
public javax.ejb.EJBLocalObjectgetEJBLocalObject()

      try
      {
         Object id = baseContext.getId();
         EJBLocalObject proxy =  (EJBLocalObject)((SessionContainer)container).createLocalProxy(id);
         return proxy;
      }
      catch (Exception e)
      {
         throw new IllegalStateException(e);
      }
   
public javax.ejb.EJBObjectgetEJBObject()

      try
      {
         Object id = baseContext.getId();
         EJBObject proxy =  (EJBObject)((SessionContainer)container).createRemoteProxy(id);
         return proxy;
      }
      catch (Exception e)
      {
         throw new IllegalStateException(e);
      }
   
public java.util.PropertiesgetEnvironment()

      throw new EJBException("Deprecated");
   
public java.lang.ClassgetInvokedBusinessInterface()

      return ((SessionContainer)container).getInvokedBusinessInterface();
   
public javax.xml.rpc.handler.MessageContextgetMessageContext()

      // disallowed for stateful session beans (EJB3 FR 4.4.1 p 81)
      if(baseContext instanceof StatelessBeanContext)
      {
         MessageContext ctx = ((StatelessBeanContext) baseContext).getMessageContextJAXRPC();
         if(ctx == null)
            throw new IllegalStateException("No message context found");
         return ctx;
      }
      throw new UnsupportedOperationException("Only stateless beans can have a message context");
   
protected org.jboss.security.RealmMappinggetRm()

      return rm;
   
public booleangetRollbackOnly()

      // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
      TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) getContainer()));
      if (type != TransactionManagementType.CONTAINER)
         throw new IllegalStateException("Container " + getContainer().getEjbName() + ": it is illegal to call getRollbackOnly from BMT: " + type);

      try
      {
         TransactionManager tm = TxUtil.getTransactionManager();

         // The getRollbackOnly and setRollBackOnly method of the SessionContext interface should be used
         // only in the session bean methods that execute in the context of a transaction.
         if (tm.getTransaction() == null)
            throw new IllegalStateException("getRollbackOnly() not allowed without a transaction.");

         // EJBTHREE-805, consider an asynchronous rollback due to timeout
         int status = tm.getStatus();
         return status == Status.STATUS_MARKED_ROLLBACK
             || status == Status.STATUS_ROLLING_BACK
             || status == Status.STATUS_ROLLEDBACK;
      }
      catch (SystemException e)
      {
         log.warn("failed to get tx manager status; ignoring", e);
         return true;
      }
   
public javax.ejb.TimerServicegetTimerService()

      return getContainer().getTimerService();
   
public javax.transaction.UserTransactiongetUserTransaction()

      TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) getContainer()));
      if (type != TransactionManagementType.BEAN) throw new IllegalStateException("Container " + getContainer().getEjbName() + ": it is illegal to inject UserTransaction into a CMT bean");

      return new UserTransactionImpl();
   
public booleanisCallerInRole(java.security.Identity role)

      throw new IllegalStateException("deprecated");
   
public booleanisCallerInRole(java.lang.String roleName)

      // TODO revert to aspects.security.SecurityContext impl when JBoss AOP 1.1 is out.
      Principal principal = getCallerPrincipal();
      // Check the caller of this beans run-as identity
      // todo use priveleged stuff in ejb class
      RunAsIdentity runAsIdentity = SecurityActions.peekRunAsIdentity(1);

      if (principal == null && runAsIdentity == null)
         return false;

      if (getRm() == null)
      {
         String msg = "isCallerInRole() called with no security context. "
                      + "Check that a security-domain has been set for the application.";
         throw new IllegalStateException(msg);
      }
      
//    Ensure that you go through the security role references that may be configured
      EJBContainer ejbc = (EJBContainer)container;
      if(ejbc.getXml() != null)
      {
         Collection<SecurityRoleRef> securityRoleRefs = ejbc.getXml().getSecurityRoleRefs();
         for(SecurityRoleRef roleRef: securityRoleRefs)
         {
            String refName = roleRef.getRoleName(); 
            if(roleName.equals(refName))
               roleName = roleRef.getRoleLink();
         } 
      } 

      HashSet set = new HashSet();
      set.add(new SimplePrincipal(roleName));

      if (runAsIdentity == null)
         return getRm().doesUserHaveRole(principal, set);
      else
         return runAsIdentity.doesUserHaveRole(set);
   
public java.lang.Objectlookup(java.lang.String name)

      String newName;
      if (name.startsWith("/"))
      {
         newName = "env" + name;
      }
      else
      {
         newName = "env/" + name;
      }
      try
      {
         return getContainer().getEnc().lookup(newName);
      }
      catch (NamingException ignored)
      {
         try 
         {
            return getContainer().getInitialContext().lookup(name);
         } 
         catch (NamingException ignored2)
         {
            
         }
      }
      return null;
   
public voidreadExternal(java.io.ObjectInput in)

      container = Ejb3Registry.getContainer(in.readUTF());
      InitialContext ctx = container.getInitialContext();
      try
      {
         setupSecurityDomain(container, ctx);
      }
      catch (NamingException e)
      {
         throw new RuntimeException(e);
      }

   
public voidsetBaseContext(BaseContext baseContext)

      this.baseContext = baseContext;
   
public voidsetContainer(Container container)

      this.container = container;
      try
      {
         InitialContext ctx = container.getInitialContext();
         setupSecurityDomain(container, ctx);
      }
      catch (NamingException e)
      {
         throw new RuntimeException(e);
      }
   
public voidsetRollbackOnly()

      // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
      TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) getContainer()));
      if (type != TransactionManagementType.CONTAINER) throw new IllegalStateException("Container " + getContainer().getEjbName() + ": it is illegal to call setRollbackOnly from BMT: " + type);

      try
      {
         TransactionManager tm = TxUtil.getTransactionManager();

         // The getRollbackOnly and setRollBackOnly method of the SessionContext interface should be used
         // only in the session bean methods that execute in the context of a transaction.
         if (tm.getTransaction() == null)
            throw new IllegalStateException("setRollbackOnly() not allowed without a transaction.");

         tm.setRollbackOnly();
      }
      catch (SystemException e)
      {
         log.warn("failed to set rollback only; ignoring", e);
      }
   
private voidsetupSecurityDomain(Container container, javax.naming.InitialContext ctx)

      SecurityDomain securityAnnotation = (SecurityDomain) ((Advisor) container).resolveAnnotation(SecurityDomain.class);
      if (securityAnnotation == null) return;
      Object domain = SecurityDomainManager.getSecurityManager(securityAnnotation.value(), ctx);
      rm = (RealmMapping) domain;
   
public voidwriteExternal(java.io.ObjectOutput out)

      out.writeUTF(container.getObjectName().getCanonicalName());