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

import javax.ejb.ActivationConfigProperty;
import javax.ejb.AroundInvoke;
import javax.ejb.InvocationContext;
import javax.ejb.MessageDriven;
import javax.ejb.PostConstruct;
import javax.ejb.PreDestroy;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.annotation.security.RunAs;

import org.jboss.annotation.security.SecurityDomain;
import org.jboss.logging.Logger;

/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.6.6.4 $
 */
@MessageDriven(activateConfig =
        {
        @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
        @ActivationConfigProperty(propertyName="destination", propertyValue="queue/mdbtest")
        })
@RunAs("TestRole")
@SecurityDomain("other")
public class QueueTestMDB implements MessageListener
{
   private static final Logger log = Logger.getLogger(QueueTestMDB.class);
   
   public void onMessage(Message recvMsg)
   {
      System.out.println("QueueTestMDB onMessage");
      TestStatusBean.queueRan = true;
      
      try {
         InitialContext jndiContext = new InitialContext();
         Stateless stateful = (Stateless)jndiContext.lookup("Stateless");
         stateful.setState("Set");
      } catch (Exception e)
      {
         e.printStackTrace();
      }
   }

   @AroundInvoke
   public Object intercept(InvocationContext ctx) throws Exception
   {
      TestStatusBean.interceptedQueue = true;
      return ctx.proceed();
   }

   @PostConstruct
   public void postConstruct()
   {
      TestStatusBean.postConstruct = true;
   }

   @PreDestroy
   public void preDestroy()
   {
      TestStatusBean.preDestroy = true;
   }
}
