Search in sources :

Example 1 with ChangeHook

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

the class Clockwork method invokeHooks.

/**
     * Invokes hooks, if there are any.
     *
     * @return
     *  - ERROR, if any hook reported error; otherwise returns
     *  - BACKGROUND, if any hook reported switching to background; otherwise
     *  - FOREGROUND (if all hooks reported finishing on foreground) 
     */
private HookOperationMode invokeHooks(LensContext context, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, PolicyViolationException {
    // TODO: following two parts should be merged together in later versions
    // Execute configured scripting hooks
    PrismObject<SystemConfigurationType> systemConfiguration = systemObjectCache.getSystemConfiguration(result);
    // systemConfiguration may be null in some tests
    if (systemConfiguration != null) {
        ModelHooksType modelHooks = systemConfiguration.asObjectable().getModelHooks();
        if (modelHooks != null) {
            HookListType changeHooks = modelHooks.getChange();
            if (changeHooks != null) {
                for (HookType hookType : changeHooks.getHook()) {
                    String shortDesc;
                    if (hookType.getName() != null) {
                        shortDesc = "hook '" + hookType.getName() + "'";
                    } else {
                        shortDesc = "scripting hook in system configuration";
                    }
                    if (hookType.isEnabled() != null && !hookType.isEnabled()) {
                        // Disabled hook, skip
                        continue;
                    }
                    if (hookType.getState() != null) {
                        if (!context.getState().toModelStateType().equals(hookType.getState())) {
                            continue;
                        }
                    }
                    if (hookType.getFocusType() != null) {
                        if (context.getFocusContext() == null) {
                            continue;
                        }
                        QName hookFocusTypeQname = hookType.getFocusType();
                        ObjectTypes hookFocusType = ObjectTypes.getObjectTypeFromTypeQName(hookFocusTypeQname);
                        if (hookFocusType == null) {
                            throw new SchemaException("Unknown focus type QName " + hookFocusTypeQname + " in " + shortDesc);
                        }
                        Class focusClass = context.getFocusClass();
                        Class<? extends ObjectType> hookFocusClass = hookFocusType.getClassDefinition();
                        if (!hookFocusClass.isAssignableFrom(focusClass)) {
                            continue;
                        }
                    }
                    ScriptExpressionEvaluatorType scriptExpressionEvaluatorType = hookType.getScript();
                    if (scriptExpressionEvaluatorType == null) {
                        continue;
                    }
                    try {
                        evaluateScriptingHook(context, hookType, scriptExpressionEvaluatorType, shortDesc, task, result);
                    } catch (ExpressionEvaluationException e) {
                        LOGGER.error("Evaluation of {} failed: {}", new Object[] { shortDesc, e.getMessage(), e });
                        throw new ExpressionEvaluationException("Evaluation of " + shortDesc + " failed: " + e.getMessage(), e);
                    } catch (ObjectNotFoundException e) {
                        LOGGER.error("Evaluation of {} failed: {}", new Object[] { shortDesc, e.getMessage(), e });
                        throw new ObjectNotFoundException("Evaluation of " + shortDesc + " failed: " + e.getMessage(), e);
                    } catch (SchemaException e) {
                        LOGGER.error("Evaluation of {} failed: {}", new Object[] { shortDesc, e.getMessage(), e });
                        throw new SchemaException("Evaluation of " + shortDesc + " failed: " + e.getMessage(), e);
                    }
                }
            }
        }
    }
    // Execute registered Java hooks
    HookOperationMode resultMode = HookOperationMode.FOREGROUND;
    if (hookRegistry != null) {
        for (ChangeHook hook : hookRegistry.getAllChangeHooks()) {
            HookOperationMode mode = hook.invoke(context, task, result);
            if (mode == HookOperationMode.ERROR) {
                resultMode = HookOperationMode.ERROR;
            } else if (mode == HookOperationMode.BACKGROUND) {
                if (resultMode != HookOperationMode.ERROR) {
                    resultMode = HookOperationMode.BACKGROUND;
                }
            }
        }
    }
    return resultMode;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) QName(javax.xml.namespace.QName) ChangeHook(com.evolveum.midpoint.model.api.hooks.ChangeHook) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) HookOperationMode(com.evolveum.midpoint.model.api.hooks.HookOperationMode) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Aggregations

ChangeHook (com.evolveum.midpoint.model.api.hooks.ChangeHook)1 HookOperationMode (com.evolveum.midpoint.model.api.hooks.HookOperationMode)1 ObjectTypes (com.evolveum.midpoint.schema.constants.ObjectTypes)1 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)1 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 QName (javax.xml.namespace.QName)1