use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.
the class ConfigInitializer method testConnection.
public void testConnection(boolean isStart) {
if (this.dataNodes != null && this.dataHosts != null) {
Map<String, Boolean> map = new HashMap<>();
for (PhysicalDBNode dataNode : dataNodes.values()) {
String database = dataNode.getDatabase();
PhysicalDBPool pool = dataNode.getDbPool();
if (isStart) {
// start for first time, 1.you can set write host as empty
if (pool.getSources() == null || pool.getSources().length == 0) {
continue;
}
DBHostConfig wHost = pool.getSource().getConfig();
// start for first time, 2.you can set write host as yourself
if (("localhost".equalsIgnoreCase(wHost.getIp()) || "127.0.0.1".equalsIgnoreCase(wHost.getIp())) && wHost.getPort() == this.system.getServerPort()) {
continue;
}
}
for (PhysicalDatasource ds : pool.getAllDataSources()) {
String key = ds.getName() + "_" + database;
if (map.get(key) == null) {
map.put(key, false);
try {
boolean isConnected = ds.testConnection(database);
map.put(key, isConnected);
} catch (IOException e) {
LOGGER.info("test conn " + key + " error:", e);
}
}
}
}
boolean isConnectivity = true;
for (Map.Entry<String, Boolean> entry : map.entrySet()) {
String key = entry.getKey();
Boolean value = entry.getValue();
if (!value && isConnectivity) {
LOGGER.info("SelfCheck### test " + key + " database connection failed ");
isConnectivity = false;
} else {
LOGGER.info("SelfCheck### test " + key + " database connection success ");
}
}
if (!isConnectivity) {
throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!");
}
}
}
use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.
the class SystemConfigLoader method load.
public void load(Element root, XMLServerLoader xsl, boolean isLowerCaseTableNames) throws IllegalAccessException, InvocationTargetException {
SystemConfig system = xsl.getSystem();
NodeList list = root.getElementsByTagName("system");
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Map<String, Object> props = ConfigUtil.loadElements((Element) node);
ParameterMapping.mapping(system, props);
}
}
if (system.getFakeMySQLVersion() != null) {
boolean validVersion = false;
String majorMySQLVersion = system.getFakeMySQLVersion();
int pos = majorMySQLVersion.indexOf(".") + 1;
majorMySQLVersion = majorMySQLVersion.substring(0, majorMySQLVersion.indexOf(".", pos));
for (String ver : SystemConfig.MYSQL_VERSIONS) {
// version is x.y.z ,just compare the x.y
if (majorMySQLVersion.equals(ver)) {
validVersion = true;
}
}
if (validVersion) {
Versions.setServerVersion(system.getFakeMySQLVersion());
} else {
throw new ConfigException("The specified MySQL Version (" + system.getFakeMySQLVersion() + ") is not valid.");
}
}
}
use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.
the class UserConfigLoader method load.
public void load(Element root, XMLServerLoader xsl, boolean isLowerCaseTableNames) throws IllegalAccessException, InvocationTargetException {
Map<String, UserConfig> users = xsl.getUsers();
NodeList list = root.getElementsByTagName("user");
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String name = e.getAttribute("name");
UserConfig user = new UserConfig();
Map<String, Object> props = ConfigUtil.loadElements(e);
String password = (String) props.get("password");
String usingDecrypt = (String) props.get("usingDecrypt");
String passwordDecrypt = DecryptUtil.decrypt(usingDecrypt, name, password);
user.setName(name);
user.setPassword(passwordDecrypt);
user.setEncryptPassword(password);
String benchmark = (String) props.get("benchmark");
if (null != benchmark) {
user.setBenchmark(Integer.parseInt(benchmark));
}
String readOnly = (String) props.get("readOnly");
if (null != readOnly) {
user.setReadOnly(Boolean.parseBoolean(readOnly));
}
String manager = (String) props.get("manager");
if (null != manager) {
user.setManager(Boolean.parseBoolean(manager));
user.setSchemas(new HashSet<String>(0));
}
String schemas = (String) props.get("schemas");
if (user.isManager() && schemas != null) {
throw new ConfigException("manager user can't set any schema!");
} else if (!user.isManager()) {
if (schemas != null) {
if (isLowerCaseTableNames) {
schemas = schemas.toLowerCase();
}
String[] strArray = SplitUtil.split(schemas, ',', true);
user.setSchemas(new HashSet<>(Arrays.asList(strArray)));
}
// load DML
loadPrivileges(user, isLowerCaseTableNames, e);
}
if (users.containsKey(name)) {
throw new ConfigException("user " + name + " duplicated!");
}
users.put(name, user);
}
}
}
use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.
the class XMLRuleLoader method loadTableRules.
/**
* tableRule tag:
* <tableRule name="sharding-by-month">
* <rule>
* <columns>create_date</columns>
* <algorithm>partbymonth</algorithm>
* </rule>
* </tableRule>
*
* @param root
* @throws SQLSyntaxErrorException
*/
private void loadTableRules(Element root) throws SQLSyntaxErrorException {
NodeList list = root.getElementsByTagName("tableRule");
for (int i = 0, n = list.getLength(); i < n; ++i) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String name = e.getAttribute("name");
if (StringUtil.isEmpty(name)) {
throw new ConfigException("name is null or empty");
}
if (tableRules.containsKey(name)) {
throw new ConfigException("table rule " + name + " duplicated!");
}
NodeList ruleNodes = e.getElementsByTagName("rule");
int length = ruleNodes.getLength();
if (length > 1) {
throw new ConfigException("only one rule can defined :" + name);
}
// rule has only one element now. Maybe it will not contains one rule in feature
// RuleConfig:rule->function
RuleConfig rule = loadRule((Element) ruleNodes.item(0));
tableRules.put(name, new TableRuleConfig(name, rule));
}
}
}
use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.
the class XMLRuleLoader method load.
private void load(String dtdFile, String xmlFile) {
InputStream dtd = null;
InputStream xml = null;
try {
dtd = ResourceUtil.getResourceAsStream(dtdFile);
xml = ResourceUtil.getResourceAsStream(xmlFile);
Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
loadFunctions(root);
loadTableRules(root);
} catch (ConfigException e) {
throw e;
} catch (Exception e) {
throw new ConfigException(e);
} finally {
if (dtd != null) {
try {
dtd.close();
} catch (IOException e) {
// ignore error
}
}
if (xml != null) {
try {
xml.close();
} catch (IOException e) {
// ignore error
}
}
}
}
Aggregations