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 getExpressionOperators(BusinessObject bo,String matchSql) throws Exception{ List scripts = new ArrayList(); Iterator 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 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 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; } }