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 ServiceFinder.java
Date Tue, 09 Jul 2002 20:45:21 GMT
rsitze      2002/07/09 13:45:21

  Modified:    discovery/src/java/org/apache/commons/discovery
                        ServiceFinder.java
  Log:
  Add find methods that accept an optional prefix for property file name.
  For example, if the property filename is 'file.props', an invoking component
  could specify a 'prefix'.  The logic then looks for 'prefix.file.props' first
  (using all available classloaders), and failing that falls back and looks
  for 'file.props'.
  
  Revision  Changes    Path
  1.2       +86 -15    jakarta-commons/discovery/src/java/org/apache/commons/discovery/ServiceFinder.java
  
  Index: ServiceFinder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/ServiceFinder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ServiceFinder.java	9 Jul 2002 18:03:23 -0000	1.1
  +++ ServiceFinder.java	9 Jul 2002 20:45:20 -0000	1.2
  @@ -154,20 +154,16 @@
        *            cannot be instantiated,
        *            or is not an instance of <code>spi</code>.
        */
  -    public static Object find(Class rootFinderClass,
  -                              SPIContext spiContext,
  -                              Properties properties,
  -                              String defaultImplName)
  +    private static Object find(ClassFinder classFinder,
  +                               SPIContext spiContext,
  +                               Properties properties,
  +                               String defaultImplName)
           throws ServiceException
       {
  -        // thread context can change on each call,
  -        // so establish context for this one call.
  -        ClassFinder classFinder = new ClassFinder(spiContext, rootFinderClass);
  -
           /**
            * Return previously registered service object (not class)
            * for this spi.  Try contextClassLoader first, and if that
  -         * fails then try the local class loader.
  +         * fails then try the spi's class loader.
            */
           Object service = (spiContext.getThreadContextClassLoader() == null)
                   ? null
  @@ -177,8 +173,8 @@
               service = get(spiContext.getSPI().getName(),
                             spiContext.getSPI().getClassLoader());
           }
  -        
  -        if (service == null) {
  +
  +        if (service != null) {        
               // First, try the system property
               Class clazz = classFinder.systemFindClass();
       
  @@ -222,6 +218,18 @@
           return service;
       }
       
  +    public static Object find(Class rootFinderClass,
  +                              SPIContext spiContext,
  +                              Properties properties,
  +                              String defaultImplName)
  +        throws ServiceException
  +    {
  +        // thread context can change on each call,
  +        // so establish context for this one call.
  +        ClassFinder classFinder = new ClassFinder(spiContext, rootFinderClass);
  +        return find(classFinder, spiContext, properties, defaultImplName);
  +    }
  +    
       /**
        * Equivalent to
        * <code>find(ServiceFinder.class, spiContext, properties, defaultImplName)</code>.
  @@ -261,20 +269,30 @@
   
       /**
        * Load properties file, and call
  -     * <code>find(spiContext, properties, defaultImplName)</code>.
  +     * <code>find(rootFinderClass, spiContext, properties, defaultImplName)</code>.
        */    
       public static Object find(Class rootFinderClass,
                                 SPIContext spiContext,
  +                              String overloadPrefix,
                                 String propertiesFileName,
                                 String defaultImplName)
           throws ServiceException
       {
  +        // thread context can change on each call,
  +        // so establish context for this one call.
  +        ClassFinder classFinder = new ClassFinder(spiContext, rootFinderClass);
  +
           Properties properties = null;
           
           if (propertiesFileName != null) {
               try {
                   InputStream stream =
  -                    spiContext.getThreadContextClassLoader().getResourceAsStream(propertiesFileName);
  +                    (overloadPrefix == null)
  +                    ? null
  +                    : classFinder.findResourceAsStream(overloadPrefix + "." + propertiesFileName,
false);
  +
  +                if (stream == null)    
  +                    stream = classFinder.findResourceAsStream(propertiesFileName, false);
       
                   if (stream != null) {
                       properties = new Properties();
  @@ -289,11 +307,64 @@
               }
           }
           
  -        return find(rootFinderClass, spiContext, properties, defaultImplName);
  +        return find(classFinder, spiContext, properties, defaultImplName);
  +    }
  +
  +    /**
  +     * Load properties file, and call
  +     * <code>find(ServiceFinder.class, spiContext, propertiesFileName, defaultImplName)</code>.
  +     */    
  +    public static Object find(SPIContext spiContext,
  +                              String overloadPrefix,
  +                              String propertiesFileName,
  +                              String defaultImplName)
  +        throws ServiceException
  +    {
  +        return find(ServiceFinder.class, spiContext, overloadPrefix, propertiesFileName,
defaultImplName);
  +    }
  +    
  +    /**
  +     * Equivalent to
  +     * <code>find(rootFinderClass, new SPIContext(spi), propertiesFileName, defaultImplName)</code>.
  +     */    
  +    public static Object find(Class rootFinderClass,
  +                              Class spi,
  +                              String overloadPrefix,
  +                              String propertiesFileName,
  +                              String defaultImplName)
  +        throws ServiceException
  +    {
  +        return find(rootFinderClass, new SPIContext(spi), overloadPrefix, propertiesFileName,
defaultImplName);
  +    }
  +    
  +    /**
  +     * Equivalent to
  +     * <code>find(new SPIContext(spi), propertiesFileName, defaultImplName)</code>.
  +     */    
  +    public static Object find(Class spi,
  +                              String overloadPrefix,
  +                              String propertiesFileName,
  +                              String defaultImplName)
  +        throws ServiceException
  +    {
  +        return find(new SPIContext(spi), overloadPrefix, propertiesFileName, defaultImplName);
       }
   
       /**
        * Load properties file, and call
  +     * <code>find(rootFinderClass, spiContext, properties, defaultImplName)</code>.
  +     */    
  +    public static Object find(Class rootFinderClass,
  +                              SPIContext spiContext,
  +                              String propertiesFileName,
  +                              String defaultImplName)
  +        throws ServiceException
  +    {
  +        return find(rootFinderClass, spiContext, (String)null, propertiesFileName, defaultImplName);
  +    }
  +    
  +    /**
  +     * Load properties file, and call
        * <code>find(ServiceFinder.class, spiContext, propertiesFileName, defaultImplName)</code>.
        */    
       public static Object find(SPIContext spiContext,
  @@ -301,7 +372,7 @@
                                 String defaultImplName)
           throws ServiceException
       {
  -        return find(ServiceFinder.class, spiContext, propertiesFileName, defaultImplName);
  +        return find(ServiceFinder.class, spiContext, (String)null, propertiesFileName,
defaultImplName);
       }
       
       /**
  
  
  

--
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