KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > config > ConfigImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.config;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * Default implementation of the Config interface, this should be used as the
29  * base class for any customisations
30  *
31  * @author gavinc
32  */

33 public class ConfigImpl implements Config
34 {
35    private static final Log logger = LogFactory.getLog(ConfigImpl.class);
36
37    private Map JavaDoc<String JavaDoc, Object JavaDoc> configElements;
38
39    /**
40     * Default constructor
41     */

42    public ConfigImpl()
43    {
44       this.configElements = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
45    }
46
47    /**
48     * Construct a ConfigImpl using the contents of an existing ConfigImpl
49     *
50     * @param config
51     * The instance to create this one from
52     */

53    public ConfigImpl(ConfigImpl config)
54    {
55       this();
56
57       this.configElements.putAll(config.getConfigElements());
58    }
59
60    /**
61     * @see org.alfresco.config.Config#getConfigElement(java.lang.String)
62     */

63    public ConfigElement getConfigElement(String JavaDoc name)
64    {
65       return (ConfigElement) this.configElements.get(name);
66    }
67
68    /**
69     * @see org.alfresco.config.Config#getConfigElementList(java.lang.String)
70     */

71    public List JavaDoc<ConfigElement> getConfigElementList(String JavaDoc name)
72    {
73       List JavaDoc<ConfigElement> list = null;
74       
75       Object JavaDoc obj = this.configElements.get(name);
76       if (obj != null)
77       {
78          if (obj instanceof ConfigElement)
79          {
80             list = new ArrayList JavaDoc<ConfigElement>(1);
81             list.add((ConfigElement)obj);
82          }
83          else if (obj instanceof List JavaDoc)
84          {
85             list = (List JavaDoc<ConfigElement>)obj;
86          }
87       }
88       
89       return list;
90    }
91
92    /**
93     * @see org.alfresco.config.Config#hasConfigElement(java.lang.String)
94     */

95    public boolean hasConfigElement(String JavaDoc name)
96    {
97       return this.configElements.containsKey(name);
98    }
99
100    /**
101     * @see org.alfresco.config.Config#getConfigElements()
102     */

103    public Map JavaDoc<String JavaDoc, Object JavaDoc> getConfigElements()
104    {
105       return this.configElements;
106    }
107
108    /**
109     * Adds a config element to the results of the lookup replacing any config
110     * element already present with the same name
111     *
112     * @param configElement
113     * The config element to add
114     */

115    public void putConfigElement(ConfigElement configElement)
116    {
117       this.configElements.put(configElement.getName(), configElement);
118    }
119
120    /**
121     * Adds a config element to the results of the lookup. If a config element
122     * with the same name already exists a list of them is created
123     *
124     * @param configElement
125     * The config element to add
126     */

127    public void addConfigElement(ConfigElement configElement)
128    {
129       Object JavaDoc obj = this.configElements.get(configElement.getName());
130       if (obj == null)
131       {
132          putConfigElement(configElement);
133       }
134       else
135       {
136          if (obj instanceof ConfigElement)
137          {
138             List JavaDoc<ConfigElement> list = new ArrayList JavaDoc<ConfigElement>(2);
139             list.add((ConfigElement)obj);
140             list.add(configElement);
141          }
142          else if (obj instanceof List JavaDoc)
143          {
144             ((List JavaDoc<ConfigElement>)obj).add(configElement);
145          }
146       }
147    }
148 }
149
Popular Tags