Search in sources :

Example 51 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class ValueTupleTransformation method evaluate.

void evaluate() {
    try {
        if (!combinatorialEvaluation.evaluator.isIncludeNullInputs() && MiscUtil.isAllNull(valuesTuple)) {
            // The case that all the sources are null. There is no point executing the expression.
            setTraceComment("All sources are null and includeNullInputs is true.");
            return;
        }
        VariablesMap staticVariables = createStaticVariablesFromSources();
        recordBeforeTransformation();
        augmentStaticVariablesWithInputVariables(staticVariables);
        evaluateConditionAndTransformation(staticVariables);
        recordTransformationResult();
        outputTriple.addAllToSet(outputSet, transformationResult);
    } catch (Throwable t) {
        result.recordFatalError(t.getMessage(), t);
        throw t;
    }
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 52 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class CustomFunctions method execute.

/**
 * This method is invoked by the scripts. It is supposed to be only public method exposed
 * by this class.
 */
public <V extends PrismValue, D extends ItemDefinition> Object execute(String functionName, Map<String, Object> params) throws ExpressionEvaluationException {
    Validate.notNull(functionName, "Function name must be specified");
    ScriptExpressionEvaluationContext ctx = ScriptExpressionEvaluationContext.getThreadLocal();
    Task task;
    OperationResult result;
    if (ctx != null) {
        if (ctx.getTask() != null) {
            task = ctx.getTask();
        } else {
            // We shouldn't use task of unknown provenience.
            throw new IllegalStateException("No task in ScriptExpressionEvaluationContext for the current thread found");
        }
        if (ctx.getResult() != null) {
            result = ctx.getResult();
        } else {
            // This situation should never occur anyway.
            throw new IllegalStateException("No operation result in ScriptExpressionEvaluationContext for the current thread found");
        }
    } else {
        // This situation should never occur anyway.
        throw new IllegalStateException("No ScriptExpressionEvaluationContext for current thread found");
    }
    List<ExpressionType> functions = library.getFunction().stream().filter(expression -> functionName.equals(expression.getName())).collect(Collectors.toList());
    LOGGER.trace("functions {}", functions);
    ExpressionType expressionType = MiscUtil.extractSingletonRequired(functions, () -> new ExpressionEvaluationException(functions.size() + " functions named '" + functionName + "' present"), () -> new ExpressionEvaluationException("No function named '" + functionName + "' present"));
    LOGGER.trace("function to execute {}", expressionType);
    try {
        VariablesMap variables = new VariablesMap();
        if (MapUtils.isNotEmpty(params)) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                variables.put(entry.getKey(), convertInput(entry, expressionType));
            }
        }
        QName returnType = defaultIfNull(expressionType.getReturnType(), DOMUtil.XSD_STRING);
        D outputDefinition = prepareOutputDefinition(returnType, expressionType.getReturnMultiplicity());
        String shortDesc = "custom function execute";
        Expression<V, D> expression = expressionFactory.makeExpression(expressionType, outputDefinition, expressionProfile, shortDesc, task, result);
        ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, shortDesc, task);
        PrismValueDeltaSetTriple<V> outputTriple = expression.evaluate(context, result);
        LOGGER.trace("Result of the expression evaluation: {}", outputTriple);
        if (outputTriple == null) {
            return null;
        }
        Collection<V> nonNegativeValues = outputTriple.getNonNegativeValues();
        if (nonNegativeValues.isEmpty()) {
            return null;
        }
        if (outputDefinition.isMultiValue()) {
            return PrismValueCollectionsUtil.getRealValuesOfCollection(nonNegativeValues);
        }
        if (nonNegativeValues.size() > 1) {
            throw new ExpressionEvaluationException("Expression returned more than one value (" + nonNegativeValues.size() + ") in " + shortDesc);
        }
        return nonNegativeValues.iterator().next().getRealValue();
    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException e) {
        throw new ExpressionEvaluationException(e.getMessage(), e);
    }
}
Also used : com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Trace(com.evolveum.midpoint.util.logging.Trace) ObjectUtils.defaultIfNull(org.apache.commons.lang3.ObjectUtils.defaultIfNull) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) DOMUtil(com.evolveum.midpoint.util.DOMUtil) SchemaConstantsGenerated(com.evolveum.midpoint.schema.SchemaConstantsGenerated) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) Map(java.util.Map) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) XmlTypeConverter(com.evolveum.midpoint.prism.xml.XmlTypeConverter) MapUtils(org.apache.commons.collections4.MapUtils) ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) Expression(com.evolveum.midpoint.repo.common.expression.Expression) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue) MiscUtil(com.evolveum.midpoint.util.MiscUtil) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) Task(com.evolveum.midpoint.task.api.Task) Collectors(java.util.stream.Collectors) List(java.util.List) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) Validate(org.apache.commons.lang.Validate) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) Task(com.evolveum.midpoint.task.api.Task) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) QName(javax.xml.namespace.QName) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Map(java.util.Map) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 53 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap 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;
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Example 54 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class TransformationalAsyncUpdateMessageListener method onMessage.

