2018-06-03 22:26:41 +08:00

194 lines
6.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.amarsoft.app.base.script;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
import com.amarsoft.app.base.antlr.OQLLexer;
import com.amarsoft.app.base.antlr.OQLParser;
import com.amarsoft.app.base.businessobject.BusinessObject;
import com.amarsoft.app.base.businessobject.BusinessObjectCache;
import com.amarsoft.app.base.config.impl.XMLConfig;
import com.amarsoft.app.base.script.operater.CompareOperator;
import com.amarsoft.are.ARE;
import com.amarsoft.are.jbo.JBOException;
import com.amarsoft.are.lang.StringX;
import com.amarsoft.are.util.xml.Document;
import com.amarsoft.are.util.xml.Element;
public class ScriptConfig extends XMLConfig{
public static final String SCRIPT_TYPE_JAVA="java";
public static final String SCRIPT_TYPE_EL="el";
private static BusinessObjectCache operatorCache=new BusinessObjectCache(100);
private static BusinessObjectCache OQCache=new BusinessObjectCache(10000);
private static BusinessObjectCache ELScriptCache=new BusinessObjectCache(2000);
//µ¥Àýģʽ
private static ScriptConfig sc = null;
private ScriptConfig(){
}
public static ScriptConfig getInstance(){
if(sc == null)
sc = new ScriptConfig();
return sc;
}
public static ParseTree getParseTree(String sql) {
ParseTree tree=(ParseTree)OQCache.getCacheObject(sql);
if(tree==null){
OQLLexer lexer=new OQLLexer(new ANTLRInputStream(sql));
CommonTokenStream tokens = new CommonTokenStream(lexer);
OQLParser parser=new OQLParser(tokens);
tree=parser.equality_expression();
OQCache.setCache(sql, tree);
}
return tree;
}
public static CompareOperator getCompareOperator(String operator) throws Exception{
BusinessObject expressionOperator=(BusinessObject)operatorCache.getCacheObject(operator);
if(expressionOperator==null){
return null;
}
String className=expressionOperator.getString("class");
Class<?> c = Class.forName(className);
return (CompareOperator)c.newInstance();
}
/**
* ͨ¹ý´«ÈëµÄÊý¾Ý¶ÔÏ󣬯¥Åä¿ÉÒÔʹÓõıȽϱí´ïʽ
* @param bo
* @return
* @throws Exception
*/
public static List<BusinessObject> getExpressionOperators(BusinessObject bo,String matchSql) throws Exception{
List<BusinessObject> scripts = new ArrayList<BusinessObject>();
Iterator<String> iterator = operatorCache.getCacheObjects().keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next();
BusinessObject script = (BusinessObject)operatorCache.getCacheObject(key);
if(!StringX.isEmpty(matchSql) && !script.matchSql(matchSql, null)) continue;
if(StringX.isEmpty(script.getString("Filter")) || bo.matchSql(script.getString("Filter"), null))
{
scripts.add(script);
}
}
return scripts;
}
/**
* ͨ¹ýjava±í´ïʽֱ½Óµ÷ÓÃʵÀý¶ÔÏóÏà¹Ø·½·¨ºÍÊôÐÔ£¬Ê¾Àý´úÂëÈçÏ£º
*
* BusinessObject bo = BusinessObject.createBusinessObject();
* bo.setAttributeValue("SerialNo", "2014");
* bo.setAttributeValue("BusinessSum", 1000d);
* bo.setAttributeValue("BusinessTerm", 10);
* bo.setAttributeValue("Object", bo);
*
* BusinessObject bd = BusinessObject.createBusinessObject();
* bd.setAttributeValue("SerialNo", "2015");
* bd.setAttributeValue("BusinessSum", 100d);
* bd.setAttributeValue("BusinessTerm", 1);
* bo.appendBusinessObject("bd", bd);
*
* System.out.println(ScriptCache.executeELScript("bo.getBusinessObject('bd').getString('SerialNo')","bo",bo));
*
* ×¢Ò⣺ΪÁ˼ӿìËÙ¶È£¬ËùÓбí´ïʽ¶¼ÔÚ±àÒë³ÉExpressionºó½øÐлº´æ¡£
* @param expression
* @param parameters
* @return
*/
public static Object executeELScript(String expression,Object... parameters){
JexlEngine jexl = new JexlEngine();
Expression e = (Expression)ELScriptCache.getCacheObject(expression);
if(e == null) e = jexl.createExpression( expression );
JexlContext jc = new MapContext();
for(int i=0;i<parameters.length;i++){
jc.set((String)parameters[i],parameters[i+1]);
i++;
}
Object o = e.evaluate(jc);
return o;
}
public static double getELDoubleValue(String expression,Object... parameters) throws Exception{
try{
Object result=executeELScript(expression,parameters);
if(result==null||result.equals(0)) return 0d;
else return (Double)result;
}
catch(Exception e){
Exception e1 = new JBOException("Ö´Ðбí´ïʽ{"+expression+"}ʱ³ö´í£¡");
e1.addSuppressed(e);
throw e1;
}
}
public static boolean getELBooleanValue(String expression,Object... parameters) throws Exception{
try{
Object result=executeELScript(expression,parameters);
if(result==null) return false;
else return (boolean)result;
}
catch(Exception e){
Exception e1 = new JBOException("Ö´Ðбí´ïʽ{"+expression+"}ʱ³ö´í£¡");
e1.addSuppressed(e);
throw e1;
}
}
public static String getELStringValue(String expression,Object... parameters) throws Exception{
try{
Object result=executeELScript(expression,parameters);
if(result==null) return null;
else return (String)result;
}
catch(Exception e){
Exception e1 = new JBOException("Ö´Ðбí´ïʽ{"+expression+"}ʱ³ö´í£¡");
e1.addSuppressed(e);
throw e1;
}
}
public synchronized void init(String file,int size) throws Exception {
BusinessObjectCache operatorCache=new BusinessObjectCache(size);
Document document = getDocument(ARE.replaceARETags(file));
Element root = document.getRootElement();
List<Element> operatorElements = root.getChild("ExpressionOperators").getChildren("ExpressionOperator");
if (operatorElements!=null) {
for (Element e : operatorElements) {
BusinessObject bo = BusinessObject.createBusinessObject();
bo.setAttributeValue("ID", e.getAttributeValue("ID"));
bo.setAttributeValue("CLASS", e.getAttributeValue("CLASS"));
operatorCache.setCache(bo.getString("ID"), bo);
}
}
ScriptConfig.operatorCache = operatorCache;
operatorCache=new BusinessObjectCache(size);
List<BusinessObject> expressionOperators = this.convertToBusinessObjectList(root.getChild("ExpressionOperators").getChildren("ExpressionOperator"));
if (expressionOperators!=null) {
for (BusinessObject expressionOperator: expressionOperators) {
String operator=expressionOperator.getString("id");
operatorCache.setCache(operator, expressionOperator);
}
}
ScriptConfig.operatorCache = operatorCache;
}
}