/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 General Public License for more details.
*
*/
package org.chaidb.db.helper;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.exception.ErrorCode;
import java.io.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Config {
/**
* The flag to enable debug code. It should be set to false before releasing.
* Since this flag is constant, when it is set to false, the debug will
* be optimized by compiler.
* --Zhidong
*/
public static final boolean DEBUG_CODE = false;
//private static ConfigTool configtool;
private static String chaidbHome;
private static String SPEARATOR = File.separator;
private static boolean isOutput = false;
private static final String VALUE_KEY_WORD = "value";
private static final String NAME_KEY_WORD = "name";
private static final String DELETED_KEY_WORD = "deleted";
public static final String TYPE_KEY_WORD = "type";
public static final String MIN_KEY_WORD = "min";
public static final String MAX_KEY_WORD = "max";
private static final String FORBIDDEN_KEY_WORD = "forbidden";
private static final String NECESSARY_KEY_WORD = "necessary";
private static final String DEFAULT_KEY_WORD = "default";
private static final String HIDDEN_KEY_WORD = "hidden";
private static final String COMMENT = "comment";
private static final String BOOLEAN_TRUE = "true";
// private static final String BOOLEAN_FALSE = "false";
public static final String PROPERTY_TXN_SUPPORT = "TransactionSupport";
public static final boolean VALUE_TXN_SUPPORT_ON = true;
public static final boolean VALUE_TXN_SUPPORT_OFF = false;
public static final boolean TXN_SUPPORT = true;
//=== client retry if fail to connect to server
public static final String CLIENT_RETRY_TIMES = "client.retry.times";
public static final String CLIENT_RETRY_INTERVAL = "client.retry.interval";
public static final int DEFAULT_CLIENT_RETRY_TIMES = 3;
public static final long DEFAULT_CLIENT_RETRY_INTERVAL = 1000;
//Stylesheet Caching properties
public static final String USE_SS_CACHE = "useStylesheetCache";
public static final int SS_CACHE_ON = 1;
public static final int SS_CACHE_OFF = 0;
public static final String CHAIDB_HOME = "chaidb.home";
public static final String DB_CONF_FILE = "chaidb.conf";
public static final String CHAIDB_DATA_PATH = "chaidb.data.path";
static {
//load config from chaidb.conf
chaidbHome = System.getProperty(CHAIDB_HOME);
if (chaidbHome == null) {
throw new RuntimeException("System property chaidb.home was not defined.");
}
chaidbHome = chaidbHome.endsWith(SPEARATOR) ? chaidbHome.substring(0, chaidbHome.length() - 1) : chaidbHome;
if (!new File(chaidbHome).exists()) {
System.err.println("ChaiDB home does not exist!");
System.exit(-1);
}
InputStream is = getConfig();
if (is == null) {
System.err.println("chaidb.conf does not exist!");
System.exit(-1);
}
try {
ConfigTool.load(is);
} catch (Exception ie) {
System.err.println("Error: chaidb.conf couldn't load normally!");
System.err.println("Throw exception: " + ie.toString());
ie.printStackTrace();
System.err.println("Server couldn't run normally, exit!");
System.exit(-1);
}
// DEBUG_CODE=Config.getConfig("lock.log",false);
// TXN_SUPPORT = Config.getConfig(PROPERTY_TXN_SUPPORT,
// VALUE_TXN_SUPPORT_ON);
/* get current dbname and db directory */
}
public Config() {
}
/**
* reload the config from chaidb.conf
*
* @param dbHome
* @throws ChaiDBException
*/
public static void load(String dbHome) throws ChaiDBException {
//load config from chaidb.conf
chaidbHome = dbHome;
chaidbHome = chaidbHome.endsWith(SPEARATOR) ? chaidbHome.substring(0, chaidbHome.length() - 1) : chaidbHome;
InputStream is = getConfig();
if (is == null) {
System.err.println("chaidb.conf does not exist!");
System.exit(-1);
}
try {
ConfigTool.load(is);
} catch (ChaiDBException ie) {
System.err.println("Error: chaidb.conf couldn't load normally!");
System.err.println("Throw exception: " + ie.toString());
ie.printStackTrace();
System.err.println("Server couldn't run normally, exit!");
throw ie;
}
// DEBUG_CODE=Config.getConfig("lock.log",false);
// TXN_SUPPORT = Config.getConfig(PROPERTY_TXN_SUPPORT,
// VALUE_TXN_SUPPORT_ON);
}
/**
* get all child config key names except onfigurations which attribute
* 'hidden' is "N", the parent config was found by parentPath and
* parentConfigName.
* <p/>
* For exp:
* if getAllChildConfigsByPath("/",null), it will return first level tag name
*
* @param parentPath
* @param parentConfigName attribute
* @return Enumeration configNameEnumeration a vector whose element is string
*/
public static Enumeration getAllChildConfigsByPath(String parentPath, String parentConfigName) throws ChaiDBException {
if ((parentConfigName != null) && parentConfigName.equals("")) {
parentConfigName = null;
}
Hashtable filter = new Hashtable();
filter.put(HIDDEN_KEY_WORD, "N");
return ConfigTool.getElementChildAllElementNames(parentPath, parentConfigName, filter);
}
/**
* We use attribute 'name' to distinguish same tag name configurations,exp:
* <view>
* <driver name="oracle" value="oracle.jdbc.OracleDriver" default="oracle.jdbc.OracleDriver" hidden="N" type="String">
* <comment>
* Oracle jdbc driver
* </comment>
* </driver>
* <driver name="db2" value="db2.jdbc.Db2Driver" default="db2.jdbc.Db2Driver" hidden="N" type="String">
* <comment>
* Oracle jdbc driver
* </comment>
* </driver>
* </view>
* if getChildConfigNamesByPath("/view",null,"driver"),it will return
* enumeration that includes 'oracle',db2' two String elements.
* <p/>
* This method to get all attribute 'name's of input childConfig
* except configurations which attribute 'hidden' is "N",if no same tag,
* configuration won't have attribute 'name'.
* the parent config was found by parentPath and parentConfigName
*
* @param parentPath
* @param parentConfigName
* @param childConfig
* @return Enumeration whose element is String value of attribute 'name'.
* @throws ChaiDBException
*/
public static Enumeration getChildConfigNamesByPath(String parentPath, String parentConfigName, String childConfig) throws ChaiDBException {
if ((parentConfigName != null) && parentConfigName.equals("")) {
parentConfigName = null;
}
return ConfigTool.getElementChildElementIDByChildElementName(parentPath, parentConfigName, childConfig);
}
/**
* get configuration's comment
* if there are more than one config named commentID
* it will return the first config's comment.
*
* @param commentID
* @return String comment
*/
public static String getComment(String commentID) {
ConfigTool configTool = ConfigTool.findElement(commentID);
if (configTool == null) {
return null;
}
Iterator children = configTool.getChildren().iterator();
while ((children != null) && children.hasNext()) {
ConfigTool child = (ConfigTool) children.next();
if (child.getName().equalsIgnoreCase(COMMENT)) {
return child.getValue();
}
}
return null;
}
/**
* get configuration's comment by path and attribute 'name'. if no attribute
* name, configNameAttribute could be input null.
* <view>
* <driver name="oracle" value="oracle.jdbc.OracleDriver" default="oracle.jdbc.OracleDriver" hidden="N" type="String">
* <comment>
* Oracle jdbc driver
* </comment>
* </driver>
* <driver name="db2" value="db2.jdbc.Db2Driver" default="db2.jdbc.Db2Driver" hidden="N" type="String">
* <comment>
* Oracle jdbc driver
* </comment>
* </driver>
* </view>
* <p/>
* if getCommentByPath("/view/driver","oracle"),it will return comment
* 'Oracle jdbc driver'.
* <p/>
* For exp2:
* <system>
* <version value="3.1" default="3.1" hidden="Y" type="String">
* <comment>ChaiDB version</comment>
* </version>
* ...
* </system>
* <p/>
* if getCommentByPath("/system/version",null),it will return comment
* 'ChaiDB version'.
*
* @param path configPath
* @param commentName config's name attribute
* @return String config's comment
*/
public static String getCommentByPath(String path, String commentName) throws ChaiDBException {
if ((commentName != null) && (commentName.equals("null") || commentName.equals(""))) {
commentName = null;
}
ConfigTool configTool = ConfigTool.findElementByPath(path, commentName);
if (configTool == null) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the comment " + path + commentName + "has not existed!");
}
Iterator children = configTool.getChildren().iterator();
while ((children != null) && children.hasNext()) {
ConfigTool child = (ConfigTool) children.next();
if (child.getName().equalsIgnoreCase(COMMENT)) {
return child.getValue();
}
}
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the comment " + path + " " + commentName + "has not content!");
}
/**
* get configuration's comment
* the configuration was found by path
*
* @param path
*/
public static String getCommentByPath(String path) throws ChaiDBException {
return getCommentByPath(path, null);
}
/**
* @param config
* @param defaultConfigValue
*/
public static String getConfig(String config, String defaultConfigValue) {
try {
String value = getConfig(config);
if ((value == null) || value.equals("")) {
return defaultConfigValue;
} else {
return value;
}
// ConfigTool configTool = ConfigTool.findElement(config,
// DEFAULT_KEY_WORD,defaultConfigValue);
//
// if(configTool == null ){
// return defaultConfigValue;
// }
//
// return configTool.getAttributeValue(VALUE_KEY_WORD);
} catch (Exception e) {
return defaultConfigValue;
}
}
/**
* @param config
* @param defaultConfigValue
*/
public static boolean getConfig(String config, boolean defaultConfigValue) {
String defaultValue = defaultConfigValue + "";
try {
return Boolean.valueOf(getConfig(config, defaultValue)).booleanValue();
} catch (Exception ex) {
return defaultConfigValue;
}
}
/**
* get boolean type config value
* in chaidb.conf,the value must be "true" or "false"
*/
public static boolean getConfigAsBoolean(String configId) {
String config = getConfig(configId);
if (config == null) {
return false;
}
if (config.equals(BOOLEAN_TRUE)) {
return true;
} else {
return false;
}
}
public static int getConfigAsInt(String configId, int defaultConfigValue) {
return getConfig(configId, defaultConfigValue);
}
public static long getConfigAsLong(String configId, long defaultConfigValue) {
return getConfig(configId, defaultConfigValue);
}
/**
* overload for the getConfig
*
* @param config Name of the config properties
* @param defaultConfigValue Default property value, if nothing else is found
* @return double
*/
public static double getConfig(String config, double defaultConfigValue) {
String defaultValue = Double.toString(defaultConfigValue);
try {
return Double.parseDouble(getConfig(config, defaultValue));
} catch (Exception ex) {
return defaultConfigValue;
}
}
/**
* overload for the getConfig
*
* @param config Name of the config properties
* @param defaultConfigValue Default property value, if nothing else is found
* @return int=0 if error
*/
public static int getConfig(String config, int defaultConfigValue) {
String defaultValue = Integer.toString(defaultConfigValue);
try {
return Integer.parseInt(getConfig(config, defaultValue));
} catch (Exception ex) {
return defaultConfigValue;
}
}
/**
* overload for the getConfig
*
* @param config Name of the config properties
* @param defaultConfigValue Default property value, if nothing else is found
* @return long
*/
public static long getConfig(String config, long defaultConfigValue) {
String defaultValue = Long.toString(defaultConfigValue);
try {
return (long) Integer.parseInt(getConfig(config, defaultValue));
} catch (Exception ex) {
return defaultConfigValue;
}
}
/**
* get configId's value from xmlConfigHash which is parsed from chaidb.conf
*
* @param config
* @return String value
*/
public static String getConfig(String config) {
ConfigTool configTool = ConfigTool.findElement(config);
if (configTool == null) {
log("no such comment was found!");
return null;
}
return replaceSystemProperties(configTool.getAttributeValue(Config.VALUE_KEY_WORD));
}
public static String replaceSystemProperties(String oldString) {
Pattern p = Pattern.compile("\\$\\{[^\\}]*\\}");
Matcher m = p.matcher(oldString);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String pp = m.group();
String rep = null;
if (pp.length() > 3) {
pp = pp.substring(2, pp.length() - 1);
rep = System.getProperty(pp);
}
if (rep == null) {
rep = "";
} else {
rep = rep.replaceAll("\\\\", "\\\\\\\\");
rep = rep.replaceAll("\\$", "\\\\\\$");
}
m.appendReplacement(sb, rep);
}
m.appendTail(sb);
return sb.toString();
}
/**
* get configId's type from xmlConfigHash which is parsed from chaidb.conf
*
* @param config
* @return String type
*/
public static String getConfigType(String config) {
ConfigTool configTool = ConfigTool.findElement(config);
if (configTool == null) {
log("no such comment was found!");
return null;
}
return configTool.getAttributeValue(Config.TYPE_KEY_WORD);
}
/**
* get config by path and attribute 'name'. if no attribute name,
* configNameAttribute could be input null.
* For exp1:
* <view>
* <driver name="oracle" value="oracle.jdbc.OracleDriver" default="oracle.jdbc.OracleDriver" hidden="N" type="String">
* <comment>
* Oracle jdbc driver
* </comment>
* </driver>
* <driver name="db2" value="db2.jdbc.Db2Driver" default="db2.jdbc.Db2Driver" hidden="N" type="String">
* <comment>
* Oracle jdbc driver
* </comment>
* </driver>
* </view>
* <p/>
* if getConfigByPath("/view/driver","oracle"),it will return
* 'oracle.jdbc.OracleDriver'.
* <p/>
* For exp2:
* <system>
* <version value="3.1" default="3.1" hidden="Y" type="String">
* <comment>ChaiDB version</comment>
* </version>
* ...
* </system>
* <p/>
* if getConfigByPath("/system/version",null),it will return '3.1'.
*
* @param path
* @param configName config's name attribute
* @return String config's value
*/
public static String getConfigByPath(String path, String configName) throws ChaiDBException {
if ((configName != null) && (configName.equals("null") || configName.equals(""))) {
configName = null;
}
ConfigTool configTool = ConfigTool.findElementByPath(path, configName);
if (configTool == null) {
return null;
}
return configTool.getAttributeValue(Config.VALUE_KEY_WORD);
}
/**
* get configuration's value
* the configuration was found by path
*
* @param path
*/
public static String getConfigByPath(String path) throws ChaiDBException {
return getConfigByPath(path, null);
}
/**
* get current db directory from db.map
*
* @return String the current data directory, without end File.seperator
*/
public static String getCurrentDBDirectory() throws ChaiDBException {
String dbpath;
if ((dbpath = System.getProperty(CHAIDB_DATA_PATH)) != null) {
while (dbpath.endsWith(File.separator)) {
dbpath = dbpath.substring(0, dbpath.length() - 1);
}
return dbpath;
// String dbname = currentDbName + "_copy";
// return getDbDirecotry(dbname);
}
return getDbHome();
}
/**
* get DB home
*
* @return String
* author add by leon
*/
public static String getDbHome() {
return chaidbHome;
}
/**
* set configuration named config as newConfigVlaue
*
* @param config
* @param newConfigValue
* @throws ChaiDBException
*/
public static void setConfig(String config, String newConfigValue) throws ChaiDBException {
setConfig(config, VALUE_KEY_WORD, newConfigValue);
}
/**
* set the configuration's attribute named attributeName as the
* new attribute value - attributeValue
*
* @param config
* @param attributeName
* @param newAttributeValue
* @throws ChaiDBException
*/
public static void setConfig(String config, String attributeName, String newAttributeValue) throws ChaiDBException {
ConfigTool configTool = ConfigTool.findElement(config);
setConfig(configTool, attributeName, newAttributeValue);
}
/**
* set configuration's attribute named updateAttributeName as new
* attribute value- updateAttributeValue
* the configuration was found by findingAttributeName and
* findingAttributeVlue. the default attribute for example:
* 'default','hidden','min','max','forbidden','necessary','deleted'
* can't be updated.
*
* @param config
* @param findingAttributeName
* @param findingAttributeValue
* @param updateAttributeName
* @param updatedAttributeValue
* @throws ChaiDBException
*/
public static void setConfig(String config, String findingAttributeName, String findingAttributeValue, String updateAttributeName, String updatedAttributeValue) throws ChaiDBException {
Hashtable attributes = new Hashtable();
try {
attributes.put(findingAttributeName, findingAttributeValue);
} catch (Exception ex) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "finding condtion can't be null");
}
setConfig(config, attributes, updateAttributeName, updatedAttributeValue);
}
/**
* set configuration's attribute named updateAttributeName as new
* attribute value- updateAttributeValue
* the configuration is configTool. the default attribute for example:
* 'default','hidden','min','max','forbidden','necessary','deleted'
* can't be updated.
*
* @param configTool
* @param updateAttributeName
* @param updatedAttributeValue
* @throws ChaiDBException
*/
private static void setConfig(ConfigTool configTool, String updateAttributeName, String updatedAttributeValue) throws ChaiDBException {
if (configTool != null) {
if (updateAttributeName == null) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "null can't be used as input!");
} else
if (updateAttributeName.equalsIgnoreCase(TYPE_KEY_WORD) || // updateAttributeName.equalsIgnoreCase(DEFAULT_KEY_WORD) ||
updateAttributeName.equalsIgnoreCase(MIN_KEY_WORD) || updateAttributeName.equalsIgnoreCase(MAX_KEY_WORD) || updateAttributeName.equalsIgnoreCase(FORBIDDEN_KEY_WORD) || updateAttributeName.equalsIgnoreCase(NECESSARY_KEY_WORD) || updateAttributeName.equalsIgnoreCase(DELETED_KEY_WORD)) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the attribute " + updateAttributeName + "can't be modified!");
}
if (updateAttributeName.equalsIgnoreCase(VALUE_KEY_WORD) && varifyInput(configTool.getAttributes(), updatedAttributeValue)) {
configTool.setAttributeValue(updateAttributeName, updatedAttributeValue);
return;
}
configTool.setAttributeValue(updateAttributeName, updatedAttributeValue);
return;
}
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the config has not existed!");
}
/**
* set configuration's attribute named updateAttributeName as new
* attribute value- updateAttributeValue
* the configuration was found by findingAttributes. the default attribute for example:
* 'default','hidden','min','max','forbidden','necessary','deleted'
* can't be updated.
*
* @param config
* @param findingAttributes
* @param updateAttributeName
* @param updatedAttributeValue
* @throws ChaiDBException
*/
public static void setConfig(String config, Hashtable findingAttributes, String updateAttributeName, String updatedAttributeValue) throws ChaiDBException {
ConfigTool configTool = ConfigTool.findElement(config, findingAttributes);
setConfig(configTool, updateAttributeName, updatedAttributeValue);
}
/**
* set configuration's attribute named updateAttributeName as new
* attribute value- updateAttributeValue
* the configuration was found by path and configName
* the default attribute for example:
* 'default','hidden','min','max','forbidden','necessary','deleted'
* can't be updated.
*
* @param path
* @param configName
* @param updateAttributeName
* @param updatedAttributeValue
* @throws ChaiDBException
*/
public static void setConfigByPath(String path, String configName, String updateAttributeName, String updatedAttributeValue) throws ChaiDBException {
ConfigTool configTool = ConfigTool.findElementByPath(path, configName);
setConfig(configTool, updateAttributeName, updatedAttributeValue);
}
/**
* set configuration's 'value' attribute
* the configuration was found by path and configName
* <p/>
* For exp:
* setConfigByPath("/view/driver","newdbdriver","new.db.driver1");
*
* @param path
* @param configName
* @param newValue
* @throws ChaiDBException
*/
public static void setConfigByPath(String path, String configName, String newValue) throws ChaiDBException {
if ((configName != null) && (configName.equals("null") || configName.equals(""))) {
configName = null;
}
ConfigTool configTool = ConfigTool.findElementByPath(path, configName);
setConfig(configTool, VALUE_KEY_WORD, newValue);
}
/**
* remove configuration's all child config whose attribute delted
* must be 'Y'.
* the configuration was found by path and configName
*
* @param path
* @param configName
*/
public static void removeAllChildConfigByPath(String path, String configName) throws ChaiDBException {
Hashtable filterCannotRemoved = new Hashtable();
filterCannotRemoved.put(DELETED_KEY_WORD, "N");
ConfigTool.removeElementAllChildElements(path, configName, filterCannotRemoved);
}
/**
* remove configuration's child config whose tag is childConfig
* and name is childConfigName.and the config's attribute delted
* must be 'Y'.
* <p/>
* For exp:
* removeChildConfigByPath("/view",null,"driver","newdbdirver");
*
* @param parentPath
* @param parentConfigName
* @param childConfig
* @param childConfigName attribute
*/
public static void removeChildConfigByPath(String parentPath, String parentConfigName, String childConfig, String childConfigName) throws ChaiDBException {
if ((parentConfigName != null) && (parentConfigName.equals("") || parentConfigName.equals("null"))) {
parentConfigName = null;
}
if ((childConfigName != null) && (childConfigName.equals("") || childConfigName.equals("null"))) {
childConfigName = null;
}
Hashtable filterCannotRemoved = new Hashtable();
String deletedAttribute = ConfigTool.getElementAttributeValue(parentPath + "//" + childConfig, childConfigName, DELETED_KEY_WORD);
if (deletedAttribute == null) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the config " + childConfig + " can't be removed!");
} else {
filterCannotRemoved.put(DELETED_KEY_WORD, "N");
}
ConfigTool.removeElementChildElement(parentPath, parentConfigName, childConfig, childConfigName, filterCannotRemoved);
}
/**
* add new config to configuration.
*
* @param parenPath
* @param parentConfigName
* @param addedConfig
* @param addedConfigName
*/
public static void addChildConfigByPath(String parenPath, String parentConfigName, String addedConfig, String addedConfigName) throws ChaiDBException {
if (addedConfig == null) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "adding config can't be null!");
}
addChildConfigByPath(parenPath, parentConfigName, addedConfig, addedConfigName, null, null, null, null, null, null, null);
}
/**
* add new config to configuration.
* a new 'comment' element will be added to the new configuration.
* the new configuration's attibute 'deleted' was set to 'Y','hidden' to 'N'
* For exp:
* if addChildConfigByPath("/view", null, "driver", "newdbdirver",
* "new.db.driver", "String", null, null,"zz","e","new added driver");
* a new view's driver will be added to chaidb.conf.
*
* @param parenPath
* @param parentConfigName
* @param addedConfig
* @param addedConfigName
* @param addedConfigValue
* @param addedConfigType
* @param addedConfigMinValue
* @param addedConfigMaxValue
* @param addedConfigForbidden
* @param addedConfigNecessary
*/
public static void addChildConfigByPath(String parenPath, String parentConfigName, String addedConfig, String addedConfigName, String addedConfigValue, String addedConfigType, String addedConfigMinValue, String addedConfigMaxValue, String addedConfigForbidden, String addedConfigNecessary, String comment) throws ChaiDBException {
Hashtable attributes = new Hashtable();
if (addedConfigName != null) {
if (ConfigTool.containElement(parenPath + "//" + addedConfig, addedConfigName)) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the config " + addedConfig + "has existed!");
}
attributes.put(NAME_KEY_WORD, addedConfigName);
}
if ((parentConfigName != null) && (parentConfigName.equals("null") || parentConfigName.equals(""))) {
parentConfigName = null;
}
if ((addedConfigMinValue != null) && (addedConfigMinValue.equals("null") || addedConfigMinValue.equals(""))) {
addedConfigMinValue = null;
}
if ((addedConfigMaxValue != null) && (addedConfigMaxValue.equals("null") || addedConfigMaxValue.equals(""))) {
addedConfigMaxValue = null;
}
if ((addedConfigForbidden != null) && (addedConfigForbidden.equals("null") || addedConfigForbidden.equals(""))) {
addedConfigForbidden = null;
}
if ((addedConfigNecessary != null) && (addedConfigNecessary.equals("null") || addedConfigNecessary.equals(""))) {
addedConfigNecessary = null;
}
if (addedConfigType != null) {
attributes.put(TYPE_KEY_WORD, addedConfigType);
if (addedConfigType.equalsIgnoreCase("String")) {
if (addedConfigForbidden != null) {
attributes.put(FORBIDDEN_KEY_WORD, addedConfigForbidden);
}
if (addedConfigNecessary != null) {
attributes.put(NECESSARY_KEY_WORD, addedConfigNecessary);
}
} else if (!addedConfigType.equalsIgnoreCase("boolean")) {
if (addedConfigMinValue != null) {
attributes.put(MIN_KEY_WORD, addedConfigMinValue);
}
if (addedConfigMaxValue != null) {
attributes.put(MAX_KEY_WORD, addedConfigMaxValue);
}
}
}
if ((addedConfigValue != null) && varifyInput(attributes, addedConfigValue)) {
attributes.put(VALUE_KEY_WORD, addedConfigValue);
attributes.put(DEFAULT_KEY_WORD, addedConfigValue);
}
attributes.put(HIDDEN_KEY_WORD, "N");
attributes.put(DELETED_KEY_WORD, "Y");
ConfigTool.addElementChildElement(parenPath, parentConfigName, addedConfig, attributes);
ConfigTool configTool = ConfigTool.findElementByPath(parenPath + "//" + addedConfig, addedConfigName);
configTool.addChildElement(COMMENT, null);
configTool.setChildElementValue(COMMENT, (comment == null) ? ("new Comment" + " should be added here...") : comment);
}
/**
* add new attibute to configuration.if there has the attributes ,
* ChaiDBException will be thrown
*
* @param path
* @param configName
* @param attributeName
* @param attributeValue
*/
public static void addAttributeByPath(String path, String configName, String attributeName, String attributeValue) throws ChaiDBException {
ConfigTool.addElementAttribute(path, configName, attributeName, attributeValue);
}
/**
* @param info
*/
private static void log(String info) {
if (isOutput) {
System.out.println(info);
}
}
/**
* varified input
*
* @param attributes
* @param value
* @return
* @throws ChaiDBException
*/
private static boolean varifyInput(Hashtable attributes, String value) throws ChaiDBException {
String type = (String) attributes.get(TYPE_KEY_WORD);
String min = (String) attributes.get(MIN_KEY_WORD);
String max = (String) attributes.get(MAX_KEY_WORD);
if (type.equalsIgnoreCase("String")) {
String forbidden = (String) attributes.get(FORBIDDEN_KEY_WORD);
String necessary = (String) attributes.get(Config.NECESSARY_KEY_WORD);
if ((forbidden != null) && !forbidden.equals("") && (value.indexOf(forbidden) >= 0)) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "You input new value: \"" + value + "\",it includes forbidden string \'" + forbidden + "\'!");
}
if ((necessary != null) && !necessary.equals("") && (value.indexOf(necessary) < 0)) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "You input new value: \"" + value + "\",it doen't include necessary string \'" + necessary + "\'!");
}
return true;
}
try {
if (type.equalsIgnoreCase("boolean")) {
try {
Boolean.getBoolean(value);
return true;
} catch (Exception ex) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "This parameter is boolean type,please input" + "\"true\" or \"false\"");
}
}
boolean exceed = false;
if (type.equalsIgnoreCase("int")) {
int intValue = Integer.parseInt(value);
int iMin = Integer.MIN_VALUE;
int iMax = Integer.MAX_VALUE;
if ((min != null) && (!min.equals("null")) && (!min.equals(""))) {
iMin = Integer.parseInt(min);
}
if ((max != null) && (!max.equals("null")) && (!max.equals(""))) {
iMax = Integer.parseInt(max);
}
min = String.valueOf(iMin);
max = String.valueOf(iMax);
if ((intValue < iMin) || (intValue > iMax)) {
exceed = true;
}
}
if (type.equalsIgnoreCase("short")) {
short shortValue = Short.parseShort(value);
short iMin = Short.MIN_VALUE;
short iMax = Short.MAX_VALUE;
if ((min != null) && (!min.equals("null")) && (!min.equals(""))) {
iMin = Short.parseShort(min);
}
if ((max != null) && (!max.equals("null")) && (!max.equals(""))) {
iMax = Short.parseShort(max);
}
min = String.valueOf(iMin);
max = String.valueOf(iMax);
if ((shortValue < iMin) || (shortValue > iMax)) {
exceed = true;
}
}
if (type.equalsIgnoreCase("long")) {
long longValue = Long.parseLong(value);
long iMin = Long.MIN_VALUE;
long iMax = Long.MAX_VALUE;
if ((min != null) && (!min.equals("null")) && (!min.equals(""))) {
iMin = Long.parseLong(min);
}
if ((max != null) && (!max.equals("null")) && (!max.equals(""))) {
iMax = Long.parseLong(max);
}
min = String.valueOf(iMin);
max = String.valueOf(iMax);
if ((longValue < iMin) || (longValue > iMax)) {
exceed = true;
}
}
if (type.equalsIgnoreCase("float")) {
float floatValue = Float.parseFloat(value);
float iMin = Float.MIN_VALUE;
float iMax = Float.MAX_VALUE;
if ((min != null) && (!min.equals("null")) && (!min.equals(""))) {
iMin = Float.parseFloat(min);
}
if ((max != null) && (!max.equals("null")) && (!max.equals(""))) {
iMax = Float.parseFloat(max);
}
min = String.valueOf(iMin);
max = String.valueOf(iMax);
if ((floatValue < iMin) || (floatValue > iMax)) {
exceed = true;
}
}
if (type.equalsIgnoreCase("double")) {
double doubleValue = Double.parseDouble(value);
double iMin = Double.MIN_VALUE;
double iMax = Double.MAX_VALUE;
if ((min != null) && (!min.equals("null")) && (!min.equals(""))) {
iMin = Double.parseDouble(min);
}
if ((max != null) && (!max.equals("null")) && (!max.equals(""))) {
iMax = Double.parseDouble(max);
}
min = String.valueOf(iMin);
max = String.valueOf(iMax);
if ((doubleValue < iMin) || (doubleValue > iMax)) {
exceed = true;
}
}
if (exceed) {
String detail = "You input new value: \"" + value + "\",its value exceed ";
if ((max != null) && (!max.equals("null")) && (!max.equals("")) && (min != null) && (!min.equals("null")) && (!min.equals(""))) {
detail += ("available scope(" + min + "," + max + ")");
} else if ((max != null) && (!max.equals("null")) && (!max.equals(""))) {
detail += ("maximal value " + max);
} else if ((min != null) && (!min.equals("null")) && (!min.equals(""))) {
detail += ("minimal value " + min);
}
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, detail);
}
} catch (NumberFormatException e) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "You input new value: \"" + value + "\",its type is invalid,you should input \'" + type + "\' type!");
}
return false;
}
private static InputStream getConfig() {
URL url = Config.class.getResource("/org/chaidb/chaidb.conf");
if (url == null) {
return null;
}
try {
return url.openStream();
} catch (IOException e) {
return null;
}
}
}