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

import org.jboss.logging.Logger;
import org.jboss.mx.server.Invocation;
import org.jboss.system.InterceptorServiceMBeanSupport;

/**
 * Base class that can be used for writing services
 * that dynamically hook to other interceptable deployers
 * in order to add functionality in the deployment cycle.
 *
 * attach()/detach() is used to install an interceptor
 * to the configured Interceptable SubDeployers.
 * 
 * We override attach() to install a different interceptor
 * from that of the base class that understand SubDeployer
 * calls. Note that invoke(Invocation) is not used.
 * 
 * Simply override any of the init/create/start/stop/destroy
 * methods to apply the extra functionality, and don't forget
 * to forward the call using invokeNext().
 * 
 * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
 * @version $Revision: 1.2.2.2 $
**/
public abstract class SubDeployerInterceptorSupport extends InterceptorServiceMBeanSupport
   implements SubDeployerInterceptorMBean
{

   // Constructors -------------------------------------------------
    
   /**
    * Constructs an <tt>SubDeployerInterceptorSupport</tt>.
   **/
   public SubDeployerInterceptorSupport()
   {
        super();
   }

   /**
    * Constructs an <tt>SubDeployerInterceptorSupport</tt>.
    *
    * Pass-through to InterceptorServiceMBeanSupport.
    *
    * @param type   The class type to determine Logger name from.
   **/
   public SubDeployerInterceptorSupport(final Class type)
   {
      super(type);
   }
   
   /**
    * Constructs an <tt>SubDeployerInterceptorSupport</tt>.
    *
    * Pass-through to InterceptorServiceMBeanSupport.
    *
    * @param category   The logger category name.
   **/
   public SubDeployerInterceptorSupport(final String category)
   {
      super(category);
   }

   /**
    * Constructs an <tt>SubDeployerInterceptorSupport</tt>.
    *
    * Pass-through to InterceptorServiceMBeanSupport.
    *
    * @param log   The logger to use.
   **/
   public SubDeployerInterceptorSupport(final Logger log)
   {
      super(log);
   }    
    
   // Protected API -------------------------------------------------
   
   /**
    * We override attach() from InterceptorServiceMBeanSupport
    * to attach a different interceptor that knows how to switch
    * init/create/start/stop/destroy SubDeployer calls.
    * 
    * @throws Exception thrown on any interceptor registration error
    */
   protected void attach() throws Exception
   {
      super.attach(new XMBeanInterceptor());
   }
   
   // Override ------------------------------------------------------
   
   /**
    * Override
    */   
   protected Object init(Invocation invocation, DeploymentInfo di) throws Throwable
   {
      return invokeNext(invocation);
   }

   /**
    * Override
    */
   protected Object create(Invocation invocation, DeploymentInfo di) throws Throwable
   {
      return invokeNext(invocation);
   }

   /**
    * Override
    */
   protected Object start(Invocation invocation, DeploymentInfo di) throws Throwable
   {
      return invokeNext(invocation);
   }

   /**
    * Override
    */
   protected Object stop(Invocation invocation, DeploymentInfo di) throws Throwable
   {
      return invokeNext(invocation);
   }

   /**
    * Override
    */
   protected Object destroy(Invocation invocation, DeploymentInfo di) throws Throwable
   {
      return invokeNext(invocation);
   }

   // Private Inner Class -------------------------------------------
   
   /**
    * Simple SubDeployerInterceptor delegating to
    * the SubDeployerInterceptorSupport callbacks
    */
   private class XMBeanInterceptor extends SubDeployerInterceptor
   {
      public XMBeanInterceptor()
      {
         super("XMBeanInterceptor('" + SubDeployerInterceptorSupport.this.getServiceName() + "')");
      }
      
      protected Object init(Invocation invocation, DeploymentInfo di) throws Throwable
      {
         // delegate
         return SubDeployerInterceptorSupport.this.init(invocation, di);
      }
      
      protected Object create(Invocation invocation, DeploymentInfo di) throws Throwable
      {
         // delegate
         return SubDeployerInterceptorSupport.this.create(invocation, di);
      }
      
      protected Object start(Invocation invocation, DeploymentInfo di) throws Throwable
      {
         // delegate
         return SubDeployerInterceptorSupport.this.start(invocation, di);
      }
      
      protected Object stop(Invocation invocation, DeploymentInfo di) throws Throwable
      {
         // delegate
         return SubDeployerInterceptorSupport.this.stop(invocation, di);
      }
      
      protected Object destroy(Invocation invocation, DeploymentInfo di) throws Throwable
      {
         // delegate
         return SubDeployerInterceptorSupport.this.destroy(invocation, di);
      }
   }
}    
