001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.configuration;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.commons.collections.map.LinkedMap;
026:
027: /**
028: * Basic configuration classe. Stores the configuration data but does not
029: * provide any load or save functions. If you want to load your Configuration
030: * from a file use PropertiesConfiguration or XmlConfiguration.
031: *
032: * This class extends normal Java properties by adding the possibility
033: * to use the same key many times concatenating the value strings
034: * instead of overwriting them.
035: *
036: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
037: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
038: * @author <a href="mailto:daveb@miceda-data">Dave Bryson</a>
039: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
040: * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
041: * @author <a href="mailto:kjohnson@transparent.com">Kent Johnson</a>
042: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
043: * @author <a href="mailto:ipriha@surfeu.fi">Ilkka Priha</a>
044: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
045: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
046: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
047: * @author <a href="mailto:ksh@scand.com">Konstantin Shaposhnikov</a>
048: * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
049: * @version $Id: BaseConfiguration.java 495918 2007-01-13 16:33:02Z oheger $
050: */
051: public class BaseConfiguration extends AbstractConfiguration implements
052: Cloneable {
053: /** stores the configuration key-value pairs */
054: private Map store = new LinkedMap();
055:
056: /**
057: * Adds a key/value pair to the map. This routine does no magic morphing.
058: * It ensures the keylist is maintained
059: *
060: * @param key key to use for mapping
061: * @param value object to store
062: */
063: protected void addPropertyDirect(String key, Object value) {
064: Object previousValue = getProperty(key);
065:
066: if (previousValue == null) {
067: store.put(key, value);
068: } else if (previousValue instanceof List) {
069: // the value is added to the existing list
070: ((List) previousValue).add(value);
071: } else {
072: // the previous value is replaced by a list containing the previous value and the new value
073: List list = new ArrayList();
074: list.add(previousValue);
075: list.add(value);
076:
077: store.put(key, list);
078: }
079: }
080:
081: /**
082: * Read property from underlying map.
083: *
084: * @param key key to use for mapping
085: *
086: * @return object associated with the given configuration key.
087: */
088: public Object getProperty(String key) {
089: return store.get(key);
090: }
091:
092: /**
093: * Check if the configuration is empty
094: *
095: * @return <code>true</code> if Configuration is empty,
096: * <code>false</code> otherwise.
097: */
098: public boolean isEmpty() {
099: return store.isEmpty();
100: }
101:
102: /**
103: * check if the configuration contains the key
104: *
105: * @param key the configuration key
106: *
107: * @return <code>true</code> if Configuration contain given key,
108: * <code>false</code> otherwise.
109: */
110: public boolean containsKey(String key) {
111: return store.containsKey(key);
112: }
113:
114: /**
115: * Clear a property in the configuration.
116: *
117: * @param key the key to remove along with corresponding value.
118: */
119: protected void clearPropertyDirect(String key) {
120: if (containsKey(key)) {
121: store.remove(key);
122: }
123: }
124:
125: /**
126: * {@inheritDoc}
127: */
128: public void clear() {
129: fireEvent(EVENT_CLEAR, null, null, true);
130: store.clear();
131: fireEvent(EVENT_CLEAR, null, null, false);
132: }
133:
134: /**
135: * Get the list of the keys contained in the configuration
136: * repository.
137: *
138: * @return An Iterator.
139: */
140: public Iterator getKeys() {
141: return store.keySet().iterator();
142: }
143:
144: /**
145: * Creates a copy of this object. This implementation will create a deep
146: * clone, i.e. the map that stores the properties is cloned, too. So changes
147: * performed at the copy won't affect the original and vice versa.
148: *
149: * @return the copy
150: * @since 1.3
151: */
152: public Object clone() {
153: try {
154: BaseConfiguration copy = (BaseConfiguration) super .clone();
155: copy.store = (Map) ConfigurationUtils.clone(store);
156: return copy;
157: } catch (CloneNotSupportedException cex) {
158: // should not happen
159: throw new ConfigurationRuntimeException(cex);
160: }
161: }
162: }
|