/*
* 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.Resource;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.Remote;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.RemoteBinding;
import org.jboss.annotation.security.SecurityDomain;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;

/** A simple session bean for testing unchecked declarative security.

 @author Scott.Stark@jboss.org
 @version $Revision: 1.1.2.3 $
 */
@Stateless(name="UncheckedSessionRemote")
@Remote(org.jboss.ejb3.test.security.StatelessSession.class)
@RemoteBinding(jndiBinding = "spec.UncheckedSession")
@SecurityDomain("spec-test")
@PermitAll
public class UncheckedSessionBean
{
   Logger log = Logger.getLogger(getClass());
   
   @Resource SessionContext sessionContext;

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

   public String forward(String echoArg)
   {
      log.debug("forward, echoArg=" + echoArg);
      return echo(echoArg);
   }

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

   public void npeError()
   {
      log.debug("npeError");
      Object obj = null;
      obj.toString();
   }

   public void unchecked()
   {
      Principal p = sessionContext.getCallerPrincipal();
      log.debug("unchecked, callerPrincipal=" + p);
   }

   @RolesAllowed({"InternalRole"})
   public void excluded()
   {
      throw new EJBException("excluded, no access should be allowed");
   }

}
