org.zodiac.script.engine.spel.SpelEngine Maven / Gradle / Ivy
package org.zodiac.script.engine.spel;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
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 java.util.List;
import java.util.Map;
public class SpelEngine extends ListenerSupportEngine {
protected final ExpressionParser parser;
protected final Map cache;
private List contextCalls;
public SpelEngine() {
cache = CollUtil.concurrentMap();
parser = new SpelExpressionParser();
contextCalls = CollUtil.list();
}
@Override
public boolean compiled(String id) {
return cache.containsKey(id);
}
@Override
public void init(String... contents) throws Exception {}
@Override
public String getScriptEngineName() {
return ScriptEngineConstants.SPEL_ENGINE_NAME;
}
@Override
public boolean remove(String id) {
return cache.remove(id) != null;
}
@Override
public ScriptContext getContext(String id) {
return cache.get(id);
}
@Override
public boolean compile(String id, String code) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("Compile SpEL {} : {}", id, code);
}
cache.put(id, new SpelScriptContext(id, Md5.md5Hex(code), parser.parseExpression(code)));
//return false;
return true;
}
@Override
public ExecuteResult execute(String id) {
return execute(id, CollUtil.map());
}
@Override
public ExecuteResult execute(String id, final Map param) {
if (logger.isDebugEnabled()) {
logger.debug("Execute SpEL {} : {}", id, param);
}
ExecuteResult result = new ExecuteResult();
long start = SystemClock.nowTimeMillis();
SpelScriptContext scriptContext = cache.get(id);
try {
if (scriptContext != null) {
doListenerBefore(scriptContext);
scriptContext = cache.get(id);
Map parameter = CollUtil.map(param);
parameter.putAll(getGlobalVariable());
StandardEvaluationContext context = new StandardEvaluationContext(parameter);
for (Map.Entry entry : parameter.entrySet()) {
context.setVariable(entry.getKey(), entry.getValue());
}
contextCalls.forEach(contextCall -> contextCall.init(context));
Object obj = scriptContext.getScript().getValue(context);
result.setSuccess(true).setResult(obj);
} else {
result.setSuccess(false).setMessage(String.format("SpEL: %s not found!", id));
}
long end = SystemClock.nowTimeMillis();
result.setUseTime(end - start);
} catch (Exception e) {
logger.error("Execute SpEL error", e);
result.setException(e);
}
doListenerAfter(scriptContext, result);
return result;
}
public void addContextCall(ContextCall contextCall) {
this.contextCalls.add(contextCall);
}
public interface ContextCall {
void init(StandardEvaluationContext context);
}
class SpelScriptContext extends ScriptContext {
private Expression script;
public SpelScriptContext(String id, String md5, Expression script) {
super(id, md5);
this.script = script;
}
public Expression getScript() {
return script;
}
}
}