001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.param;
028:
029: import java.io.FileNotFoundException;
030: import java.io.InputStream;
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.Enumeration;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Map;
038: import java.util.Vector;
039:
040: import org.apache.xerces.parsers.SAXParser;
041: import org.cougaar.lib.plugin.UTILEntityResolver;
042: import org.cougaar.lib.xml.parser.ParamParser;
043: import org.cougaar.util.ConfigFinder;
044: import org.cougaar.util.log.Logger;
045: import org.xml.sax.InputSource;
046:
047: /**
048: * <pre>
049: * Class that encapsulates a parameter table. The table consists of
050: * [name, org.cougaar.lib.param.ParamTable] pairs. If a parameter does not exist,
051: * a ParamException will be thrown. this is not a RuntimeException,
052: * so clients must use the try{}catch{} trick. The catch block is a
053: * good place to put "default" values for the parameters.
054: * </pre>
055: */
056: public class ParamTable implements ParamMap {
057: /**
058: * No default Constructor.
059: */
060: private ParamTable() {
061: }
062:
063: /**
064: * <pre>
065: * Constructor. In the act of building a the parameter handler
066: * we pass that class a pointer to ourselves so that it can
067: * call our addParam() method.
068: *
069: * Need to provide an entity resolver to the parser.
070: * The entity resolver is just a wrapper of the configFinder.
071: * The resolver is expected to resolve a systemID, where:
072: *
073: * systemID is of the form : file:/ferris/bueller/day
074: * user.dir is : /ferris/bueller
075: * and we want just the last part (day)
076: * NOTE: on NT -->
077: * systemID is of the form : file:/C:/ferris/bueller/day
078: * user.dir is : C:/ferris/bueller
079: * No slash in front of the path on the user.dir.
080: *
081: * envParams MUST have a param "envFile". It specifies the parse file!
082: *
083: * </pre>
084: * @param logger the Logger to use
085: */
086: public ParamTable(Logger logger) {
087: paramTable = new HashMap();
088: paramParser = new ParamParser();
089: this .logger = logger;
090: }
091:
092: protected void addIniParameters(Vector envParams) {
093: // environment parameters are "name={type}value" or "name=StringValue"
094: // parse parameters. Remember these special ones: envDir, envFile
095:
096: String envDir = null;
097: String envFile = null;
098: Vector envPV = new Vector();
099:
100: Enumeration ps = envParams.elements();
101: while (ps.hasMoreElements()) {
102: String envS = (String) ps.nextElement();
103: Param p = paramParser.getParam(envS);
104: if (p != null) {
105: envPV.add(p);
106: String n = p.getName();
107: try {
108: if (n.equals("envDir")) {
109: envDir = p.getStringValue();
110: } else if (n.equals("envFile")) {
111: envFile = p.getStringValue();
112: }
113: } catch (Exception e) {
114: logger.error(e.getMessage(), e);
115: }
116: }
117: }
118:
119: // set env dir.
120: if (envDir == null)
121: envDir = "";
122: else {
123: // append slash if needed.
124: envDir = getWithSlashAppended(envDir);
125: }
126:
127: String pfile = envDir + envFile;
128:
129: populateParamMap(pfile);
130:
131: // add environment parameters, replacing any from file
132: for (Enumeration pv = envPV.elements(); pv.hasMoreElements();) {
133: Param p = (Param) pv.nextElement();
134: addParam(p.getName(), p);
135: }
136: }
137:
138: /**
139: * Given the XML parameter file <code>pfile</code>, parse it and <p>
140: * use the ParamHandler to populate the parameter map.
141: *
142: * Also uses UTILEntityResolver to find any entities that have <p>
143: * been included in the xml file. The Entity Resolver uses the <p>
144: * ConfigFinder to find referenced files.
145: *
146: * @param pfile parameter file to read
147: * @see org.cougaar.lib.param.ParamHandler#startElement
148: * @see org.cougaar.lib.plugin.UTILEntityResolver#resolveEntity
149: **/
150: protected void populateParamMap(String pfile) {
151: try {
152: InputStream inputStream = ConfigFinder.getInstance().open(
153: pfile);
154:
155: SAXParser parser = new SAXParser();
156: parser.setContentHandler(new ParamHandler(this ));
157: parser.setEntityResolver(new UTILEntityResolver(logger));
158: InputSource is = new InputSource(inputStream);
159: is.setSystemId(pfile);
160: parser.parse(is);
161: } catch (Exception e) {
162: if (logger.isDebugEnabled()) {
163: logger.debug(e.getMessage(), e);
164: }
165: if (logger.isInfoEnabled()
166: && (e instanceof FileNotFoundException)) {
167: logger
168: .info("ParamTable.populateParamMap - could not find env.xml file : "
169: + e.getMessage()
170: + "\nTurn this message off by setting logger to normal setting.");
171: }
172: }
173: }
174:
175: /** append slash if needed. */
176: static String getWithSlashAppended(final String envDir) {
177: String copy = new String(envDir);
178: char lastDirChar = copy.charAt(copy.length() - 1);
179: if ((lastDirChar != '/') && (lastDirChar != '\\')) {
180: int bwSlash = copy.indexOf('\\');
181: if (bwSlash > 0) {
182: copy += "\\";
183: } else {
184: copy += "/";
185: }
186: }
187: return copy;
188: }
189:
190: /**
191: * Add a parameter into the parameter table.
192: * @param n name of the paramter
193: * @param p the parameter to add
194: */
195: public void addParam(String n, Param p) {
196: paramTable.put(n, p);
197: }
198:
199: /**
200: * Get the value of a boolean parameter.
201: * @param name of the parameter to get
202: * @return the boolean value of the parameter
203: */
204: public boolean getBooleanParam(String name) throws ParamException {
205: if (paramTable.containsKey(name)) {
206: Param p = (Param) paramTable.get(name);
207: return p.getBooleanValue();
208: }
209: throw new ParamException("parameter " + name + " not found");
210: }
211:
212: /**
213: * Get the value of a double parameter.
214: * @param name of the parameter to get
215: * @return the double value of the parameter
216: */
217: public double getDoubleParam(String name) throws ParamException {
218: if (paramTable.containsKey(name)) {
219: Param p = (Param) paramTable.get(name);
220: return p.getDoubleValue();
221: }
222: throw new ParamException("parameter " + name + " not found");
223: }
224:
225: /**
226: * Get the value of a float parameter.
227: * @param name of the parameter to get
228: * @return the float value of the parameter
229: */
230: public float getFloatParam(String name) throws ParamException {
231: if (paramTable.containsKey(name)) {
232: Param p = (Param) paramTable.get(name);
233: return p.getFloatValue();
234: }
235: throw new ParamException("parameter " + name + " not found");
236: }
237:
238: /**
239: * Get the value of a int parameter.
240: * @param name of the parameter to get
241: * @return the int value of the parameter
242: */
243: public int getIntParam(String name) throws ParamException {
244: if (paramTable.containsKey(name)) {
245: Param p = (Param) paramTable.get(name);
246: return p.getIntValue();
247: }
248: throw new ParamException("parameter " + name + " not found");
249: }
250:
251: /**
252: * Get the value of a long parameter.
253: * @param name of the parameter to get
254: * @return the long value of the parameter
255: */
256: public long getLongParam(String name) throws ParamException {
257: if (paramTable.containsKey(name)) {
258: Param p = (Param) paramTable.get(name);
259: return p.getLongValue();
260: }
261: throw new ParamException("parameter " + name + " not found");
262: }
263:
264: /**
265: * Get the value of a short parameter.
266: * @param name of the parameter to get
267: * @return the short value of the parameter
268: */
269: public short getShortParam(String name) throws ParamException {
270: if (paramTable.containsKey(name)) {
271: Param p = (Param) paramTable.get(name);
272: return p.getShortValue();
273: }
274: throw new ParamException("parameter " + name + " not found");
275: }
276:
277: /**
278: * Get the value of a String parameter.
279: * @param name of the parameter to get
280: * @return the String value of the parameter
281: */
282: public String getStringParam(String name) throws ParamException {
283: if (paramTable.containsKey(name)) {
284: Param p = (Param) paramTable.get(name);
285: return p.getStringValue();
286: }
287: throw new ParamException("parameter " + name + " not found");
288: }
289:
290: /** is the parameter in the table? */
291: public boolean hasParam(String name) {
292: return paramTable.containsKey(name);
293: }
294:
295: /** show the parameters in the map **/
296: public String toString() {
297: StringBuffer sb = new StringBuffer();
298: List keys = new ArrayList(paramTable.keySet());
299: Collections.sort(keys);
300:
301: for (Iterator i = keys.iterator(); i.hasNext();) {
302: Object key = i.next();
303: sb.append("" + key + "->" + paramTable.get(key) + "\n");
304: }
305: return sb.toString();
306: }
307:
308: private Map paramTable;
309: protected Logger logger;
310: protected ParamParser paramParser;
311: }
|