commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons/chain/src/java/org/apache/commons/chain/web MapEntry.java
Date Sun, 17 Oct 2004 01:53:53 GMT
craigmcc    2004/10/16 18:53:53

  Modified:    chain/src/java/org/apache/commons/chain CatalogFactory.java
               chain/src/java/org/apache/commons/chain/web MapEntry.java
  Added:       chain/src/java/org/apache/commons/chain/impl
                        CatalogFactoryBase.java
  Log:
  Add a clear() method to CatalogFactory() so that, for example, a web
  application can clean up allocated catalogs when it shuts down.
  
  Commit a new CatalogFactoryBase concrete implementation of CatalogFactory,
  originally contribted by Sean Schofield, with the following tweaks:
  
  * Use Java2 collections classes (including necessary synchronization)
    instead of Hashtable.
  
  * Make the private string constant used to name the default Catalog
    instance less susceptible to being stepped on by applications.
  
  * Support the getNames() and clear() methods that were added to
    the CatalogFactory interface.
  
  * Factor out the common logic used to look up the relevant class
    loader (also making it more robust by using the class loader that
    loaded this class if there is no thread context class loader)
    and the logic to retrieve the appropriate Map of Catalogs related
    to a particular ClassLoader.
  
  Hint hint ... we still need unit tests for this class :-).
  
  Revision  Changes    Path
  1.3       +7 -4      jakarta-commons/chain/src/java/org/apache/commons/chain/CatalogFactory.java
  
  Index: CatalogFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/CatalogFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CatalogFactory.java	17 Oct 2004 01:23:01 -0000	1.2
  +++ CatalogFactory.java	17 Oct 2004 01:53:53 -0000	1.3
  @@ -15,10 +15,8 @@
    */
   package org.apache.commons.chain;
   
  -
   import java.util.Iterator;
   
  -
   /**
    * <p>A {@link CatalogFactory} is a class used to store and retrieve
    * {@link Catalog}s.  The factory allows for a default {@link Catalog}
  @@ -75,6 +73,11 @@
       public Iterator getNames();
   
   
  -}
  +    /**
  +     * <p>Release all {@link Catalog}s currently registered with
  +     * this {@link CatalogFactory}.</p>
  +     */
  +    public void clear();
   
   
  +}
  
  
  
  1.1                  jakarta-commons/chain/src/java/org/apache/commons/chain/impl/CatalogFactoryBase.java
  
  Index: CatalogFactoryBase.java
  ===================================================================
  /*
   * Copyright 1999-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.chain.impl;
  
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import org.apache.commons.chain.CatalogFactory;
  import org.apache.commons.chain.Catalog;
  
  /**
   * <p>A simple implementation of {@link CatalogFactory}.</p>
   *
   * @author Sean Schofield 
   * @version $Revision: 1.1 $ $Date: 2004/10/17 01:53:53 $
   */
  
  public class CatalogFactoryBase implements CatalogFactory {
      
  
      // ------------------------------------------------------- Static Variables
      
  
      /**
       * <p>The singleton instance.</p>
       */
      private static CatalogFactoryBase instance = new CatalogFactoryBase();
  
  
      /**
       * <p>A default value to store an unamed {@link Catalog} in the local 
       * Map.</p>
       */
      private static String DEFAULT_CATALOG =
          "org.apache.commons.chain.DEFAULT_CATALOG";
      
  
      // ----------------------------------------------------------- Constructors
      
      
      /**
       * <p>Constructs an instance of CatalogFactoryBase.  NOTE: This is private 
       * because it is not meant to be created directly outside of the class.  
       * Use the <code>getInstance()</code> method instead.</p>
       */
      private CatalogFactoryBase() {}
      
      
      // ----------------------------------------------------- Instance Variables
      
      
      /**
       * <p>Map of Maps of {@link Catalog}s, keyed by the object's 
       * ClassLoader.</p>
       */
      protected Map loaderMap = new HashMap();
      
      
      // --------------------------------------------------------- Public Methods
      
  
      /**
       * <p>Gets the singleton instance of CatalogFactoryBase.</p>
       * 
       * @return the singleton instance of CatalogFactoryBase.
       */
      public CatalogFactory getInstance() {
          
          return instance;    
          
      }
      
  
      // Documented in CatalogFactory interface
      public Catalog getCatalog() {
          
          return getCatalog(DEFAULT_CATALOG);        
  
      }
  
  
      // Documented in CatalogFactory interface
      public void setCatalog(Catalog catalog) {
          
          addCatalog(DEFAULT_CATALOG, catalog);
          
      }
  
  
      // Documented in CatalogFactory interface
      public Catalog getCatalog(String name) {
          
          /**
           * In the event that this class will be used by multiple applications 
           * with the same class loader (as with web applications), there will 
           * be a separate Map of Catalogs for each instance of 
           * ClassLoader.  That way each web application (which will always have 
           * its own) can operate without interfering with one another.
           */
          Map catalogMap = getCatalogMap();
          if (catalogMap != null) {
              synchronized (catalogMap) {
                  return (Catalog) catalogMap.get(name);
              }
          } else {
              return null;
          }
  
      }
  
  
      // Documented in CatalogFactory interface
      public void addCatalog(String name, Catalog catalog) {
  
          // Retrieve or create the catalog map for the correct class loader
          Map catalogMap = getCatalogMap();
          if (catalogMap == null) {
              catalogMap = new HashMap();
              synchronized (loaderMap) {
                  loaderMap.put(getLoaderMapKey(), catalogMap);
              }
          }
  
          // Add or replace the specified Catalog
          synchronized (catalogMap) {
              catalogMap.put(name, catalog);
          }
  
      }
      
  
      // Documented in CatalogFactory interface
      public Iterator getNames() {
  
          Map catalogMap = getCatalogMap();
          if (catalogMap != null) {
              synchronized (catalogMap) {
                  return catalogMap.keySet().iterator();
              }
          } else {
              return Collections.EMPTY_LIST.iterator();
          }
  
      }
  
  
      // Documented in CatalogFactory interface
      public void clear() {
  
          Map catalogMap = getCatalogMap();
          if (catalogMap != null) {
              synchronized (catalogMap) {
                  catalogMap.clear();
              }
          }
  
      }
  
  
      // ------------------------------------------------------- Private Methods
  
  
      /**
       * <p>Return the <code>Map</code> containing the relevant
       * {@link Catalog}s for an application, keyed by cataog name.
       * If there is no such <code>Map</code>, return <code>null</code>.</p>
       */
      private Map getCatalogMap() {
  
          ClassLoader cl = getLoaderMapKey();
          synchronized (loaderMap) {
              return (Map) loaderMap.get(cl);
          }
  
      }
  
  
      /**
       * <p>Return the <code>ClassLoader</code> to serve as the key
       * for a <code>loaderMap</code> entry that consists of the
       * <code>Map</code> of {@link Catalog}s for that class laoder.</p>
       */
      private ClassLoader getLoaderMapKey() {
  
          ClassLoader cl = Thread.currentThread().getContextClassLoader();
          if (cl == null) { // If context class loader is not defined ...
              cl = this.getClass().getClassLoader(); // Get CL that loaded us
          }
          return cl;
  
      }
  
  
  }
  
  
  
  1.2       +2 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/MapEntry.java
  
  Index: MapEntry.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/MapEntry.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MapEntry.java	10 Sep 2004 22:43:20 -0000	1.1
  +++ MapEntry.java	17 Oct 2004 01:53:53 -0000	1.2
  @@ -84,7 +84,7 @@
        * 
        * @param val The new value
        * @return The old entry value
  -     * @throw UnsupportedOperationException If the entry cannot be modified
  +     * @throws UnsupportedOperationException If the entry cannot be modified
        */
       public Object setValue(Object val) {
           if (modifiable) {
  
  
  

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


Mime
View raw message