Search in sources :

Example 11 with FunctionLibrary

use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.

the class ScriptExpressionFactory method initializeCustomFunctionsLibraryCache.

private void initializeCustomFunctionsLibraryCache(ExpressionFactory expressionFactory, OperationResult result) throws ExpressionSyntaxException {
    if (repositoryService != null) {
        OperationResult subResult = result.createMinorSubresult(ScriptExpressionFactory.class.getName() + ".searchCustomFunctions");
        ResultHandler<FunctionLibraryType> functionLibraryHandler = (object, parentResult) -> {
            // TODO: determine profile from function library archetype
            ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
            FunctionLibrary customLibrary = new FunctionLibrary();
            customLibrary.setVariableName(object.getName().getOrig());
            customLibrary.setGenericFunctions(new CustomFunctions(object.asObjectable(), expressionFactory, expressionProfile));
            customLibrary.setNamespace(MidPointConstants.NS_FUNC_CUSTOM);
            customFunctionLibraryCache.put(object.getName().getOrig(), customLibrary);
            return true;
        };
        try {
            repositoryService.searchObjectsIterative(FunctionLibraryType.class, null, functionLibraryHandler, createReadOnlyCollection(), true, subResult);
            subResult.recordSuccessIfUnknown();
        } catch (SchemaException | RuntimeException e) {
            subResult.recordFatalError("Failed to initialize custom functions", e);
            throw new ExpressionSyntaxException("An error occurred during custom libraries initialization. " + e.getMessage(), e);
        }
    } else {
        LOGGER.warn("No repository service set for ScriptExpressionFactory; custom functions will not be loaded. This" + " can occur during low-level testing; never during standard system execution.");
    }
}
Also used : GetOperationOptions.createReadOnlyCollection(com.evolveum.midpoint.schema.GetOperationOptions.createReadOnlyCollection) java.util(java.util) ExpressionEvaluatorProfile(com.evolveum.midpoint.schema.expression.ExpressionEvaluatorProfile) CacheInvalidationContext(com.evolveum.midpoint.CacheInvalidationContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) Cache(com.evolveum.midpoint.repo.api.Cache) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Trace(com.evolveum.midpoint.util.logging.Trace) PreDestroy(javax.annotation.PreDestroy) ScriptExpressionProfile(com.evolveum.midpoint.schema.expression.ScriptExpressionProfile) AccessDecision(com.evolveum.midpoint.schema.AccessDecision) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType) PrismContext(com.evolveum.midpoint.prism.PrismContext) MiscSchemaUtil(com.evolveum.midpoint.schema.util.MiscSchemaUtil) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) CacheRegistry(com.evolveum.midpoint.repo.api.CacheRegistry) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) FunctionLibraryType(com.evolveum.midpoint.xml.ns._public.common.common_3.FunctionLibraryType) SingleCacheStateInformationType(com.evolveum.midpoint.xml.ns._public.common.common_3.SingleCacheStateInformationType) CustomFunctions(com.evolveum.midpoint.model.common.expression.functions.CustomFunctions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectResolver(com.evolveum.midpoint.repo.common.ObjectResolver) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) MidPointConstants(com.evolveum.midpoint.schema.constants.MidPointConstants) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) PostConstruct(javax.annotation.PostConstruct) NotNull(org.jetbrains.annotations.NotNull) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) FunctionLibraryType(com.evolveum.midpoint.xml.ns._public.common.common_3.FunctionLibraryType) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionProfile(com.evolveum.midpoint.schema.expression.ScriptExpressionProfile) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) CustomFunctions(com.evolveum.midpoint.model.common.expression.functions.CustomFunctions) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)

Example 12 with FunctionLibrary

use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.

the class AbstractScriptEvaluator method prepareScriptVariablesValueMap.

/**
 * Returns simple variable map: name -> value.
 */
protected Map<String, Object> prepareScriptVariablesValueMap(ScriptExpressionEvaluationContext context) throws ExpressionSyntaxException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    Map<String, Object> scriptVariableMap = new HashMap<>();
    // Functions
    if (context.getFunctions() != null) {
        for (FunctionLibrary funcLib : context.getFunctions()) {
            scriptVariableMap.put(funcLib.getVariableName(), funcLib.getGenericFunctions());
        }
    }
    // Variables
    VariablesMap variables = context.getVariables();
    if (variables != null) {
        for (Entry<String, TypedValue> variableEntry : variables.entrySet()) {
            if (variableEntry.getKey() == null) {
                // This is the "root" node. We have no use for it in script expressions, just skip it
                continue;
            }
            String variableName = variableEntry.getKey();
            ValueVariableModeType valueVariableMode = ObjectUtils.defaultIfNull(context.getExpressionType().getValueVariableMode(), ValueVariableModeType.REAL_VALUE);
            // noinspection rawtypes
            TypedValue variableTypedValue = ExpressionUtil.convertVariableValue(variableEntry.getValue(), variableName, context.getObjectResolver(), context.getContextDescription(), context.getExpressionType().getObjectVariableMode(), valueVariableMode, prismContext, context.getTask(), context.getResult());
            scriptVariableMap.put(variableName, variableTypedValue.getValue());
            if (context.getTrace() != null && !variables.isAlias(variableName)) {
                ScriptVariableEvaluationTraceType variableTrace = new ScriptVariableEvaluationTraceType(prismContext);
                variableTrace.setName(new QName(variableName));
                Object clonedValue = cloneIfPossible(variableTypedValue.getValue());
                variableTrace.getValue().addAll(TraceUtil.toAnyValueTypeList(clonedValue, prismContext));
                variables.getAliases(variableName).forEach(alias -> variableTrace.getAlias().add(new QName(alias)));
                context.getTrace().getVariable().add(variableTrace);
            }
        }
    }
    putIfMissing(scriptVariableMap, ExpressionConstants.VAR_PRISM_CONTEXT, prismContext);
    putIfMissing(scriptVariableMap, ExpressionConstants.VAR_LOCALIZATION_SERVICE, localizationService);
    return scriptVariableMap;
}
Also used : ScriptVariableEvaluationTraceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptVariableEvaluationTraceType) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) ValueVariableModeType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValueVariableModeType) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Aggregations

FunctionLibrary (com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary)12 ArrayList (java.util.ArrayList)6 Jsr223ScriptEvaluator (com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator)5 QName (javax.xml.namespace.QName)4 ObjectResolver (com.evolveum.midpoint.repo.common.ObjectResolver)3 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 Clock (com.evolveum.midpoint.common.Clock)2 ConstantsManager (com.evolveum.midpoint.model.common.ConstantsManager)2 ConstExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.evaluator.ConstExpressionEvaluatorFactory)2 GenerateExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.evaluator.GenerateExpressionEvaluatorFactory)2 ScriptExpressionEvaluatorFactory (com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluatorFactory)2 ScriptExpressionFactory (com.evolveum.midpoint.model.common.expression.script.ScriptExpressionFactory)2 ValuePolicyProcessor (com.evolveum.midpoint.model.common.stringpolicy.ValuePolicyProcessor)2 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)2 Protector (com.evolveum.midpoint.prism.crypto.Protector)2 DirectoryFileObjectResolver (com.evolveum.midpoint.repo.common.DirectoryFileObjectResolver)2 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)2 AsIsExpressionEvaluatorFactory (com.evolveum.midpoint.repo.common.expression.evaluator.AsIsExpressionEvaluatorFactory)2