commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rsi...@apache.org
Subject cvs commit: jakarta-commons/discovery/src/java/org/apache/commons/discovery/strategy DefaultLoadStrategy.java
Date Wed, 21 Aug 2002 22:48:46 GMT
rsitze      2002/08/21 15:48:46

  Modified:    discovery/src/java/org/apache/commons/discovery
                        DiscoverSingleton.java ResourceDiscovery.java
                        DiscoverClass.java ServiceDiscoveryTask.java
               discovery/src/java/org/apache/commons/discovery/tools
                        ManagedProperties.java EnvironmentCache.java
               discovery/src/java/org/apache/commons/discovery/base
                        ImplClass.java Environment.java SPInterface.java
               discovery/src/java/org/apache/commons/discovery/strategy
                        DefaultLoadStrategy.java
  Added:       discovery/src/java/org/apache/commons/discovery/tools
                        ClassLoaderUtils.java
               discovery/src/java/org/apache/commons/discovery/base
                        ClassLoaders.java
  Removed:     discovery/src/java/org/apache/commons/discovery
                        ClassLoaders.java
               discovery/src/java/org/apache/commons/discovery/load
                        Loaders.java ClassLoaderUtils.java
  Log:
  1.  Restored addClassLoader to ResourceDiscovery.. I think this is
  probably the best of both worlds. (I can play directly with ResourceDiscovery,
  or work with the ClassLoaders holder when I've got many calls into this..)
  2.  Reorganized a bit as I bring other parts in line with the new classes..
  more to come tomorrow.
  
  Revision  Changes    Path
  1.7       +16 -11    jakarta-commons/discovery/src/java/org/apache/commons/discovery/DiscoverSingleton.java
  
  Index: DiscoverSingleton.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/DiscoverSingleton.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DiscoverSingleton.java	15 Aug 2002 19:58:21 -0000	1.6
  +++ DiscoverSingleton.java	21 Aug 2002 22:48:45 -0000	1.7
  @@ -69,7 +69,7 @@
   import org.apache.commons.discovery.base.Environment;
   import org.apache.commons.discovery.base.ImplClass;
   import org.apache.commons.discovery.base.SPInterface;
  -import org.apache.commons.discovery.load.ClassLoaderUtils;
  +import org.apache.commons.discovery.tools.ClassLoaderUtils;
   import org.apache.commons.discovery.tools.EnvironmentCache;
   
   
  @@ -321,10 +321,11 @@
       public static Object find(Class spiClass, String defaultImpl)
           throws DiscoveryException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return find(new Environment(DiscoverSingleton.class),
  -                    new SPInterface(spiClass),
  +                    spi,
                       DiscoverClass.nullProperties,
  -                    new ImplClass(defaultImpl));
  +                    spi.createImplClass(defaultImpl));
       }
   
       /**
  @@ -350,10 +351,11 @@
                                 String defaultImpl)
           throws DiscoveryException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return find(new Environment(DiscoverSingleton.class),
  -                    new SPInterface(spiClass),
  +                    spi,
                       properties,
  -                    new ImplClass(defaultImpl));
  +                    spi.createImplClass(defaultImpl));
       }
       
       /**
  @@ -377,10 +379,11 @@
                                 String defaultImpl)
           throws DiscoveryException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return find(new Environment(DiscoverSingleton.class),
  -                    new SPInterface(spiClass),
  +                    spi,
                       propertiesFileName,
  -                    new ImplClass(defaultImpl));
  +                    spi.createImplClass(defaultImpl));
       }
   
       /**
  @@ -412,10 +415,11 @@
                                 String defaultImpl)
           throws DiscoveryException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return find(new Environment(groupContext, DiscoverSingleton.class),
  -                    new SPInterface(spiClass),
  +                    spi,
                       properties,
  -                    new ImplClass(defaultImpl));
  +                    spi.createImplClass(defaultImpl));
       }
       
       /**
  @@ -445,10 +449,11 @@
                                 String defaultImpl)
           throws DiscoveryException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return find(new Environment(groupContext, DiscoverSingleton.class),
  -                    new SPInterface(spiClass),
  +                    spi,
                       propertiesFileName,
  -                    new ImplClass(defaultImpl));
  +                    spi.createImplClass(defaultImpl));
       }
   
       
  
  
  
  1.4       +38 -37    jakarta-commons/discovery/src/java/org/apache/commons/discovery/ResourceDiscovery.java
  
  Index: ResourceDiscovery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/ResourceDiscovery.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ResourceDiscovery.java	21 Aug 2002 21:18:32 -0000	1.3
  +++ ResourceDiscovery.java	21 Aug 2002 22:48:45 -0000	1.4
  @@ -57,16 +57,13 @@
   
   package org.apache.commons.discovery;
   
  -import java.io.BufferedReader;
   import java.io.IOException;
  -import java.io.InputStream;
  -import java.io.InputStreamReader;
  -import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.Enumeration;
   import java.util.Vector;
   
   import org.apache.commons.discover.jdk.JDKHooks;
  +import org.apache.commons.discovery.base.ClassLoaders;
   
   
   /**
  @@ -87,7 +84,7 @@
        */
       protected static JDKHooks jdkHooks = JDKHooks.getJDKHooks();
       
  -    protected Vector classLoaders = null;
  +    protected ClassLoaders classLoaders = new ClassLoaders();
       
       /** Construct a new resource discoverer
        */
  @@ -107,44 +104,48 @@
        * Specify set of class loaders to be used in searching.
        */
       public void setClassLoaders(ClassLoaders loaders) {
  -        classLoaders = loaders.getClassLoaders();
  +        classLoaders = loaders;
  +    }
  +
  +    /**
  +     * Specify a new class loader to be used in searching.
  +     * The order of loaders determines the order of the result.
  +     * It is recommended to add the most specific loaders first.
  +     */
  +    public void addClassLoader(ClassLoader loader) {
  +        classLoaders.put(loader);
       }
   
       public ResourceInfo[] findResources(String resourceName) {
  -        ResourceInfo resultA[];
  -        
  -        if (classLoaders != null) {
  -            Vector results = new Vector();
  -        
  -            // For each loader
  -            for( int i=0; i<classLoaders.size() ; i++ ) {
  -                ClassLoader loader=(ClassLoader)classLoaders.elementAt(i);
  -    
  -                Enumeration enum=null;
  -    
  -                try {
  -                    enum=jdkHooks.getResources(loader, resourceName);
  -                } catch( IOException ex ) {
  -                    ex.printStackTrace();
  -                }
  -                if( enum==null ) continue;
  +        Vector results = new Vector();
       
  -                while( enum.hasMoreElements() ) {
  -                    URL url=(URL)enum.nextElement();
  -    
  -                    System.out.println("XXX URL " + url );
  -                    
  -                    ResourceInfo sinfo = new ResourceInfo(resourceName, loader, url);
  -                    results.add(sinfo);
  -                    System.out.println("XXX " + sinfo.toString());
  -                }
  +        // For each loader
  +        for( int i=0; i<classLoaders.size() ; i++ ) {
  +            ClassLoader loader=classLoaders.get(i);
  +
  +            Enumeration enum=null;
  +
  +            try {
  +                enum=jdkHooks.getResources(loader, resourceName);
  +            } catch( IOException ex ) {
  +                ex.printStackTrace();
               }
  -            resultA = new ResourceInfo[ results.size() ];
  -            results.copyInto( resultA );
  -        } else {
  -            resultA = new ResourceInfo[0];
  -        }
  +            if( enum==null ) continue;
   
  +            while( enum.hasMoreElements() ) {
  +                URL url=(URL)enum.nextElement();
  +
  +                System.out.println("XXX URL " + url );
  +                
  +                ResourceInfo sinfo = new ResourceInfo(resourceName, loader, url);
  +                results.add(sinfo);
  +                System.out.println("XXX " + sinfo.toString());
  +            }
  +        }
  +        
  +        ResourceInfo resultA[] = new ResourceInfo[ results.size() ];
  +        results.copyInto( resultA );
  +        
           return resultA;
       }
   }
  
  
  
  1.7       +12 -8     jakarta-commons/discovery/src/java/org/apache/commons/discovery/DiscoverClass.java
  
  Index: DiscoverClass.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/DiscoverClass.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DiscoverClass.java	16 Aug 2002 22:29:35 -0000	1.6
  +++ DiscoverClass.java	21 Aug 2002 22:48:45 -0000	1.7
  @@ -368,10 +368,11 @@
       public Class find(Class spiClass, Properties properties, String defaultImpl)
           throws DiscoveryException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return find(getEnvironment(),
  -                    new SPInterface(spiClass),
  +                    spi,
                       properties,
  -                    new ImplClass(defaultImpl));
  +                    spi.createImplClass(defaultImpl));
       }
       
       /**
  @@ -401,10 +402,11 @@
       public Class find(Class spiClass, String propertiesFileName, String defaultImpl)
           throws DiscoveryException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return find(getEnvironment(),
  -                    new SPInterface(spiClass),
  +                    spi,
                       propertiesFileName,
  -                    new ImplClass(defaultImpl));
  +                    spi.createImplClass(defaultImpl));
       }
   
       /**
  @@ -622,10 +624,11 @@
                  NoSuchMethodException,
                  InvocationTargetException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return newInstance(getEnvironment(),
  -                           new SPInterface(spiClass),
  +                           spi,
                              properties,
  -                           new ImplClass(defaultImpl));
  +                           spi.createImplClass(defaultImpl));
       }
       
       /**
  @@ -660,10 +663,11 @@
                  NoSuchMethodException,
                  InvocationTargetException
       {
  +        SPInterface spi = new SPInterface(spiClass);
           return newInstance(getEnvironment(),
  -                           new SPInterface(spiClass),
  +                           spi,
                              propertiesFileName,
  -                           new ImplClass(defaultImpl));
  +                           spi.createImplClass(defaultImpl));
       }
   
       /**
  
  
  
  1.6       +3 -5      jakarta-commons/discovery/src/java/org/apache/commons/discovery/ServiceDiscoveryTask.java
  
  Index: ServiceDiscoveryTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/ServiceDiscoveryTask.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ServiceDiscoveryTask.java	21 Aug 2002 21:18:32 -0000	1.5
  +++ ServiceDiscoveryTask.java	21 Aug 2002 22:48:45 -0000	1.6
  @@ -65,6 +65,7 @@
   import java.util.Vector;
   
   import org.apache.commons.discover.jdk.JDKHooks;
  +import org.apache.commons.discovery.base.*;
   
   
   /**
  @@ -96,12 +97,9 @@
       public void execute() throws Exception {
           System.out.println("XXX ");
           
  -        ClassLoaders classLoaders = new ClassLoaders();
  -        classLoaders.addClassLoader( JDKHooks.getJDKHooks().getThreadContextClassLoader()
);
  -        classLoaders.addClassLoader( this.getClass().getClassLoader() );
  -
           ResourceDiscovery disc=ResourceDiscovery.newInstance();
  -        disc.setClassLoaders(classLoaders);
  +        disc.addClassLoader( JDKHooks.getJDKHooks().getThreadContextClassLoader() );
  +        disc.addClassLoader( this.getClass().getClassLoader() );
           
           drivers=disc.findResources(name);
   
  
  
  
  1.4       +3 -5      jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/ManagedProperties.java
  
  Index: ManagedProperties.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/ManagedProperties.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ManagedProperties.java	21 Aug 2002 20:31:40 -0000	1.3
  +++ ManagedProperties.java	21 Aug 2002 22:48:46 -0000	1.4
  @@ -55,17 +55,15 @@
   
   package org.apache.commons.discovery.tools;
   
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
   import java.util.Enumeration;
   import java.util.HashMap;
   import java.util.Hashtable;
   import java.util.Map;
   import java.util.Properties;
   
  -import java.security.AccessController;
  -import java.security.PrivilegedAction;
  -
  -import org.apache.commons.discover.jdk.*;
  -import org.apache.commons.discovery.load.ClassLoaderUtils;
  +import org.apache.commons.discover.jdk.JDKHooks;
   
   
   
  
  
  
  1.4       +1 -9      jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/EnvironmentCache.java
  
  Index: EnvironmentCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/EnvironmentCache.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- EnvironmentCache.java	21 Aug 2002 20:31:40 -0000	1.3
  +++ EnvironmentCache.java	21 Aug 2002 22:48:46 -0000	1.4
  @@ -61,18 +61,10 @@
   
   package org.apache.commons.discovery.tools;
   
  -import java.lang.reflect.InvocationTargetException;
  -import java.util.Map;
  -import java.util.Collection;
   import java.util.HashMap;
  -import java.util.Iterator;
  -import java.util.Properties;
   
  -import org.apache.commons.discover.jdk.*;
  +import org.apache.commons.discover.jdk.JDKHooks;
   import org.apache.commons.discovery.base.Environment;
  -import org.apache.commons.discovery.base.ImplClass;
  -import org.apache.commons.discovery.base.SPInterface;
  -import org.apache.commons.discovery.load.ClassLoaderUtils;
   
   
   /**
  
  
  
  1.1                  jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/ClassLoaderUtils.java
  
  Index: ClassLoaderUtils.java
  ===================================================================
  /*
   * $Header$
   * $Revision$
   * $Date$
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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", "Commons", 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 apache@apache.org.
   *
   * 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/>.
   *
   */
  
  package org.apache.commons.discovery.tools;
  
  import java.io.InputStream;
  
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  
  import org.apache.commons.discovery.DiscoveryException;
  import org.apache.commons.discovery.base.ClassLoaders;
  
  
  /**
   * Mechanisms to locate and load a class.
   * The load methods locate a class only.
   * The find methods locate a class and verify that the
   * class implements an given interface or extends a given class.
   * 
   * @author Richard A. Sitze
   * @author Craig R. McClanahan
   * @author Costin Manolache
   */
  public class ClassLoaderUtils {
      private static final boolean debug = false;
      
      /**
       * Get package name.
       * Not all class loaders 'keep' package information,
       * in which case Class.getPackage() returns null.
       * This means that calling Class.getPackage().getName()
       * is unreliable at best.
       */
      public static String getPackageName(Class clazz) {
          Package clazzPackage = clazz.getPackage();
          String packageName;
          if (clazzPackage != null) {
              packageName = clazzPackage.getName();
          } else {
              String clazzName = clazz.getName();
              packageName = new String(clazzName.toCharArray(), 0, clazzName.lastIndexOf('.'));
          }
          return packageName;
      }
      
      /**
       * Load the class <code>serviceImplName</code>, no safety checking
       * 
       * @param serviceImplName The name of the class to load.
       */
      private static Class rawLoadClass(String serviceImplName, ClassLoader loader)
          throws DiscoveryException
      {
          Class clazz = null;
          
          try {
              // first the thread class loader
              clazz = loader.loadClass(serviceImplName);
          } catch (ClassNotFoundException e) {
              clazz = null;
          }
          
          return clazz;
      }
      
      /**
       * Load the class <code>serviceImplName</code>.
       * Try each classloader in succession,
       * until first succeeds, or all fail.
       * 
       * @param serviceImplName The name of the class to load.
       */
      public static Class loadClass(String serviceImplName,
                                    ClassLoaders loaders)
          throws DiscoveryException
      {
          Class clazz = null;
          
          if (serviceImplName != null  &&  serviceImplName.length() > 0) {
              if (debug)
                  System.out.println("Loading class '" + serviceImplName + "'");
  
              for (int i = 0; i < loaders.size() && clazz == null; i++)
              {
                  ClassLoader loader = loaders.get(i);
                  if (loader != null)
                      clazz = rawLoadClass(serviceImplName, loader);
              }
          }
          
          return clazz;
      }
      
  
      /**
       * Load the resource <code>resourceName</code>.
       * Try each classloader in succession,
       * until first succeeds, or all fail.
       * 
       * @param resourceName The name of the resource to load.
       */
      public static InputStream getResourceAsStream(String resourceName,
                                                    ClassLoaders loaders)
          throws DiscoveryException
      {
          InputStream stream = null;
          
          if (resourceName != null  &&  resourceName.length() > 0) {
              if (debug)
                  System.out.println("Loading resource '" + resourceName + "'");
  
              for (int i = 0; i < loaders.size() && stream == null; i++)
              {
                  ClassLoader loader = loaders.get(i);
                  if (loader != null)
                      stream = loader.getResourceAsStream(resourceName);
              }
          }
          
          return stream;
      }
      
      /**
       * Load the resource <code>resourceName</code>.
       * Try each classloader in succession,
       * until first succeeds, or all fail.
       * If all fail and <code>resouceName</code> is not absolute
       * (doesn't start with '/' character), then retry with
       * <code>packageName/resourceName</code> after changing all
       * '.' to '/'.
       * 
       * @param resourceName The name of the resource to load.
       */
      public static InputStream getResourceAsStream(String packageName,
                                                    String resourceName,
                                                    ClassLoaders loaders)
          throws DiscoveryException
      {
          InputStream stream = getResourceAsStream(resourceName, loaders);
          
          /**
           * If we didn't find the resource, and if the resourceName
           * isn't an 'absolute' path name, then qualify with
           * package name of the spi.
           */
          return (stream == null)
                 ? getResourceAsStream(qualifyName(packageName, resourceName),
                                       loaders)
                 : stream;
      }
      
      
      /**
       * If <code>name</code> represents an absolute path name, then return null.
       * Otherwise, prepend packageName to name, convert all '.' to '/', and
       * return results.
       */
      private static final String qualifyName(String packageName, String name) {
          return (name.charAt(0)=='/')
                 ? null
                 : packageName.replace('.','/') + "/" + name;
      }
  }
  
  
  
  1.3       +22 -34    jakarta-commons/discovery/src/java/org/apache/commons/discovery/base/ImplClass.java
  
  Index: ImplClass.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/base/ImplClass.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ImplClass.java	15 Aug 2002 18:22:44 -0000	1.2
  +++ ImplClass.java	21 Aug 2002 22:48:46 -0000	1.3
  @@ -65,7 +65,7 @@
   import java.lang.reflect.InvocationTargetException;
   
   import org.apache.commons.discovery.DiscoveryException;
  -import org.apache.commons.discovery.load.Loaders;
  +import org.apache.commons.discovery.tools.ClassLoaderUtils;
   
   
   /**
  @@ -74,6 +74,8 @@
   public class ImplClass {
       private final String implName;
   
  +    private Class spiClass;
  +    
       private Class implClass;
       
       private Class  paramClasses[] = null;
  @@ -85,29 +87,6 @@
        * Class whose name is <code>implName</code>.
        * 
        * @param implName The SPI class name
  -     */
  -    public ImplClass(String implName) {
  -        this.implName = implName;
  -        this.implClass = null;
  -    }
  -    
  -    /**
  -     * Construct object representing implementation
  -     * Class <code>implClass</code>.
  -     * 
  -     * @param implClass The SPI class
  -     */
  -    public ImplClass(Class implClass) {
  -        this.implName = (implClass != null) ? implClass.getName() : null;
  -        this.implClass = implClass;
  -    }
  -    
  -
  -    /**
  -     * Construct object representing implementation
  -     * Class whose name is <code>implName</code>.
  -     * 
  -     * @param implName The SPI class name
        * 
        * @param constructorParamClasses classes representing the
        *        constructor argument types.
  @@ -115,11 +94,13 @@
        * @param constructorParams objects representing the
        *        constructor arguments.
        */
  -    public ImplClass(String implName,
  -                     Class constructorParamClasses[],
  -                     Object constructorParams[])
  +    ImplClass(Class spiClass,
  +              String implName,
  +              Class constructorParamClasses[],
  +              Object constructorParams[])
       {
  -        this(implName);
  +        this.implName = implName;
  +        this.implClass = null;
           this.paramClasses = constructorParamClasses;
           this.params = constructorParams;
       }
  @@ -136,11 +117,13 @@
        * @param constructorParams objects representing the
        *        constructor arguments.
        */
  -    public ImplClass(Class implClass,
  -                     Class constructorParamClasses[],
  -                     Object constructorParams[])
  +    ImplClass(Class spiClass,
  +              Class implClass,
  +              Class constructorParamClasses[],
  +              Object constructorParams[])
       {
  -        this(implClass);
  +        this.implName = (implClass != null) ? implClass.getName() : null;
  +        this.implClass = implClass;
           this.paramClasses = constructorParamClasses;
           this.params = constructorParams;
       }
  @@ -177,9 +160,14 @@
        *                the thread context class loader or the calling class'
        *                class loader.
        */    
  -    public Class loadImplClass(Loaders loaders, boolean libOnly) {
  +    public Class loadImplClass(ClassLoaders loaders) {
           if (implClass == null) {
  -            implClass = loaders.loadClass(getImplName(), libOnly);
  +            implClass = ClassLoaderUtils.loadClass(getImplName(), loaders);
  +            
  +            if (implClass != null  &&  !spiClass.isAssignableFrom(implClass)) {
  +                throw new DiscoveryException("Class " + getImplName() +
  +                              " does not implement " + spiClass.getName());
  +            }
           }
   
           return implClass;
  
  
  
  1.6       +1 -1      jakarta-commons/discovery/src/java/org/apache/commons/discovery/base/Environment.java
  
  Index: Environment.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/base/Environment.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Environment.java	21 Aug 2002 20:31:41 -0000	1.5
  +++ Environment.java	21 Aug 2002 22:48:46 -0000	1.6
  @@ -63,7 +63,7 @@
   
   import org.apache.commons.discover.jdk.JDKHooks;
   import org.apache.commons.discovery.DiscoverClass;
  -import org.apache.commons.discovery.load.ClassLoaderUtils;
  +import org.apache.commons.discovery.tools.ClassLoaderUtils;
   
   
   /**
  
  
  
  1.3       +2 -2      jakarta-commons/discovery/src/java/org/apache/commons/discovery/base/SPInterface.java
  
  Index: SPInterface.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/base/SPInterface.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SPInterface.java	16 Aug 2002 22:29:35 -0000	1.2
  +++ SPInterface.java	21 Aug 2002 22:48:46 -0000	1.3
  @@ -190,7 +190,7 @@
        * if set, the constructor type/object arguments.
        */    
       public ImplClass createImplClass(String className) {
  -        return new ImplClass(className, paramClasses, params);
  +        return new ImplClass(getSPClass(), className, paramClasses, params);
       }
   
       /**
  @@ -198,6 +198,6 @@
        * if set, the constructor type/object arguments.
        */    
       public ImplClass createImplClass(Class clazz) {
  -        return new ImplClass(clazz, paramClasses, params);
  +        return new ImplClass(getSPClass(), clazz, paramClasses, params);
       }
   }
  
  
  
  1.1                  jakarta-commons/discovery/src/java/org/apache/commons/discovery/base/ClassLoaders.java
  
  Index: ClassLoaders.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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", "Commons", 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 apache@apache.org.
   *
   * 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/>.
   *
   */
  
  package org.apache.commons.discovery.base;
  
  import java.util.Vector;
  
  
  /**
   * There are many different contexts in which
   * loaders can be used.  This provides a holder
   * for a set of class loaders, so that they
   * don't have to be build back up everytime...
   *
   * @author Richard A. Sitze
   * @author Craig R. McClanahan
   * @author Costin Manolache
   */
  public class ClassLoaders
  {
      protected Vector classLoaders = new Vector();
      
      /** Construct a new class loader set
       */
      public ClassLoaders() {
      }
      
      public int size() {
          return classLoaders.size();
      }
      
      public ClassLoader get(int idx) {
          return (ClassLoader)classLoaders.elementAt(idx);
      }
  
      /**
       * Specify a new class loader to be used in searching.
       * The order of loaders determines the order of the result.
       * It is recommended to add the most specific loaders first.
       */
      public void put(ClassLoader classLoader) {
          boolean isNew = true;
          
          for (int idx = 0; idx < size(); idx++) {
              if (wouldUseClassLoader(get(idx), classLoader)) {
                  isNew = false;
                  break;
              }
          }
  
          if (isNew) {
              classLoaders.addElement(classLoader);
          }
      }
      
      
      /**
       * Would <code>thisClassLoader</code> use <code>classLoader</code>?
       * Return <code>true</code> if <code>classLoader</code> is the
same
       * as </code>thisClassLoader</code> or if <code>classLoader</code>
       * is an ancestor of </code>thisClassLoader</code>.
       */
      private static final boolean wouldUseClassLoader(final ClassLoader thisClassLoader,
                                                       final ClassLoader classLoader) {
          /* bootstrap classloader, at root of all trees! */
          if (classLoader == null)
              return true;
                          
          for(ClassLoader walker = thisClassLoader;
              walker != null;
              walker = walker.getParent())
          {
              if (walker == classLoader) {
                  return true;
              }
          }
          
          return true;
      }
  }
  
  
  
  1.5       +62 -6     jakarta-commons/discovery/src/java/org/apache/commons/discovery/strategy/DefaultLoadStrategy.java
  
  Index: DefaultLoadStrategy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/strategy/DefaultLoadStrategy.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DefaultLoadStrategy.java	15 Aug 2002 18:22:44 -0000	1.4
  +++ DefaultLoadStrategy.java	21 Aug 2002 22:48:46 -0000	1.5
  @@ -65,11 +65,13 @@
   import java.io.InputStream;
   import java.util.Properties;
   
  +import org.apache.commons.discover.jdk.JDKHooks;
   import org.apache.commons.discovery.DiscoveryException;
  +import org.apache.commons.discovery.base.ClassLoaders;
   import org.apache.commons.discovery.base.Environment;
   import org.apache.commons.discovery.base.ImplClass;
   import org.apache.commons.discovery.base.SPInterface;
  -import org.apache.commons.discovery.load.Loaders;
  +import org.apache.commons.discovery.tools.ClassLoaderUtils;
   
   
   /**
  @@ -92,12 +94,10 @@
   
       private final Environment env;
       private final SPInterface spi;
  -    private final Loaders loaders;
       
       public DefaultLoadStrategy(Environment env, SPInterface spi) {
           this.env = env;
           this.spi = spi;
  -        this.loaders = new Loaders(env, spi);
       }
       
       /**
  @@ -169,7 +169,7 @@
   
           if (className != null) {
               implClass = spi.createImplClass(className);
  -            implClass.loadImplClass(loaders, env.getSearchLibOnly());
  +            implClass.loadImplClass(getLoaders(env.getSearchLibOnly()));
           } else {
               // All else fails: try the fallback implementation class,
               // but limit loaders to 'system' loaders, in an
  @@ -177,7 +177,7 @@
               // the one that was intended.
               implClass = defaultImpl;
               if (implClass != null) {
  -                implClass.loadImplClass(loaders, true);
  +                implClass.loadImplClass(getLoaders(true));
               }
           }
   
  @@ -218,8 +218,19 @@
           
           if (propertiesFileName != null) {
               try {
  +                String packageName = ClassLoaderUtils.getPackageName(spi.getSPClass());
  +        
                   InputStream stream =
  -                    loaders.loadResourceAsStream(propertiesFileName);
  +                    (env.getGroupContext() == null)
  +                        ? null
  +                        : ClassLoaderUtils.getResourceAsStream(packageName,
  +                                  env.getGroupContext() + "." + propertiesFileName,
  +                                  appLoaders);
  +        
  +                if (stream == null)
  +                    stream = ClassLoaderUtils.getResourceAsStream(packageName,
  +                                                                  propertiesFileName,
  +                                                                  appLoaders);
       
                   if (stream != null) {
                       properties = new Properties();
  @@ -237,5 +248,50 @@
           }
           
           return properties;
  +    }
  +    
  +    
  +    public final ClassLoaders getLoaders(boolean libOnly) {
  +        return libOnly ? getLibLoaders() : getAppLoaders();
  +    }
  +
  +
  +    /**
  +     * List of 'library' class loaders to the SPI.
  +     * The last should always return a non-null loader, so we
  +     * always (?!) have a list of at least one classloader.
  +     */
  +    private ClassLoaders libLoaders;
  +    public ClassLoaders getLibLoaders()
  +    {
  +        if (libLoaders == null) {
  +            libLoaders = new ClassLoaders();
  +            
  +            libLoaders.put(spi.getSPClass().getClassLoader());
  +            libLoaders.put(env.getRootDiscoveryClass().getClassLoader());
  +            libLoaders.put(JDKHooks.getJDKHooks().getSystemClassLoader());
  +        }
  +        
  +        return libLoaders;
  +    }
  +    
  +    private ClassLoaders appLoaders;
  +    public ClassLoaders getAppLoaders()
  +    {
  +        if (appLoaders == null) {
  +            appLoaders = new ClassLoaders();
  +            
  +            appLoaders.put(env.getThreadContextClassLoader());
  +            
  +            if (env.getCallingClass() != null) {
  +                appLoaders.put(env.getCallingClass().getClassLoader());
  +            }
  +            
  +            appLoaders.put(spi.getSPClass().getClassLoader());
  +            appLoaders.put(env.getRootDiscoveryClass().getClassLoader());
  +            appLoaders.put(JDKHooks.getJDKHooks().getSystemClassLoader());
  +        }
  +        
  +        return appLoaders;
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message