org.zodiac.script.engine.aviator.AviatorEngine Maven / Gradle / Ivy
package org.zodiac.script.engine.aviator;
import java.util.Map;
import java.util.Optional;
import org.zodiac.script.engine.ExecuteResult;
import org.zodiac.script.engine.ListenerSupportEngine;
import org.zodiac.script.engine.ScriptContext;
import org.zodiac.script.engine.costants.ScriptEngineConstants;
import org.zodiac.sdk.toolkit.crypto.Md5;
import org.zodiac.sdk.toolkit.util.SystemClock;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Expression;
import com.googlecode.aviator.FunctionMissing;
import com.googlecode.aviator.runtime.JavaMethodReflectionFunctionMissing;
public class AviatorEngine extends ListenerSupportEngine {
private AviatorEvaluatorInstance engine;
protected final Map cache;
public AviatorEngine() {
this(null);
}
public AviatorEngine(FunctionMissing functionMissing) {
this.cache = CollUtil.concurrentMap();
this.engine = AviatorEvaluator.newInstance();
this.engine.setFunctionMissing(null != functionMissing ? functionMissing : JavaMethodReflectionFunctionMissing.getInstance());
}
@Override
public void init(String... contents) throws Exception {
}
@Override
public String getScriptEngineName() {
return ScriptEngineConstants.AVIATOR_ENGINE_NAME;
}
@Override
public boolean compile(String id, String code) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("Compile Aviator {} : {}", id, code);
}
cache.put(id, new AviatorScriptContext(id, Md5.md5Hex(code), this.engine.compile(code)));
return true;
}
@Override
public ScriptContext getContext(String id) {
return cache.get(id);
}
@Override
public boolean compiled(String id) {
return cache.containsKey(id);
}
@Override
public boolean remove(String id) {
return cache.remove(id) != null;
}
@Override
public ExecuteResult execute(String id, final Map param) {
if (logger.isDebugEnabled()) {
logger.debug("Execute Aviator {} : {}", id, param);
}
ExecuteResult result = new ExecuteResult();
long start = SystemClock.nowTimeMillis();
AviatorScriptContext scriptContext = cache.get(id);
try {
if (scriptContext != null) {
doListenerBefore(scriptContext);
scriptContext = cache.get(id);
Map parameter = CollUtil.map(param);
parameter.putAll(getGlobalVariable());
Object obj = scriptContext.getScript().execute(parameter);
result.setSuccess(true).setResult(obj);
} else {
result.setSuccess(false).setMessage(String.format("Aviator: %s not found!", id));
}
long end = SystemClock.nowTimeMillis();
result.setUseTime(end - start);
} catch (Exception e) {
logger.error("Execute Aviator error", e);
result.setException(e);
}
doListenerAfter(scriptContext, result);
return result;
}
@Override
public ExecuteResult execute(String id) {
return execute(id, CollUtil.map());
}
class AviatorScriptContext extends ScriptContext {
private Expression script;
public AviatorScriptContext(String id, String md5, Expression script) {
super(id, md5);
this.script = script;
}
public Expression getScript() {
return script;
}
}
public static void setFunctionMissing(final FunctionMissing functionMissing) {
Optional.ofNullable(functionMissing).ifPresent(f -> AviatorEvaluator.getInstance().setFunctionMissing(f));
}
}