/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3.test.security;

import java.security.Principal;
import javax.ejb.EJBException;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import javax.annotation.EJB;
import javax.annotation.EJBs;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.annotation.security.RunAs;

import org.apache.log4j.Category;
import org.jboss.annotation.ejb.RemoteBinding;
import org.jboss.annotation.security.SecurityDomain;
import org.jboss.ejb3.Container;

/**
 * A simple session bean that calls the CalleeBean
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.2.2 $
 */
@Stateless(name="Level1CallerBean")
@Remote(org.jboss.ejb3.test.security.CalledSession.class)
@RemoteBinding(jndiBinding = "spec.Level1CallerBean")
@RunAs("InternalRole")
@RolesAllowed({"Echo"})
@SecurityDomain("spec-test")
@EJBs({@EJB(name="Caller2", businessInterface=org.jboss.ejb3.test.security.CalledSession.class, beanName="CallerBean2"),
   @EJB(name="Callee", businessInterface=org.jboss.ejb3.test.security.StatelessSessionLocal.class, beanName="CalleeBean")})
public class Level1CallerBean 
{
   private static Category log = Category.getInstance(CallerBean.class);
   @Resource  SessionContext sessionContext;

   /**
    * This method calls echo on a StatelessSessionLocal and asserts that the
    * caller is in the EchoCaller role.
    */
   public String invokeEcho(String arg)
   {
      log.info("echo, arg=" + arg);
      Principal p = sessionContext.getCallerPrincipal();
      log.info("echo, callerPrincipal=" + p);
      boolean isEchoCaller = sessionContext.isCallerInRole("Echo");
      log.info("echo, isCallerInRole('Echo')=" + isEchoCaller);
      boolean isInternalRole = sessionContext.isCallerInRole("InternalRole");
      log.info("echo, isCallerInRole('InternalRole')=" + isInternalRole);
      
      if (isEchoCaller == false && isInternalRole == false)
         throw new SecurityException("isEchoCaller == false && isInternalRole == false");
    
      try
      {
         InitialContext ic = new InitialContext();
         StatelessSessionLocal localBean = (StatelessSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/ejb/Callee");
         String echo2 = localBean.echo(arg);
         log.info("echo#1, callee.echo=" + echo2);
         echo2 = localBean.echo(arg);
         log.info("echo#2, callee.echo=" + echo2);
      }
      catch (Exception e)
      {
         log.error("Failed to invoke Callee.echo", e);
         throw new EJBException("Failed to invoke Callee.echo", e);
      }

      isEchoCaller = sessionContext.isCallerInRole("Echo");
      log.info("echo, isCallerInRole#2('Echo')=" + isEchoCaller);
      isInternalRole = sessionContext.isCallerInRole("InternalRole");
      log.info("echo, isCallerInRole#2('InternalRole')=" + isInternalRole);

      if (isEchoCaller == false && isInternalRole == false)
         throw new SecurityException("isEchoCaller == false && isInternalRole == false post calls");
      
      return arg;
   }

   /**
    * This method should call invokeEcho on another CalledSession
    */
   public void callEcho()
   {
      try
      {
         InitialContext ic = new InitialContext();
       
         javax.naming.NamingEnumeration list = ic.list(Container.ENC_CTX_NAME + "/env/ejb");
      
         CalledSession bean = (CalledSession)ic.lookup(Container.ENC_CTX_NAME + "/env/ejb/Caller2");
         String echo = bean.invokeEcho("Level1");
         log.info("echo, callee.invokeEcho=" + echo);
      }
      catch (Exception e)
      {
         log.error("Failed to invoke Callee.invokeEcho", e);
         throw new EJBException("Failed to invoke Callee.invokeEcho", e);
      }

   }

   /**
    * This method should call invokeEcho on a CalledSession
    */
   public String callLocalEcho(String arg)
   {
      try
      {
         InitialContext ic = new InitialContext();
         Context enc = (Context) ic.lookup("java:comp/env");
         CalledSessionLocal bean = (CalledSessionLocal)enc.lookup("ejb/Caller");
         String echo2 = bean.invokeEcho(arg + "Level1");
         log.info("echo, callee.invokeEcho=" + echo2);
         return echo2;
      }
      catch (Exception e)
      {
         log.error("Failed to invoke Callee.invokeEcho", e);
         throw new EJBException("Failed to invoke Callee.invokeEcho", e);
      }
   }

   public void noop()
   {
      log.info("noop");
   }

}
