001: /*
002: * Copyright (c) 2001 by Matt Welsh and The Regents of the University of
003: * California. All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Matt Welsh <mdw@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.sandStorm.internal;
026:
027: import seda.sandStorm.api.*;
028: import seda.sandStorm.core.*;
029: import seda.sandStorm.main.*;
030: import java.io.IOException;
031: import java.util.*;
032:
033: /**
034: * ConfigData is used to pass configuration arguments into various
035: * components.
036: */
037: public class ConfigData implements ConfigDataIF {
038: private static final boolean DEBUG = false;
039:
040: private Hashtable vals;
041: private ManagerIF mgr;
042: private StageIF stage;
043:
044: /**
045: * Create a ConfigData with the given manager and no argument list.
046: */
047: public ConfigData(ManagerIF mgr) {
048: this .mgr = mgr;
049: this .vals = new Hashtable(1);
050: }
051:
052: /**
053: * Create a ConfigData with the given manager and argument list.
054: */
055: public ConfigData(ManagerIF mgr, Hashtable args) {
056: this .mgr = mgr;
057: this .vals = args;
058: if (vals == null)
059: vals = new Hashtable(1);
060: }
061:
062: /**
063: * Create a ConfigData with the given manager and argument list,
064: * specified as an array of strings of the form "key=value".
065: *
066: * @throws IOException If any of the strings to not match the
067: * pattern "key=value".
068: */
069: public ConfigData(ManagerIF mgr, String args[]) throws IOException {
070: this .mgr = mgr;
071: this .vals = stringArrayToHT(args);
072: if (vals == null)
073: vals = new Hashtable(1);
074: }
075:
076: /**
077: * Returns true if the given key is set.
078: */
079: public boolean contains(String key) {
080: if (vals.get(key) != null)
081: return true;
082: else
083: return false;
084: }
085:
086: /**
087: * Get the string value corresponding to the given key.
088: * Returns null if not set.
089: */
090: public String getString(String key) {
091: return (String) vals.get(key);
092: }
093:
094: /**
095: * Get the integer value corresponding to the given key.
096: * Returns -1 if not set or if the value is not an integer.
097: */
098: public int getInt(String key) {
099: String val = (String) vals.get(key);
100: if (val == null)
101: return -1;
102: try {
103: return Integer.parseInt(val);
104: } catch (NumberFormatException e) {
105: return -1;
106: }
107: }
108:
109: /**
110: * Get the double value corresponding to the given key.
111: * Returns -1.0 if not set or if the value is not a double.
112: */
113: public double getDouble(String key) {
114: String val = (String) vals.get(key);
115: if (val == null)
116: return -1;
117: try {
118: return new Double(val).doubleValue();
119: } catch (NumberFormatException e) {
120: return -1.0;
121: }
122: }
123:
124: /**
125: * Get the boolean value corresponding to the given key.
126: * Returns false if not set.
127: */
128: public boolean getBoolean(String key) {
129: String val = (String) vals.get(key);
130: if (val == null)
131: return false;
132: else if (val.equals("true") || val.equals("TRUE"))
133: return true;
134: else
135: return false;
136: }
137:
138: /**
139: * Get the string list value corresponding to the given key.
140: * Returns null if not set.
141: */
142: public String[] getStringList(String key) {
143: String ret[];
144: String val = (String) vals.get(key);
145: if (val == null)
146: return null;
147: StringTokenizer st = new StringTokenizer(val,
148: SandstormConfig.LIST_ELEMENT_DELIMITER);
149: Vector v = new Vector(1);
150: while (st.hasMoreElements()) {
151: v.addElement(st.nextElement());
152: }
153: ret = new String[v.size()];
154: for (int i = 0; i < ret.length; i++) {
155: ret[i] = (String) v.elementAt(i);
156: }
157: return ret;
158: }
159:
160: /**
161: * Set the given key to the given string value.
162: */
163: public void setString(String key, String val) {
164: vals.put(key, val);
165: }
166:
167: /**
168: * Set the given key to the given integer value.
169: */
170: public void setInt(String key, int val) {
171: vals.put(key, Integer.toString(val));
172: }
173:
174: /**
175: * Set the given key to the given double value.
176: */
177: public void setDouble(String key, double val) {
178: vals.put(key, Double.toString(val));
179: }
180:
181: /**
182: * Set the given key to the given boolean value.
183: */
184: public void setBoolean(String key, boolean val) {
185: vals.put(key, (val == true) ? "true" : "false");
186: }
187:
188: /**
189: * Set the given key to the given string list value.
190: */
191: public void setStringList(String key, String valarr[]) {
192: String s = "";
193: for (int i = 0; i < valarr.length; i++) {
194: s += valarr[i];
195: if (i != valarr.length - 1)
196: s += SandstormConfig.LIST_ELEMENT_DELIMITER;
197: }
198: vals.put(key, s);
199: }
200:
201: /**
202: * Return the local manager.
203: */
204: public ManagerIF getManager() {
205: return mgr;
206: }
207:
208: /**
209: * Return the stage for this ConfigData.
210: */
211: public StageIF getStage() {
212: return stage;
213: }
214:
215: // Used to set stage after creating wrapper
216: public void setStage(StageIF stage) {
217: this .stage = stage;
218: }
219:
220: // Used to reset manager after creating stage
221: // (for proxying the manager)
222: void setManager(ManagerIF mgr) {
223: this .mgr = mgr;
224: }
225:
226: // Convert an array of "key=value" strings to a Hashtable
227: private Hashtable stringArrayToHT(String arr[]) throws IOException {
228: if (arr == null)
229: return null;
230: Hashtable ht = new Hashtable(1);
231: for (int i = 0; i < arr.length; i++) {
232: StringTokenizer st = new StringTokenizer(arr[i], "=");
233: String key;
234: String val;
235: try {
236: key = st.nextToken();
237: val = st.nextToken();
238: while (st.hasMoreTokens())
239: val += "=" + st.nextToken();
240: } catch (NoSuchElementException e) {
241: throw new IOException("Could not convert string '"
242: + arr[i] + "' to key=value pair");
243: }
244: ht.put(key, val);
245: }
246: return ht;
247: }
248:
249: }
|