abstractqueryrouter.java
来自「mysql集群」· Java 代码 · 共 963 行 · 第 1/3 页
JAVA
963 行
private Map<Table,TableRule> loadConfig(String configFileName){
DocumentBuilder db;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(false);
db = dbf.newDocumentBuilder();
db.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
if (systemId.endsWith("rule.dtd")) {
InputStream in = AbstractQueryRouter.class.getResourceAsStream("/com/meidusa/amoeba/xml/rule.dtd");
if (in == null) {
LogLog.error("Could not find [rule.dtd]. Used [" + AbstractQueryRouter.class.getClassLoader()
+ "] class loader in the search.");
return null;
} else {
return new InputSource(in);
}
} else {
return null;
}
}
});
db.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException exception) {
}
public void error(SAXParseException exception) throws SAXException {
logger.error(exception.getMessage() + " at (" + exception.getLineNumber() + ":" + exception.getColumnNumber() + ")");
throw exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
logger.fatal(exception.getMessage() + " at (" + exception.getLineNumber() + ":" + exception.getColumnNumber() + ")");
throw exception;
}
});
return loadConfigurationFile(configFileName, db);
} catch (Exception e) {
logger.fatal("Could not load configuration file, failing", e);
throw new ConfigurationException("Error loading configuration file " + configFileName, e);
}
}
private Map<Table,TableRule> loadConfigurationFile(String fileName, DocumentBuilder db) throws InitialisationException {
Document doc = null;
InputStream is = null;
Map<Table,TableRule> tableRuleMap = new HashMap<Table,TableRule>();
try {
is = new FileInputStream(new File(fileName));
doc = db.parse(is);
} catch (Exception e) {
final String s = "Caught exception while loading file " + fileName;
logger.error(s, e);
throw new ConfigurationException(s, e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
logger.error("Unable to close input stream", e);
}
}
}
Element rootElement = doc.getDocumentElement();
NodeList children = rootElement.getChildNodes();
int childSize = children.getLength();
for (int i = 0; i < childSize; i++) {
Node childNode = children.item(i);
if (childNode instanceof Element) {
Element child = (Element) childNode;
final String nodeName = child.getNodeName();
if (nodeName.equals("tableRule")) {
TableRule rule = loadTableRule(child);
tableRuleMap.put(rule.table.getName() == null?null:rule.table, rule);
}
}
}
if (logger.isInfoEnabled()) {
logger.info("Loaded rule configuration from: " + fileName);
}
return tableRuleMap;
}
private TableRule loadTableRule(Element current) throws InitialisationException {
TableRule tableRule = new TableRule();
String name = current.getAttribute("name");
String schemaName = current.getAttribute("schema");
Table table = new Table();
table.setName(name);
if(!StringUtil.isEmpty(schemaName)){
Schema schema = new Schema();
schema.setName(schemaName);
table.setSchema(schema);
}
tableRule.table = table;
String defaultPools = current.getAttribute("defaultPools");
if(defaultPools != null){
tableRule.defaultPools = readTokenizedString(defaultPools," ,");
}
String readPools = current.getAttribute("readPools");
if(readPools != null){
tableRule.readPools = readTokenizedString(readPools," ,");
}
String writePools = current.getAttribute("writePools");
if(writePools != null){
tableRule.writePools = readTokenizedString(writePools," ,");
}
NodeList children = current.getChildNodes();
int childSize = children.getLength();
for (int i = 0; i < childSize; i++) {
Node childNode = children.item(i);
if (childNode instanceof Element) {
Element child = (Element) childNode;
final String nodeName = child.getNodeName();
if (nodeName.equals("rule")) {
tableRule.ruleList.add(loadRule(child,tableRule));
}
}
}
return tableRule;
}
private Rule loadRule(Element current, TableRule tableRule) throws InitialisationException {
Rule rule = new Rule();
rule.name = current.getAttribute("name");
String group = current.getAttribute("group");
rule.group = StringUtil.isEmpty(group)?null:group;
String ignoreArray = current.getAttribute("ignoreArray");
rule.ignoreArray = Boolean.parseBoolean(ignoreArray);
Element expression = DocumentUtil.getTheOnlyElement(current, "expression");
rule.expression = expression.getTextContent();
Element defaultPoolsNode = DocumentUtil.getTheOnlyElement(current, "defaultPools");
if(defaultPoolsNode != null){
String defaultPools = defaultPoolsNode.getTextContent();
rule.defaultPools = readTokenizedString(defaultPools," ,");
}
Element readPoolsNode = DocumentUtil.getTheOnlyElement(current, "readPools");
if(readPoolsNode != null){
rule.readPools = readTokenizedString(readPoolsNode.getTextContent()," ,");
}
Element writePoolsNode = DocumentUtil.getTheOnlyElement(current, "writePools");
if(writePoolsNode != null){
rule.writePools = readTokenizedString(writePoolsNode.getTextContent()," ,");
}
Element parametersNode = DocumentUtil.getTheOnlyElement(current, "parameters");
if(parametersNode != null){
String[] tokens = readTokenizedString(parametersNode.getTextContent()," ,");
int index = 0;
for(String parameter:tokens){
rule.parameterMap.put(parameter, index);
Column column = new Column();
column.setName(parameter);
column.setTable(tableRule.table);
rule.cloumnMap.put(column, index);
index++;
}
tokens = readTokenizedString(parametersNode.getAttribute("excludes")," ,");
if(tokens != null){
for(String parameter:tokens){
Column column = new Column();
column.setName(parameter);
column.setTable(tableRule.table);
rule.excludes.add(column);
}
}
}
rule.rowJep = new RowJEP(rule.expression);
try {
rule.rowJep.parseExpression(rule.parameterMap,(Map<String,Variable>)null,this.ruleFunctionMap);
} catch (com.meidusa.amoeba.sqljep.ParseException e) {
throw new InitialisationException("parser expression:"+rule.expression+" error",e);
}
return rule;
}
public static String[] readTokenizedString(String string,String delim){
if(string == null|| string.trim().length() == 0) return null;
StringTokenizer tokenizer = new StringTokenizer(string,delim);
String[] tokens = new String[tokenizer.countTokens()];
int index = 0;
while(tokenizer.hasMoreTokens()){
String token = tokenizer.nextToken().trim();
tokens[index++] = token;
}
if(tokens.length>0){
return tokens;
}
return null;
}
public int getLRUMapSize() {
return LRUMapSize;
}
public String getDefaultPool() {
return defaultPool;
}
public void setDefaultPool(String defaultPoolName) {
this.defaultPool = defaultPoolName;
}
public void setLRUMapSize(int mapSize) {
LRUMapSize = mapSize;
}
public String getFunctionConfig() {
return functionConfig;
}
public void setFunctionConfig(String functionConfig) {
this.functionConfig = functionConfig;
}
public boolean isNeedParse() {
return needParse;
}
public void setNeedParse(boolean needParse) {
this.needParse = needParse;
}
public void setRuleFunctionConfig(String ruleFunctionConfig) {
this.ruleFunctionConfig = ruleFunctionConfig;
}
public ObjectPool getObjectPool(Object key){
if(key instanceof String){
return ProxyRuntimeContext.getInstance().getPoolMap().get(key);
}else{
for(ObjectPool pool : ProxyRuntimeContext.getInstance().getPoolMap().values()){
if(pool.hashCode() == key.hashCode()){
return pool;
}
}
}
return null;
}
public Statment parseSql(DatabaseConnection connection,String sql){
Statment statment = null;
String defaultSchema = (connection ==null || StringUtil.isEmpty(connection.getSchema())) ?null: connection.getSchema();
int sqlWithSchemaHashcode = defaultSchema != null? (defaultSchema.hashCode()^sql.hashCode()):sql.hashCode();
mapLock.lock();
try{
statment = (Statment)map.get(sqlWithSchemaHashcode);
}finally{
mapLock.unlock();
}
if(statment == null){
synchronized (sql) {
statment = (Statment)map.get(sqlWithSchemaHashcode);
if(statment != null) return statment;
Parser parser = newParser(sql);
parser.setFunctionMap(this.functionMap);
if(defaultSchema != null){
Schema schema = new Schema();
schema.setName(defaultSchema);
parser.setDefaultSchema(schema);
}
try {
try{
statment = parser.doParse();
mapLock.lock();
try{
map.put(sqlWithSchemaHashcode, statment);
}finally{
mapLock.unlock();
}
}catch(Error e){
logger.error(sql,e);
return null;
}
} catch (ParseException e) {
logger.error(sql,e);
}
}
}
return statment;
}
public int parseParameterCount(DatabaseConnection connection,String sql){
Statment statment = parseSql(connection,sql);
return statment.getParameterCount();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?