Search in sources :

Example 16 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class RecomputeExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean dryRun = getParamDryRun(expression, input, context, globalResult);
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue && FocusType.class.isAssignableFrom(((PrismObjectValue) value).asPrismObject().getCompileTimeClass())) {
            PrismObject<FocusType> focalPrismObject = ((PrismObjectValue) value).asPrismObject();
            FocusType focusType = focalPrismObject.asObjectable();
            long started = operationsHelper.recordStart(context, focusType);
            Throwable exception = null;
            try {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Recomputing object {} with dryRun={}: context:\n{}", focalPrismObject, dryRun);
                }
                ObjectDelta<? extends FocusType> emptyDelta = ObjectDelta.createEmptyDelta(focusType.getClass(), focusType.getOid(), prismContext, ChangeType.MODIFY);
                operationsHelper.applyDelta(emptyDelta, ModelExecuteOptions.createReconcile(), dryRun, context, result);
                LOGGER.trace("Recomputing of object {}: {}", focalPrismObject, result.getStatus());
                operationsHelper.recordEnd(context, focusType, started, null);
            } catch (Throwable e) {
                operationsHelper.recordEnd(context, focusType, started, e);
                exception = processActionException(e, NAME, value, context);
            }
            context.println((exception != null ? "Attempted to recompute " : "Recomputed ") + focalPrismObject.toString() + drySuffix(dryRun) + exceptionSuffix(exception));
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject<FocusType>"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return input;
}
Also used : PrismObjectValue(com.evolveum.midpoint.prism.PrismObjectValue) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) FocusType(com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismValue(com.evolveum.midpoint.prism.PrismValue)

Example 17 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class ResolveExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean noFetch = expressionHelper.getArgumentAsBoolean(expression.getParameter(), PARAM_NO_FETCH, input, context, false, NAME, globalResult);
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismReferenceValue) {
            PrismReferenceValue prismReferenceValue = (PrismReferenceValue) value;
            String oid = prismReferenceValue.getOid();
            QName targetTypeQName = prismReferenceValue.getTargetType();
            if (targetTypeQName == null) {
                throw new ScriptExecutionException("Couldn't resolve reference, because target type is unknown: " + prismReferenceValue);
            }
            Class<? extends ObjectType> typeClass = prismContext.getSchemaRegistry().determineCompileTimeClass(targetTypeQName);
            if (typeClass == null) {
                throw new ScriptExecutionException("Couldn't resolve reference, because target type class is unknown for target type " + targetTypeQName);
            }
            try {
                PrismObjectValue<? extends ObjectType> resolved = operationsHelper.getObject(typeClass, oid, noFetch, context, result).getValue();
                output.add(new PipelineItem(resolved, item.getResult()));
            } catch (Throwable e) {
                //noinspection ThrowableNotThrown
                processActionException(e, NAME, value, context);
                // to keep track of failed item (may trigger exceptions downstream)
                output.add(item);
            }
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismReference"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return output;
}
Also used : PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) QName(javax.xml.namespace.QName) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Example 18 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class PageBulkAction method startPerformed.

