Search in sources :

Example 1 with FunctionModel

use of org.mule.runtime.api.meta.model.function.FunctionModel in project mule by mulesoft.

the class NameClashModelValidatorTestCase method differentFunctionNames.

@Test
public void differentFunctionNames() {
    FunctionModel anotherFunctionModel = mock(FunctionModel.class);
    when(anotherFunctionModel.getName()).thenReturn("another-" + FUNCTION_NAME);
    when(anotherFunctionModel.getAllParameterModels()).thenReturn(emptyList());
    when(extensionModel.getFunctionModels()).thenReturn(asList(functionModel, anotherFunctionModel));
    validate();
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 2 with FunctionModel

use of org.mule.runtime.api.meta.model.function.FunctionModel in project mule by mulesoft.

the class ReflectiveFunctionExecutorFactory method createExecutor.

@Override
public FunctionExecutor createExecutor(FunctionModel functionModel, FunctionParameterDefaultValueResolverFactory defaultResolverFactory) {
    DataType returnType = fromType(getType(functionModel.getOutput().getType()).orElseThrow(() -> new MuleRuntimeException(createStaticMessage(format("Failed to obtain the return type for function [%s]", functionModel.getName())))));
    List<FunctionParameter> functionParameters = functionModel.getAllParameterModels().stream().map(p -> {
        MetadataType paramType = p.getType();
        DataType type = isTypedValue(paramType) ? fromType(TypedValue.class) : toDataType(paramType);
        if (p.isRequired()) {
            return new FunctionParameter(p.getName(), type);
        }
        Object defaultValue = p.getDefaultValue();
        if (defaultValue == null) {
            return new FunctionParameter(p.getName(), type, context -> Defaults.defaultValue(type.getType()));
        }
        return new FunctionParameter(p.getName(), type, defaultResolverFactory.create(defaultValue, type));
    }).collect(toList());
    return new ReflectiveExpressionFunctionExecutor(functionModel, returnType, functionParameters, method, getDelegateInstance());
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) DataType(org.mule.runtime.api.metadata.DataType) Defaults(com.google.common.base.Defaults) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) ReflectiveMethodOperationExecutor(org.mule.runtime.module.extension.internal.runtime.operation.ReflectiveMethodOperationExecutor) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) String.format(java.lang.String.format) TypedValue(org.mule.runtime.api.metadata.TypedValue) Collectors.toList(java.util.stream.Collectors.toList) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) List(java.util.List) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) MetadataType(org.mule.metadata.api.model.MetadataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) Method(java.lang.reflect.Method) ExtensionMetadataTypeUtils.isTypedValue(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isTypedValue) ComponentExecutorFactory(org.mule.runtime.extension.api.runtime.operation.ComponentExecutorFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MetadataType(org.mule.metadata.api.model.MetadataType) DataType(org.mule.runtime.api.metadata.DataType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter)

Example 3 with FunctionModel

use of org.mule.runtime.api.meta.model.function.FunctionModel in project mule by mulesoft.

the class ExtensionActivator method registerExpressionFunctions.

private void registerExpressionFunctions(Stream<FunctionModel> functions, ExpressionModule.Builder module) {
    final FunctionParameterDefaultValueResolverFactory valueResolverFactory = (defaultValue, type) -> context -> {
        ExtendedExpressionManager em = muleContext.getExpressionManager();
        String value = String.valueOf(defaultValue);
        return em.isExpression(value) ? em.evaluate(value, type, context) : new TypedValue<>(defaultValue, type);
    };
    functions.forEach(function -> function.getModelProperty(FunctionExecutorModelProperty.class).ifPresent(mp -> {
        FunctionExecutor executor = mp.getExecutorFactory().createExecutor(function, valueResolverFactory);
        lifecycleAwareElements.add(executor);
        module.addBinding(function.getName(), new TypedValue<>(executor, fromFunction(executor)));
    }));
}
Also used : IntrospectionUtils.getSubtypeClasses(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getSubtypeClasses) FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) IntrospectionUtils.getParameterClasses(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getParameterClasses) FunctionExecutor(org.mule.runtime.module.extension.internal.runtime.function.FunctionExecutor) MuleExtensionUtils.getClassLoader(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils.getClassLoader) BindingContext(org.mule.runtime.api.el.BindingContext) StringToEnum(org.mule.runtime.core.internal.transformer.simple.StringToEnum) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) DefaultExpressionModuleBuilder(org.mule.runtime.core.internal.el.DefaultExpressionModuleBuilder) HashSet(java.util.HashSet) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) FunctionExecutorModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.FunctionExecutorModelProperty) BeanUtils.getName(org.mule.runtime.core.privileged.util.BeanUtils.getName) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) LinkedList(java.util.LinkedList) Startable(org.mule.runtime.api.lifecycle.Startable) FunctionParameterDefaultValueResolverFactory(org.mule.runtime.module.extension.internal.runtime.function.FunctionParameterDefaultValueResolverFactory) DataType.fromFunction(org.mule.runtime.api.metadata.DataType.fromFunction) DefaultBindingContextBuilder(org.mule.runtime.core.internal.el.DefaultBindingContextBuilder) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Set(java.util.Set) LifecycleUtils.startIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.startIfNeeded) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Transformer(org.mule.runtime.core.api.transformer.Transformer) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) TypedValue(org.mule.runtime.api.metadata.TypedValue) List(java.util.List) Stream(java.util.stream.Stream) ExpressionModule(org.mule.runtime.api.el.ExpressionModule) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) ModuleNamespace(org.mule.runtime.api.el.ModuleNamespace) ExtendedExpressionManager(org.mule.runtime.core.api.el.ExtendedExpressionManager) GlobalBindingContextProvider(org.mule.runtime.core.privileged.el.GlobalBindingContextProvider) FunctionParameterDefaultValueResolverFactory(org.mule.runtime.module.extension.internal.runtime.function.FunctionParameterDefaultValueResolverFactory) FunctionExecutor(org.mule.runtime.module.extension.internal.runtime.function.FunctionExecutor) ExtendedExpressionManager(org.mule.runtime.core.api.el.ExtendedExpressionManager) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 4 with FunctionModel

use of org.mule.runtime.api.meta.model.function.FunctionModel in project mule by mulesoft.

the class NameClashModelValidatorTestCase method repeatedFunctionNames.

@Test
public void repeatedFunctionNames() {
    exception.expect(IllegalModelDefinitionException.class);
    FunctionModel anotherFunctionModel = mock(FunctionModel.class);
    when(anotherFunctionModel.getName()).thenReturn(FUNCTION_NAME);
    when(anotherFunctionModel.getAllParameterModels()).thenReturn(emptyList());
    when(extensionModel.getFunctionModels()).thenReturn(asList(functionModel, anotherFunctionModel));
    validate();
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

FunctionModel (org.mule.runtime.api.meta.model.function.FunctionModel)4 List (java.util.List)2 Test (org.junit.Test)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)2 TypedValue (org.mule.runtime.api.metadata.TypedValue)2 Defaults (com.google.common.base.Defaults)1 String.format (java.lang.String.format)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 BindingContext (org.mule.runtime.api.el.BindingContext)1 ExpressionModule (org.mule.runtime.api.el.ExpressionModule)1 ModuleNamespace (org.mule.runtime.api.el.ModuleNamespace)1 MuleException (org.mule.runtime.api.exception.MuleException)1 Startable (org.mule.runtime.api.lifecycle.Startable)1