/*
* 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;
}
}
// ---------------------------------------------------
- 1
- 2
前往页