use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class MySQLDataNode method setHeartbeat.
private void setHeartbeat(String heartbeat) {
if (heartbeat == null) {
heartbeatAST = null;
placeHolderToStringer = null;
return;
}
try {
final Set<PlaceHolder> plist = new HashSet<PlaceHolder>(1, 1);
SQLStatement ast = SQLParserDelegate.parse(heartbeat);
ast.accept(new EmptySQLASTVisitor() {
@Override
public void visit(PlaceHolder node) {
plist.add(node);
}
});
if (plist.isEmpty()) {
heartbeatAST = null;
placeHolderToStringer = null;
return;
}
Map<PlaceHolder, Object> phm = new HashMap<PlaceHolder, Object>(plist.size(), 1);
for (PlaceHolder ph : plist) {
final String content = ph.getName();
final int low = Integer.parseInt(content.substring(content.indexOf('(') + 1, content.indexOf(',')).trim());
final int high = Integer.parseInt(content.substring(content.indexOf(',') + 1, content.indexOf(')')).trim());
phm.put(ph, new Object() {
private Random rnd = new Random();
@Override
public String toString() {
return String.valueOf(rnd.nextInt(high - low + 1) + low);
}
});
}
heartbeatAST = ast;
placeHolderToStringer = phm;
} catch (SQLSyntaxErrorException e) {
throw new ConfigException("heartbeat syntax err: " + heartbeat, e);
}
}
use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class RouteRuleInitializer method initRouteRule.
public static void initRouteRule(SchemaLoader loader) throws SQLSyntaxErrorException {
Map<String, RuleAlgorithm> functions = loader.getFunctions();
MySQLFunctionManager functionManager = new MySQLFunctionManager(true);
buildFuncManager(functionManager, functions);
for (RuleConfig conf : loader.listRuleConfig()) {
String algorithmString = conf.getAlgorithm();
MySQLLexer lexer = new MySQLLexer(algorithmString);
MySQLExprParser parser = new MySQLExprParser(lexer, functionManager, false, MySQLParser.DEFAULT_CHARSET);
Expression expression = parser.expression();
if (lexer.token() != MySQLToken.EOF) {
throw new ConfigException("route algorithm not end with EOF: " + algorithmString);
}
RuleAlgorithm algorithm;
if (expression instanceof RuleAlgorithm) {
algorithm = (RuleAlgorithm) expression;
} else {
algorithm = new ExpressionAdapter(expression);
}
conf.setRuleAlgorithm(algorithm);
}
}
use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class XMLSchemaLoader method loadTables.
private Map<String, TableConfig> loadTables(Element node) {
Map<String, TableConfig> tables = new HashMap<String, TableConfig>();
NodeList nodeList = node.getElementsByTagName("table");
for (int i = 0; i < nodeList.getLength(); i++) {
Element tableElement = (Element) nodeList.item(i);
String name = tableElement.getAttribute("name").toUpperCase();
String dataNode = tableElement.getAttribute("dataNode");
TableRuleConfig tableRule = null;
if (tableElement.hasAttribute("rule")) {
String ruleName = tableElement.getAttribute("rule");
tableRule = tableRules.get(ruleName);
if (tableRule == null) {
throw new ConfigException("rule " + ruleName + " is not found!");
}
}
boolean ruleRequired = false;
if (tableElement.hasAttribute("ruleRequired")) {
ruleRequired = Boolean.parseBoolean(tableElement.getAttribute("ruleRequired"));
}
String[] tableNames = SplitUtil.split(name, ',', true);
for (String tableName : tableNames) {
TableConfig table = new TableConfig(tableName, dataNode, tableRule, ruleRequired);
checkDataNodeExists(table.getDataNodes());
if (tables.containsKey(table.getName())) {
throw new ConfigException("table " + tableName + " duplicated!");
}
tables.put(table.getName(), table);
}
}
return tables;
}
use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class XMLSchemaLoader method load.
private void load(String dtdFile, String xmlFile) {
InputStream dtd = null;
InputStream xml = null;
try {
dtd = XMLSchemaLoader.class.getResourceAsStream(dtdFile);
xml = XMLSchemaLoader.class.getResourceAsStream(xmlFile);
Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
loadDataSources(root);
loadDataNodes(root);
loadSchemas(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) {
}
}
}
}
use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.
the class XMLSchemaLoader method loadSchemas.
private void loadSchemas(Element root) {
NodeList list = root.getElementsByTagName("schema");
for (int i = 0, n = list.getLength(); i < n; i++) {
Element schemaElement = (Element) list.item(i);
String name = schemaElement.getAttribute("name");
String dataNode = schemaElement.getAttribute("dataNode");
// 在非空的情况下检查dataNode是否存在
if (dataNode != null && dataNode.length() != 0) {
checkDataNodeExists(dataNode);
} else {
// 确保非空
dataNode = "";
}
String group = "default";
if (schemaElement.hasAttribute("group")) {
group = schemaElement.getAttribute("group").trim();
}
Map<String, TableConfig> tables = loadTables(schemaElement);
if (schemas.containsKey(name)) {
throw new ConfigException("schema " + name + " duplicated!");
}
boolean keepSqlSchema = false;
if (schemaElement.hasAttribute("keepSqlSchema")) {
keepSqlSchema = Boolean.parseBoolean(schemaElement.getAttribute("keepSqlSchema").trim());
}
schemas.put(name, new SchemaConfig(name, dataNode, group, keepSqlSchema, tables));
}
}
Aggregations