Search in sources :

Example 6 with ConfigException

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;
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ConfigException(com.alibaba.cobar.config.util.ConfigException) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with ConfigException

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);
        }
    }
}
Also used : RuleAlgorithm(com.alibaba.cobar.config.model.rule.RuleAlgorithm) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.alibaba.cobar.config.util.ConfigException)

Example 8 with ConfigException

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) {
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) Element(org.w3c.dom.Element) ConfigException(com.alibaba.cobar.config.util.ConfigException) IOException(java.io.IOException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigException(com.alibaba.cobar.config.util.ConfigException)

Example 9 with ConfigException

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));
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ConfigException(com.alibaba.cobar.config.util.ConfigException) TableRuleConfig(com.alibaba.cobar.config.model.rule.TableRuleConfig) RuleConfig(com.alibaba.cobar.config.model.rule.RuleConfig) TableRuleConfig(com.alibaba.cobar.config.model.rule.TableRuleConfig)

Example 10 with ConfigException

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);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.alibaba.cobar.config.util.ConfigException) UserConfig(com.alibaba.cobar.config.model.UserConfig)

Aggregations

ConfigException (com.alibaba.cobar.config.util.ConfigException)16 Element (org.w3c.dom.Element)13 NodeList (org.w3c.dom.NodeList)10 Node (org.w3c.dom.Node)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 InputStream (java.io.InputStream)3 HashSet (java.util.HashSet)3 DataNodeConfig (com.alibaba.cobar.config.model.DataNodeConfig)2 TableConfig (com.alibaba.cobar.config.model.TableConfig)2 UserConfig (com.alibaba.cobar.config.model.UserConfig)2 RuleAlgorithm (com.alibaba.cobar.config.model.rule.RuleAlgorithm)2 RuleConfig (com.alibaba.cobar.config.model.rule.RuleConfig)2 TableRuleConfig (com.alibaba.cobar.config.model.rule.TableRuleConfig)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 ClusterConfig (com.alibaba.cobar.config.model.ClusterConfig)1 DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)1 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)1 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)1