@Override
public void onMessage(AsyncUpdateMessageType message, AcknowledgementSink acknowledgementSink) {
    int messageNumber = messagesSeen.getAndIncrement();
    LOGGER.trace("Got message number {}: {}", messageNumber, message);
    SecurityContextManager securityContextManager = connectorInstance.getSecurityContextManager();
    Authentication oldAuthentication = securityContextManager.getAuthentication();
    try {
        securityContextManager.setupPreAuthenticatedSecurityContext(authentication);
        Task task = connectorInstance.getTaskManager().createTaskInstance(OP_ON_MESSAGE_PREPARATION);
        task.setChannel(CHANNEL_ASYNC_UPDATE_URI);
        if (authentication != null && authentication.getPrincipal() instanceof MidPointPrincipal) {
            task.setOwner(((MidPointPrincipal) authentication.getPrincipal()).getFocus().asPrismObject().clone());
        }
        Tracer tracer = connectorInstance.getTracer();
        OperationResult result = task.getResult();
        OperationResultBuilder resultBuilder = OperationResult.createFor(OP_ON_MESSAGE);
        try {
            ActivityTracingDefinitionType tracing = connectorInstance.getConfiguration().getProcessTracingConfiguration();
            if (tracing != null) {
                int interval = defaultIfNull(tracing.getInterval(), 1);
                boolean matches = interval > 0 && messageNumber % interval == 0;
                if (matches) {
                    task.setTracingProfile(tracing.getTracingProfile());
                    if (tracing.getTracingPoint().isEmpty()) {
                        task.addTracingRequest(TracingRootType.ASYNCHRONOUS_MESSAGE_PROCESSING);
                    } else {
                        tracing.getTracingPoint().forEach(task::addTracingRequest);
                    }
                }
            }
            if (task.getTracingRequestedFor().contains(TracingRootType.ASYNCHRONOUS_MESSAGE_PROCESSING)) {
                TracingProfileType profile = task.getTracingProfile() != null ? task.getTracingProfile() : tracer.getDefaultProfile();
                resultBuilder.tracingProfile(tracer.compileProfile(profile, task.getResult()));
            }
            // replace task result with the newly-built one
            result = resultBuilder.build();
            task.setResult(result);
            VariablesMap variables = new VariablesMap();
            variables.put(VAR_MESSAGE, message, AsyncUpdateMessageType.class);
            List<UcfChangeType> changeBeans;
            try {
                ExpressionType transformExpression = connectorInstance.getTransformExpression();
                if (transformExpression != null) {
                    changeBeans = connectorInstance.getUcfExpressionEvaluator().evaluate(transformExpression, variables, SchemaConstantsGenerated.C_UCF_CHANGE, "computing UCF change from async update", task, result);
                } else {
                    changeBeans = unwrapMessage(message);
                }
            } catch (RuntimeException | SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
                throw new SystemException("Couldn't evaluate message transformation expression: " + e.getMessage(), e);
            }
            if (changeBeans.isEmpty()) {
                acknowledgementSink.acknowledge(true, result);
            } else {
                AcknowledgementSink aggregatedSink = createAggregatingAcknowledgeSink(acknowledgementSink, changeBeans.size());
                for (UcfChangeType changeBean : changeBeans) {
                    // For this to work reliably, we have to run in a single thread. But that's ok.
                    // If we receive messages in multiple threads, there is no message ordering.
                    int changeSequentialNumber = changesProduced.incrementAndGet();
                    // intentionally in this order - to process changes even after failure
                    // (if listener wants to fail fast, it can throw an exception)
                    UcfAsyncUpdateChange change = createChange(changeBean, result, changeSequentialNumber, aggregatedSink);
                    changeListener.onChange(change, task, result);
                }
            }
        } catch (Exception e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Got exception while processing asynchronous message in {}", e, task);
            result.recordFatalError(e.getMessage(), e);
            int changeSequentialNumber = changesProduced.incrementAndGet();
            UcfAsyncUpdateChange change = new UcfAsyncUpdateChange(changeSequentialNumber, UcfErrorState.error(e), acknowledgementSink);
            changeListener.onChange(change, task, result);
        } finally {
            result.computeStatusIfUnknown();
            // (Otherwise it captures only the pre-processing activities.)
            if (result.isTraced()) {
                tracer.storeTrace(task, result, null);
            }
        }
    } finally {
        securityContextManager.setupPreAuthenticatedSecurityContext(oldAuthentication);
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) AcknowledgementSink(com.evolveum.midpoint.schema.AcknowledgementSink) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) UcfAsyncUpdateChange(com.evolveum.midpoint.provisioning.ucf.api.UcfAsyncUpdateChange) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) OperationResultBuilder(com.evolveum.midpoint.schema.result.OperationResultBuilder) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal) Tracer(com.evolveum.midpoint.task.api.Tracer) Authentication(org.springframework.security.core.Authentication) SecurityContextManager(com.evolveum.midpoint.security.api.SecurityContextManager)

Example 55 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class CommandLineScriptExecutor method executeScript.

public void executeScript(CommandLineScriptType scriptType, VariablesMap variables, String shortDesc, Task task, OperationResult parentResult) throws IOException, InterruptedException, SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    OperationResult result = parentResult.createSubresult(CommandLineScriptExecutor.class.getSimpleName() + ".run");
    if (variables == null) {
        variables = new VariablesMap();
    }
    String expandedCode = expandMacros(scriptType, variables, shortDesc, task, result);
    // TODO: later: prepare agruments and environment
    String preparedCode = expandedCode.trim();
    LOGGER.debug("Prepared shell code: {}", preparedCode);
    CommandLineRunner runner = new CommandLineRunner(preparedCode, result);
    runner.setExecutionMethod(scriptType.getExecutionMethod());
    runner.execute();
    result.computeStatus();
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)166 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)48 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)30 Test (org.testng.annotations.Test)28 Task (com.evolveum.midpoint.task.api.Task)23 NotNull (org.jetbrains.annotations.NotNull)23 QName (javax.xml.namespace.QName)15 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)12 Source (com.evolveum.midpoint.repo.common.expression.Source)12 AbstractModelCommonTest (com.evolveum.midpoint.model.common.AbstractModelCommonTest)11 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)11 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)10 Trace (com.evolveum.midpoint.util.logging.Trace)10 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)10 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)9 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)9 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)8 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)8 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)7