001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.chain.impl;
019    
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.Map;
023    import org.apache.commons.chain.Catalog;
024    import org.apache.commons.chain.CatalogFactory;
025    
026    /**
027     * <p>A simple implementation of {@link CatalogFactory}.</p>
028     *
029     * @author Sean Schofield
030     * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
031     */
032    
033    public class CatalogFactoryBase extends CatalogFactory {
034    
035    
036        // ----------------------------------------------------------- Constructors
037    
038    
039        /**
040         * <p>Construct an empty instance of {@link CatalogFactoryBase}.  This
041         * constructor is intended solely for use by {@link CatalogFactory}.</p>
042         */
043        public CatalogFactoryBase() { }
044    
045    
046        // ----------------------------------------------------- Instance Variables
047    
048    
049        /**
050         * <p>The default {@link Catalog} for this {@link CatalogFactory}.</p>
051         */
052        private Catalog catalog = null;
053    
054    
055        /**
056         * <p>Map of named {@link Catalog}s, keyed by catalog name.</p>
057         */
058        private Map catalogs = new HashMap();
059    
060    
061        // --------------------------------------------------------- Public Methods
062    
063    
064        /**
065         * <p>Gets the default instance of Catalog associated with the factory
066         * (if any); otherwise, return <code>null</code>.</p>
067         *
068         * @return the default Catalog instance
069         */
070        public Catalog getCatalog() {
071    
072            return catalog;
073    
074        }
075    
076    
077        /**
078         * <p>Sets the default instance of Catalog associated with the factory.</p>
079         *
080         * @param catalog the default Catalog instance
081         */
082        public void setCatalog(Catalog catalog) {
083    
084            this.catalog = catalog;
085    
086        }
087    
088    
089        /**
090         * <p>Retrieves a Catalog instance by name (if any); otherwise
091         * return <code>null</code>.</p>
092         *
093         * @param name the name of the Catalog to retrieve
094         * @return the specified Catalog
095         */
096        public Catalog getCatalog(String name) {
097    
098            synchronized (catalogs) {
099                return (Catalog) catalogs.get(name);
100            }
101    
102        }
103    
104    
105        /**
106         * <p>Adds a named instance of Catalog to the factory (for subsequent
107         * retrieval later).</p>
108         *
109         * @param name the name of the Catalog to add
110         * @param catalog the Catalog to add
111         */
112        public void addCatalog(String name, Catalog catalog) {
113    
114            synchronized (catalogs) {
115                catalogs.put(name, catalog);
116            }
117    
118        }
119    
120    
121        /**
122         * <p>Return an <code>Iterator</code> over the set of named
123         * {@link Catalog}s known to this {@link CatalogFactory}.
124         * If there are no known catalogs, an empty Iterator is returned.</p>
125         * @return An Iterator of the names of the Catalogs known by this factory.
126         */
127        public Iterator getNames() {
128    
129            synchronized (catalogs) {
130                return catalogs.keySet().iterator();
131            }
132    
133        }
134    
135    
136    }