001: package net.myvietnam.mvncore.configuration;
002:
003: /* ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowledgement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgement may appear in the software itself,
026: * if and wherever such third-party acknowledgements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: */
056:
057: import java.util.Iterator;
058:
059: import org.apache.commons.collections.SequencedHashMap;
060:
061: /**
062: * Basic configuration classe. Stores the configuration data but does not
063: * provide any load or save functions. If you want to load your Configuration
064: * from a file use PropertiesConfiguration or XmlConfiguration.
065: *
066: * This class extends normal Java properties by adding the possibility
067: * to use the same key many times concatenating the value strings
068: * instead of overwriting them.
069: *
070: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
071: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
072: * @author <a href="mailto:daveb@miceda-data">Dave Bryson</a>
073: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
074: * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
075: * @author <a href="mailto:kjohnson@transparent.com">Kent Johnson</a>
076: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
077: * @author <a href="mailto:ipriha@surfeu.fi">Ilkka Priha</a>
078: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
079: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
080: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
081: * @author <a href="mailto:ksh@scand.com">Konstantin Shaposhnikov</a>
082: * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
083: *
084: * @version $Id: BaseConfiguration.java,v 1.1 2003/12/09 08:25:30 huumai Exp $
085: */
086: public class BaseConfiguration extends AbstractConfiguration {
087: /** stores the configuration key-value pairs */
088: private SequencedHashMap store = new SequencedHashMap();
089:
090: /**
091: * Empty constructor. You must add all the values to this configuration.
092: */
093: public BaseConfiguration() {
094: super ();
095: }
096:
097: /**
098: * Creates an empty BaseConfiguration object with
099: * a Super-Object which is queries for every key.
100: *
101: * @param defaults Configuration defaults to use if key not in file
102: */
103: public BaseConfiguration(Configuration defaults) {
104: super (defaults);
105: }
106:
107: /**
108: * Adds a key/value pair to the map. This routine does no magic morphing.
109: * It ensures the keylist is maintained
110: *
111: * @param key key to use for mapping
112: * @param obj object to store
113: */
114: protected void addPropertyDirect(String key, Object obj) {
115: Object o = getPropertyDirect(key);
116: Object objAdd = null;
117:
118: if (o == null) {
119: objAdd = obj;
120: } else {
121: if (o instanceof Container) {
122: ((Container) o).add(obj);
123: } else {
124: // The token key is not a container.
125: Container c = new Container();
126:
127: // There is an element. Put it into the container
128: // at the first position
129: c.add(o);
130:
131: // Now gobble up the supplied object
132: c.add(obj);
133:
134: objAdd = c;
135: }
136: }
137:
138: if (objAdd != null) {
139: store.put(key, objAdd);
140: }
141: }
142:
143: /**
144: * Read property from underlying map.
145: *
146: * @param key key to use for mapping
147: *
148: * @return object associated with the given configuration key.
149: */
150: protected Object getPropertyDirect(String key) {
151: return store.get(key);
152: }
153:
154: /**
155: * Check if the configuration is empty
156: *
157: * @return <code>true</code> if Configuration is empty,
158: * <code>false</code> otherwise.
159: */
160: public boolean isEmpty() {
161: return store.isEmpty();
162: }
163:
164: /**
165: * check if the configuration contains the key
166: *
167: * @param key the configuration key
168: *
169: * @return <code>true</code> if Configuration contain given key,
170: * <code>false</code> otherwise.
171: */
172: public boolean containsKey(String key) {
173: return store.containsKey(key);
174: }
175:
176: /**
177: * Clear a property in the configuration.
178: *
179: * @param key the key to remove along with corresponding value.
180: */
181: public void clearProperty(String key) {
182: if (containsKey(key)) {
183: store.remove(key);
184: }
185: }
186:
187: /**
188: * Get the list of the keys contained in the configuration
189: * repository.
190: *
191: * @return An Iterator.
192: */
193: public Iterator getKeys() {
194: return store.iterator();
195: }
196: }
|