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