private void startPerformed(AjaxRequestTarget target) {
    Task task = createSimpleTask(OPERATION_PERFORM_BULK);
    OperationResult result = new OperationResult(OPERATION_PERFORM_BULK);
    BulkActionDto bulkActionDto = model.getObject();
    if (StringUtils.isEmpty(bulkActionDto.getScript())) {
        warn(getString("PageBulkAction.message.emptyString"));
        target.add(getFeedbackPanel());
        return;
    }
    Object parsed = null;
    try {
        parsed = getPrismContext().parserFor(bulkActionDto.getScript()).parseRealValue();
        if (parsed == null) {
            result.recordFatalError("No bulk action object was provided.");
        } else if (!(parsed instanceof ExecuteScriptType) && !(parsed instanceof ScriptingExpressionType)) {
            result.recordFatalError("Provided text is not a bulk action object. An instance of {scripting-3}ScriptingExpressionType is expected; you have provided " + parsed.getClass() + " instead.");
        }
    } catch (SchemaException | RuntimeException e) {
        result.recordFatalError("Couldn't parse bulk action object", e);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't parse bulk action object", e);
    }
    if (parsed != null) {
        if (bulkActionDto.isAsync()) {
            try {
                if (parsed instanceof ExecuteScriptType) {
                    getScriptingService().evaluateExpressionInBackground((ExecuteScriptType) parsed, task, result);
                } else {
                    //noinspection ConstantConditions
                    getScriptingService().evaluateExpressionInBackground((ScriptingExpressionType) parsed, task, result);
                }
                result.recordStatus(OperationResultStatus.IN_PROGRESS, task.getName() + " has been successfully submitted to execution");
            } catch (SchemaException | SecurityViolationException e) {
                result.recordFatalError("Couldn't submit bulk action to execution", e);
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't submit bulk action to execution", e);
            }
        } else {
            try {
                //noinspection ConstantConditions
                ScriptExecutionResult executionResult = parsed instanceof ExecuteScriptType ? getScriptingService().evaluateExpression((ExecuteScriptType) parsed, task, result) : getScriptingService().evaluateExpression((ScriptingExpressionType) parsed, task, result);
                result.recordStatus(OperationResultStatus.SUCCESS, "Action executed. Returned " + executionResult.getDataOutput().size() + " item(s). Console and data output available via 'Export to XML' function.");
                result.addReturn("console", executionResult.getConsoleOutput());
                result.addCollectionOfSerializablesAsReturn("data", executionResult.getDataOutput());
            } catch (ScriptExecutionException | SchemaException | SecurityViolationException e) {
                result.recordFatalError("Couldn't execute bulk action", e);
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute bulk action", e);
            }
        }
    }
    showResult(result);
    target.add(getFeedbackPanel());
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Task(com.evolveum.midpoint.task.api.Task) ExecuteScriptType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) ScriptExecutionResult(com.evolveum.midpoint.model.api.ScriptExecutionResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) BulkActionDto(com.evolveum.midpoint.web.page.admin.configuration.dto.BulkActionDto)

Example 19 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class ScriptingExpressionEvaluator method evaluateExpression.

// main entry point from the outside
private ExecutionContext evaluateExpression(ScriptingExpressionType expression, PipelineData data, ScriptingExpressionEvaluationOptionsType options, Task task, OperationResult result) throws ScriptExecutionException {
    ExecutionContext context = new ExecutionContext(options, task);
    PipelineData output;
    try {
        output = evaluateExpression(expression, data, context, result);
    } catch (RuntimeException e) {
        result.recordFatalError("Couldn't execute script", e);
        throw new ScriptExecutionException("Couldn't execute script: " + e.getMessage(), e);
    }
    result.computeStatusIfUnknown();
    context.setFinalOutput(output);
    return context;
}
Also used : ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException)

Example 20 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class AddExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean raw = getParamRaw(expression, input, context, globalResult);
    boolean dryRun = getParamDryRun(expression, input, context, globalResult);
    for (PipelineItem item : input.getData()) {
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        PrismValue value = item.getValue();
        if (value instanceof PrismObjectValue) {
            @SuppressWarnings({ "unchecked", "raw" }) PrismObject<? extends ObjectType> prismObject = ((PrismObjectValue) value).asPrismObject();
            ObjectType objectType = prismObject.asObjectable();
            long started = operationsHelper.recordStart(context, objectType);
            Throwable exception = null;
            try {
                operationsHelper.applyDelta(createAddDelta(objectType), operationsHelper.createExecutionOptions(raw), dryRun, context, result);
                operationsHelper.recordEnd(context, objectType, started, null);
            } catch (Throwable ex) {
                operationsHelper.recordEnd(context, objectType, started, ex);
                exception = processActionException(ex, NAME, value, context);
            }
            context.println((exception != null ? "Attempted to add " : "Added ") + prismObject.toString() + rawDrySuffix(raw, dryRun) + exceptionSuffix(exception));
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return input;
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) PrismObjectValue(com.evolveum.midpoint.prism.PrismObjectValue) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismValue(com.evolveum.midpoint.prism.PrismValue)

Aggregations

ScriptExecutionException (com.evolveum.midpoint.model.api.ScriptExecutionException)25 PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)16 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)16 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)11 PrismValue (com.evolveum.midpoint.prism.PrismValue)8 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)7 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)6 PrismObjectValue (com.evolveum.midpoint.prism.PrismObjectValue)5 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)5 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)4 ResourceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType)4 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)3 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)3 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)3 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)3 FocusType (com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType)3 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)3 ActionParameterValueType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType)3 ScriptExecutionResult (com.evolveum.midpoint.model.api.ScriptExecutionResult)2 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)2