View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.chain.impl;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  import org.apache.commons.chain.Catalog;
23  import org.apache.commons.chain.CatalogFactory;
24  
25  /***
26   * <p>A simple implementation of {@link CatalogFactory}.</p>
27   *
28   * @author Sean Schofield
29   * @version $Revision: 411876 $ $Date: 2006-06-05 19:02:19 +0100 (Mon, 05 Jun 2006) $
30   */
31  
32  public class CatalogFactoryBase extends CatalogFactory {
33  
34  
35      // ----------------------------------------------------------- Constructors
36  
37  
38      /***
39       * <p>Construct an empty instance of {@link CatalogFactoryBase}.  This
40       * constructor is intended solely for use by {@link CatalogFactory}.</p>
41       */
42      public CatalogFactoryBase() { }
43  
44  
45      // ----------------------------------------------------- Instance Variables
46  
47  
48      /***
49       * <p>The default {@link Catalog} for this {@link CatalogFactory).</p>
50       */
51      private Catalog catalog = null;
52  
53  
54      /***
55       * <p>Map of named {@link Catalog}s, keyed by catalog name.</p>
56       */
57      private Map catalogs = new HashMap();
58  
59  
60      // --------------------------------------------------------- Public Methods
61  
62  
63      /***
64       * <p>Gets the default instance of Catalog associated with the factory
65       * (if any); otherwise, return <code>null</code>.</p>
66       *
67       * @return the default Catalog instance
68       */
69      public Catalog getCatalog() {
70  
71          return catalog;
72  
73      }
74  
75  
76      /***
77       * <p>Sets the default instance of Catalog associated with the factory.</p>
78       *
79       * @param catalog the default Catalog instance
80       */
81      public void setCatalog(Catalog catalog) {
82  
83          this.catalog = catalog;
84  
85      }
86  
87  
88      /***
89       * <p>Retrieves a Catalog instance by name (if any); otherwise
90       * return <code>null</code>.</p>
91       *
92       * @param name the name of the Catalog to retrieve
93       * @return the specified Catalog
94       */
95      public Catalog getCatalog(String name) {
96  
97          synchronized (catalogs) {
98              return (Catalog) catalogs.get(name);
99          }
100 
101     }
102 
103 
104     /***
105      * <p>Adds a named instance of Catalog to the factory (for subsequent
106      * retrieval later).</p>
107      *
108      * @param name the name of the Catalog to add
109      * @param catalog the Catalog to add
110      */
111     public void addCatalog(String name, Catalog catalog) {
112 
113         synchronized (catalogs) {
114             catalogs.put(name, catalog);
115         }
116 
117     }
118 
119 
120     /***
121      * <p>Return an <code>Iterator</code> over the set of named
122      * {@link Catalog}s known to this {@link CatalogFactory}.
123      * If there are no known catalogs, an empty Iterator is returned.</p>
124      * @return An Iterator of the names of the Catalogs known by this factory.
125      */
126     public Iterator getNames() {
127 
128         synchronized (catalogs) {
129             return catalogs.keySet().iterator();
130         }
131 
132     }
133 
134 
135 }