⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configuration.java

📁 peersim仿真chord协议的测试过的环境
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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.util.*;/** * Fully static class to store configuration information. It defines a * method, {@link #setConfig(Properties)}, to set configuration data. This * method is called by the simulator engines as the very first thing they * do. It can be called only once, after that the class becomes read only. * All components can then access this configuration and utility methods to * read property values based on their names. * <p> * The design of this class also hides the actual implementation of the * configuration which can be Properties, XML, whatever. Currently only * Properties is supported. * <p> * Apart from storing (name,value) pairs, this class also does some * processing, and offers some utility functions. This extended * functionality consists of the following: reading values with type * checking, ordering of entries, pre-processing protocol names, parsing * expressions, resolving underspecified classnames, and finally some basic * debugging possibilities. We discuss these in the following. * <p> * Note that the configuration is initialized using a Properties object. * The class of this object might implement some additional pre-processing * on the file or provide an extended syntax for defining property files. * See {@link ParsedProperties} for more details. This is the class that is * currently used by simulation engines. * <h3>Typed reading of values</h3> * Properties can have arbitrary values of type String. This class offers a * set of read methods that perform the appropriate conversion of the * string value to the given type, eg long. They also allow for specifying * default values in case the given property is not specified. * <h3>Resolving class names</h3> *  * The possibilities for the typed reading of a value includes interpreting * the value as a class name. In this case an object will be constructed. * It is described at method {@link #getInstance(String)} how this is * achieved exactly. What needs to be noted here is that the property value * need not be a fully specified classname. It might contain only the short * class name without the package specification. In this case, it is * attempted to locate a class with that name in the classpath, and if a * unique class is found, it will be used. This simplifies the * configuration files and also allows to remove their dependence on the * exact location of the class. *  * <h3>Components and their ordering</h3> * The idea of the configuration is that it mostly contains components and * their descriptions (parameters). Although this class is blind to the * semantics of these components, it offers some low level functionality * that helps dealing with them. This functionality is based on the * assumption that components have a type and a name. Both types and names * are strings of alphanumeric and underscore characters. For example, * {@value #PAR_PROT} is a type, "foo" can be a name. Method * {@link #getNames} allow the caller to get the list of names for a given * type. Some other methods, like {@link #getInstanceArray} use this list * to return a list of components. *  * <p> * Assuming the configuration is in Properties format (which is currently * the only format available) component types and names are defined as * follows. Property names containing two non-empty words separated by one * dot (".") character are treated specially (the words contain word * characters: alphanumeric and underscore ("_")). The first word will be * the type, and the second is the name of a component. For example, *  * <pre> *   control.conn ConnectivityObserver *   control.1 WireKOut *   control.2 PrintGraph * </pre> *  * defines control components of names "conn","1" an "2" (arguments of the * components not shown). When {@link #getNames} or * {@link #getInstanceArray} are called, eg * <code>getNames("control")</code>, then the order in which these are * returned is alphabetical: * <code>["control.1","control.2","control.conn"]</code>. If you are not * satisfied with lexicographic order, you can specify the order in this * way. *  * <pre> *   order.control 1,conn,2 * </pre> *  * where the names are separated by any non-word character (non * alphanumeric or underscore). If not all names are listed then the given * order is followed by alphabetical order of the rest of the items, e.g. *  * <pre> *   order.control 2 * </pre> *  * results in <code>["control.2","control.1","control.conn"]</code>. * <p> * It is also possible to exclude elements from the list, while ordering * them. The syntax is identical to that of the above, only the parameter * name begins with <code>include</code>. For example *  * <pre> *   include.control conn 2 * </pre> *  * will result in returning <em>only</em> <code>control.conn</code> and * <code>control.2</code>, in this order. Note that for example the * empty list results in a zero length array in this case. * <em>Important!</em> If include is defined then ordering is ignored. * That is, include is stronger than order. * <h3>Protocol names</h3> * As mentioned, the configuration is generally blind to the actual names * of the components. There is an exception: the components of type * {@value #PAR_PROT}. These are pre-processed a bit to enhance * performance: protocol names are mapped to numeric protocol identifiers. * The numeric identifier of a protocol is its index in the array returned * by {@link #getNames}. See above how to control this order. The numeric * identifiers then can be looked up based on the name and vice versa. * Besides, the identifier can be directly requested based on a property * name when the protocol name is the value of a property which is * frequently the case. * <p> * <h3>Expressions</h3> * Numeric property values can be complex expressions, that are parsed * using <a href="http://www.singularsys.com/jep/">JEP</a>. You can write * expression using the syntax that you can find <a * href="http://www.singularsys.com/jep/doc/html/op_and_func.html"> here</a>. * For example, *  * <pre> *   MAG 2 *   SIZE 2&circ;MAG * </pre> *  * SIZE=4. You can also have complex expression trees like this: *  * <pre> *   A B+C *   B D+E *   C E+F *   D 1 *   E F *   F 2 * </pre> *  * that results in A=7, B=3, C=4, D=1, E=2, F=2 *  * <p> * Expressions like "sub-expression op sub-expression" are computed based * on the type of the sub-expressions. If both sub-expressions are integer, * the computation is done using integer arithmetics and the result is an * integer. So, for example, 5/2 returns 2. If one of the sub-expression is * floating point, the computation is based on floating-point arithmetics * (double precision) and the result is a floating point value. So, for * example, 5.0/2 returns 2.5. *  * <p> * Expressions are parsed recursively. Note that no optimization is done, * so expression F is evaluated three times here (due to the fact that * appears twice in C and once in B). But since properties are read just * once at initialization, this is not a performance problem. *  * <p> * Finally, recursive definitions are not allowed (and without function * definitions, they make no sense). Since it is difficult to discover * complex recursive chains, a simple trick is used: if the depth of * recursion is greater than a given threshold (configurable, currently * {@value #DEFAULT_MAXDEPTH}, an error message is printed. This avoids to * fill the stack, that results in an anonymous OutOfMemoryError. So, if * you write *  * <pre> *   overlay.size SIZE *   SIZE SIZE-1 * </pre> *  * you get an error message: Parameter "overlay.size": Probable recursive * definition - exceeded maximum depth {@value #DEFAULT_MAXDEPTH} *  * <h3>Debug</h3> *  * It is possible to obtain debug information about the configuration * properties by activating special configuration properties. * <p> * If property {@value #PAR_DEBUG} is defined, each config property and the * associated value are printed. Properties that are not present in the * config file but have default values are postfixed with the string * "(DEFAULT)". * <p> * If property {@value #PAR_DEBUG} is defined and it is equal to * {@value #DEBUG_EXTENDED}, information about the configuration method * invoked, and where this method is invoked, is also printed. If it is * equal to {@value #DEBUG_FULL}, all the properties are printed, even if * they are not read. * <p> * Each line printed by this debug feature is prefixed by the string * "DEBUG". *  * <h3>Use of brackets</h3> *  * For the sake of completeness, we mention it here that if this class is * initialized using {@link ParsedProperties}, then it is possible to use * some more compressed format to specify the components. See * {@link ParsedProperties#load}. *  */public class Configuration{// =================== static fields =================================// ===================================================================/** Default max depth limit to avoid recursive definitions */public static final int DEFAULT_MAXDEPTH = 100;/** * The debug level for the configuration. If defined, a line is printed for * each configuration parameter read. If defined and equal to * {@value #DEBUG_EXTENDED}, additional context information for debug is * printed. If defined and equal to {@value #DEBUG_FULL}, all the * configuration properties are printed at the beginning, not just when * they are called. * @config */static final String PAR_DEBUG = "debug.config";/** * If parameter {@value #PAR_DEBUG} is equal to this string, additional * context information for debug is printed. */static final String DEBUG_EXTENDED = "context";/** * If parameter {value #PAR_DEBUG} is equal to this string, all the * configuration properties are printed at the beginning, not just when * they are called. */static final String DEBUG_FULL = "full";/** * The maximum depth for expressions. This is a simple mechanism to avoid * unbounded recursion. The default is {@value #DEFAULT_MAXDEPTH}, and you * probably don't want to change it. * @config */static final String PAR_MAXDEPTH = "expressions.maxdepth";/** * Used to configure ordering of the components. Determines the ordering in * the array as returned by {@link #getNames}. See the general description * of {@link Configuration} for details. * @config */static final String PAR_ORDER = "order";/** * Used to configure ordering of the components. Determines the ordering in * the array as returned by {@link #getNames}, and can bu used to also * exclude elements. See the general description of {@link Configuration} * for details. * @config */static final String PAR_INCLUDE = "include";// XXX it's ugly because it replicates the definition of Node.PAR_PROT, but// this would be the only dependence on the rest of the core.../** * The type name of components describing protocols. This is the only point * at which the class is not blind to the actual semantics of the * configuration. */static final String PAR_PROT = "protocol";/** * The properties object that stores all configuration information. */private static ConfigContainer config = null;// =================== initialization ================================// ===================================================================/** to prevent construction */private Configuration(){}// =================== static public methods =========================// ===================================================================/** * Sets the system-wide configuration in Properties format. It can be * called only once. After that the configuration becomes unmodifiable * (read only). If modification is attempted, a RuntimeException is thrown * and no change is made. * @param p *          The Properties object containing configuration info */public static void setConfig(Properties p){	if (config != null) {		throw new RuntimeException("Setting configuration was attempted twice.");	}	config = new ConfigContainer(p, false);}// -------------------------------------------------------------------/** * Sets the system-wide configuration in Properties format. It can be * called only once. After that the configuration becomes unmodifiable * (read only). If modification is attempted, a RuntimeException is thrown * and no change is made. * @param p *          The Properties object containing configuration info */public static void setConfig(Properties p, boolean check){	if (config != null) {		throw new RuntimeException("Setting configuration was attempted twice.");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -