/*
 * 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 java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.SessionBean;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;

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

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

/**
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.2.2 $
 */
@Stateless(name="PublicSessionFacade")
@Remote(org.jboss.ejb3.test.security.SessionFacade.class)
@RemoteBinding(jndiBinding = "spec.PublicSessionFacade")
@SecurityDomain("spec-test")
@RolesAllowed({"Echo"})
@EJBs({@EJB(name="TargetEJB", businessInterface=org.jboss.ejb3.test.security.Session.class, beanName="PublicSession")})
public class PublicSessionFacade
{
   @Resource SessionContext sessionContext;
   
   public String callEcho(String arg)
      throws RemoteException
   {
      Principal user = sessionContext.getCallerPrincipal();
      String echoMsg = null;
      try
      {
         InitialContext ctx = new InitialContext();
         Session bean = (Session) ctx.lookup(Container.ENC_CTX_NAME + "/env/ejb/TargetEJB");
         echoMsg = bean.echo("Hello, arg="+arg);
         echoMsg = bean.echo("Hello 2, arg="+arg);
      }
      catch (NamingException e)
      {
         throw new RemoteException("callEcho failed", e);
      }
    
      return echoMsg;
   }
}
