%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.configuration.CompositeConfiguration |
|
|
1 | /* |
|
2 | * Copyright 2001-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.configuration; |
|
18 | ||
19 | import java.util.ArrayList; |
|
20 | import java.util.Iterator; |
|
21 | import java.util.LinkedList; |
|
22 | import java.util.List; |
|
23 | import java.util.Vector; |
|
24 | ||
25 | /** |
|
26 | * This Configuration class allows you to add multiple different types of Configuration |
|
27 | * to this CompositeConfiguration. If you add Configuration1, and then Configuration2, |
|
28 | * any properties shared will mean that Configuration1 will be returned. |
|
29 | * You can add multiple different types or the same type of properties file. |
|
30 | * If Configuration1 doesn't have the property, then Configuration2 will be checked. |
|
31 | * |
|
32 | * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> |
|
33 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
|
34 | * @version $Id: CompositeConfiguration.java,v 1.19 2004/10/05 22:56:58 ebourg Exp $ |
|
35 | */ |
|
36 | public class CompositeConfiguration extends AbstractConfiguration |
|
37 | { |
|
38 | /** List holding all the configuration */ |
|
39 | 98 | private List configList = new LinkedList(); |
40 | ||
41 | /** |
|
42 | * Configuration that holds in memory stuff. Inserted as first so any |
|
43 | * setProperty() override anything else added. |
|
44 | */ |
|
45 | private Configuration inMemoryConfiguration; |
|
46 | ||
47 | /** |
|
48 | * Creates an empty CompositeConfiguration object which can then |
|
49 | * be added some other Configuration files |
|
50 | */ |
|
51 | public CompositeConfiguration() |
|
52 | 94 | { |
53 | 94 | clear(); |
54 | 94 | } |
55 | ||
56 | /** |
|
57 | * Creates an CompositeConfiguration object with a specified InMemory |
|
58 | * configuration. This configuration will store any changes made to |
|
59 | * the CompositeConfiguration. |
|
60 | * |
|
61 | * @param inMemoryConfiguration the in memory configuration to use |
|
62 | */ |
|
63 | public CompositeConfiguration(Configuration inMemoryConfiguration) |
|
64 | 4 | { |
65 | 4 | configList.clear(); |
66 | 4 | this.inMemoryConfiguration = inMemoryConfiguration; |
67 | 4 | configList.add(inMemoryConfiguration); |
68 | 4 | } |
69 | ||
70 | /** |
|
71 | * Add a configuration. |
|
72 | * |
|
73 | * @param config the configuration to add |
|
74 | */ |
|
75 | public void addConfiguration(Configuration config) |
|
76 | { |
|
77 | 131 | if (!configList.contains(config)) |
78 | { |
|
79 | // As the inMemoryConfiguration contains all manually added keys, |
|
80 | // we must make sure that it is always last. "Normal", non composed |
|
81 | // configuration add their keys at the end of the configuration and |
|
82 | // we want to mimic this behaviour. |
|
83 | 129 | configList.add(configList.indexOf(inMemoryConfiguration), config); |
84 | ||
85 | 129 | if (config instanceof AbstractConfiguration) |
86 | { |
|
87 | 129 | ((AbstractConfiguration) config).setThrowExceptionOnMissing(isThrowExceptionOnMissing()); |
88 | } |
|
89 | } |
|
90 | 131 | } |
91 | ||
92 | /** |
|
93 | * Remove a configuration. The in memory configuration cannot be removed. |
|
94 | * |
|
95 | * @param config The configuration to remove |
|
96 | */ |
|
97 | public void removeConfiguration(Configuration config) |
|
98 | { |
|
99 | // Make sure that you can't remove the inMemoryConfiguration from |
|
100 | // the CompositeConfiguration object |
|
101 | 4 | if (!config.equals(inMemoryConfiguration)) |
102 | { |
|
103 | 2 | configList.remove(config); |
104 | } |
|
105 | 4 | } |
106 | ||
107 | /** |
|
108 | * Return the number of configurations. |
|
109 | * |
|
110 | * @return the number of configuration |
|
111 | */ |
|
112 | public int getNumberOfConfigurations() |
|
113 | { |
|
114 | 21 | return configList.size(); |
115 | } |
|
116 | ||
117 | /** |
|
118 | * Remove all configuration reinitialize the in memory configuration. |
|
119 | */ |
|
120 | public void clear() |
|
121 | { |
|
122 | 102 | configList.clear(); |
123 | // recreate the in memory configuration |
|
124 | 102 | inMemoryConfiguration = new BaseConfiguration(); |
125 | 102 | ((BaseConfiguration) inMemoryConfiguration).setThrowExceptionOnMissing(isThrowExceptionOnMissing()); |
126 | 102 | configList.add(inMemoryConfiguration); |
127 | 102 | } |
128 | ||
129 | /** |
|
130 | * Add this property to the inmemory Configuration. |
|
131 | * |
|
132 | * @param key The Key to add the property to. |
|
133 | * @param token The Value to add. |
|
134 | */ |
|
135 | protected void addPropertyDirect(String key, Object token) |
|
136 | { |
|
137 | 22 | inMemoryConfiguration.addProperty(key, token); |
138 | 22 | } |
139 | ||
140 | /** |
|
141 | * Read property from underlying composite |
|
142 | * |
|
143 | * @param key key to use for mapping |
|
144 | * |
|
145 | * @return object associated with the given configuration key. |
|
146 | */ |
|
147 | protected Object getPropertyDirect(String key) |
|
148 | { |
|
149 | 118 | Configuration firstMatchingConfiguration = null; |
150 | 118 | for (Iterator i = configList.iterator(); i.hasNext();) |
151 | { |
|
152 | 181 | Configuration config = (Configuration) i.next(); |
153 | 181 | if (config.containsKey(key)) |
154 | { |
|
155 | 93 | firstMatchingConfiguration = config; |
156 | 93 | break; |
157 | } |
|
158 | } |
|
159 | ||
160 | 118 | if (firstMatchingConfiguration != null) |
161 | { |
|
162 | 93 | return firstMatchingConfiguration.getProperty(key); |
163 | } |
|
164 | else |
|
165 | { |
|
166 | 25 | return null; |
167 | } |
|
168 | } |
|
169 | ||
170 | /** |
|
171 | * {@inheritDoc} |
|
172 | */ |
|
173 | public Iterator getKeys() |
|
174 | { |
|
175 | 12 | List keys = new ArrayList(); |
176 | 12 | for (Iterator i = configList.iterator(); i.hasNext();) |
177 | { |
|
178 | 22 | Configuration config = (Configuration) i.next(); |
179 | ||
180 | 22 | Iterator j = config.getKeys(); |
181 | 126 | while (j.hasNext()) |
182 | { |
|
183 | 104 | String key = (String) j.next(); |
184 | 104 | if (!keys.contains(key)) |
185 | { |
|
186 | 103 | keys.add(key); |
187 | } |
|
188 | } |
|
189 | } |
|
190 | ||
191 | 12 | return keys.iterator(); |
192 | } |
|
193 | ||
194 | /** |
|
195 | * {@inheritDoc} |
|
196 | */ |
|
197 | public Iterator getKeys(String key) |
|
198 | { |
|
199 | 7 | List keys = new ArrayList(); |
200 | 7 | for (Iterator i = configList.iterator(); i.hasNext();) |
201 | { |
|
202 | 16 | Configuration config = (Configuration) i.next(); |
203 | ||
204 | 16 | Iterator j = config.getKeys(key); |
205 | 149 | while (j.hasNext()) |
206 | { |
|
207 | 133 | String newKey = (String) j.next(); |
208 | 133 | if (!keys.contains(newKey)) |
209 | { |
|
210 | 131 | keys.add(newKey); |
211 | } |
|
212 | } |
|
213 | } |
|
214 | ||
215 | 7 | return keys.iterator(); |
216 | } |
|
217 | ||
218 | /** |
|
219 | * {@inheritDoc} |
|
220 | */ |
|
221 | public boolean isEmpty() |
|
222 | { |
|
223 | 4 | boolean isEmpty = true; |
224 | 4 | for (Iterator i = configList.iterator(); i.hasNext();) |
225 | { |
|
226 | 4 | Configuration config = (Configuration) i.next(); |
227 | 4 | if (!config.isEmpty()) |
228 | { |
|
229 | 4 | return false; |
230 | } |
|
231 | } |
|
232 | ||
233 | 0 | return isEmpty; |
234 | } |
|
235 | ||
236 | /** |
|
237 | * {@inheritDoc} |
|
238 | */ |
|
239 | public Object getProperty(String key) |
|
240 | { |
|
241 | 37 | return getPropertyDirect(key); |
242 | } |
|
243 | ||
244 | /** |
|
245 | * {@inheritDoc} |
|
246 | */ |
|
247 | public void setProperty(String key, Object value) |
|
248 | { |
|
249 | 8 | clearProperty(key); |
250 | 8 | addProperty(key, value); |
251 | 8 | } |
252 | ||
253 | /** |
|
254 | * {@inheritDoc} |
|
255 | */ |
|
256 | public void clearProperty(String key) |
|
257 | { |
|
258 | 14 | for (Iterator i = configList.iterator(); i.hasNext();) |
259 | { |
|
260 | 32 | Configuration config = (Configuration) i.next(); |
261 | 32 | config.clearProperty(key); |
262 | } |
|
263 | 14 | } |
264 | ||
265 | /** |
|
266 | * {@inheritDoc} |
|
267 | */ |
|
268 | public boolean containsKey(String key) |
|
269 | { |
|
270 | 53 | for (Iterator i = configList.iterator(); i.hasNext();) |
271 | { |
|
272 | 67 | Configuration config = (Configuration) i.next(); |
273 | 67 | if (config.containsKey(key)) |
274 | { |
|
275 | 44 | return true; |
276 | } |
|
277 | } |
|
278 | 9 | return false; |
279 | } |
|
280 | ||
281 | /** |
|
282 | * {@inheritDoc} |
|
283 | */ |
|
284 | public List getList(String key, List defaultValue) |
|
285 | { |
|
286 | 71 | List list = new ArrayList(); |
287 | ||
288 | // add all elements from the first configuration containing the requested key |
|
289 | 71 | Iterator it = configList.iterator(); |
290 | 162 | while (it.hasNext() && list.isEmpty()) |
291 | { |
|
292 | 91 | Configuration config = (Configuration) it.next(); |
293 | 91 | if (config != inMemoryConfiguration && config.containsKey(key)) |
294 | { |
|
295 | 56 | list.addAll(config.getList(key)); |
296 | } |
|
297 | } |
|
298 | ||
299 | // add all elements from the in memory configuration |
|
300 | 71 | list.addAll(inMemoryConfiguration.getList(key)); |
301 | ||
302 | 71 | if (list.isEmpty()) |
303 | { |
|
304 | 8 | return defaultValue; |
305 | } |
|
306 | ||
307 | 63 | return list; |
308 | } |
|
309 | ||
310 | /** |
|
311 | * {@inheritDoc} |
|
312 | */ |
|
313 | public Vector getVector(String key, Vector defaultValue) |
|
314 | { |
|
315 | 27 | return new Vector(getList(key, defaultValue)); |
316 | } |
|
317 | ||
318 | /** |
|
319 | * {@inheritDoc} |
|
320 | */ |
|
321 | public String[] getStringArray(String key) |
|
322 | { |
|
323 | 13 | List list = getList(key); |
324 | ||
325 | // interpolate the strings |
|
326 | 13 | String[] tokens = new String[list.size()]; |
327 | ||
328 | 33 | for (int i = 0; i < tokens.length; i++) |
329 | { |
|
330 | 20 | tokens[i] = interpolate(String.valueOf(list.get(i))); |
331 | } |
|
332 | ||
333 | 13 | return tokens; |
334 | } |
|
335 | ||
336 | /** |
|
337 | * Return the configuration at the specified index. |
|
338 | * |
|
339 | * @param index The index of the configuration to retrieve |
|
340 | */ |
|
341 | public Configuration getConfiguration(int index) |
|
342 | { |
|
343 | 14 | return (Configuration) configList.get(index); |
344 | } |
|
345 | ||
346 | /** |
|
347 | * {@inheritDoc} |
|
348 | */ |
|
349 | public Configuration getInMemoryConfiguration() |
|
350 | { |
|
351 | 0 | return inMemoryConfiguration; |
352 | } |
|
353 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |