1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.util.config;
18
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.Map.Entry;
25
26 /**
27 * The base class for generic configurations. It is a hierarchical structure of
28 * configuration entries, where entries can be added, inherited and overridden.
29 *
30 * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/config/GenericConfig.java $
31 *
32 * @author Stefan Wismer (SWI)
33 */
34 public class GenericConfig {
35
36 /**
37 * The configuration entries that are added or override configurations
38 * declared in m_parentConfig.
39 */
40 protected Map<String, Object> m_map;
41
42 /**
43 * The default constructor, which creates an empty configuration.
44 */
45 public GenericConfig() {
46 m_map = new HashMap<String, Object>();
47 }
48
49 /**
50 * @param parent the parent {@link GenericConfig} which configuration
51 * is inherited
52 */
53 public void setParent(GenericConfig parent) {
54 Iterator<Entry<String, Object>> mapIterator = parent.getMap().entrySet().iterator();
55
56 while (mapIterator.hasNext()) {
57 Entry<String, Object> entry = mapIterator.next();
58 if (!m_map.containsKey(entry.getKey())) {
59 add(entry.getKey(), entry.getValue());
60 }
61 }
62 }
63
64 /**
65 * @param parents the parent {@link GenericConfig}s which configuration
66 * is inherited
67 */
68 public void setParents(List<GenericConfig> parents) {
69 for (GenericConfig genericConfig : parents) {
70 setParent(genericConfig);
71 }
72 }
73
74 /**
75 * @return a copy of all configuration entries declared in this config
76 */
77 public Map<String, Object> getMap() {
78 Map<String, Object> map = new HashMap<String, Object>();
79 map.putAll(m_map);
80 return map;
81 }
82
83 /**
84 * @param map the configuration entries to set
85 */
86 public void setMap(Map<String, Object> map) {
87 m_map = map;
88 }
89
90 /**
91 * @param properties the configuration entries to set
92 */
93 public void setMap(Properties properties) {
94 m_map.clear();
95 setOverrideMap(properties);
96 }
97
98 /**
99 * @param map the configuration entries to add to the current entries
100 */
101 public void setOverrideMap(Map<String, Object> map) {
102 if (m_map != null) {
103 m_map.putAll(map);
104 } else {
105 m_map = map;
106 }
107 }
108
109 /**
110 * @param properties the configuration entries to add
111 */
112 public void setOverrideMap(Properties properties) {
113 Iterator<Entry<Object, Object>> propertiesIterator = properties.entrySet().iterator();
114 while (propertiesIterator.hasNext()) {
115 Entry<Object, Object> property = propertiesIterator.next();
116 m_map.put((String) property.getKey(), property.getValue());
117 }
118 }
119
120 /**
121 * @param key the key of the configuration entry to add
122 * @param value the value of the configuration entry to add
123 */
124 public void add(String key, Object value) {
125 m_map.put(key, value);
126 }
127
128 /**
129 * @param key the key of the configuration entry that should be returned
130 * @return the corresponding value or <code>null</code> if not found
131 */
132 public Object get(String key) {
133 return m_map.get(key);
134 }
135
136
137 }