step.commons.activation.Activator Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (C) 2020, exense GmbH
*
* This file is part of STEP
*
* STEP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* STEP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with STEP. If not, see .
******************************************************************************/
package step.commons.activation;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator {
public static final String DEFAULT_SCRIPT_ENGINE = "groovy";
public static final Logger logger = LoggerFactory.getLogger(Activator.class);
public static List compileActivationExpressions(List objects, String defaultScriptEngine) throws ScriptException {
for(ActivableObject object:objects) {
compileActivationExpression(object, defaultScriptEngine);
}
return objects;
}
public static void compileActivationExpression(ActivableObject object, String defaultScriptEngine) throws ScriptException {
Expression expression = object.getActivationExpression();
compileExpression(expression, defaultScriptEngine);
}
protected static void compileExpression(Expression expression, String defaultScriptEngine) throws ScriptException {
if(expression!=null && expression.compiledScript==null) {
String scriptEngine = expression.scriptEngine!=null?expression.scriptEngine:defaultScriptEngine;
if(expression.script!=null && expression.script.trim().length()>0) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(scriptEngine);
CompiledScript script = ((Compilable)engine).compile(expression.script);
expression.compiledScript = script;
} else {
expression.compiledScript = null;
}
}
}
public static Boolean evaluateActivationExpression(Bindings bindings, Expression activationExpression, String defaultScriptEngine) {
Boolean expressionResult;
if(activationExpression!=null) {
try {
compileExpression(activationExpression, defaultScriptEngine);
} catch (ScriptException e1) {
logger.error("Error while evaluating expression "+activationExpression, e1);
}
CompiledScript script = activationExpression.compiledScript;
if(script!=null) {
try {
Object evaluationResult = script.eval(bindings);
if(evaluationResult instanceof Boolean) {
expressionResult = (Boolean) evaluationResult;
} else {
expressionResult = false;
}
} catch (ScriptException e) {
expressionResult = false;
}
} else {
expressionResult = true;
}
} else {
expressionResult = true;
}
return expressionResult;
}
public static T findBestMatch(Map bindings, List objects,String defaultScriptEngine) {
return findBestMatch(bindings!=null?new SimpleBindings(bindings):null, objects, defaultScriptEngine);
}
private static T findBestMatch(Bindings bindings, List objects, String defaultScriptEngine) {
List matchingObjects = new ArrayList<>(objects);
matchingObjects.sort(new Comparator() {
@Override
public int compare(T o1, T o2) {
return -Integer.compare(getPriority(o1), getPriority(o2));
}
private int getPriority(T o1) {
return o1.getActivationExpression()==null?0:(o1.getPriority()==null?1:o1.getPriority());
}
});
for(T object:matchingObjects) {
if(evaluateActivationExpression(bindings, object.getActivationExpression(), defaultScriptEngine)) {
return object;
}
}
return null;
}
public static List findAllMatches(Map bindings, List objects, String defaultScriptEngine) {
List result = new ArrayList<>();
for(T object:objects) {
Boolean expressionResult = evaluateActivationExpression(bindings!=null?new SimpleBindings(bindings):null, object.getActivationExpression(), defaultScriptEngine);
if(expressionResult) {
result.add(object);
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy