Search in sources :

Example 1 with ScriptExecutionPolicyActionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExecutionPolicyActionType in project midpoint by Evolveum.

the class PolicyRuleScriptExecutor method executeScriptsFromCollectedRules.

private <O extends ObjectType> void executeScriptsFromCollectedRules(List<EvaluatedPolicyRuleImpl> rules, LensContext<O> context, Task task, OperationResult parentResult) {
    // Must not be minor because of background OID information.
    OperationResult result = parentResult.createSubresult(OP_EXECUTE_SCRIPTS_FROM_RULES);
    try (RunAsRunner runAsRunner = runAsRunnerFactory.runner()) {
        for (EvaluatedPolicyRuleImpl rule : rules) {
            List<ScriptExecutionPolicyActionType> enabledActions = rule.getEnabledActions(ScriptExecutionPolicyActionType.class);
            LOGGER.trace("Rule {} has {} enabled script execution actions", rule, enabledActions.size());
            for (ScriptExecutionPolicyActionType action : enabledActions) {
                ActionContext actx = new ActionContext(action, rule, context, task, this);
                try {
                    // We should consider ordering actions to be executed by runAsRef to avoid unnecessary context switches.
                    runAsRunner.runAs(() -> executeScriptingAction(actx, result), actx.action.getRunAsRef(), result);
                } catch (CommonException e) {
                    LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute scripting action - continuing with others (if present)", e);
                }
            }
        }
    } catch (Throwable t) {
        result.recordFatalError(t);
        throw t;
    } finally {
        result.computeStatusIfUnknown();
        // as we are used to. So this hack is definitely the lesser evil for now.
        if (result.getStatus() == OperationResultStatus.FATAL_ERROR) {
            result.setStatus(OperationResultStatus.PARTIAL_ERROR);
        }
    }
}
Also used : ScriptExecutionPolicyActionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExecutionPolicyActionType) EvaluatedPolicyRuleImpl(com.evolveum.midpoint.model.impl.lens.EvaluatedPolicyRuleImpl) RunAsRunner(com.evolveum.midpoint.model.impl.security.RunAsRunner) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) CommonException(com.evolveum.midpoint.util.exception.CommonException)

Aggregations

EvaluatedPolicyRuleImpl (com.evolveum.midpoint.model.impl.lens.EvaluatedPolicyRuleImpl)1 RunAsRunner (com.evolveum.midpoint.model.impl.security.RunAsRunner)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 CommonException (com.evolveum.midpoint.util.exception.CommonException)1 ScriptExecutionPolicyActionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExecutionPolicyActionType)1