/*
* 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.rmi.RemoteException;
import java.security.Principal;

import javax.annotation.EJB;
import javax.annotation.EJBs;
import javax.annotation.Resource;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import javax.annotation.security.RolesAllowed;

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

/** A SessionBean that access the Entity bean to test Principal
identity propagation.

@author Scott.Stark@jboss.org
@version $Revision: 1.2.2.4 $
*/
@Stateless(name="StatelessSession2")
@Remote(org.jboss.ejb3.test.security.StatelessSession.class)
@RemoteBinding(jndiBinding = "spec.StatelessSession2")
@RolesAllowed({"Echo"})
@SecurityDomain("spec-test")
@EJBs({@EJB(name="Session", businessInterface=org.jboss.ejb3.test.security.StatelessSession.class, beanName="spec.StatelessSession")})
public class StatelessSessionBean2
{
   org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(getClass());
   
   @Resource SessionContext sessionContext;
    
    public void testGetBusinessObject()
    {
       Object businessObject = sessionContext.getBusinessObject(org.jboss.ejb3.test.security.StatelessSession.class);
    }

    public String echo(String arg)
    {
        log.info("echo, arg="+arg);
 
        // This call should fail if the bean is not secured
        Principal p = sessionContext.getCallerPrincipal();
        log.info("echo, callerPrincipal="+p);
        
        String echo = null;
        try
        {
            InitialContext ctx = new InitialContext();
            StatefulSession bean = (StatefulSession) ctx.lookup("spec.StatefulSession");
            echo = bean.echo(arg);
        }
        catch(Exception e)
        {
            e.fillInStackTrace();
            throw new EJBException("Stateful.echo failed", e);
        }
        
        return echo;
    }

    public String forward(String echoArg)
    {
        log.info("forward, echoArg="+echoArg);
        String echo = null;
        try
        {
            InitialContext ctx = new InitialContext();
            StatelessSession bean = (StatelessSession)ctx.lookup(Container.ENC_CTX_NAME + "/env/ejb/Session");
            echo = bean.echo(echoArg);
        }
        catch(Exception e)
        {
            log.info("StatelessSession.echo failed", e);
            e.fillInStackTrace();
            throw new EJBException("StatelessSession.echo failed", e);
        }
        return echo;
    }

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

    public void npeError()
    {
        log.info("npeError");
        Object obj = null;
        obj.toString();
    }
    public void unchecked()
    {
        Principal p = sessionContext.getCallerPrincipal();
        log.info("StatelessSessionBean.unchecked, callerPrincipal="+p);
    }

    public void excluded()
    {
        throw new EJBException("StatelessSessionBean.excluded, no access should be allowed");
    }
}
