User: starksm 
  Date: 01/06/15 01:48:25

  Added:       src/main/org/jboss/test/security/ejb
                        StatelessSessionBean3.java
  Log:
  Add tests of the EJB2.0 security-identity/run-as element
  
  Revision  Changes    Path
  1.1                  
jbosstest/src/main/org/jboss/test/security/ejb/StatelessSessionBean3.java
  
  Index: StatelessSessionBean3.java
  ===================================================================
  package org.jboss.test.security.ejb;
  
  import java.rmi.RemoteException;
  import java.security.Principal;
  import javax.ejb.CreateException;
  import javax.ejb.EJBException;
  import javax.ejb.SessionBean;
  import javax.ejb.SessionContext;
  import javax.naming.InitialContext;
  
  import org.jboss.test.security.interfaces.Entity;
  import org.jboss.test.security.interfaces.EntityHome;
  import org.jboss.test.security.interfaces.StatelessSession;
  import org.jboss.test.security.interfaces.StatelessSessionHome;
  
  /** A SessionBean that accesses an Entity bean in its echo() method to test runAs
  identity propagation. It also access its own excluded() method to test that the runAs
  identity is also see on methods of this bean that are invoked through the
  remote interface.
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class StatelessSessionBean3 implements SessionBean
  {
      private SessionContext sessionContext;
  
      public void ejbCreate() throws RemoteException, CreateException
      {
          System.out.println("StatelessSessionBean3.ejbCreate() called");
      }
  
      public void ejbActivate() throws RemoteException
      {
          System.out.println("StatelessSessionBean3.ejbActivate() called");
      }
  
      public void ejbPassivate() throws RemoteException
      {
          System.out.println("StatelessSessionBean3.ejbPassivate() called");
      }
  
      public void ejbRemove() throws RemoteException
      {
          System.out.println("StatelessSessionBean3.ejbRemove() called");
      }
  
      public void setSessionContext(SessionContext context) throws RemoteException
      {
          sessionContext = context;
      }
  
      /** This method creates an instance of the entity bean bound under
       java:comp/env/ejb/Entity and then invokes its echo method. This
       method should be accessible by user's with a role of Echo, while
       the Entity bean should only be accessible by the runAs role.
       */
      public String echo(String arg)
      {
          System.out.println("StatelessSessionBean3.echo, arg="+arg);
          // This call should fail if the bean is not secured
          Principal p = sessionContext.getCallerPrincipal();
          System.out.println("StatelessSessionBean3.echo, callerPrincipal="+p);
          String echo = null;
          try
          {
              InitialContext ctx = new InitialContext();
              EntityHome home = (EntityHome) ctx.lookup("java:comp/env/ejb/Entity");
              Entity bean = home.findByPrimaryKey(arg);
              echo = bean.echo(arg);
          }
          catch(Exception e)
          {
              e.printStackTrace();
              e.fillInStackTrace();
              throw new EJBException(e);
          }
          return echo;
      }
  
      public String forward(String echoArg)
      {
          System.out.println("StatelessSessionBean3.forward, echoArg="+echoArg);
          String echo = null;
          try
          {
              InitialContext ctx = new InitialContext();
              StatelessSessionHome home = (StatelessSessionHome) 
ctx.lookup("java:comp/env/ejb/Session");
              StatelessSession bean = home.create();
              echo = bean.echo(echoArg);
          }
          catch(Exception e)
          {
              e.printStackTrace();
              e.fillInStackTrace();
              throw new EJBException(e);
          }
          return echo;
      }
  
      /** This method gets this bean's remote interface and invokes the
       excluded() method to test that the method is accessed as the
       runAs role.
       */
      public void noop()
      {
          System.out.println("StatelessSessionBean3.noop calling excluded...");
          StatelessSession myEJB = (StatelessSession) sessionContext.getEJBObject();
          try
          {
              myEJB.excluded();
          }
          catch(RemoteException e)
          {
              throw new EJBException("Failed to access excluded: "+e.detail);
          }
      }
  
      public void npeError()
      {
          System.out.println("StatelessSessionBean3.npeError");
          Object obj = null;
          obj.toString();
      }
      public void unchecked()
      {
          Principal p = sessionContext.getCallerPrincipal();
          System.out.println("StatelessSessionBean.unchecked, callerPrincipal="+p);
      }
  
      /** This method should be assigned access to the runAs role and no user
       should have this role.
       */
      public void excluded()
      {
          System.out.println("StatelessSessionBean3.excluded, accessed");
          // This call should fail if the bean is not secured
          Principal p = sessionContext.getCallerPrincipal();
          System.out.println("StatelessSessionBean3.excluded, callerPrincipal="+p);
      }
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to