/*
 * 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.Hashtable;
import org.apache.commons.chain.CatalogFactory;
import org.apache.commons.chain.Catalog;


/**
 * <p>A simple implementation of {@link CatalogFactory}.</p>
 *
 * @author Sean Schofield 
 * @version 
 */

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 
     * Hashtable.</p>
     */
    private static String DEFAULT_CATALOG = "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>The Hashtable of {@link Catalog}s, keyed by the object's 
     * ClassLoader.</p>
     */
    protected Hashtable loaderTable = new Hashtable();
    
    
    // --------------------------------------------------------- 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 Hashtable 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.
         */
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Hashtable catalogTable = (Hashtable)loaderTable.get(cl);
        
        if (catalogTable != null) {
            return (Catalog)catalogTable.get(name);            
        } else {
            return null;
        }

    }

    // Documented in CatalogFactory interface
    public void addCatalog(String name, Catalog catalog) {

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Hashtable catalogTable = (Hashtable)loaderTable.get(cl);
        
        if (catalogTable == null) {
            catalogTable = new Hashtable();
            loaderTable.put(cl, catalogTable);
        }
        
        catalogTable.put(name, catalog);
    }
    
}


