use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class ScriptExpressionUtil method prepareScriptVariables.
public static Map<String, Object> prepareScriptVariables(ExpressionVariables variables, ObjectResolver objectResolver, Collection<FunctionLibrary> functions, String contextDescription, PrismContext prismContext, Task task, OperationResult result) throws ExpressionSyntaxException, ObjectNotFoundException {
Map<String, Object> scriptVariables = new HashMap<>();
// Functions
if (functions != null) {
for (FunctionLibrary funcLib : functions) {
scriptVariables.put(funcLib.getVariableName(), funcLib.getGenericFunctions());
}
}
// Variables
if (variables != null) {
for (Entry<QName, Object> variableEntry : variables.entrySet()) {
if (variableEntry.getKey() == null) {
// This is the "root" node. We have no use for it in JSR223, just skip it
continue;
}
String variableName = variableEntry.getKey().getLocalPart();
Object variableValue = ExpressionUtil.convertVariableValue(variableEntry.getValue(), variableName, objectResolver, contextDescription, prismContext, task, result);
scriptVariables.put(variableName, variableValue);
}
}
return scriptVariables;
}
use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class ReportServiceImpl method evaluateAuditScript.
public Collection<AuditEventRecord> evaluateAuditScript(String script, Map<QName, Object> parameters) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
Collection<AuditEventRecord> results = new ArrayList<AuditEventRecord>();
ExpressionVariables variables = new ExpressionVariables();
variables.addVariableDefinition(new QName("auditParams"), getConvertedParams(parameters));
Task task = taskManager.createTaskInstance(ReportService.class.getName() + ".searchObjects()");
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) {
if (!(obj instanceof AuditEventRecord)) {
LOGGER.warn("Skipping result, not an audit event record " + obj);
continue;
}
results.add((AuditEventRecord) obj);
}
}
} else {
results.add((AuditEventRecord) 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, Protector protector, PrismContext prismContext, Clock clock, SecurityContextManager securityContextManager, RepositoryService repositoryService) {
ExpressionFactory expressionFactory = new ExpressionFactory(securityContextManager, prismContext, LocalizationTestUtil.getLocalizationService());
expressionFactory.setObjectResolver(resolver);
// NOTE: we need to register the evaluator factories to expressionFactory manually here
// this is not spring-wired test. PostConstruct methods are not invoked here
// asIs
AsIsExpressionEvaluatorFactory asIsFactory = new AsIsExpressionEvaluatorFactory(prismContext, protector);
expressionFactory.registerEvaluatorFactory(asIsFactory);
expressionFactory.setDefaultEvaluatorFactory(asIsFactory);
// value
LiteralExpressionEvaluatorFactory valueFactory = new LiteralExpressionEvaluatorFactory(prismContext);
expressionFactory.registerEvaluatorFactory(valueFactory);
// const
ConstantsManager constManager = new ConstantsManager(createConfiguration());
ConstExpressionEvaluatorFactory constFactory = new ConstExpressionEvaluatorFactory(protector, constManager, prismContext);
expressionFactory.registerEvaluatorFactory(constFactory);
// path
PathExpressionEvaluatorFactory pathFactory = new PathExpressionEvaluatorFactory(expressionFactory, prismContext, protector, null);
pathFactory.setObjectResolver(resolver);
expressionFactory.registerEvaluatorFactory(pathFactory);
// generate
ValuePolicyProcessor valuePolicyGenerator = new ValuePolicyProcessor();
valuePolicyGenerator.setExpressionFactory(expressionFactory);
GenerateExpressionEvaluatorFactory generateFactory = new GenerateExpressionEvaluatorFactory(expressionFactory, protector, valuePolicyGenerator, prismContext, null);
generateFactory.setObjectResolver(resolver);
expressionFactory.registerEvaluatorFactory(generateFactory);
// script
Collection<FunctionLibrary> functions = new ArrayList<>();
functions.add(FunctionLibraryUtil.createBasicFunctionLibrary(prismContext, protector, clock));
functions.add(FunctionLibraryUtil.createLogFunctionLibrary(prismContext));
ScriptExpressionFactory scriptExpressionFactory = new ScriptExpressionFactory(prismContext, repositoryService);
scriptExpressionFactory.setObjectResolver(resolver);
scriptExpressionFactory.setFunctions(functions);
GroovyScriptEvaluator groovyEvaluator = new GroovyScriptEvaluator(prismContext, protector, LocalizationTestUtil.getLocalizationService());
scriptExpressionFactory.registerEvaluator(groovyEvaluator.getLanguageUrl(), groovyEvaluator);
Jsr223ScriptEvaluator jsEvaluator = new Jsr223ScriptEvaluator("ECMAScript", prismContext, protector, LocalizationTestUtil.getLocalizationService());
if (jsEvaluator.isInitialized()) {
scriptExpressionFactory.registerEvaluator(jsEvaluator.getLanguageUrl(), jsEvaluator);
}
ScriptExpressionEvaluatorFactory scriptExpressionEvaluatorFactory = new ScriptExpressionEvaluatorFactory(scriptExpressionFactory, securityContextManager, prismContext);
expressionFactory.registerEvaluatorFactory(scriptExpressionEvaluatorFactory);
return expressionFactory;
}
use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class SandboxTypeCheckingExtension method handleUnresolvedVariableExpression.
@Override
public boolean handleUnresolvedVariableExpression(VariableExpression vexp) {
String variableName = vexp.getName();
ScriptExpressionEvaluationContext context = getContext();
String contextDescription = context.getContextDescription();
if (!isDynamic(vexp)) {
LOGGER.error("Unresolved script variable {} because it is not dynamic, in {}", variableName, contextDescription);
return false;
}
VariablesMap variables = context.getVariables();
if (variables != null) {
TypedValue variableTypedValue = variables.get(variableName);
if (variableTypedValue != null) {
Class variableClass;
try {
variableClass = variableTypedValue.determineClass();
} catch (SchemaException e) {
String msg = "Cannot determine type of variable '" + variableName + "' (" + variableTypedValue + ") in " + contextDescription + ": " + e.getMessage();
LOGGER.error("{}", msg);
throw new IllegalStateException(msg, e);
}
LOGGER.trace("Determine script variable {} as expression variable, class {} in {}", variableName, variableClass, contextDescription);
storeType(vexp, ClassHelper.make(variableClass));
setHandled(true);
return true;
}
}
Collection<FunctionLibrary> functions = context.getFunctions();
if (functions != null) {
for (FunctionLibrary function : functions) {
if (function.getVariableName().equals(variableName)) {
Class functionClass = function.getGenericFunctions().getClass();
LOGGER.trace("Determine script variable {} as function library, class {} in {}", variableName, functionClass, contextDescription);
storeType(vexp, ClassHelper.make(functionClass));
setHandled(true);
return true;
}
}
}
LOGGER.error("Unresolved script variable {} because no declaration for it cannot be found in {}", variableName, contextDescription);
return false;
}
use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class ReportServiceImpl method createFunctionLibraries.
private Collection<FunctionLibrary> createFunctionLibraries() {
FunctionLibrary midPointLib = new FunctionLibrary();
midPointLib.setVariableName("report");
midPointLib.setNamespace("http://midpoint.evolveum.com/xml/ns/public/function/report-3");
ReportFunctions reportFunctions = new ReportFunctions(prismContext, schemaService, model, taskManager, modelAuditService);
midPointLib.setGenericFunctions(reportFunctions);
Collection<FunctionLibrary> functions = new ArrayList<>();
functions.add(basicFunctionLibrary);
functions.add(logFunctionLibrary);
functions.add(midpointFunctionLibrary);
functions.add(midPointLib);
return functions;
}
Aggregations