Search in sources :

Example 1 with ScriptExpression

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

the class Clockwork method evaluateScriptingHook.

private void evaluateScriptingHook(LensContext context, HookType hookType, ScriptExpressionEvaluatorType scriptExpressionEvaluatorType, String shortDesc, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    LOGGER.trace("Evaluating {}", shortDesc);
    // TODO: it would be nice to cache this
    // null output definition: this script has no output
    ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptExpressionEvaluatorType, null, shortDesc);
    ExpressionVariables variables = new ExpressionVariables();
    variables.addVariableDefinition(ExpressionConstants.VAR_PRISM_CONTEXT, prismContext);
    variables.addVariableDefinition(ExpressionConstants.VAR_MODEL_CONTEXT, context);
    LensFocusContext focusContext = context.getFocusContext();
    PrismObject focus = null;
    if (focusContext != null) {
        focus = focusContext.getObjectAny();
    }
    variables.addVariableDefinition(ExpressionConstants.VAR_FOCUS, focus);
    Utils.evaluateScript(scriptExpression, context, variables, false, shortDesc, task, result);
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression)

Example 2 with ScriptExpression

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

the class ScriptExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    checkRootAuthorization(globalResult, NAME);
    ScriptExpressionEvaluatorType script = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_SCRIPT, true, true, NAME, input, context, ScriptExpressionEvaluatorType.class, globalResult);
    String outputItem = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
    ItemDefinition<?> outputDefinition = getItemDefinition(outputItem);
    ScriptExpression scriptExpression;
    try {
        scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, "script");
    } catch (ExpressionSyntaxException e) {
        throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
    }
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        String valueDescription;
        long started;
        if (value instanceof PrismObjectValue) {
            started = operationsHelper.recordStart(context, asObjectType(value));
            valueDescription = asObjectType(value).asPrismObject().toString();
        } else {
            started = 0;
            valueDescription = value.toHumanReadableString();
        }
        Throwable exception = null;
        try {
            Object outObject = executeScript(scriptExpression, value, context, result);
            if (outObject != null) {
                addToData(outObject, item.getResult(), output);
            } else {
                output.add(item);
            }
            if (value instanceof PrismObjectValue) {
                operationsHelper.recordEnd(context, asObjectType(value), started, null);
            }
        } catch (Throwable ex) {
            if (value instanceof PrismObjectValue) {
                operationsHelper.recordEnd(context, asObjectType(value), started, ex);
            }
            exception = processActionException(ex, NAME, value, context);
        }
        context.println((exception != null ? "Attempted to execute " : "Executed ") + "script on " + valueDescription + exceptionSuffix(exception));
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return output;
}
Also used : ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem)

Example 3 with ScriptExpression

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

the class BasicExpressionFunctions method getPropertyValues.

public <T> Collection<T> getPropertyValues(ObjectType object, String path) {
    if (object == null) {
        return null;
    }
    ScriptExpressionEvaluationContext scriptContext = ScriptExpressionEvaluationContext.getThreadLocal();
    ScriptExpression scriptExpression = scriptContext.getScriptExpression();
    ItemPath itemPath = scriptExpression.parsePath(path);
    PrismProperty property = object.asPrismObject().findProperty(itemPath);
    if (property == null) {
        return new ArrayList<T>(0);
    }
    return property.getRealValues();
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) ArrayList(java.util.ArrayList) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 4 with ScriptExpression

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

the class ClockworkHookHelper method evaluateScriptingHook.

private void evaluateScriptingHook(LensContext<?> context, ScriptExpressionEvaluatorType scriptExpressionEvaluatorType, String shortDesc, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    LOGGER.trace("Evaluating {}", shortDesc);
    // TODO: it would be nice to cache this
    // null output definition: this script has no output
    ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptExpressionEvaluatorType, null, context.getPrivilegedExpressionProfile(), expressionFactory, shortDesc, result);
    VariablesMap variables = new VariablesMap();
    variables.put(ExpressionConstants.VAR_PRISM_CONTEXT, prismContext, PrismContext.class);
    variables.put(ExpressionConstants.VAR_MODEL_CONTEXT, context, ModelContext.class);
    LensFocusContext<?> focusContext = context.getFocusContext();
    if (focusContext != null) {
        variables.put(ExpressionConstants.VAR_FOCUS, focusContext.getObjectAny(), focusContext.getObjectDefinition());
    } else {
        variables.put(ExpressionConstants.VAR_FOCUS, null, FocusType.class);
    }
    ModelImplUtils.evaluateScript(scriptExpression, context, variables, false, shortDesc, task, result);
    LOGGER.trace("Finished evaluation of {}", shortDesc);
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression)

Example 5 with ScriptExpression

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

the class ExecuteScriptExecutor method getParameters.

private Parameters getParameters(ActionExpressionType action, PipelineData input, ExecutionContext context, OperationResult globalResult) throws CommunicationException, ObjectNotFoundException, SchemaException, ScriptExecutionException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
    ScriptExpressionEvaluatorType script = expressionHelper.getActionArgument(ScriptExpressionEvaluatorType.class, action, ExecuteScriptActionExpressionType.F_SCRIPT, PARAM_SCRIPT, input, context, null, PARAM_SCRIPT, globalResult);
    ItemDefinition<?> outputDefinition;
    String outputItemUri = expressionHelper.getSingleArgumentValue(action.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
    if (StringUtils.isNotBlank(outputItemUri)) {
        outputDefinition = getItemDefinition(outputItemUri);
    } else if (action instanceof ExecuteScriptActionExpressionType) {
        ExecuteScriptActionExpressionType execute = (ExecuteScriptActionExpressionType) action;
        if (execute.getOutputItemName() != null) {
            outputDefinition = getItemDefinitionFromItemName(execute.getOutputItemName());
        } else if (execute.getOutputTypeName() != null) {
            outputDefinition = getItemDefinitionFromTypeName(execute.getOutputTypeName());
        } else {
            outputDefinition = null;
        }
    } else {
        outputDefinition = null;
    }
    boolean forWholeInput = expressionHelper.getActionArgument(Boolean.class, action, ExecuteScriptActionExpressionType.F_FOR_WHOLE_INPUT, PARAM_FOR_WHOLE_INPUT, input, context, false, PARAM_FOR_WHOLE_INPUT, globalResult);
    boolean quiet = expressionHelper.getActionArgument(Boolean.class, action, ExecuteScriptActionExpressionType.F_QUIET, PARAM_QUIET, input, context, false, PARAM_QUIET, globalResult);
    if (script == null) {
        throw new IllegalArgumentException("No script provided");
    }
    // TODO
    ExpressionProfile expressionProfile = null;
    ScriptExpression scriptExpression;
    try {
        scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, expressionProfile, expressionFactory, "script", globalResult);
    } catch (ExpressionSyntaxException | SecurityViolationException e) {
        throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
    }
    return new Parameters(scriptExpression, outputDefinition, forWholeInput, quiet);
}
Also used : ExecuteScriptActionExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptActionExpressionType) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)

Aggregations

ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)10 ScriptExpressionEvaluatorType (com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType)6 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)5 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)4 ScriptExpressionEvaluationContext (com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext)2 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)2 ExpressionSyntaxException (com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)2 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)2 Task (com.evolveum.midpoint.task.api.Task)2 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)2 Test (org.testng.annotations.Test)2 PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)1 ScriptExecutionException (com.evolveum.midpoint.model.api.ScriptExecutionException)1 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)1 ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)1 PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)1 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)1 ExecuteScriptActionExpressionType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptActionExpressionType)1