Search in sources :

Example 1 with Jsr223ScriptEvaluator

use of com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator 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;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Jsr223ScriptEvaluator(com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator) 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) PrismObject(com.evolveum.midpoint.prism.PrismObject) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord)

Example 2 with Jsr223ScriptEvaluator

use of com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator 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;
}
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) GroovyScriptEvaluator(com.evolveum.midpoint.model.common.expression.script.groovy.GroovyScriptEvaluator) 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.path.PathExpressionEvaluatorFactory)

Example 3 with Jsr223ScriptEvaluator

use of com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator 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 4 with Jsr223ScriptEvaluator

use of com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator 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 5 with Jsr223ScriptEvaluator

use of com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator in project midpoint by Evolveum.

the class TestScriptCaching method setupFactory.

@BeforeClass
public void setupFactory() {
    display("Setting up expression factory and evaluator");
    PrismContext prismContext = getPrismContext();
    ObjectResolver resolver = new DirectoryFileObjectResolver(OBJECTS_DIR);
    Protector protector = KeyStoreBasedProtectorBuilder.create(prismContext).buildOnly();
    Clock clock = new Clock();
    Collection<FunctionLibrary> functions = new ArrayList<>();
    functions.add(FunctionLibraryUtil.createBasicFunctionLibrary(prismContext, protector, clock));
    scriptExpressionfactory = new ScriptExpressionFactory(prismContext, null);
    scriptExpressionfactory.setObjectResolver(resolver);
    scriptExpressionfactory.setFunctions(functions);
    evaluator = new Jsr223ScriptEvaluator("groovy", prismContext, protector, LocalizationTestUtil.getLocalizationService());
    String languageUrl = evaluator.getLanguageUrl();
    scriptExpressionfactory.registerEvaluator(languageUrl, evaluator);
}
Also used : Jsr223ScriptEvaluator(com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator) PrismContext(com.evolveum.midpoint.prism.PrismContext) PrismTestUtil.getPrismContext(com.evolveum.midpoint.prism.util.PrismTestUtil.getPrismContext) DirectoryFileObjectResolver(com.evolveum.midpoint.repo.common.DirectoryFileObjectResolver) ObjectResolver(com.evolveum.midpoint.repo.common.ObjectResolver) DirectoryFileObjectResolver(com.evolveum.midpoint.repo.common.DirectoryFileObjectResolver) ArrayList(java.util.ArrayList) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) Clock(com.evolveum.midpoint.common.Clock) Protector(com.evolveum.midpoint.prism.crypto.Protector) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

FunctionLibrary (com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary)5 Jsr223ScriptEvaluator (com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator)5 ArrayList (java.util.ArrayList)5 ConstantsManager (com.evolveum.midpoint.model.common.ConstantsManager)2 ConstExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.evaluator.ConstExpressionEvaluatorFactory)2 GenerateExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.evaluator.GenerateExpressionEvaluatorFactory)2 ScriptExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluatorFactory)2 ScriptExpressionFactory (com.evolveum.midpoint.model.common.expression.script.ScriptExpressionFactory)2 ValuePolicyProcessor (com.evolveum.midpoint.model.common.stringpolicy.ValuePolicyProcessor)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)2 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)2 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)2 AsIsExpressionEvaluatorFactory (com.evolveum.midpoint.repo.common.expression.evaluator.AsIsExpressionEvaluatorFactory)2 LiteralExpressionEvaluatorFactory (com.evolveum.midpoint.repo.common.expression.evaluator.LiteralExpressionEvaluatorFactory)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 Task (com.evolveum.midpoint.task.api.Task)2 Collection (java.util.Collection)2 QName (javax.xml.namespace.QName)2 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)1 Clock (com.evolveum.midpoint.common.Clock)1