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

import java.security.Principal;
import javax.ejb.CreateException;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import javax.annotation.Resource;
import javax.annotation.security.RunAs;

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

import javax.annotation.security.RolesAllowed;

/** A simple session bean that is called by the CallerBean to test
 run-as identity and role propagation.
 
 @author Scott.Stark@jboss.org
 @version $Revision: 1.1.2.3 $
 */
@Stateless(name="CalleeBean")
@Local(org.jboss.ejb3.test.security.StatelessSessionLocal.class)
@Remote(org.jboss.ejb3.test.security.StatelessSessionLocal.class)
@LocalBinding(jndiBinding = "spec.CalleeBean")
@RemoteBinding(jndiBinding = "spec.CalleeBeanRemote")
@SecurityDomain("spec-test")
public class CalleeBean
{
   private static Category log = Category.getInstance(CalleeBean.class);
   
   @Resource SessionContext sessionContext;

   @RolesAllowed({"Echo"})
   public String echo(String arg)
   {
      log.info("------------ CalleeBean.echo");
      log.info("echo, arg="+arg);
      Principal p = sessionContext.getCallerPrincipal();
      log.info("echo, callerPrincipal="+p);
      boolean isCaller = sessionContext.isCallerInRole("Echo");
      log.info("echo, isCallerInRole('Echo')="+isCaller);
      isCaller = sessionContext.isCallerInRole("InternalRole");
      log.info("echo, isCallerInRole('InternalRole')="+isCaller);
      
      log.info("------------ CalleeBean.echo done");
      return arg;
   }

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