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 ClassFinder.java SPIContext.java Discovery.java
Date Thu, 08 Aug 2002 17:02:08 GMT
rsitze      2002/08/08 10:02:08

  Modified:    discovery/src/java/org/apache/commons/discovery
                        ClassFinder.java SPIContext.java Discovery.java
  Log:
  Added groupContext back as cache qualifier
  
  Revision  Changes    Path
  1.8       +4 -8      jakarta-commons/discovery/src/java/org/apache/commons/discovery/ClassFinder.java
  
  Index: ClassFinder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/ClassFinder.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ClassFinder.java	5 Aug 2002 22:06:03 -0000	1.7
  +++ ClassFinder.java	8 Aug 2002 17:02:07 -0000	1.8
  @@ -90,7 +90,6 @@
        * looking for an implementation of.
        */
       private final SPIContext spiContext;
  -    private final String     groupContext;
       private final Class      rootFinderClass;
       
       /**
  @@ -103,11 +102,9 @@
       
       ClassLoader[] getAllLoaders() { return allLoaders; }
   
  -    public ClassFinder(String groupContext,
  -                       SPIContext spiContext,
  +    public ClassFinder(SPIContext spiContext,
                          Class rootFinderClass)
       {
  -        this.groupContext = groupContext;
           this.spiContext = spiContext;
           this.rootFinderClass = rootFinderClass;
           this.systemLoaders = getSystemLoaders(spiContext, rootFinderClass);
  @@ -120,11 +117,10 @@
                          Class spi,
                          Class rootFinderClass)
       {
  -        this (groupContext, new SPIContext(spi), rootFinderClass);
  +        this (new SPIContext(groupContext, spi), rootFinderClass);
       }
       
       
  -    public String getGroupContext() { return groupContext; }
       public SPIContext getSPIContext() { return spiContext; }
       
       
  @@ -179,10 +175,10 @@
           String packageName = spiContext.getSPI().getPackage().getName();
   
           InputStream stream =
  -            (getGroupContext() == null)
  +            (spiContext.getGroupContext() == null)
                   ? null
                   : ClassLoaderUtils.getResourceAsStream(packageName,
  -                          getGroupContext() + "." + resourceName,
  +                          spiContext.getGroupContext() + "." + resourceName,
                             allLoaders);
   
           if (stream == null)
  
  
  
  1.4       +10 -2     jakarta-commons/discovery/src/java/org/apache/commons/discovery/SPIContext.java
  
  Index: SPIContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/SPIContext.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SPIContext.java	26 Jul 2002 22:35:05 -0000	1.3
  +++ SPIContext.java	8 Aug 2002 17:02:07 -0000	1.4
  @@ -85,14 +85,18 @@
        */
       private final ClassLoader[] loaders;
   
  +    private final String groupContext;
  +    
       /**
        * The service programming interface: intended to be
        * an interface or abstract class, but not limited
        * to those two.
        */        
  -    private Class spi;
  +    private final Class spi;
  +    
   
  -    public SPIContext(Class spi) {
  +    public SPIContext(String groupContext, Class spi) {
  +        this.groupContext = groupContext;
           this.spi = spi;
           this.loaders = ClassLoaderUtils.compactUniq(
               new ClassLoader[] { threadContextClassLoader,
  @@ -106,6 +110,10 @@
       
       public ClassLoader[] getClassLoaders() {
           return loaders;
  +    }
  +    
  +    public String getGroupContext() {
  +        return groupContext;
       }
       
       public Class getSPI() {
  
  
  
  1.10      +114 -44   jakarta-commons/discovery/src/java/org/apache/commons/discovery/Discovery.java
  
  Index: Discovery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/Discovery.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Discovery.java	5 Aug 2002 22:06:03 -0000	1.9
  +++ Discovery.java	8 Aug 2002 17:02:07 -0000	1.10
  @@ -724,7 +724,7 @@
                   }
                   
                   if (service instanceof Service) {
  -                    ((Service)service).init(classFinder.getGroupContext(), properties);
  +                    ((Service)service).init(classFinder.getSPIContext().getGroupContext(),
properties);
                   }
               }
           }
  @@ -796,34 +796,78 @@
            * 'null' (bootstrap/system class loader) thread context class loader
            * is ok...  Until we learn otherwise.
            */
  -        synchronized (spi_caches) {
  -            HashMap spis =
  -                (HashMap)spi_caches.get(threadContextClassLoader);
  +        synchronized (root_cache) {
  +            HashMap groups = (HashMap)root_cache.get(threadContextClassLoader);
   
  -            if (spis != null) {
  -                Iterator it = spis.values().iterator();
  +            if (groups != null) {
  +                Iterator groupIter = groups.values().iterator();
   
  -                while (it.hasNext()) {
  -                    Object service = (Object)it.next();
  +                while (groupIter.hasNext()) {
  +                    HashMap spis = (HashMap)groupIter.next();
                       
  -                    if (service instanceof Service)
  -                        ((Service)service).release();
  +                    if (spis != null) {
  +                        Iterator spiIter = spis.values().iterator();
  +        
  +                        while (spiIter.hasNext()) {
  +                            Object service = (Object)spiIter.next();
  +                            
  +                            if (service instanceof Service)
  +                                ((Service)service).release();
  +                        }
  +                        spis.clear();
  +                    }
                   }
  +                groups.clear();
  +            }
  +            root_cache.remove(threadContextClassLoader);
  +        }
  +    }
  +    
  +    
  +    /**
  +     * Release any internal references to a previously created service
  +     * instance associated with the current thread context class loader.
  +     * If the SPI instance implements <code>Service</code>, then call
  +     * <code>release()</code>.
  +     */
  +    public static void release(String groupContext) {
  +        ClassLoader threadContextClassLoader =
  +            ClassLoaderUtils.getThreadContextClassLoader();
   
  -                spis.clear();
  +        /**
  +         * 'null' (bootstrap/system class loader) thread context class loader
  +         * is ok...  Until we learn otherwise.
  +         */
  +        synchronized (root_cache) {
  +            HashMap groups = (HashMap)root_cache.get(threadContextClassLoader);
   
  -                spi_caches.remove(threadContextClassLoader);
  +            if (groups != null) {
  +                HashMap spis = (HashMap)groups.get(groupContext);
  +                
  +                if (spis != null) {
  +                    Iterator spiIter = spis.values().iterator();
  +    
  +                    while (spiIter.hasNext()) {
  +                        Object service = (Object)spiIter.next();
  +                        
  +                        if (service instanceof Service)
  +                            ((Service)service).release();
  +                    }
  +                    spis.clear();
  +                }
  +                groups.remove(groupContext);
               }
  +            root_cache.remove(threadContextClassLoader);
           }
       }
  -    
  +
       /**
        * Release any internal references to a previously created service
        * instance associated with the current thread context class loader.
        * If the SPI instance implements <code>Service</code>, then call
        * <code>release()</code>.
        */
  -    public static void release(Class spi) {
  +    public static void release(String groupContext, Class spi) {
           ClassLoader threadContextClassLoader =
               ClassLoaderUtils.getThreadContextClassLoader();
   
  @@ -832,32 +876,39 @@
            * is ok...  Until we learn otherwise.
            */
           if (spi != null) {
  -            synchronized (spi_caches) {
  -                HashMap spis =
  -                    (HashMap)spi_caches.get(threadContextClassLoader);
  +            synchronized (root_cache) {
  +                HashMap groups = (HashMap)root_cache.get(threadContextClassLoader);
   
  -                if (spis != null) {
  -                    Object service = (Object)spis.get(spi.getName());
  -                    
  -                    if (service instanceof Service)
  -                        ((Service)service).release();
  -                    
  -                    spis.remove(spi.getName());
  +                if (groups != null) {
  +                    HashMap spis = (HashMap)groups.get(groupContext);
  +
  +                    if (spis != null) {
  +                        Object service = (Object)spis.get(spi.getName());
  +                        
  +                        if (service instanceof Service)
  +                            ((Service)service).release();
  +                        
  +                        spis.remove(spi.getName());
  +                    }
                   }
               }
           }
       }
   
  +    /**
  +     * Release any internal references to a previously created service
  +     * instance associated with the current thread context class loader.
  +     * If the SPI instance implements <code>Service</code>, then call
  +     * <code>release()</code>.
  +     */
  +    public static void release(Class spi) {
  +        release(nullGroupContext, spi);
  +    }
  +    
       
       /************************* SPI CACHE SUPPORT *************************
        * 
        * Cache services by a 'key' unique to the requesting class/environment:
  -     * - ONE way to do this is by thread context class loader.
  -     * - ANOTHER way is to allow caching by an ID that crossing the thread
  -     *   context boundries... much like the <code>'groupContext'</code>.
  -     * 
  -     * Until such time as we can see more clearly the benefits of the second,
  -     * will only cache by the first (thread context class loader).
        * 
        * When we 'release', it is expected that the caller of the 'release'
        * have the same thread context class loader... as that will be used
  @@ -866,21 +917,26 @@
        * We will manage synchronization directly, so all caches are implemented
        * as HashMap (unsynchronized).
        * 
  -     * - Cache of SPI Caches, by ClassLoader:
  +     * - ClassLoader::groupContext::SPI::Instance Cache
        *         Cache : HashMap
        *         Key   : Thread Context Class Loader (<code>ClassLoader</code>).
  +     *         Value : groupContext::SPI Cache (<code>HashMap</code>).
  +     * 
  +     * - groupContext::SPI::Instance Cache
  +     *         Cache : HashMap
  +     *         Key   : groupContext (<code>String</code>).
        *         Value : SPI Cache (<code>HashMap</code>).
        * 
  -     * - Cache of SPIs :
  +     * - SPI::Instance Cache
        *         Cache : HashMap
  -     *         Key   : SPI Interface/Class Name (<code>String</code>).
  -     *         Value : SPI Implementation (<code>Object</code>.
  +     *         Key   : SPI Class Name (<code>String</code>).
  +     *         Value : SPI Instance/Implementation (<code>Object</code>.
        */
   
       /**
        * Allows null key, important as default groupContext is null.
        */
  -    private static final HashMap spi_caches = new HashMap();
  +    private static final HashMap root_cache = new HashMap();
   
       /**
        * Initial hash size for SPI's, default just seem TO big today..
  @@ -900,12 +956,18 @@
            */
           if (spiContext.getSPI() != null)
           {
  -            synchronized (spi_caches) {
  -                HashMap spis =
  -                    (HashMap)spi_caches.get(spiContext.getThreadContextClassLoader());
  -
  -                if (spis != null) {
  -                    service = (Object)spis.get(spiContext.getSPI().getName());
  +            synchronized (root_cache) {
  +                HashMap groups =
  +                    (HashMap)root_cache.get(spiContext.getThreadContextClassLoader());
  +
  +                if (groups != null) {
  +                    HashMap spis =
  +                        (HashMap)groups.get(spiContext.getGroupContext());
  +    
  +                    if (spis != null) {
  +                        
  +                        service = (Object)spis.get(spiContext.getSPI().getName());
  +                    }
                   }
               }
           }
  @@ -925,13 +987,21 @@
           if (spiContext.getSPI() != null  &&
               service != null)
           {
  -            synchronized (spi_caches) {
  +            synchronized (root_cache) {
  +                HashMap groups =
  +                    (HashMap)root_cache.get(spiContext.getThreadContextClassLoader());
  +                    
  +                if (groups == null) {
  +                    groups = new HashMap(smallHashSize);
  +                    root_cache.put(spiContext.getThreadContextClassLoader(), groups);
  +                }
  +
                   HashMap spis =
  -                    (HashMap)spi_caches.get(spiContext.getThreadContextClassLoader());
  +                    (HashMap)groups.get(spiContext.getGroupContext());
   
                   if (spis == null) {
                       spis = new HashMap(smallHashSize);
  -                    spi_caches.put(spiContext.getThreadContextClassLoader(), spis);
  +                    root_cache.put(spiContext.getGroupContext(), spis);
                   }
   
                   spis.put(spiContext.getSPI().getName(), service);
  
  
  

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