0001: /*
0002: * Enhydra Java Application Server Project
0003: *
0004: * The contents of this file are subject to the Enhydra Public License
0005: * Version 1.1 (the "License"); you may not use this file except in
0006: * compliance with the License. You may obtain a copy of the License on
0007: * the Enhydra web site ( http://www.enhydra.org/ ).
0008: *
0009: * Software distributed under the License is distributed on an "AS IS"
0010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
0011: * the License for the specific terms governing rights and limitations
0012: * under the License.
0013: *
0014: * The Initial Developer of the Enhydra Application Server is Lutris
0015: * Technologies, Inc. The Enhydra Application Server and portions created
0016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
0017: * All Rights Reserved.
0018: *
0019: * Contributor(s):
0020: *
0021: * $Id: Config.java,v 1.3 2007-10-19 10:35:46 sinisa Exp $
0022: */
0023:
0024: package com.lutris.util;
0025:
0026: import java.lang.reflect.Array;
0027: import java.lang.reflect.Constructor;
0028: import java.util.Hashtable;
0029:
0030: import org.enhydra.util.ConfigFileInterface;
0031:
0032: /**
0033: * Config is essentially a KeywordValueTable used for recursive
0034: * storage of data derived from a config file. The contents is
0035: * initialized but <CODE>ConfigFile</CODE>.
0036: *
0037: * @see ConfigFile
0038: * @see KeywordValueTable
0039: * @author John Marco
0040: * @author Shawn McMurdo
0041: * @version $Revision: 1.3 $
0042: */
0043: public class Config extends KeywordValueTable {
0044:
0045: /**
0046: * Passed as the "count" argument to getInts, getLongs(), etc. to
0047: * indicate that all available elements are to be retrieved.
0048: */
0049: private static final int GET_ALL = -1;
0050:
0051: /**
0052: * The ConfigFile object this Config is associated with (if any)
0053: */
0054: private ConfigFileInterface configFile = null;
0055:
0056: /**
0057: * Default constructor for an empty Config.
0058: */
0059: public Config() {
0060: super ();
0061: }
0062:
0063: /**
0064: * Constructor that takes a KeywordValueTable as initialization.
0065: * @param kvt KeywordValueTable with which to initialize Config
0066: */
0067: public Config(KeywordValueTable kvt) {
0068: super ();
0069: String[] keys = kvt.keys();
0070: for (int i = 0; i < keys.length; i++) {
0071: try {
0072: set(keys[i], kvt.get(keys[i]));
0073: } catch (KeywordValueException e) {
0074: // This shouldn't happen
0075: throw new FatalExceptionError(e);
0076: }
0077: }
0078: }
0079:
0080: /**
0081: * Constructor that takes a KeywordValueTable and a ConfigFile as
0082: * initialization. The ConfigFile is associated with this Config
0083: * object.
0084: * @param kvt KeywordValueTable with which to initialize Config
0085: * @param configFile ConfigFile to associate this Config object with
0086: */
0087: public Config(KeywordValueTable kvt, ConfigFileInterface configFile) {
0088: this (kvt);
0089: this .configFile = configFile;
0090: }
0091:
0092: /**
0093: * Constructor that associates this Config with a given ConfigFile.
0094: * @param configFile ConfigFile to associate this object with
0095: */
0096: public Config(ConfigFileInterface configFile) {
0097: this ();
0098: this .configFile = configFile;
0099: }
0100:
0101: /**
0102: * Returnes cloned Config object. The cloned and original objects have exatly
0103: * same key value pairs, but free of any common object references. Only
0104: * reference which is same for both objects (the original, and it's clone) is
0105: * reference to associated ConfigFile object (private argument of Config class).
0106: * @return A cloned Config object.
0107: * @exception KeywordValueException
0108: */
0109: public Config getClonedConfig() throws KeywordValueException { // VR 13.12.2002.
0110:
0111: Config returnConfig = this .getClonedConfigParams();
0112:
0113: // TJ 08.11.2003. put under comment begin
0114: // ConfigFileInterface cf = new ConfigFile(returnConfig);
0115: // cf.setFile(this.getConfigFile().getFile());
0116: // returnConfig.setConfigFile(cf);
0117: // return returnConfig;
0118: // TJ 08.11.2003. put under comment end
0119:
0120: ConfigFileInterface cf = null;
0121: Class classObj = this .getConfigFile().getClass();
0122: // String className = classObj.getName();
0123:
0124: Class[] classParam = new Class[1];
0125: Constructor constr = null;
0126: Object[] arguments = new Object[1];
0127: try {
0128: classParam[0] = Class.forName("com.lutris.util.Config");
0129: constr = classObj.getConstructor(classParam);
0130: arguments[0] = (Object) (this );
0131: cf = (ConfigFileInterface) constr.newInstance(arguments);
0132: cf.setFile(this .getConfigFile().getFile());
0133: returnConfig.setConfigFile(cf);
0134: } catch (Exception ex) {
0135: returnConfig.setConfigFile(cf);
0136: }
0137: return returnConfig;
0138: }
0139:
0140: /**
0141: * Returnes cloned Config object. The cloned and original objects have exatly
0142: * same key-value pairs, but free of any common object references. Returned
0143: * cloned Config object has not defined it's private argument of type ConfigFile
0144: * (reference to it's configuration file on hard disc). Method is specialy
0145: * designed to enable recursion calls, and it should be called by public method
0146: * getClonedConfig().
0147: * @return A cloned Config object withouth reference to it's configuration file.
0148: * @exception KeywordValueException
0149: */
0150: private Config getClonedConfigParams() throws KeywordValueException { // VR 13.12.2002.
0151:
0152: Config returnConfig = new Config();
0153: String[] keys = this .keys();
0154:
0155: for (int i = 0; i < keys.length; i++) {
0156: Object tempValue = this .get(keys[i]);
0157:
0158: if (tempValue instanceof String) {
0159: returnConfig.set(keys[i], (String) tempValue);
0160: } else if (tempValue instanceof KeywordValueTable) { // useing of recursion
0161: Config kvtTempConfig = (new Config(
0162: (KeywordValueTable) tempValue))
0163: .getClonedConfigParams();
0164: returnConfig.set(keys[i],
0165: (KeywordValueTable) kvtTempConfig);
0166: } else if (tempValue instanceof Config) { // useing of recursion
0167: Config tempConfig = ((Config) tempValue)
0168: .getClonedConfigParams();
0169: returnConfig.set(keys[i], tempConfig);
0170: } else if (tempValue.getClass().isArray()) {
0171: int len = Array.getLength(tempValue);
0172: String[] newTemp = new String[len];
0173: for (int k = 0; k < len; k++) {
0174: newTemp[k] = Array.get(tempValue, k).toString();
0175: }
0176: returnConfig.set(keys[i], newTemp);
0177: }
0178: }
0179: return returnConfig;
0180: }
0181:
0182: // DT 30.12.2003 BEG
0183: /**
0184: * Returns the hashtable containing names and values of all config parameters
0185: * Names are in the form name-level-0_name-level-1_name-level-2...
0186: *
0187: * @param prefix - previous level name
0188: * @return hashtable of all config parameters
0189: */
0190: public Hashtable allConfigParams(String prefix)
0191: throws KeywordValueException {
0192:
0193: String[] keys = this .keys();
0194: Hashtable ht = new Hashtable();
0195: for (int i = 0; i < keys.length; i++) {
0196: Object tempValue = this .get(keys[i]);
0197: String name;
0198:
0199: if (prefix != null && prefix.length() > 0) {
0200: name = prefix + "_" + keys[i];
0201: } else {
0202: name = keys[i];
0203: }
0204:
0205: if (tempValue instanceof String) {
0206: ht.put(name, (String) tempValue);
0207: } else if (tempValue instanceof KeywordValueTable) { // useing of recursion
0208: ht.putAll(((Config) tempValue).allConfigParams(name));
0209: } else if (tempValue instanceof Config) { // useing of recursion
0210: ht.putAll(((Config) tempValue).allConfigParams(name));
0211: } else if (tempValue.getClass().isArray()) {
0212: int len = Array.getLength(tempValue);
0213: StringBuffer tempBuffer = new StringBuffer("");
0214: if (len > 0) {
0215: for (int k = 0; k < len; k++) {
0216: tempBuffer.append(","
0217: + (String) Array.get(tempValue, k)
0218: .toString());
0219: }
0220: ht.put(name + "_Array", tempBuffer.toString()
0221: .substring(1));
0222: } else {
0223: ht.put(name + "_Array", tempBuffer.toString());
0224: }
0225: }
0226: }
0227: return ht;
0228: }
0229:
0230: // DT 30.12.2003 END
0231: /**
0232: * Imorts and synchronizes to, all parameters (key-value pairs) according to
0233: * given Config object parameters. All parameters which do not exist in imported
0234: * Config object will be removed.
0235: * @param config Config object which has key-value pairs for importing.
0236: * @exception KeywordValueException
0237: */
0238: public void importConfig(Config config)
0239: throws KeywordValueException { // VR 14.12.2002.
0240:
0241: String[] original = this .keys();
0242: String[] in = config.keys();
0243:
0244: //adding or changing keys from imported Config object
0245: for (int i = 0; i < in.length; i++)
0246: this .set(in[i], config.get(in[i]));
0247: //removing keys which did not exist in imported Config object
0248: for (int i = 0; i < original.length; i++) {
0249: if (!config.containsKey(original[i]))
0250: this .remove(in[i]);
0251: }
0252: }
0253:
0254: /**
0255: * Allocates a new section. The overrides the default method to allocate a
0256: * section that is of class <CODE>Config<CODE>.
0257: * @return A reference to a new section.
0258: * @see KeywordValueTable#newSection
0259: */
0260: protected KeywordValueTable newSection() {
0261: return new Config(configFile);
0262: }
0263:
0264: /**
0265: * Gets the <code>ConfigFile</code> associated with this object.
0266: * @return the associated <code>ConfigFile</code>,
0267: * <code>null</code> if there is no config file associated with this
0268: * object.
0269: */
0270: public ConfigFileInterface getConfigFile() {
0271: return configFile;
0272: }
0273:
0274: /**
0275: * Sets the <code>ConfigFile</code> associated with this object.
0276: * For use by <code>ConfigFile</code> only, anyone else please use
0277: * the appropriate constructor
0278: * @param configFile ConfigFile object associated with this object
0279: */
0280: public void setConfigFile(ConfigFileInterface configFile) {
0281: this .configFile = configFile;
0282: }
0283:
0284: /**
0285: * Get the value of a section as a <CODE>Config</CODE> object.
0286: * @param keyword The keyword of the field. This can be a simple keyword
0287: * or a recursive, dot-seperated keyword path.
0288: * @return A reference to the section object or null if not found.
0289: * @exception KeywordValueException If the keyword is not syntactically
0290: * legal or a non-leaf element of the keyword is not a section or the value
0291: * object is not a KeywordValueTable.
0292: * @see KeywordValueTable#getSection
0293: */
0294: public synchronized Config getConfig(String keyword)
0295: throws KeywordValueException {
0296: return (Config) getSection(keyword);
0297: }
0298:
0299: /**
0300: * Gets the value of a section as a <CODE>KeywordValueTable</CODE> object.
0301: * This method overrides the KeywordValueTable.getSection in order to
0302: * insure that Config.getSection() always returns a Config object even
0303: * if a KeywordValueTable was inserted into the Config as a section.
0304: * @param keyword The keyword of the field. This can be a simple keyword
0305: * or a recursive, dot-seperated keyword path.
0306: * @return A reference to the section object or null if not found.
0307: * @exception KeywordValueException If the keyword is not syntactically legal
0308: * or a non-leaf element of the keyword is not a section or the value object is
0309: * not a KeywordValueTable.
0310: * @see KeywordValueTable#getSection
0311: */
0312: public synchronized KeywordValueTable getSection(String keyword)
0313: throws KeywordValueException {
0314: KeywordValueTable kvt = super .getSection(keyword);
0315: if (kvt == null) {
0316: return null;
0317: }
0318: if (kvt instanceof Config) {
0319: return kvt;
0320: } else {
0321: return new Config(kvt, configFile);
0322: }
0323: }
0324:
0325: /**
0326: * Returns <code>true</code> if the specified key is found,
0327: * <code>false</code> otherwise.
0328: * @param key The key whose existence is to be tested.
0329: * @return <code>true</code> if the key was found, otherwise <code>false</code>.
0330: */
0331: public boolean containsKey(String key) {
0332: boolean result = false;
0333: try {
0334: result = super .containsKey(key);
0335: } catch (KeywordValueException e) {
0336: result = false;
0337: }
0338: return result;
0339: }
0340:
0341: /**
0342: * Returns the number of data elements for a given key, or <code>-1</code> if
0343: * the key is not found.
0344: * @param key The key to search for.
0345: * @return The number of entries for the given key, or <code>-1</code> if
0346: * the key is not found.
0347: * @exception ConfigException
0348: */
0349: public int containsCount(String key) throws ConfigException {
0350: Object valObj = null;
0351: try {
0352: valObj = get(key);
0353: if (valObj == null)
0354: return -1;
0355: return Array.getLength(valObj);
0356: } catch (KeywordValueException e) {
0357: throw new ConfigException(e.getMessage());
0358: } catch (IllegalArgumentException e) {
0359: // Caused by object not being array.
0360: }
0361: // Assume if object was not null, it was single-valued entity.
0362: if (valObj == null)
0363: return -1;
0364: return 1;
0365: }
0366:
0367: /**
0368: * Is the key is an array, or a single value. If this returns true,
0369: * you should use <CODE>getStrings()</CODE> (or if you know the type
0370: * of the data, you can use, for example, <CODE>getInts()</CODE>).
0371: * If this returns false, you shoud use <CODE>getString()</CODE>
0372: * (or if you know the type of the data, you can use, for example,
0373: * <CODE>getInt()</CODE>).
0374: * @param key The key to search for.
0375: * @return True if the key is an array, false if it is a single value.
0376: * @exception ConfigException If the key is not found.
0377: */
0378: public boolean isArray(String key) throws ConfigException {
0379: Object valObj = null;
0380: try {
0381: valObj = get(key);
0382: if (valObj == null)
0383: throw new ConfigException("Key \"" + key
0384: + "\" not found.");
0385: // Attempt array access. This will fail if not an array.
0386: Array.getLength(valObj);
0387: // It must be an array if we made it to here.
0388: return true;
0389: } catch (KeywordValueException e) {
0390: throw new ConfigException(e.getMessage());
0391: } catch (IllegalArgumentException e) {
0392: // Caused by object not being array.
0393: return false;
0394: }
0395: }
0396:
0397: /**
0398: * Returns the array of longs associated with a given key. If the
0399: * <code>count</code> parameter is not <code>GET_ALL</code> and the
0400: * number of elements in the retrieved array does not match <code>
0401: * count</code> then a ConfigException is thrown with <code>reason</code>
0402: * set to <code>COUNT</code>.
0403: * If any of the retrieved elements cannot be converted to
0404: * longs due to invalid syntax or overflow, then a ConfigException
0405: * is thrown with <code>reason</code> set to <code>FORMAT</code>.
0406: * @param key The key to use to search for the configuration entries.
0407: * @param count The number of entries expected in the result. If the number of
0408: * retrieved entries does not match <code>count</code> and <code>count</code>
0409: * is not <code> GET_ALL</code> then a <code>ConfigException</code> error is
0410: * thrown.
0411: * @return An array of longs containing the list of long values from
0412: * the configuration input stream.
0413: * @exception ConfigException Thrown if the requested entry does not exist
0414: * or elements are not in the requested format.
0415: * @see ConfigException
0416: * @see Config#GET_ALL
0417: */
0418: private final long[] getLongsInternal(String key, int count)
0419: throws ConfigException {
0420:
0421: Object obj;
0422: try {
0423: obj = get(key);
0424: } catch (KeywordValueException e) {
0425: throw new ConfigException(e.getMessage());
0426: }
0427: if (obj == null) {
0428: throw new ConfigException(ConfigException.NOT_FOUND,
0429: "Key \"" + key + "\" not found in configuration.");
0430: }
0431: long[] la = null;
0432: if (obj.getClass().isArray()) {
0433: int len = Array.getLength(obj);
0434: la = new long[len];
0435: for (int i = 0; i < len; i++) {
0436: try {
0437: la[i] = (long) Long.parseLong(Array.get(obj, i)
0438: .toString());
0439: } catch (Throwable e) {
0440: throw new ConfigException("Element " + i
0441: + " is not a long.");
0442: }
0443: }
0444: } else {
0445: la = new long[1];
0446: try {
0447: if (obj instanceof java.lang.Long) {
0448: la[0] = ((java.lang.Long) obj).longValue();
0449: } else {
0450: la[0] = Long.parseLong(obj.toString());
0451: }
0452: } catch (Throwable e) {
0453: throw new ConfigException("Element 0 is not a long.");
0454: }
0455: }
0456: if ((count != GET_ALL) && (la.length != count)) {
0457: throw new ConfigException(ConfigException.COUNT, "Key \""
0458: + key + "\" has " + la.length
0459: + " elements. (expected " + count + ")");
0460: }
0461: return la;
0462: }
0463:
0464: /**
0465: * Returns a single long integer value associated with a given key.
0466: * If the key is associated with more than one element, or the
0467: * retrieved element cannot be converted to a long integer then a
0468: * <code>ConfigException</code> exception is thrown.
0469: * @param key The key to use to search for the configuration entry.
0470: * @return The long integer value associated with the given key.
0471: * @exception ConfigException Thrown if the requested entry does not exist
0472: * or elements are not in the requested format.
0473: * @see ConfigException
0474: */
0475: public long getLong(String key) throws ConfigException {
0476: return (getLongsInternal(key, 1))[0];
0477: }
0478:
0479: /**
0480: * Returns a single long integer value associated with a given key.
0481: * If the key is associated with more than one element then a
0482: * <code>ConfigException</code> error is thrown with <code>reason</code>
0483: * set to <code>COUNT</code>. If the retrieved element cannot be
0484: * converted to a long integer then a <code>ConfigException</code> error
0485: * is thrown with <code>reason</code> set to <code>FORMAT</code>.
0486: * @param key The key to use to search for the configuration entry.
0487: * @param defaultValue The default value to use if the requested entry
0488: * does not exist.
0489: * @return The long integer value associated with the given key.
0490: * @exception ConfigException Thrown if there was not exactly one requested
0491: * element, or if the element is of the wrong data type or format.
0492: * @see ConfigException
0493: */
0494: public long getLong(String key, long defaultValue)
0495: throws ConfigException {
0496: try {
0497: return (getLongsInternal(key, 1))[0];
0498: } catch (ConfigException e) {
0499: if (e.reason != e.NOT_FOUND) {
0500: throw e;
0501: }
0502: return defaultValue;
0503: }
0504: }
0505:
0506: /**
0507: * Returns all long integer values associated with a given key.
0508: * If any of the elements associated with the key cannot be converted
0509: * to a long integer then a <code>ConfigException</code> error is thrown.
0510: * @param key The key to use to search for the configuration entry.
0511: * @return An array of longs containing the list of long values from
0512: * the configuration input stream.
0513: * @exception ConfigException Thrown if the requested entry does not exist
0514: * or elements are not in the requested format.
0515: * @see ConfigException
0516: */
0517: public long[] getLongs(String key) throws ConfigException {
0518: return getLongsInternal(key, GET_ALL);
0519: }
0520:
0521: /**
0522: * Returns all long integer values associated with a given key.
0523: * If any of the elements associated with the key cannot be converted
0524: * to a long integer then a <code>ConfigException</code> error is thrown.
0525: * @param key The key to use to search for the configuration entry.
0526: * @param defaultValue The default value to use if the requested entry
0527: * does not exist.
0528: * @return An array of longs containing the list of long values from the
0529: * configuration input stream.
0530: * @exception ConfigException Thrown if the requested entries are of the wrong
0531: * data type or format.
0532: * @see ConfigException
0533: */
0534: public long[] getLongs(String key, long[] defaultValue)
0535: throws ConfigException {
0536: try {
0537: return getLongsInternal(key, GET_ALL);
0538: } catch (ConfigException e) {
0539: if (e.reason != e.NOT_FOUND) {
0540: throw e;
0541: }
0542: return defaultValue;
0543: }
0544: }
0545:
0546: /**
0547: * Returns the array of integers associated with a given key. If the
0548: * <code>count</code> parameter is not <code>GET_ALL</code> and the
0549: * number of elements in the retrieved array does not match <code>
0550: * count</code> then a ConfigException is thrown with <code>reason</code>
0551: * set to <code>COUNT</code>.
0552: * If any of the retrieved elements cannot be converted to
0553: * integers due to invalid syntax or overflow, then a ConfigException
0554: * is thrown with <code>reason</code> set to <code>FORMAT</code>.
0555: * @param key The key to use to search for the configuration entries.
0556: * @param count The number of entries expected in the result. If the number of
0557: * retrieved entries does not match <code>count</code> and <code>count</code> is
0558: * not <code> GET_ALL</code> then a <code>ConfigException</code> error is thrown.
0559: * @return An array of integers containing the list of integer values from
0560: * the configuration input stream.
0561: * @exception ConfigException Thrown if the requested entry does not exist or
0562: * elements are not in the requested format.
0563: * @see ConfigException
0564: * @see Config#GET_ALL
0565: */
0566: private final int[] getIntsInternal(String key, int count)
0567: throws ConfigException {
0568: Object obj;
0569: try {
0570: obj = get(key);
0571: } catch (KeywordValueException e) {
0572: throw new ConfigException(e.getMessage());
0573: }
0574: if (obj == null) {
0575: throw new ConfigException(ConfigException.NOT_FOUND,
0576: "Key \"" + key + "\" not found in configuration.");
0577: }
0578: int[] ia = null;
0579: if (obj.getClass().isArray()) {
0580: int len = Array.getLength(obj);
0581: ia = new int[len];
0582: for (int i = 0; i < len; i++) {
0583: try {
0584: ia[i] = (int) Integer.parseInt(Array.get(obj, i)
0585: .toString());
0586: } catch (Throwable e) {
0587: throw new ConfigException("Element " + i
0588: + " is not an integer.");
0589: }
0590: }
0591: } else {
0592: ia = new int[1];
0593: try {
0594: if (obj instanceof java.lang.Integer) {
0595: ia[0] = ((java.lang.Integer) obj).intValue();
0596: } else {
0597: ia[0] = Integer.parseInt(obj.toString());
0598: }
0599: } catch (Throwable e) {
0600: throw new ConfigException(
0601: "Element 0 is not an integer.");
0602: }
0603: }
0604: if ((count != GET_ALL) && (ia.length != count)) {
0605: throw new ConfigException(ConfigException.COUNT, "Key \""
0606: + key + "\" has " + ia.length
0607: + " elements. (expected " + count + ")");
0608: }
0609: return ia;
0610: }
0611:
0612: /**
0613: * Returns a single integer value associated with a given key.
0614: * If the key is associated with more than one element, or the
0615: * retrieved element cannot be converted to a integer then a
0616: * <code>ConfigException</code> exception is thrown.
0617: * @param key The key to use to search for the configuration entry.
0618: * @return The integer value associated with the given key.
0619: * @exception ConfigException Thrown if the requested entry does not exist
0620: * or elements are not in the requested format.
0621: * @see ConfigException
0622: */
0623: public int getInt(String key) throws ConfigException {
0624: return (getIntsInternal(key, 1))[0];
0625: }
0626:
0627: /**
0628: * Returns a single integer value associated with a given key.
0629: * If the key is associated with more than one element, or the
0630: * retrieved element cannot be converted to a integer then a
0631: * <code>ConfigException</code> exception is thrown.
0632: * @param key The key to use to search for the configuration entry.
0633: * @param defaultValue The default value to use if the requested entry
0634: * does not exist.
0635: * @return The integer value associated with the given key.
0636: * @exception ConfigException Thrown if there was not exactly one
0637: * requested element, or if the element is of the wrong data type or format.
0638: * @see ConfigException
0639: */
0640: public int getInt(String key, int defaultValue)
0641: throws ConfigException {
0642: try {
0643: return (getIntsInternal(key, 1))[0];
0644: } catch (ConfigException e) {
0645: if (e.reason != e.NOT_FOUND) {
0646: throw e;
0647: }
0648: return defaultValue;
0649: }
0650: }
0651:
0652: /**
0653: * Returns all integer values associated with a given key.
0654: * If any of the elements associated with the key cannot be converted
0655: * to a integer then a <code>ConfigException</code> error is thrown.
0656: * @param key The key to use to search for the configuration entry.
0657: * @return An array of integers containing the list of integer values from
0658: * the configuration input stream.
0659: * @exception ConfigException Thrown if the requested entry does not exist
0660: * or elements are not in the requested format.
0661: * @see ConfigException
0662: */
0663: public int[] getInts(String key) throws ConfigException {
0664: return getIntsInternal(key, GET_ALL);
0665: }
0666:
0667: /**
0668: * Returns all integer values associated with a given key.
0669: * If any of the elements associated with the key cannot be converted
0670: * to a integer then a <code>ConfigException</code> error is thrown.
0671: * @param key The key to use to search for the configuration entry.
0672: * @param defaultValue The default value to use if the requested entry
0673: * does not exist.
0674: * @return An array of integers containing the list of integer values from
0675: * the configuration input stream.
0676: * @exception ConfigException Thrown if the requested entries are of
0677: * the wrong data type or format.
0678: * @see ConfigException
0679: */
0680: public int[] getInts(String key, int[] defaultValue)
0681: throws ConfigException {
0682: try {
0683: return getIntsInternal(key, GET_ALL);
0684: } catch (ConfigException e) {
0685: if (e.reason != e.NOT_FOUND) {
0686: throw e;
0687: }
0688: return defaultValue;
0689: }
0690: }
0691:
0692: /**
0693: * Returns the array of strings associated with a given key. If the
0694: * <code>count</code> parameter is not <code>GET_ALL</code> and the
0695: * number of elements in the retrieved array does not match <code>
0696: * count</code> then a ConfigException is thrown with <code>reason</code>
0697: * set to <code>COUNT</code>.
0698: * @param key The key to use to search for the configuration entries.
0699: * @param count The number of entries expected in the result. If the number of
0700: * retrieved entries does not match <code>count</code> and <code>count</code> is
0701: * not <code> GET_ALL</code> then a <code>ConfigException</code> error is thrown.
0702: * @return An array of Strings containing the list of String values from the
0703: * configuration input stream.
0704: * @exception ConfigException Thrown if the requested entry does not exist
0705: * or elements are not in the requested format.
0706: * @see ConfigException
0707: * @see Config#GET_ALL
0708: */
0709: private final String[] getStringsInternal(String key, int count)
0710: throws ConfigException {
0711: Object obj;
0712: try {
0713: obj = get(key);
0714: } catch (KeywordValueException e) {
0715: throw new ConfigException(e.getMessage());
0716: }
0717: if (obj == null) {
0718: throw new ConfigException(ConfigException.NOT_FOUND,
0719: "Key \"" + key + "\" not found in configuration.");
0720: }
0721: String[] sa = null;
0722: if (obj.getClass().isArray()) {
0723: int len = Array.getLength(obj);
0724: sa = new String[len];
0725: for (int i = 0; i < len; i++) {
0726: try {
0727: sa[i] = Array.get(obj, i).toString();
0728: } catch (Throwable e) {
0729: throw new ConfigException("Element " + i
0730: + " is not a String.");
0731: }
0732: }
0733: } else {
0734: sa = new String[1];
0735: try {
0736: sa[0] = obj.toString();
0737: } catch (Throwable e) {
0738: throw new ConfigException("Element 0 is not a String.");
0739: }
0740: }
0741: if ((count != GET_ALL) && (sa.length != count)) {
0742: throw new ConfigException(ConfigException.COUNT, "Key \""
0743: + key + "\" has " + sa.length
0744: + " elements. (expected " + count + ")");
0745: }
0746: return sa;
0747: }
0748:
0749: /**
0750: * Returns a single String value associated with a given key.
0751: * If the key is associated with more than one element, or the
0752: * retrieved element cannot be converted to a String then a
0753: * <code>ConfigException</code> exception is thrown.
0754: * @param key The key to use to search for the configuration entry.
0755: * @return The string value associated with the given key.
0756: * @exception ConfigException Thrown if the requested entry does not exist
0757: * or elements are not in the requested format.
0758: * @see ConfigException
0759: */
0760: public String getString(String key) throws ConfigException {
0761: return (getStringsInternal(key, 1))[0];
0762: }
0763:
0764: /**
0765: * Returns a single String value associated with a given key.
0766: * If the key is associated with more than one element, or the
0767: * retrieved element cannot be converted to a String then a
0768: * <code>ConfigException</code> exception is thrown.
0769: * @param key The key to use to search for the configuration entry.
0770: * @param defaultValue The default value to use if the requested entry
0771: * does not exist.
0772: * @return The string value associated with the given key.
0773: * @exception ConfigException Thrown if there was not exactly one
0774: * requested element, or if the element is of the wrong data type or format.
0775: * @see ConfigException
0776: */
0777: public String getString(String key, String defaultValue)
0778: throws ConfigException {
0779: try {
0780: return (getStringsInternal(key, 1))[0];
0781: } catch (ConfigException e) {
0782: if (e.reason != e.NOT_FOUND) {
0783: throw e;
0784: }
0785: return defaultValue;
0786: }
0787: }
0788:
0789: /**
0790: * Returns all String values associated with a given key.
0791: * If any of the elements associated with the key cannot be converted
0792: * to a String then a <code>ConfigException</code> error is thrown.
0793: * @param key The key to use to search for the configuration entry.
0794: * @return An array of strings containing the list of string values from
0795: * the configuration input stream.
0796: * @exception ConfigException Thrown if the requested entry does not exist
0797: * or elements are not in the requested format.
0798: * @see ConfigException
0799: */
0800: public String[] getStrings(String key) throws ConfigException {
0801: return getStringsInternal(key, GET_ALL);
0802: }
0803:
0804: /**
0805: * Returns all String values associated with a given key.
0806: * If any of the elements associated with the key cannot be converted
0807: * to a String then a <code>ConfigException</code> error is thrown.
0808: * @param key The key to use to search for the configuration entry.
0809: * @param defaultValue The default value to use if the requested entry
0810: * does not exist.
0811: * @return An array of strings containing the list of string values from
0812: * the configuration input stream.
0813: * @exception ConfigException Thrown if the requested entries are of
0814: * the wrong data type or format.
0815: * @see ConfigException
0816: */
0817: public String[] getStrings(String key, String[] defaultValue)
0818: throws ConfigException {
0819: try {
0820: return getStringsInternal(key, GET_ALL);
0821: } catch (ConfigException e) {
0822: if (e.reason != e.NOT_FOUND) {
0823: throw e;
0824: }
0825: return defaultValue;
0826: }
0827: }
0828:
0829: /**
0830: * Returns the array of booleans associated with a given key. If the
0831: * <code>count</code> parameter is not <code>GET_ALL</code> and the
0832: * number of elements in the retrieved array does not match <code>
0833: * count</code> then a ConfigException is thrown with <code>reason</code>
0834: * set to <code>COUNT</code>.
0835: * If any of the retrieved elements cannot be converted to
0836: * booleans due to invalid syntax or overflow, then a ConfigException
0837: * is thrown with <code>reason</code> set to <code>FORMAT</code>.
0838: * A boolean value is represented in a case independent manner as
0839: * either the string <code>true</code> or <code>false</code>.
0840: * @param key The key to use to search for the configuration entries.
0841: * @param count The number of entries expected in the result. If the number of
0842: * retrieved entries does not match <code>count</code> and <code>count</code> is
0843: * not <code> GET_ALL</code> then a <code>ConfigException</code> error is thrown.
0844: * @return An array of booleans containing the list of boolean values from
0845: * the configuration input stream.
0846: * @exception ConfigException Thrown if the requested entry does not exist
0847: * or elements are not in the requested format.
0848: * @see ConfigException
0849: * @see Config#GET_ALL
0850: */
0851: private final boolean[] getBooleansInternal(String key, int count)
0852: throws ConfigException {
0853: Object obj;
0854: try {
0855: obj = get(key);
0856: } catch (KeywordValueException e) {
0857: throw new ConfigException(e.getMessage());
0858: }
0859: if (obj == null) {
0860: throw new ConfigException(ConfigException.NOT_FOUND,
0861: "Key \"" + key + "\" not found in configuration.");
0862: }
0863: boolean[] ba = null;
0864: if (obj.getClass().isArray()) {
0865: int len = Array.getLength(obj);
0866: ba = new boolean[len];
0867: for (int i = 0; i < len; i++) {
0868: try {
0869: ba[i] = Boolean.valueOf(
0870: Array.get(obj, i).toString().toLowerCase())
0871: .booleanValue();
0872: } catch (Throwable e) {
0873: throw new ConfigException("Element " + i
0874: + " is not a boolean.");
0875: }
0876: }
0877: } else {
0878: ba = new boolean[1];
0879: try {
0880: if (obj instanceof java.lang.Boolean) {
0881: ba[0] = ((java.lang.Boolean) obj).booleanValue();
0882: } else {
0883: ba[0] = Boolean.valueOf(
0884: obj.toString().toLowerCase())
0885: .booleanValue();
0886: }
0887: } catch (Throwable e) {
0888: throw new ConfigException("Element 0 is not a boolean.");
0889: }
0890: }
0891: if ((count != GET_ALL) && (ba.length != count)) {
0892: throw new ConfigException(ConfigException.COUNT, "Key \""
0893: + key + "\" has " + ba.length
0894: + " elements. (expected " + count + ")");
0895: }
0896: return ba;
0897: }
0898:
0899: /**
0900: * Returns a single boolean value associated with a given key.
0901: * If the key is associated with more than one element, or the
0902: * retrieved element cannot be converted to a boolean then a
0903: * <code>ConfigException</code> exception is thrown.
0904: * @param key The key to use to search for the configuration entry.
0905: * @return The boolean value associated with the given key.
0906: * @exception ConfigException Thrown if the requested entry does not exist
0907: * or elements are not in the requested format.
0908: * @see ConfigException
0909: */
0910: public boolean getBoolean(String key) throws ConfigException {
0911: return (getBooleansInternal(key, 1))[0];
0912: }
0913:
0914: /**
0915: * Returns a single boolean value associated with a given key.
0916: * If the key is associated with more than one element, or the
0917: * retrieved element cannot be converted to a boolean then a
0918: * <code>ConfigException</code> exception is thrown.
0919: * @param key The key to use to search for the configuration entry.
0920: * @param defaultValue The default value to use if the requested entry
0921: * does not exist.
0922: * @return The boolean value associated with the given key.
0923: * @exception ConfigException Thrown if there was not exactly one requested
0924: * element, or if the element is of the wrong data type or format.
0925: * @see ConfigException
0926: */
0927: public boolean getBoolean(String key, boolean defaultValue)
0928: throws ConfigException {
0929: try {
0930: return (getBooleansInternal(key, 1)[0]);
0931: } catch (ConfigException e) {
0932: if (e.reason != e.NOT_FOUND) {
0933: throw e;
0934: }
0935: return defaultValue;
0936: }
0937: }
0938:
0939: /**
0940: * Returns all boolean values associated with a given key.
0941: * If any of the elements associated with the key cannot be converted
0942: * to a boolean then a <code>ConfigException</code> error is thrown.
0943: * @param key The key to use to search for the configuration entry.
0944: * @return An array of booleans containing the list of boolean values from
0945: * the configuration input stream.
0946: * @exception ConfigException Thrown if the requested entry does not exist
0947: * or elements are not in the requested format.
0948: * @see ConfigException
0949: */
0950: public boolean[] getBooleans(String key) throws ConfigException {
0951: return getBooleansInternal(key, GET_ALL);
0952: }
0953:
0954: /**
0955: * Returns all boolean values associated with a given key.
0956: * If any of the elements associated with the key cannot be converted
0957: * to a boolean then a <code>ConfigException</code> error is thrown.
0958: * @param key The key to use to search for the configuration entry.
0959: * @param defaultValue The default value to use if the requested entry does not
0960: * exist.
0961: * @return An array of booleans containing the list of boolean values from
0962: * the configuration input stream.
0963: * @exception ConfigException Thrown if the requested entries are of
0964: * the wrong data type or format.
0965: * @see ConfigException
0966: */
0967: public boolean[] getBooleans(String key, boolean[] defaultValue)
0968: throws ConfigException {
0969: try {
0970: return getBooleansInternal(key, GET_ALL);
0971: } catch (ConfigException e) {
0972: if (e.reason != e.NOT_FOUND) {
0973: throw e;
0974: }
0975: return defaultValue;
0976: }
0977: }
0978:
0979: /**
0980: * Returns the array of doubles associated with a given key. If the
0981: * <code>count</code> parameter is not <code>GET_ALL</code> and the
0982: * number of elements in the retrieved array does not match <code>
0983: * count</code> then a ConfigException is thrown with <code>reason</code>
0984: * set to <code>COUNT</code>. If any of the retrieved elements cannot be
0985: * converted to doubles due to invalid syntax or overflow, then a
0986: * ConfigException is thrown with <code>reason</code> set to
0987: * <code>FORMAT</code>.
0988: * @param key The key to use to search for the configuration entries.
0989: * @param count The number of entries expected in the result. If the number of
0990: * retrieved entries does not match <code>count</code> and <code>count</code> is
0991: * not <code> GET_ALL</code> then a <code>ConfigException</code> error is thrown.
0992: * @return An array of doubles containing the list of double values from
0993: * the configuration input stream.
0994: * @exception ConfigException Thrown if the requested entry does not exist
0995: * or elements are not in the requested format.
0996: * @see ConfigException
0997: * @see Config#GET_ALL
0998: */
0999: private final double[] getDoublesInternal(String key, int count)
1000: throws ConfigException {
1001: Object obj;
1002: try {
1003: obj = get(key);
1004: } catch (KeywordValueException e) {
1005: throw new ConfigException(e.getMessage());
1006: }
1007: if (obj == null) {
1008: throw new ConfigException(ConfigException.NOT_FOUND,
1009: "Key \"" + key + "\" not found in configuration.");
1010: }
1011: double[] da = null;
1012: if (obj.getClass().isArray()) {
1013: int len = Array.getLength(obj);
1014: da = new double[len];
1015: for (int i = 0; i < len; i++) {
1016: try {
1017: da[i] = Double
1018: .valueOf(Array.get(obj, i).toString())
1019: .doubleValue();
1020: } catch (Throwable e) {
1021: throw new ConfigException("Element " + i
1022: + " is not a double.");
1023: }
1024: }
1025: } else {
1026: da = new double[1];
1027: try {
1028: if (obj instanceof java.lang.Double) {
1029: da[0] = ((java.lang.Double) obj).doubleValue();
1030: } else {
1031: da[0] = Double.valueOf(obj.toString())
1032: .doubleValue();
1033: }
1034: } catch (Throwable e) {
1035: throw new ConfigException("Element 0 is not a double.");
1036: }
1037: }
1038: if ((count != GET_ALL) && (da.length != count)) {
1039: throw new ConfigException(ConfigException.COUNT, "Key \""
1040: + key + "\" has " + da.length
1041: + " elements. (expected " + count + ")");
1042: }
1043: return da;
1044: }
1045:
1046: /**
1047: * Returns a single double value associated with a given key. If the key is
1048: * associated with more than one element, or the retrieved element cannot be
1049: * converted to a double then a <code>ConfigException</code> exception is thrown.
1050: * @param key The key to use to search for the configuration entry.
1051: * @return The double value associated with the given key.
1052: * @exception ConfigException Thrown if the requested entry does not exist
1053: * or elements are not in the requested format.
1054: * @see ConfigException
1055: */
1056: public double getDouble(String key) throws ConfigException {
1057: return (getDoublesInternal(key, 1))[0];
1058: }
1059:
1060: /**
1061: * Returns a single double value associated with a given key.
1062: * If the key is associated with more than one element, or the
1063: * retrieved element cannot be converted to a double then a
1064: * <code>ConfigException</code> exception is thrown.
1065: * @param key The key to use to search for the configuration entry.
1066: * @param defaultValue The default value to use if the requested entry
1067: * does not exist.
1068: * @return The double value associated with the given key.
1069: * @exception ConfigException Thrown if there was not exactly one requested
1070: * element, or if the element is of the wrong data type or format.
1071: * @see ConfigException
1072: */
1073: public double getDouble(String key, double defaultValue)
1074: throws ConfigException {
1075: try {
1076: return (getDoublesInternal(key, 1)[0]);
1077: } catch (ConfigException e) {
1078: if (e.reason != e.NOT_FOUND) {
1079: throw e;
1080: }
1081: return defaultValue;
1082: }
1083: }
1084:
1085: /**
1086: * Returns all double values associated with a given key.
1087: * If any of the elements associated with the key cannot be converted
1088: * to a double then a <code>ConfigException</code> error is thrown.
1089: * @param key The key to use to search for the configuration entry.
1090: * @return An array of doubles containing the list of double values from
1091: * the configuration input stream.
1092: * @exception ConfigException Thrown if the requested entry does not exist
1093: * or elements are not in the requested format.
1094: * @see ConfigException
1095: */
1096: public double[] getDoubles(String key) throws ConfigException {
1097: return getDoublesInternal(key, GET_ALL);
1098: }
1099:
1100: /**
1101: * Returns all double values associated with a given key.
1102: * If any of the elements associated with the key cannot be converted
1103: * to a double then a <code>ConfigException</code> error is thrown.
1104: * @param key The key to use to search for the configuration entry.
1105: * @param defaultValue The default value to use if the requested entry
1106: * does not exist.
1107: * @return An array of doubles containing the list of double values from
1108: * the configuration input stream.
1109: * @exception ConfigException Thrown if the requested entries are of
1110: * the wrong data type or format.
1111: * @see ConfigException
1112: */
1113: public double[] getDoubles(String key, double[] defaultValue)
1114: throws ConfigException {
1115: try {
1116: return getDoublesInternal(key, GET_ALL);
1117: } catch (ConfigException e) {
1118: if (e.reason != e.NOT_FOUND) {
1119: throw e;
1120: }
1121: return defaultValue;
1122: }
1123: }
1124:
1125: /**
1126: * Returns a DataSource value associated with a given key.
1127: * @param key The key to use to search for the configuration entry.
1128: * @return The DataSource value associated with the given key.
1129: * @exception ConfigException Thrown if the requested entry does not exist
1130: * or elements are not in the requested format.
1131: * @see ConfigException
1132: */
1133: public Object getDataSource(String key) throws ConfigException {
1134: Object obj;
1135: try {
1136: obj = get(key);
1137: } catch (KeywordValueException e) {
1138: throw new ConfigException(e.getMessage());
1139: }
1140: if (obj == null) {
1141: throw new ConfigException(ConfigException.NOT_FOUND,
1142: "Key \"" + key + "\" not found in configuration.");
1143: }
1144: Object ds = null;
1145: if (obj.getClass().isArray()) {
1146: throw new ConfigException(
1147: "The value is an array. The method returns a single DataStruct Object.");
1148: } else {
1149:
1150: try {
1151: ds = obj;
1152: } catch (Throwable e) {
1153: throw new ConfigException(
1154: "Element is not a DataSource.");
1155: }
1156: }
1157: return ds;
1158: }
1159:
1160: /**
1161: * Returns a single DataSource value associated with a given key.
1162: * If the key is associated with more than one element, or the
1163: * retrieved element cannot be converted to a String then a
1164: * <code>ConfigException</code> exception is thrown.
1165: * @param key The key to use to search for the configuration entry.
1166: * @param defaultValue The default value to use if the requested entry
1167: * does not exist.
1168: * @return The DataSource value associated with the given key.
1169: * @exception ConfigException Thrown if there was not exactly one
1170: * requested element, or if the element is of the wrong data type or format.
1171: * @see ConfigException
1172: */
1173: public Object getDataSource(String key, Object defaultValue)
1174: throws ConfigException {
1175: try {
1176: return getDataSource(key);
1177: } catch (ConfigException e) {
1178: if (e.reason != e.NOT_FOUND) {
1179: throw e;
1180: }
1181: return defaultValue;
1182: }
1183: }
1184:
1185: }
|