📄 configcontainer.java
字号:
/* * Copyright (c) 2003-2005 The BISON Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */package peersim.config;import java.lang.reflect.*;import java.util.*;import org.lsmp.djep.groupJep.*;/** * This class is the container for the configuration data used in * {@link Configuration}; see that class for more information. */public class ConfigContainer{// =================== static fields =================================// ===================================================================/** Symbolic constant for no debug */private static final int DEBUG_NO = 0;/** Symbolic constant for regular debug */private static final int DEBUG_REG = 1;/** Symbolic constant for extended debug */private static final int DEBUG_CONTEXT = 2;//========================== fields =================================//===================================================================/** * The properties object that stores all configuration information. */private Properties config;/** * Map associating string protocol names to the numeric protocol * identifiers. The protocol names are understood without prefix. */private Map<String, Integer> protocols;/** * The maximum depth that can be reached when analyzing expressions. This * value can be substituted by setting the configuration parameter * PAR_MAXDEPTH. */private int maxdepth;/** Debug level */private int debugLevel;/** * If true, no exception is thrown. Instead, an error is printed and the * Configuration tries to return a reasonable return value */private boolean check = false;// =================== initialization ================================// ===================================================================public ConfigContainer(Properties config, boolean check){ this.config = config; this.check = check; maxdepth = getInt(Configuration.PAR_MAXDEPTH, Configuration.DEFAULT_MAXDEPTH); // initialize protocol id-s protocols = new HashMap<String, Integer>(); String[] prots = getNames(Configuration.PAR_PROT);// they're returned in correct order for (int i = 0; i < prots.length; ++i) { protocols.put(prots[i].substring(Configuration.PAR_PROT.length() + 1), Integer.valueOf(i)); } String debug = config.getProperty(Configuration.PAR_DEBUG); if (Configuration.DEBUG_EXTENDED.equals(debug)) debugLevel = DEBUG_CONTEXT; else if (Configuration.DEBUG_FULL.equals(debug)) { Map<String, String> map = new TreeMap<String, String>(); Enumeration e = config.propertyNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); String value = config.getProperty(name); map.put(name, value); } Iterator i = map.keySet().iterator(); while (i.hasNext()) { String name = (String) i.next(); System.err.println("DEBUG " + name + ("".equals(map.get(name)) ? "" : " = " + map.get(name))); } } else if (debug != null) { debugLevel = DEBUG_REG; } else { debugLevel = DEBUG_NO; }}// =================== static public methods =========================// ===================================================================/** * @return true if and only if name is a specified (existing) property. */public boolean contains(String name){ boolean ret = config.containsKey(name); debug(name, "" + ret); return ret;}// -------------------------------------------------------------------/** * Reads given configuration property. If not found, throws a * {@link MissingParameterException}. * @param name * Name of configuration property * @param def * default value */public boolean getBoolean(String name, boolean def){ try { return getBool(name); } catch (RuntimeException e) { manageDefault(name, def, e); return def; }}// -------------------------------------------------------------------/** * Reads given property. If not found, or the value is empty string then * throws a {@link MissingParameterException}. Empty string is not * accepted as false due to the similar function of {@link #contains} which * returns true in that case. True is returned if the lowercase value of * the property is "true", otherwise false is returned. * @param name * Name of configuration property */public boolean getBoolean(String name){ try { return getBool(name); } catch (RuntimeException e) { manageException(name, e); return false; }}//-------------------------------------------------------------------/** * The actual methods that implements getBoolean. */private boolean getBool(String name){ if (config.getProperty(name) == null) { throw new MissingParameterException(name);// "\nPossibly incorrect property: " + getSimilarProperty(name)); } if (config.getProperty(name).matches("\\p{Blank}*")) { throw new MissingParameterException(name, "Blank value is not accepted when parsing Boolean."); } boolean ret = Boolean.valueOf(config.getProperty(name)); debug(name, "" + ret); return ret;}// -------------------------------------------------------------------/** * Reads given configuration property. If not found, returns the default * value. * @param name * Name of configuration property * @param def * default value */public int getInt(String name, int def){ try { Number ret = getVal(name, name, 0); debug(name, "" + ret); return ret.intValue(); } catch (RuntimeException e) { manageDefault(name, def, e); return def; }}// -------------------------------------------------------------------/** * Reads given configuration property. If not found, throws a * {@link MissingParameterException}. * @param name * Name of configuration property */public int getInt(String name){ try { Number ret = getVal(name, name, 0); debug(name, "" + ret); return ret.intValue(); } catch (RuntimeException e) { manageException(name, e); return 0; }}// -------------------------------------------------------------------/** * Reads given configuration property. If not found, returns the default * value. * @param name * Name of configuration property * @param def * default value */public long getLong(String name, long def){ try { Number ret = getVal(name, name, 0); debug(name, "" + ret); return ret.longValue(); } catch (RuntimeException e) { manageDefault(name, def, e); return def; }}// -------------------------------------------------------------------/** * Reads given configuration property. If not found, throws a * {@link MissingParameterException}. * @param name * Name of configuration property */public long getLong(String name){ try { Number ret = getVal(name, name, 0); debug(name, "" + ret); return ret.longValue(); } catch (RuntimeException e) { manageException(name, e); return 0; }}// -------------------------------------------------------------------/** * Reads given configuration property. If not found, returns the default * value. * @param name * Name of configuration property * @param def * default value */public double getDouble(String name, double def){ try { Number ret = getVal(name, name, 0); debug(name, "" + ret); return ret.doubleValue(); } catch (RuntimeException e) { manageDefault(name, def, e); return def; }}// -------------------------------------------------------------------/** * Reads given configuration property. If not found, throws a * MissingParameterException. * @param name * Name of configuration property */public double getDouble(String name){ try { Number ret = getVal(name, name, 0); debug(name, "" + ret); return ret.doubleValue(); } catch (RuntimeException e) { manageException(name, e); return 0; }}// -------------------------------------------------------------------/** * Read numeric property values, parsing expression if necessary. * * @param initial * the property name that started this expression evaluation * @param property * the current property name to be evaluated * @param depth * the depth reached so far * @return the evaluation of the expression associated to property */private Number getVal(String initial, String property, int depth){ if (depth > maxdepth) { throw new IllegalParameterException(initial, "Probable recursive definition - exceeded maximum depth " + maxdepth); } String s = config.getProperty(property); if (s == null || s.equals("")) { throw new MissingParameterException(property, " when evaluating property " + initial);// + "\nPossibly incorrect property: " + getSimilarProperty(property)); } GroupJep jep = new GroupJep(new Operators()); jep.setAllowUndeclared(true); jep.parseExpression(s); String[] symbols = getSymbols(jep); for (int i = 0; i < symbols.length; i++) { Object d = getVal(initial, symbols[i], depth + 1); jep.addVariable(symbols[i], d); } Object ret = jep.getValueAsObject(); if (jep.hasError()) System.err.println(jep.getErrorInfo()); return (Number) ret;}// -------------------------------------------------------------------/** * Returns an array of string, containing the symbols contained in the * expression parsed by the specified JEP parser. * @param jep * the java expression parser containing the list of variables * @return an array of strings. */private String[] getSymbols(org.nfunk.jep.JEP jep){ Hashtable h = jep.getSymbolTable(); String[] ret = new String[h.size()]; Enumeration e = h.keys(); int i = 0; while (e.hasMoreElements()) { ret[i++] = (String) e.nextElement(); } return ret;}// -------------------------------------------------------------------/** * Reads given configuration property. If not found, returns the default * value. * @param name * Name of configuration property * @param def * default value */public String getString(String name, String def){ try { return getStr(name); } catch (RuntimeException e) { manageDefault(name, def, e); return def; } }// -------------------------------------------------------------------/** * Reads given configuration property. If not found, throws a * MissingParameterException. Removes trailing whitespace characters. * @param name * Name of configuration property */public String getString(String name){ try { return getStr(name); } catch (RuntimeException e) { manageException(name, e); return ""; }}/** * The actual method implementing getString(). */private String getStr(String name){ String result = config.getProperty(name); if (result == null) { throw new MissingParameterException(name);// "\nPossibly incorrect property: " + getSimilarProperty(name)); } debug(name, "" + result); return result.trim();}// -------------------------------------------------------------------/** * Reads the given property from the configuration interpreting it as a * protocol name. Returns the numeric protocol identifier of this protocol * name. See the discussion of protocol name at {@link Configuration} for * details on how this numeric id is calculated * * @param name * Name of configuration property * @return the numeric protocol identifier associated to the value of the * property */public int getPid(String name){ try { String protname = getStr(name); return lookupPid(protname); } catch (RuntimeException e) { manageException(name, e); return 0; }}// -------------------------------------------------------------------/** * Calls {@link #getPid(String)}, and returns the default if no property * is defined with the given name. * * @param name * Name of configuration property * @param pid * the default protocol identifier * @return the numeric protocol identifier associated to the value of the * property, or the default if not defined */public int getPid(String name, int pid){ try { String protname = getStr(name); return lookupPid(protname); } catch (RuntimeException e) { manageDefault(name, pid, e); return pid; }}// -------------------------------------------------------------------/** * Returns the numeric protocol identifier of the given protocol name. * * @param protname * the protocol name. * @return the numeric protocol identifier associated to the protocol name */public int lookupPid(String protname){ Integer ret = protocols.get(protname); if (ret == null) { throw new MissingParameterException(Configuration.PAR_PROT + "." + protname);// "\nPossibly incorrect property: "// + getSimilarProperty(PAR_PROT + "." + protname)); } return ret.intValue();}// -------------------------------------------------------------------/** * Returns the name of a protocol that has the given identifier. * <p> * Note that this is not a constant time operation in the number of * protocols, although typically there are very few protocols defined. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -