Search in sources :

Example 11 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class XMLServerLoader method load.

private void load() {
    InputStream dtd = null;
    InputStream xml = null;
    try {
        dtd = XMLServerLoader.class.getResourceAsStream("/server.dtd");
        xml = XMLServerLoader.class.getResourceAsStream("/server.xml");
        Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
        loadSystem(root);
        loadUsers(root);
        this.cluster = new ClusterConfig(root, system.getServerPort());
        loadQuarantine(root);
    } catch (ConfigException e) {
        throw e;
    } catch (Throwable 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) ClusterConfig(com.alibaba.cobar.config.model.ClusterConfig)

Example 12 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class XMLServerLoader method loadQuarantine.

private void loadQuarantine(Element root) {
    NodeList list = root.getElementsByTagName("host");
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            String host = e.getAttribute("name").trim();
            if (quarantine.getHosts().containsKey(host)) {
                throw new ConfigException("host duplicated : " + host);
            }
            Map<String, Object> props = ConfigUtil.loadElements(e);
            String[] users = SplitUtil.split((String) props.get("user"), ',', true);
            HashSet<String> set = new HashSet<String>();
            if (null != users) {
                for (String user : users) {
                    UserConfig uc = this.users.get(user);
                    if (null == uc) {
                        throw new ConfigException("[user: " + user + "] doesn't exist in [host: " + host + "]");
                    }
                    if (null == uc.getSchemas() || uc.getSchemas().size() == 0) {
                        throw new ConfigException("[host: " + host + "] contains one root privileges user: " + user);
                    }
                    if (set.contains(user)) {
                        throw new ConfigException("[host: " + host + "] contains duplicate user: " + user);
                    } else {
                        set.add(user);
                    }
                }
            }
            quarantine.getHosts().put(host, set);
        }
    }
}
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) HashSet(java.util.HashSet)

Example 13 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class XMLSchemaLoader method loadDataSources.

private void loadDataSources(Element root) {
    NodeList list = root.getElementsByTagName("dataSource");
    for (int i = 0, n = list.getLength(); i < n; ++i) {
        Element element = (Element) list.item(i);
        ArrayList<DataSourceConfig> dscList = new ArrayList<DataSourceConfig>();
        String dsNamePrefix = element.getAttribute("name");
        try {
            String dsType = element.getAttribute("type");
            Element locElement = findPropertyByName(element, "location");
            if (locElement == null) {
                throw new NullPointerException("dataSource xml Element with name of " + dsNamePrefix + " has no location Element");
            }
            NodeList locationList = locElement.getElementsByTagName("location");
            int dsIndex = 0;
            for (int j = 0, m = locationList.getLength(); j < m; ++j) {
                String locStr = ((Element) locationList.item(j)).getTextContent();
                int colonIndex = locStr.indexOf(':');
                int slashIndex = locStr.indexOf('/');
                String dsHost = locStr.substring(0, colonIndex).trim();
                int dsPort = Integer.parseInt(locStr.substring(colonIndex + 1, slashIndex).trim());
                String[] schemas = SplitUtil.split(locStr.substring(slashIndex + 1).trim(), ',', '$', '-');
                for (String dsSchema : schemas) {
                    DataSourceConfig dsConf = new DataSourceConfig();
                    ParameterMapping.mapping(dsConf, ConfigUtil.loadElements(element));
                    dscList.add(dsConf);
                    switch(dsIndex) {
                        case 0:
                            dsConf.setName(dsNamePrefix);
                            break;
                        case 1:
                            dscList.get(0).setName(dsNamePrefix + "[0]");
                        default:
                            dsConf.setName(dsNamePrefix + "[" + dsIndex + "]");
                    }
                    dsConf.setType(dsType);
                    dsConf.setDatabase(dsSchema);
                    dsConf.setHost(dsHost);
                    dsConf.setPort(dsPort);
                    ++dsIndex;
                }
            }
        } catch (Exception e) {
            throw new ConfigException("dataSource " + dsNamePrefix + " define error", e);
        }
        for (DataSourceConfig dsConf : dscList) {
            if (dataSources.containsKey(dsConf.getName())) {
                throw new ConfigException("dataSource name " + dsConf.getName() + "duplicated!");
            }
            dataSources.put(dsConf.getName(), dsConf);
        }
    }
}
Also used : DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ConfigException(com.alibaba.cobar.config.util.ConfigException) IOException(java.io.IOException) ConfigException(com.alibaba.cobar.config.util.ConfigException)

Example 14 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class XMLSchemaLoader method loadDataNodes.

private void loadDataNodes(Element root) {
    NodeList list = root.getElementsByTagName("dataNode");
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Element element = (Element) list.item(i);
        String dnNamePrefix = element.getAttribute("name");
        List<DataNodeConfig> confList = new ArrayList<DataNodeConfig>();
        try {
            Element dsElement = findPropertyByName(element, "dataSource");
            if (dsElement == null) {
                throw new NullPointerException("dataNode xml Element with name of " + dnNamePrefix + " has no dataSource Element");
            }
            NodeList dataSourceList = dsElement.getElementsByTagName("dataSourceRef");
            String[][] dataSources = new String[dataSourceList.getLength()][];
            for (int j = 0, m = dataSourceList.getLength(); j < m; ++j) {
                Element ref = (Element) dataSourceList.item(j);
                String dsString = ref.getTextContent();
                dataSources[j] = SplitUtil.split(dsString, ',', '$', '-', '[', ']');
            }
            if (dataSources.length <= 0) {
                throw new ConfigException("no dataSourceRef defined!");
            }
            for (String[] dss : dataSources) {
                if (dss.length != dataSources[0].length) {
                    throw new ConfigException("dataSource number not equals!");
                }
            }
            for (int k = 0, limit = dataSources[0].length; k < limit; ++k) {
                StringBuilder dsString = new StringBuilder();
                for (int dsIndex = 0; dsIndex < dataSources.length; ++dsIndex) {
                    if (dsIndex > 0) {
                        dsString.append(',');
                    }
                    dsString.append(dataSources[dsIndex][k]);
                }
                DataNodeConfig conf = new DataNodeConfig();
                ParameterMapping.mapping(conf, ConfigUtil.loadElements(element));
                confList.add(conf);
                switch(k) {
                    case 0:
                        conf.setName((limit == 1) ? dnNamePrefix : dnNamePrefix + "[" + k + "]");
                        break;
                    default:
                        conf.setName(dnNamePrefix + "[" + k + "]");
                        break;
                }
                conf.setDataSource(dsString.toString());
            }
        } catch (Exception e) {
            throw new ConfigException("dataNode " + dnNamePrefix + " define error", e);
        }
        for (DataNodeConfig conf : confList) {
            if (dataNodes.containsKey(conf.getName())) {
                throw new ConfigException("dataNode " + conf.getName() + " duplicated!");
            }
            dataNodes.put(conf.getName(), conf);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ConfigException(com.alibaba.cobar.config.util.ConfigException) IOException(java.io.IOException) ConfigException(com.alibaba.cobar.config.util.ConfigException) DataNodeConfig(com.alibaba.cobar.config.model.DataNodeConfig)

Example 15 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class ClusterConfig method loadNode.

private static Map<String, CobarNodeConfig> loadNode(Element root, int port) {
    Map<String, CobarNodeConfig> nodes = new HashMap<String, CobarNodeConfig>();
    NodeList list = root.getElementsByTagName("node");
    Set<String> hostSet = new HashSet<String>();
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element element = (Element) node;
            String name = element.getAttribute("name").trim();
            if (nodes.containsKey(name)) {
                throw new ConfigException("node name duplicated :" + name);
            }
            Map<String, Object> props = ConfigUtil.loadElements(element);
            String host = (String) props.get("host");
            if (null == host || "".equals(host)) {
                throw new ConfigException("host empty in node: " + name);
            }
            if (hostSet.contains(host)) {
                throw new ConfigException("node host duplicated :" + host);
            }
            String wei = (String) props.get("weight");
            if (null == wei || "".equals(wei)) {
                throw new ConfigException("weight should not be null in host:" + host);
            }
            int weight = Integer.valueOf(wei);
            if (weight <= 0) {
                throw new ConfigException("weight should be > 0 in host:" + host + " weight:" + weight);
            }
            CobarNodeConfig conf = new CobarNodeConfig(name, host, port, weight);
            nodes.put(name, conf);
            hostSet.add(host);
        }
    }
    return nodes;
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.alibaba.cobar.config.util.ConfigException) HashSet(java.util.HashSet)

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