View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.chain.impl;
19  
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  import org.apache.commons.chain.Catalog;
24  import org.apache.commons.chain.CatalogFactory;
25  
26  /**
27   * <p>A simple implementation of {@link CatalogFactory}.</p>
28   *
29   * @author Sean Schofield
30   * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
31   */
32  
33  public class CatalogFactoryBase extends CatalogFactory {
34  
35  
36      // ----------------------------------------------------------- Constructors
37  
38  
39      /**
40       * <p>Construct an empty instance of {@link CatalogFactoryBase}.  This
41       * constructor is intended solely for use by {@link CatalogFactory}.</p>
42       */
43      public CatalogFactoryBase() { }
44  
45  
46      // ----------------------------------------------------- Instance Variables
47  
48  
49      /**
50       * <p>The default {@link Catalog} for this {@link CatalogFactory}.</p>
51       */
52      private Catalog catalog = null;
53  
54  
55      /**
56       * <p>Map of named {@link Catalog}s, keyed by catalog name.</p>
57       */
58      private Map catalogs = new HashMap();
59  
60  
61      // --------------------------------------------------------- Public Methods
62  
63  
64      /**
65       * <p>Gets the default instance of Catalog associated with the factory
66       * (if any); otherwise, return <code>null</code>.</p>
67       *
68       * @return the default Catalog instance
69       */
70      public Catalog getCatalog() {
71  
72          return catalog;
73  
74      }
75  
76  
77      /**
78       * <p>Sets the default instance of Catalog associated with the factory.</p>
79       *
80       * @param catalog the default Catalog instance
81       */
82      public void setCatalog(Catalog catalog) {
83  
84          this.catalog = catalog;
85  
86      }
87  
88  
89      /**
90       * <p>Retrieves a Catalog instance by name (if any); otherwise
91       * return <code>null</code>.</p>
92       *
93       * @param name the name of the Catalog to retrieve
94       * @return the specified Catalog
95       */
96      public Catalog getCatalog(String name) {
97  
98          synchronized (catalogs) {
99              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 }