Search in sources :

Example 1 with FunctionLibraryType

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

the class FunctionExpressionEvaluator method getFunctionExpressionBean.

private ExpressionType getFunctionExpressionBean(ObjectReferenceType functionLibraryRef, ExpressionEvaluationContext context, OperationResult parentResult) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    Task task = context.getTask();
    OperationResult result = parentResult.createMinorSubresult(OP_GET_FUNCTION_TO_EXECUTE);
    try {
        FunctionLibraryType functionLibrary = objectResolver.resolve(functionLibraryRef, FunctionLibraryType.class, null, "resolving value policy reference in generateExpressionEvaluator", task, result);
        List<ExpressionType> allFunctions = functionLibrary.getFunction();
        if (CollectionUtils.isEmpty(allFunctions)) {
            throw new ObjectNotFoundException("No functions defined in referenced function library: " + functionLibrary + " used in " + context.getContextDescription());
        }
        String functionName = expressionEvaluatorBean.getName();
        if (StringUtils.isEmpty(functionName)) {
            throw new SchemaException("Missing function name in " + shortDebugDump() + " in " + context.getContextDescription());
        }
        List<ExpressionType> functionsMatchingName = allFunctions.stream().filter(expression -> functionName.equals(expression.getName())).collect(Collectors.toList());
        if (functionsMatchingName.isEmpty()) {
            String allFunctionNames = allFunctions.stream().map(ExpressionType::getName).collect(Collectors.joining(", "));
            throw new ObjectNotFoundException("No function with name " + functionName + " found in " + shortDebugDump() + ". Function defined are: " + allFunctionNames + ". In " + context.getContextDescription());
        }
        return selectFromMatchingFunctions(functionsMatchingName, context);
    } catch (Throwable t) {
        result.recordFatalError(t);
        throw t;
    } finally {
        result.computeStatusIfUnknown();
    }
}
Also used : PrismValue(com.evolveum.midpoint.prism.PrismValue) StringUtils(org.apache.commons.lang.StringUtils) 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) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) CollectionUtils(org.apache.commons.collections4.CollectionUtils) SchemaConstantsGenerated(com.evolveum.midpoint.schema.SchemaConstantsGenerated) PrismContext(com.evolveum.midpoint.prism.PrismContext) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) AbstractExpressionEvaluator(com.evolveum.midpoint.repo.common.expression.evaluator.AbstractExpressionEvaluator) MiscSchemaUtil(com.evolveum.midpoint.schema.util.MiscSchemaUtil) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) Expression(com.evolveum.midpoint.repo.common.expression.Expression) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ObjectResolver(com.evolveum.midpoint.repo.common.ObjectResolver) Task(com.evolveum.midpoint.task.api.Task) Collectors(java.util.stream.Collectors) MutablePrismPropertyDefinition(com.evolveum.midpoint.prism.MutablePrismPropertyDefinition) List(java.util.List) Protector(com.evolveum.midpoint.prism.crypto.Protector) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Example 2 with FunctionLibraryType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.FunctionLibraryType 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)

Aggregations

ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)2 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 ObjectResolver (com.evolveum.midpoint.repo.common.ObjectResolver)2 ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 MiscSchemaUtil (com.evolveum.midpoint.schema.util.MiscSchemaUtil)2 NotNull (org.jetbrains.annotations.NotNull)2 CacheInvalidationContext (com.evolveum.midpoint.CacheInvalidationContext)1 CustomFunctions (com.evolveum.midpoint.model.common.expression.functions.CustomFunctions)1 FunctionLibrary (com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary)1 MutablePrismPropertyDefinition (com.evolveum.midpoint.prism.MutablePrismPropertyDefinition)1 PrismValue (com.evolveum.midpoint.prism.PrismValue)1 Protector (com.evolveum.midpoint.prism.crypto.Protector)1 PrismValueDeltaSetTriple (com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple)1 Cache (com.evolveum.midpoint.repo.api.Cache)1 CacheRegistry (com.evolveum.midpoint.repo.api.CacheRegistry)1 RepositoryService (com.evolveum.midpoint.repo.api.RepositoryService)1 Expression (com.evolveum.midpoint.repo.common.expression.Expression)1 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)1 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)1