/*
 * 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.annotation.EJB;
import javax.annotation.EJBs;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
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 org.apache.log4j.Category;
import org.jboss.annotation.ejb.RemoteBinding;
import org.jboss.annotation.security.SecurityDomain;
import org.jboss.ejb3.Container;
import org.jboss.ejb3.test.security.CalledSessionLocal;

/**
 * A simple session bean that calls the CalleeBean
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.2.2 $
 */
@Stateless(name="CallerFacadeBean-testRunAsSFSB")
@Remote(org.jboss.ejb3.test.security.CalledSession.class)
@RemoteBinding(jndiBinding = "spec.CallerFacadeBean-testRunAsSFSB")
@SecurityDomain("spec-test")
@RolesAllowed({"Echo"})
@EJBs({@EJB(name="CalledSessionLocal", businessInterface=org.jboss.ejb3.test.security.CalledSessionLocal.class, beanName="CallerFacadeTargetSFSB")})
public class CallerFacadeBean 
{
   private static Category log = Category.getInstance(CallerFacadeBean.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.debug("echo, arg=" + arg);
      Principal p = sessionContext.getCallerPrincipal();
      log.debug("echo, callerPrincipal=" + p);
      boolean isEchoCaller = sessionContext.isCallerInRole("EchoCaller");
      log.debug("echo, isCallerInRole('EchoCaller')=" + isEchoCaller);
      boolean isInternalRole = sessionContext.isCallerInRole("InternalRole");
      log.debug("echo, isCallerInRole('InternalRole')=" + isInternalRole);

      try
      {
         InitialContext ic = new InitialContext();
         CalledSessionLocal localBean = (CalledSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/ejb/CalledSessionLocal");
         String echo2 = localBean.invokeEcho(arg);
         log.debug("echo#1, callee.invokeEcho=" + echo2);
         echo2 = localBean.invokeEcho(arg);
         log.debug("echo#2, callee.invokeEcho=" + echo2);
      }
      catch (Exception e)
      {
         e.printStackTrace();
         log.error("Failed to invoke CalledSession.invokeEcho", e);
         throw new EJBException("Failed to invoke Callee.echo", e);
      }
      return arg;
   }

   /**
    * This method should call invokeEcho on another CalledSession
    */
   public void callEcho()
   {
      try
      {
         InitialContext ic = new InitialContext();
         CalledSessionLocal bean = (CalledSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/ejb/CalledSessionLocal");
         bean.callLocalEcho("callEcho#2");
         log.debug("echo, callee.callLocalEcho#1");
         bean.callLocalEcho("callEcho#2");
         log.debug("echo, callee.callLocalEcho#2");
      }
      catch (Exception e)
      {
         log.error("Failed to invoke Callee.invokeEcho", e);
         throw new EJBException("Failed to invoke Callee.invokeEcho", e);
      }

   }

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

}
