1 /* 2 * Copyright (c) 2001 by Matt Welsh and The Regents of the University of 3 * California. All rights reserved. 4 * 5 * Permission to use, copy, modify, and distribute this software and its 6 * documentation for any purpose, without fee, and without written agreement is 7 * hereby granted, provided that the above copyright notice and the following 8 * two paragraphs appear in all copies of this software. 9 * 10 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR 11 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 12 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF 13 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14 * 15 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 17 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 18 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO 19 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 20 * 21 * Author: Matt Welsh <mdw@cs.berkeley.edu> 22 * 23 */ 24 25 package seda.sandStorm.internal; 26 27 import seda.sandStorm.api.*; 28 import seda.sandStorm.core.*; 29 import seda.sandStorm.main.*; 30 import java.io.IOException; 31 import java.util.*; 32 33 /** 34 * ConfigData is used to pass configuration arguments into various 35 * components. 36 */ 37 public class ConfigData implements ConfigDataIF { 38 private static final boolean DEBUG = false; 39 40 private Hashtable vals; 41 private ManagerIF mgr; 42 private StageIF stage; 43 44 /** 45 * Create a ConfigData with the given manager and no argument list. 46 */ ConfigData(ManagerIF mgr)47 public ConfigData(ManagerIF mgr) { 48 this.mgr = mgr; 49 this.vals = new Hashtable(1); 50 } 51 52 /** 53 * Create a ConfigData with the given manager and argument list. 54 */ ConfigData(ManagerIF mgr, Hashtable args)55 public ConfigData(ManagerIF mgr, Hashtable args) { 56 this.mgr = mgr; 57 this.vals = args; 58 if (vals == null) vals = new Hashtable(1); 59 } 60 61 /** 62 * Create a ConfigData with the given manager and argument list, 63 * specified as an array of strings of the form "key=value". 64 * 65 * @throws IOException If any of the strings to not match the 66 * pattern "key=value". 67 */ ConfigData(ManagerIF mgr, String args[])68 public ConfigData(ManagerIF mgr, String args[]) throws IOException { 69 this.mgr = mgr; 70 this.vals = stringArrayToHT(args); 71 if (vals == null) vals = new Hashtable(1); 72 } 73 74 /** 75 * Returns true if the given key is set. 76 */ contains(String key)77 public boolean contains(String key) { 78 if (vals.get(key) != null) return true; 79 else return false; 80 } 81 82 /** 83 * Get the string value corresponding to the given key. 84 * Returns null if not set. 85 */ getString(String key)86 public String getString(String key) { 87 return (String)vals.get(key); 88 } 89 90 /** 91 * Get the integer value corresponding to the given key. 92 * Returns -1 if not set or if the value is not an integer. 93 */ getInt(String key)94 public int getInt(String key) { 95 String val = (String)vals.get(key); 96 if (val == null) return -1; 97 try { 98 return Integer.parseInt(val); 99 } catch (NumberFormatException e) { 100 return -1; 101 } 102 } 103 104 /** 105 * Get the double value corresponding to the given key. 106 * Returns -1.0 if not set or if the value is not a double. 107 */ getDouble(String key)108 public double getDouble(String key) { 109 String val = (String)vals.get(key); 110 if (val == null) return -1; 111 try { 112 return new Double(val).doubleValue(); 113 } catch (NumberFormatException e) { 114 return -1.0; 115 } 116 } 117 118 /** 119 * Get the boolean value corresponding to the given key. 120 * Returns false if not set. 121 */ getBoolean(String key)122 public boolean getBoolean(String key) { 123 String val = (String)vals.get(key); 124 if (val == null) return false; 125 else if (val.equals("true") || val.equals("TRUE")) return true; 126 else return false; 127 } 128 129 /** 130 * Get the string list value corresponding to the given key. 131 * Returns null if not set. 132 */ getStringList(String key)133 public String[] getStringList(String key) { 134 String ret[]; 135 String val = (String)vals.get(key); 136 if (val == null) return null; 137 StringTokenizer st = new StringTokenizer(val, SandstormConfig.LIST_ELEMENT_DELIMITER); 138 Vector v = new Vector(1); 139 while (st.hasMoreElements()) { 140 v.addElement(st.nextElement()); 141 } 142 ret = new String[v.size()]; 143 for (int i = 0; i < ret.length; i++) { 144 ret[i] = (String)v.elementAt(i); 145 } 146 return ret; 147 } 148 149 /** 150 * Set the given key to the given string value. 151 */ setString(String key, String val)152 public void setString(String key, String val) { 153 vals.put(key, val); 154 } 155 156 /** 157 * Set the given key to the given integer value. 158 */ setInt(String key, int val)159 public void setInt(String key, int val) { 160 vals.put(key, Integer.toString(val)); 161 } 162 163 /** 164 * Set the given key to the given double value. 165 */ setDouble(String key, double val)166 public void setDouble(String key, double val) { 167 vals.put(key, Double.toString(val)); 168 } 169 170 /** 171 * Set the given key to the given boolean value. 172 */ setBoolean(String key, boolean val)173 public void setBoolean(String key, boolean val) { 174 vals.put(key, (val == true)?"true":"false"); 175 } 176 177 /** 178 * Set the given key to the given string list value. 179 */ setStringList(String key, String valarr[])180 public void setStringList(String key, String valarr[]) { 181 String s = ""; 182 for (int i = 0; i < valarr.length; i++) { 183 s += valarr[i]; 184 if (i != valarr.length-1) s += SandstormConfig.LIST_ELEMENT_DELIMITER; 185 } 186 vals.put(key, s); 187 } 188 189 /** 190 * Return the local manager. 191 */ getManager()192 public ManagerIF getManager() { 193 return mgr; 194 } 195 196 /** 197 * Return the stage for this ConfigData. 198 */ getStage()199 public StageIF getStage() { 200 return stage; 201 } 202 203 // Used to set stage after creating wrapper setStage(StageIF stage)204 public void setStage(StageIF stage) { 205 this.stage = stage; 206 } 207 208 // Used to reset manager after creating stage 209 // (for proxying the manager) setManager(ManagerIF mgr)210 void setManager(ManagerIF mgr) { 211 this.mgr = mgr; 212 } 213 214 // Convert an array of "key=value" strings to a Hashtable stringArrayToHT(String arr[])215 private Hashtable stringArrayToHT(String arr[]) throws IOException { 216 if (arr == null) return null; 217 Hashtable ht = new Hashtable(1); 218 for (int i = 0; i < arr.length; i++) { 219 StringTokenizer st = new StringTokenizer(arr[i], "="); 220 String key; 221 String val; 222 try { 223 key = st.nextToken(); 224 val = st.nextToken(); 225 while (st.hasMoreTokens()) val += "="+st.nextToken(); 226 } catch (NoSuchElementException e) { 227 throw new IOException("Could not convert string '"+arr[i]+"' to key=value pair"); 228 } 229 ht.put(key, val); 230 } 231 return ht; 232 } 233 234 } 235