use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class ReportServiceImpl method evaluateScript.
public Collection<PrismContainerValue<? extends Containerable>> evaluateScript(String script, Map<QName, Object> parameters) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
List<PrismContainerValue<? extends Containerable>> results = new ArrayList<>();
ExpressionVariables variables = new ExpressionVariables();
variables.addVariableDefinitions(parameters);
// special variable for audit report
variables.addVariableDefinition(new QName("auditParams"), getConvertedParams(parameters));
Task task = taskManager.createTaskInstance(ReportService.class.getName() + ".evaluateScript");
OperationResult parentResult = task.getResult();
Collection<FunctionLibrary> functions = createFunctionLibraries();
Jsr223ScriptEvaluator scripts = new Jsr223ScriptEvaluator("Groovy", prismContext, prismContext.getDefaultProtector());
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(task, task.getResult()));
Object o = null;
try {
o = scripts.evaluateReportScript(script, variables, objectResolver, functions, "desc", parentResult);
} finally {
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
if (o != null) {
if (Collection.class.isAssignableFrom(o.getClass())) {
Collection resultSet = (Collection) o;
if (resultSet != null && !resultSet.isEmpty()) {
for (Object obj : resultSet) {
results.add(convertResultingObject(obj));
}
}
} else {
results.add(convertResultingObject(o));
}
}
return results;
}
use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class ExpressionTestUtil method createInitializedExpressionFactory.
public static ExpressionFactory createInitializedExpressionFactory(ObjectResolver resolver, ProtectorImpl protector, PrismContext prismContext, SecurityEnforcer securityEnforcer) {
ExpressionFactory expressionFactory = new ExpressionFactory(resolver, securityEnforcer, prismContext);
// asIs
AsIsExpressionEvaluatorFactory asIsFactory = new AsIsExpressionEvaluatorFactory(prismContext, protector);
expressionFactory.addEvaluatorFactory(asIsFactory);
expressionFactory.setDefaultEvaluatorFactory(asIsFactory);
// value
LiteralExpressionEvaluatorFactory valueFactory = new LiteralExpressionEvaluatorFactory(prismContext);
expressionFactory.addEvaluatorFactory(valueFactory);
// const
ConstantsManager constManager = new ConstantsManager(createConfiguration());
ConstExpressionEvaluatorFactory constFactory = new ConstExpressionEvaluatorFactory(protector, constManager, prismContext);
expressionFactory.addEvaluatorFactory(constFactory);
// path
PathExpressionEvaluatorFactory pathFactory = new PathExpressionEvaluatorFactory(prismContext, resolver, protector);
expressionFactory.addEvaluatorFactory(pathFactory);
// generate
ValuePolicyProcessor valuePolicyGenerator = new ValuePolicyProcessor();
valuePolicyGenerator.setExpressionFactory(expressionFactory);
GenerateExpressionEvaluatorFactory generateFactory = new GenerateExpressionEvaluatorFactory(protector, resolver, valuePolicyGenerator, prismContext);
expressionFactory.addEvaluatorFactory(generateFactory);
// script
Collection<FunctionLibrary> functions = new ArrayList<FunctionLibrary>();
functions.add(FunctionLibraryUtil.createBasicFunctionLibrary(prismContext, protector));
functions.add(FunctionLibraryUtil.createLogFunctionLibrary(prismContext));
ScriptExpressionFactory scriptExpressionFactory = new ScriptExpressionFactory(resolver, prismContext, protector);
scriptExpressionFactory.setFunctions(functions);
XPathScriptEvaluator xpathEvaluator = new XPathScriptEvaluator(prismContext);
scriptExpressionFactory.registerEvaluator(XPathScriptEvaluator.XPATH_LANGUAGE_URL, xpathEvaluator);
Jsr223ScriptEvaluator groovyEvaluator = new Jsr223ScriptEvaluator("Groovy", prismContext, protector);
scriptExpressionFactory.registerEvaluator(groovyEvaluator.getLanguageUrl(), groovyEvaluator);
ScriptExpressionEvaluatorFactory scriptExpressionEvaluatorFactory = new ScriptExpressionEvaluatorFactory(scriptExpressionFactory, securityEnforcer);
expressionFactory.addEvaluatorFactory(scriptExpressionEvaluatorFactory);
return expressionFactory;
}
use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class ReflectionXPathFunctionResolver method resolveFunction.
/* (non-Javadoc)
* @see javax.xml.xpath.XPathFunctionResolver#resolveFunction(javax.xml.namespace.QName, int)
*/
@Override
public XPathFunction resolveFunction(QName functionQName, int arity) {
boolean enableDebug = false;
String namespace = functionQName.getNamespaceURI();
if (StringUtils.isEmpty(namespace)) {
namespace = MidPointConstants.NS_FUNC_BASIC;
enableDebug = true;
} else if (namespace.equals(MidPointConstants.NS_FUNC_BASIC)) {
enableDebug = true;
}
FunctionLibrary lib = findLibrary(namespace);
if (lib == null) {
LOGGER.trace("Unknown namespace for function {} function with {} arguments", functionQName, arity);
return null;
}
Object functionObject = null;
if (lib.getXmlFunctions() != null) {
functionObject = lib.getXmlFunctions();
} else {
functionObject = lib.getGenericFunctions();
}
String functionName = functionQName.getLocalPart();
LOGGER.trace("Resolving to {} function with {} arguments", functionName, arity);
ReflectionXPathFunctionWrapper xPathFunction = new ReflectionXPathFunctionWrapper(functionObject, functionName, arity, enableDebug);
return xPathFunction;
}
Aggregations