use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class ClusterConfig method loadGroup.
private static Map<String, List<String>> loadGroup(Element root, Map<String, CobarNodeConfig> nodes) {
Map<String, List<String>> groups = new HashMap<String, List<String>>();
NodeList list = root.getElementsByTagName("group");
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String groupName = e.getAttribute("name").trim();
if (groups.containsKey(groupName)) {
throw new ConfigException("group duplicated : " + groupName);
}
Map<String, Object> props = ConfigUtil.loadElements(e);
String value = (String) props.get("nodeList");
if (null == value || "".equals(value)) {
throw new ConfigException("group should contain 'nodeList'");
}
String[] sList = SplitUtil.split(value, ',', true);
if (null == sList || sList.length == 0) {
throw new ConfigException("group should contain 'nodeList'");
}
for (String s : sList) {
if (!nodes.containsKey(s)) {
throw new ConfigException("[ node :" + s + "] in [ group:" + groupName + "] doesn't exist!");
}
}
List<String> nodeList = Arrays.asList(sList);
groups.put(groupName, nodeList);
}
}
if (!groups.containsKey("default")) {
List<String> nodeList = new ArrayList<String>(nodes.keySet());
groups.put("default", nodeList);
}
return groups;
}
use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class XMLRuleLoader method loadFunctions.
private void loadFunctions(Element root) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
NodeList list = root.getElementsByTagName("function");
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 (functions.containsKey(name)) {
throw new ConfigException("rule function " + name + " duplicated!");
}
String clazz = e.getAttribute("class");
RuleAlgorithm function = createFunction(name, clazz);
ParameterMapping.mapping(function, ConfigUtil.loadElements(e));
functions.put(name, function);
}
}
}
use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class XMLRuleLoader method load.
private void load(String dtdFile, String xmlFile) {
InputStream dtd = null;
InputStream xml = null;
try {
dtd = XMLRuleLoader.class.getResourceAsStream(dtdFile);
xml = XMLRuleLoader.class.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) {
}
}
if (xml != null) {
try {
xml.close();
} catch (IOException e) {
}
}
}
}
use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class XMLRuleLoader method loadTableRules.
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 (tableRules.containsKey(name)) {
throw new ConfigException("table rule " + name + " duplicated!");
}
NodeList ruleNodes = e.getElementsByTagName("rule");
int length = ruleNodes.getLength();
List<RuleConfig> ruleList = new ArrayList<RuleConfig>(length);
for (int j = 0; j < length; ++j) {
RuleConfig rule = loadRule((Element) ruleNodes.item(j));
ruleList.add(rule);
rules.add(rule);
}
tableRules.put(name, new TableRuleConfig(name, ruleList));
}
}
}
use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class XMLServerLoader method loadUsers.
private void loadUsers(Element root) {
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();
user.setName(name);
Map<String, Object> props = ConfigUtil.loadElements(e);
user.setPassword((String) props.get("password"));
String schemas = (String) props.get("schemas");
if (schemas != null) {
String[] strArray = SplitUtil.split(schemas, ',', true);
user.setSchemas(new HashSet<String>(Arrays.asList(strArray)));
}
if (users.containsKey(name)) {
throw new ConfigException("user " + name + " duplicated!");
}
users.put(name, user);
}
}
}
Aggregations