costin      00/12/27 22:15:21

  Added:       src/share/org/apache/tomcat/util/hooks Hooks.java
  Log:
  First attempt to refactor hooks. Moving them in a util package is not a
  bad idea ( and it's inspired by the new organization in 2.0 - I was
  initially surprized to find ap_hook.h in apr-utils, but it's quite a generic
  concept ).
  
  The only disadvantage of using "generic" hooks is that a cast is
  needed ( to BaseInterceptor ), but it gives a lot more flexibility.
  I think there is a simple workaround for that.
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat/src/share/org/apache/tomcat/util/hooks/Hooks.java
  
  Index: Hooks.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  package org.apache.tomcat.util.hooks;
  
  import org.apache.tomcat.util.*;
  import java.io.*;
  import java.net.*;
  import java.util.*;
  import java.lang.reflect.*;
  
  /** Hooks support. Hooks implement a chain-of-command pattern, and
   * are commonly used in most web servers as a mechanism of extensibility.
   *
   * This class doesn't deal with hook invocation - the program is expected
   * to use interfaces or base classes with the hooks beeing methods that
   * are invoked. Reflection-based invocation is very expensive and shouldn't 
   * be used.
   * 
   * The Hooks class will provide support for registering and maintaining
   * a list of modules implementing each hook.
   *
   * The module can be added automatically to the right lists by using
   * introspection ( if the module implements a certain method, it'll
   * be added to the chain for the hook with the same name ). It is also
   * possible for a module to explicitely register hooks.
   *
   * This class is modeled after Apache2.0 hooks - most web servers are using
   * this pattern, but so far A2.0 has the most flexible and powerfull
   * implementation
   */
  public class Hooks {
      int hookCount;
      String hookNames[];
      Vector hooksV[];
  
      Object hooks[][];
  
      Vector allModulesV;
      Object allModules[];
  
      private static final int dL=0;
  
      public Hooks(int hookCount ) {
        this.hookCount=hookCount;
        hooksV=new Vector[hookCount];
        for( int i=0; i<hookCount ; i++ )
            hooksV[i]=new Vector();
        allModulesV=new Vector();
        hookNames=new String[hookCount];
        hooks=new Object[hookCount][];
      }
  
      public int registerHook( String name ) {
        for( int i=0; i<hookNames.length; i++ ) {
            if( hookNames[i]==null ) {
                hookNames[i]=name;
                return i;
            }
        }
        throw new RuntimeException("Too many hooks");
      }
  
      public void registerHook( String name, int id ) {
        hookNames[id]=name;
      }
  
      public String getHookName( int id ) {
        return hookNames[id];
      }
  
      public int getHookId( String hookName ) {
        for( int i=0; i< hookNames.length; i++ ) {
            if( hookName.equals(hookNames[i]))
                return i;
        }
        return -1;
      }
      
  
      /** Add the module to all the hook chains it's implements
       *  The hook name should match a method defined in the module
       *  ( not inherited - explicitely defined there )
       */
      public void addModule( Object bi ) {
        for( int i=0; i< hookNames.length ; i++ ) {
            if( hasHook( bi, hookNames[i] )) {
                if( dL > 0 ) debug( "Adding " + hookNames[i] + " " +bi );
                hooksV[i].addElement( bi );
                hooks[i]=null;
            }
        }
        allModules=null;
        allModulesV.addElement( bi );
      }
  
  
      public void removeModule( Object bi ) {
        for( int i=0; i<hookNames.length; i++ ) {
            if( hooksV[i].contains( bi )) {
                hooksV[i].removeElement( bi );
                hooks[i]=null;
            }
        }
        allModulesV.removeElement( bi );
        allModules=null;
      }
  
      Hooks localHooks;
      
      public Object[] getModules( int type )
      {
        if( hooks[type] != null ) {
            return hooks[type];
        }
  
        hooks[type]=new Object[hooksV[type].size()];
        hooksV[type].copyInto( hooks[type] );
        return hooks[type];
      }
      
      /** Get all interceptors
       */
      public Object[] getModules()
      {
        if( allModules!=null ) {
            return allModules;
        }
        allModules=new Object[allModulesV.size()];
        allModulesV.copyInto( allModules );
        return allModules;
      }
  
      private void debug(String s ) {
        System.out.println("Hooks: " + s );
      }
  
      /** Test if the interceptor implements a particular
       *  method
       */
      public static boolean hasHook( Object obj, String methodN ) {
        try {
            Method myMethods[]=obj.getClass().getMethods();
            for( int i=0; i< myMethods.length; i++ ) {
                if( methodN.equals ( myMethods[i].getName() )) {
                    // check if it's overriden
                    Class declaring=myMethods[i].getDeclaringClass();
                    Class parentOfDeclaring=declaring.getSuperclass();
                    // this works only if the base class doesn't extend
                    // another class.
  
                    // if the method is declared in a top level class
                    // like BaseInterceptor parent is Object, otherwise
                    // parent is BaseInterceptor or an intermediate class
                    if( ! "java.lang.Object".
                        equals(parentOfDeclaring.getName() )) {
                        return true;
                    }
                }
            }
        } catch ( Exception ex ) {
            ex.printStackTrace();
        }
        return false;
      }
  
  }
  
  
  

Reply via email to