Search in sources :

Example 6 with FunctionLibrary

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;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Jsr223ScriptEvaluator(com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) Task(com.evolveum.midpoint.task.api.Task) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Collection(java.util.Collection) Containerable(com.evolveum.midpoint.prism.Containerable) PrismObject(com.evolveum.midpoint.prism.PrismObject)

Example 7 with FunctionLibrary

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;
}
Also used : ScriptExpressionFactory(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionFactory) Jsr223ScriptEvaluator(com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator) ScriptExpressionFactory(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionFactory) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ConstantsManager(com.evolveum.midpoint.model.common.ConstantsManager) ArrayList(java.util.ArrayList) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) AsIsExpressionEvaluatorFactory(com.evolveum.midpoint.repo.common.expression.evaluator.AsIsExpressionEvaluatorFactory) LiteralExpressionEvaluatorFactory(com.evolveum.midpoint.repo.common.expression.evaluator.LiteralExpressionEvaluatorFactory) ValuePolicyProcessor(com.evolveum.midpoint.model.common.stringpolicy.ValuePolicyProcessor) ConstExpressionEvaluatorFactory(com.evolveum.midpoint.model.common.expression.evaluator.ConstExpressionEvaluatorFactory) GenerateExpressionEvaluatorFactory(com.evolveum.midpoint.model.common.expression.evaluator.GenerateExpressionEvaluatorFactory) ScriptExpressionEvaluatorFactory(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluatorFactory) PathExpressionEvaluatorFactory(com.evolveum.midpoint.model.common.expression.evaluator.PathExpressionEvaluatorFactory) XPathScriptEvaluator(com.evolveum.midpoint.model.common.expression.script.xpath.XPathScriptEvaluator)

Example 8 with FunctionLibrary

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;
}
Also used : FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary)

Aggregations

FunctionLibrary (com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary)8 ArrayList (java.util.ArrayList)6 Jsr223ScriptEvaluator (com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator)4 QName (javax.xml.namespace.QName)3 PrismObject (com.evolveum.midpoint.prism.PrismObject)2 Protector (com.evolveum.midpoint.prism.crypto.Protector)2 ProtectorImpl (com.evolveum.midpoint.prism.crypto.ProtectorImpl)2 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 ObjectResolver (com.evolveum.midpoint.schema.util.ObjectResolver)2 Task (com.evolveum.midpoint.task.api.Task)2 DirectoryFileObjectResolver (com.evolveum.midpoint.test.util.DirectoryFileObjectResolver)2 Collection (java.util.Collection)2 BeforeClass (org.testng.annotations.BeforeClass)2 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)1 ConstantsManager (com.evolveum.midpoint.model.common.ConstantsManager)1 ConstExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.evaluator.ConstExpressionEvaluatorFactory)1 GenerateExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.evaluator.GenerateExpressionEvaluatorFactory)1 PathExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.evaluator.PathExpressionEvaluatorFactory)1 ScriptExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluatorFactory)1