Save This Page
Home » jboss-5.0.0.CR1-src » org.jboss.mx » interceptor » [javadoc | source]
    1   /*
    2   * JBoss, Home of Professional Open Source
    3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
    4   * by the @authors tag. See the copyright.txt in the distribution for a
    5   * full listing of individual contributors.
    6   *
    7   * This is free software; you can redistribute it and/or modify it
    8   * under the terms of the GNU Lesser General Public License as
    9   * published by the Free Software Foundation; either version 2.1 of
   10   * the License, or (at your option) any later version.
   11   *
   12   * This software is distributed in the hope that it will be useful,
   13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   15   * Lesser General Public License for more details.
   16   *
   17   * You should have received a copy of the GNU Lesser General Public
   18   * License along with this software; if not, write to the Free
   19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   21   */
   22   package org.jboss.mx.interceptor;
   23   
   24   import javax.management.MBeanServer;
   25   import javax.management.ObjectName;
   26   
   27   import org.jboss.logging.Logger;
   28   import org.jboss.mx.server.Invocation;
   29   
   30   /**
   31    * Base class for all interceptors. This class provides some default method
   32    * implementations for interceptors.
   33    *
   34    * @see org.jboss.mx.interceptor.Interceptor
   35    * @see org.jboss.mx.server.MBeanInvoker
   36    *
   37    * @author  <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
   38    * @version $Revision: 37459 $   
   39    */
   40   public abstract class AbstractInterceptor 
   41      implements Interceptor
   42   {
   43   
   44      // Attributes ----------------------------------------------------
   45   
   46      /**
   47       * Name for this interceptor.
   48       */
   49      protected String name            = "<no name>";
   50      
   51      /**
   52       * Indicates whether this interceptor instance is shared or not.
   53       */
   54      protected boolean isShared       = false;
   55      
   56      /**
   57       * Logger reference for interceptor implementations. This reference is
   58       * set by the invoker for non-shared interceptors after construction.
   59       * Shared interceptors will should create their own logger instance.
   60       */
   61      protected Logger log;
   62      
   63      
   64      // Constructors --------------------------------------------------
   65      
   66      /**
   67       * Constructs a new intereceptor instance. This interceptor is not shared
   68       * in the MBean server.
   69       */
   70      public AbstractInterceptor()
   71      {
   72         log = Logger.getLogger(getClass());
   73      }
   74      
   75      /**
   76       * Constructs a new interceptor instance with a given name. This interceptor
   77       * is not shared in the MBean server.
   78       *
   79       * @param name name of this interceptor
   80       *
   81       * @throws IllegalArgumentException if name contains <tt>null</tt> reference
   82       */
   83      public AbstractInterceptor(String name)
   84      {
   85         if (name == null || name.equals(""))
   86            throw new IllegalArgumentException("null name");
   87            
   88         this.name = name;
   89   
   90         log = Logger.getLogger(getClass());
   91      }
   92      
   93   
   94      // Public --------------------------------------------------------
   95   
   96      /**
   97       * Sets a name for this interceptor.
   98       *
   99       * @param   name
  100       */
  101      public void setName(String name)
  102      {
  103         this.name = name;
  104      }
  105      
  106      
  107      // Interceptor implementation ------------------------------------   
  108      
  109      /**
  110       * The default invoke implementation queries the invocation object for the
  111       * next interceptor in the chain. If one exists, it is invoked. Otherwise
  112       * the invocation is dispatched to the target object.    <p>
  113       *
  114       * Concrete implementations should override this method to implement
  115       * their specific application logic.
  116       *
  117       * @see     org.jboss.mx.server.Invocation
  118       * @see     org.jboss.mx.server.MBeanInvoker
  119       *
  120       * @param   invocation  the invocation object send towards the target
  121       *                      resource by the invoker
  122       *
  123       * @return  return value from the target resource
  124       *
  125       * @throws InvocationException This exception wraps any exceptions thrown
  126       *         by either the target method of the resource object, or invocation
  127       *         interceptors in this interceptor chain. The target exception is
  128       *         unwrapped at the {@link org.jboss.mx.server.MBeanInvoker} instance.
  129       */
  130      public Object invoke(Invocation invocation) throws Throwable
  131      {
  132         Interceptor ic = invocation.nextInterceptor();
  133         
  134         // if the invocation object does not provide us with more interceptors,
  135         // invoke the dispatcher that lands the invocation to its final target
  136         // in the resource object
  137         if (ic == null)
  138            return invocation.dispatch();
  139         
  140         // see if the next interceptor in the chain is shared
  141         if (ic.isShared())
  142         {
  143            // we require a common interface for all shared interceptors
  144            SharedInterceptor shared = (SharedInterceptor)ic;
  145            
  146            // we invoke shared interceptor it via the MBean server bus, get the
  147            // interceptors view to its MBean server
  148            MBeanServer server = shared.getMBeanServer();
  149            
  150            // And the object name the interceptor is registered under
  151            ObjectName name = shared.getObjectName();
  152            
  153            return server.invoke(
  154                     name, "invoke",                              
  155                     new Object[] { invocation },                    // args
  156                     new String[] { Invocation.class.getName() }     // signature
  157            );
  158         }
  159         
  160         // invoke non-shared interceptor directly via Java reference
  161         else
  162         {
  163            return ic.invoke(invocation);
  164         }
  165      }
  166   
  167      public String getName()
  168      {
  169         return name;
  170      }
  171      
  172      public boolean isShared()
  173      {
  174         return isShared;
  175      }
  176         
  177      public void setLogger(Logger log)
  178      {
  179         this.log = log;
  180      }
  181      
  182      public void init() throws Exception {}
  183   
  184      public void start() {}
  185   
  186      public void stop() throws Exception {}
  187   
  188      public void destroy() {}
  189   
  190      
  191      // Object overrides ----------------------------------------------
  192   
  193      /**
  194       * Returns a string representation of this interceptor instance.
  195       *
  196       * @return  string representation
  197       */
  198      public String toString()
  199      {
  200         String className = getClass().getName();
  201         int index        = className.lastIndexOf('.');
  202         
  203         return className.substring((index < 0) ? 0 : index) + "[name=" + name + "]";
  204      }
  205   }
  206   
  207   

Save This Page
Home » jboss-5.0.0.CR1-src » org.jboss.mx » interceptor » [javadoc | source]