/*
 * JBoss, the OpenSource EJB server
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.tutorial.service.bean;

import org.jboss.annotation.ejb.Service;
import org.jboss.annotation.ejb.Depends;
import org.jboss.annotation.ejb.Depends;
import org.jboss.annotation.ejb.Service;

import javax.ejb.AroundInvoke;
import javax.ejb.InvocationContext;
import javax.management.ObjectName;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServer;

/**
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision: 1.2.2.4 $
 */
@Service
public class ServiceThree implements ServiceThreeManagement
{
   @Depends ("jboss.j2ee:name=org.jboss.tutorial.service.bean.ServiceOne,service=EJB3,type=service")
   public ObjectName serviceOneName;
   private ServiceTwoManagement service2;

   @Depends ("tutorial:service=serviceTwo")
   public void setServiceTwo(ServiceTwoManagement service2)
   {
      this.service2 = service2;
   }

   public String serviceOneHello()throws Exception
   {
      Object[] args = new Object[0];
      String[] signature = new String[0];
      System.out.println("ServiceThree - Calling ServiceOne.sayHello() via JMX server");
      MBeanServer server = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
      return (String)server.invoke(serviceOneName, "sayHello", args, signature);
   }

   public String serviceTwoHello()
   {
      System.out.println("ServiceThree - Calling ServiceTwo.sayHello() via MBean proxy");
      return service2.sayHello();
   }

   // Interceptors
   @AroundInvoke
   public Object intercept(InvocationContext ctx) throws Exception
   {
      System.out.println("ServiceThree - Interceptor");
      return ctx.proceed();
   }

   // Lifecycle methods
   public void start() throws Exception
   {
      System.out.println("ServiceThree - Starting");
   }

   public void stop()
   {
      System.out.println("ServiceThree - Stopping");
   }

}
