Search in sources :

Example 11 with ParameterGroupModel

use of org.mule.runtime.api.meta.model.parameter.ParameterGroupModel in project mule by mulesoft.

the class ExtensionsTestUtils method mockParameters.

public static ParameterGroupModel mockParameters(ParameterizedModel parameterizedModel, String groupName, ParameterModel... parameterModels) {
    ParameterGroupModel group = mock(ParameterGroupModel.class);
    when(group.getName()).thenReturn(groupName);
    when(group.getModelProperty(ParameterGroupModelProperty.class)).thenReturn(empty());
    when(parameterizedModel.getParameterGroupModels()).thenReturn(asList(group));
    when(group.getParameterModels()).thenReturn(asList(parameterModels));
    when(parameterizedModel.getAllParameterModels()).thenReturn(asList(parameterModels));
    return group;
}
Also used : ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)

Example 12 with ParameterGroupModel

use of org.mule.runtime.api.meta.model.parameter.ParameterGroupModel in project mule by mulesoft.

the class ConfigurationBasedElementModelFactory method addInlineGroup.

private void addInlineGroup(DslElementSyntax elementDsl, Multimap<ComponentIdentifier, ComponentConfiguration> innerComponents, Map<String, String> parameters, DslElementModel.Builder parent, ParameterGroupModel group) {
    elementDsl.getChild(group.getName()).ifPresent(groupDsl -> {
        Optional<ComponentIdentifier> identifier = getIdentifier(groupDsl);
        if (!identifier.isPresent()) {
            return;
        }
        ComponentConfiguration groupComponent = getSingleComponentConfiguration(innerComponents, identifier);
        if (groupComponent != null) {
            DslElementModel.Builder<ParameterGroupModel> groupElementBuilder = DslElementModel.<ParameterGroupModel>builder().withModel(group).withDsl(groupDsl).withConfig(groupComponent);
            Multimap<ComponentIdentifier, ComponentConfiguration> groupInnerComponents = getNestedComponents(groupComponent);
            group.getParameterModels().forEach(p -> addElementParameter(groupInnerComponents, parameters, groupDsl, groupElementBuilder, p));
            parent.containing(groupElementBuilder.build());
        } else if (shoulBuildDefaultGroup(group)) {
            builDefaultInlineGroupElement(parent, group, groupDsl, identifier.get());
        }
    });
}
Also used : InternalComponentConfiguration(org.mule.runtime.dsl.internal.component.config.InternalComponentConfiguration) ComponentConfiguration(org.mule.runtime.dsl.api.component.config.ComponentConfiguration) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) DslElementModel(org.mule.runtime.config.api.dsl.model.DslElementModel)

Example 13 with ParameterGroupModel

use of org.mule.runtime.api.meta.model.parameter.ParameterGroupModel in project mule by mulesoft.

the class AbstractComponentDefinitionParser method doParse.

@Override
protected Builder doParse(Builder definitionBuilder) throws ConfigurationException {
    Builder finalBuilder = definitionBuilder.withIdentifier(operationDsl.getElementName()).withTypeDefinition(fromType(getMessageProcessorType())).withObjectFactoryType(getMessageProcessorFactoryType()).withConstructorParameterDefinition(fromFixedValue(extensionModel).build()).withConstructorParameterDefinition(fromFixedValue(componentModel).build()).withConstructorParameterDefinition(fromReferenceObject(MuleContext.class).build()).withConstructorParameterDefinition(fromReferenceObject(Registry.class).build()).withConstructorParameterDefinition(fromReferenceObject(PolicyManager.class).build()).withSetterParameterDefinition(TARGET_PARAMETER_NAME, fromSimpleParameter(TARGET_PARAMETER_NAME).build()).withSetterParameterDefinition(TARGET_VALUE_PARAMETER_NAME, fromSimpleParameter(TARGET_VALUE_PARAMETER_NAME).build()).withSetterParameterDefinition(CONFIG_PROVIDER_ATTRIBUTE_NAME, fromSimpleReferenceParameter(CONFIG_ATTRIBUTE_NAME).build()).withSetterParameterDefinition(CURSOR_PROVIDER_FACTORY_FIELD_NAME, fromChildConfiguration(CursorProviderFactory.class).build()).withSetterParameterDefinition("retryPolicyTemplate", fromChildConfiguration(RetryPolicyTemplate.class).build());
    Optional<? extends NestableElementModel> nestedChain = componentModel.getNestedComponents().stream().filter(c -> c instanceof NestedChainModel).findFirst();
    if (nestedChain.isPresent()) {
        // TODO MULE-13483: improve parsers to support things like [source, chainOfProcessors, errorHandler]
        // or [chainOfProcessors, errorHandler]
        finalBuilder = finalBuilder.withSetterParameterDefinition("nestedProcessors", fromChildCollectionConfiguration(Processor.class).build());
        parseParameters(componentModel.getAllParameterModels());
    } else {
        List<ParameterGroupModel> inlineGroups = getInlineGroups(componentModel);
        parseParameters(getFlatParameters(inlineGroups, componentModel.getAllParameterModels()));
        for (ParameterGroupModel group : inlineGroups) {
            parseInlineParameterGroup(group);
        }
        // TODO MULE-13483
        parseNestedComponents(componentModel.getNestedComponents());
    }
    return finalBuilder;
}
Also used : ExtensionDefinitionParser(org.mule.runtime.module.extension.internal.config.dsl.ExtensionDefinitionParser) Builder.fromReferenceObject(org.mule.runtime.dsl.api.component.AttributeDefinition.Builder.fromReferenceObject) RetryPolicyTemplate(org.mule.runtime.core.api.retry.policy.RetryPolicyTemplate) CONFIG_ATTRIBUTE_NAME(org.mule.runtime.internal.dsl.DslConstants.CONFIG_ATTRIBUTE_NAME) ExtensionParsingContext(org.mule.runtime.module.extension.internal.config.dsl.ExtensionParsingContext) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) Builder.fromChildConfiguration(org.mule.runtime.dsl.api.component.AttributeDefinition.Builder.fromChildConfiguration) Builder.fromSimpleReferenceParameter(org.mule.runtime.dsl.api.component.AttributeDefinition.Builder.fromSimpleReferenceParameter) Processor(org.mule.runtime.core.api.processor.Processor) Builder.fromFixedValue(org.mule.runtime.dsl.api.component.AttributeDefinition.Builder.fromFixedValue) Builder.fromSimpleParameter(org.mule.runtime.dsl.api.component.AttributeDefinition.Builder.fromSimpleParameter) OperationMessageProcessorObjectFactory(org.mule.runtime.module.extension.internal.config.dsl.operation.OperationMessageProcessorObjectFactory) TARGET_VALUE_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TARGET_VALUE_PARAMETER_NAME) MuleContext(org.mule.runtime.core.api.MuleContext) TARGET_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TARGET_PARAMETER_NAME) Registry(org.mule.runtime.api.artifact.Registry) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) Builder.fromChildCollectionConfiguration(org.mule.runtime.dsl.api.component.AttributeDefinition.Builder.fromChildCollectionConfiguration) TypeDefinition.fromType(org.mule.runtime.dsl.api.component.TypeDefinition.fromType) NestableElementModel(org.mule.runtime.api.meta.model.nested.NestableElementModel) ComponentMessageProcessorObjectFactory(org.mule.runtime.module.extension.internal.config.dsl.ComponentMessageProcessorObjectFactory) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) NestedChainModel(org.mule.runtime.api.meta.model.nested.NestedChainModel) OperationMessageProcessor(org.mule.runtime.module.extension.internal.runtime.operation.OperationMessageProcessor) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) List(java.util.List) DslElementSyntax(org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax) ComponentMessageProcessor(org.mule.runtime.module.extension.internal.runtime.operation.ComponentMessageProcessor) DslSyntaxResolver(org.mule.runtime.extension.api.dsl.syntax.resolver.DslSyntaxResolver) Optional(java.util.Optional) CursorProviderFactory(org.mule.runtime.core.api.streaming.CursorProviderFactory) Builder(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition.Builder) MuleContext(org.mule.runtime.core.api.MuleContext) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) CursorProviderFactory(org.mule.runtime.core.api.streaming.CursorProviderFactory) Processor(org.mule.runtime.core.api.processor.Processor) OperationMessageProcessor(org.mule.runtime.module.extension.internal.runtime.operation.OperationMessageProcessor) ComponentMessageProcessor(org.mule.runtime.module.extension.internal.runtime.operation.ComponentMessageProcessor) NestedChainModel(org.mule.runtime.api.meta.model.nested.NestedChainModel) Builder(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition.Builder) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)

Example 14 with ParameterGroupModel

use of org.mule.runtime.api.meta.model.parameter.ParameterGroupModel in project mule by mulesoft.

the class ExtensionDefinitionParser method parseParameters.

protected void parseParameters(ParameterizedModel parameterizedModel) throws ConfigurationException {
    List<ParameterGroupModel> inlineGroups = getInlineGroups(parameterizedModel);
    parseParameters(getFlatParameters(inlineGroups, parameterizedModel.getAllParameterModels()));
    for (ParameterGroupModel group : inlineGroups) {
        parseInlineParameterGroup(group);
    }
}
Also used : ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)

Example 15 with ParameterGroupModel

use of org.mule.runtime.api.meta.model.parameter.ParameterGroupModel in project mule by mulesoft.

the class NullSafeModelValidator method validate.

@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
    ReflectionCache reflectionCache = new ReflectionCache();
    TypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
    new ExtensionWalker() {

        @Override
        public void onParameter(ParameterizedModel owner, ParameterGroupModel groupModel, ParameterModel model) {
            model.getType().accept(new MetadataTypeVisitor() {

                @Override
                public void visitObject(ObjectType objectType) {
                    if (objectType.getMetadataFormat().equals(JAVA) && !isMap(objectType)) {
                        objectType.getAnnotation(TypeIdAnnotation.class).map(TypeIdAnnotation::getValue).ifPresent(typeId -> typeLoader.load(typeId).ifPresent(fieldMetadataType -> objectType.getFields().stream().filter(f -> f.getAnnotation(NullSafeTypeAnnotation.class).isPresent()).forEach(f -> validateField(getLocalPart(f), f, getType(fieldMetadataType), f.getAnnotation(NullSafeTypeAnnotation.class).get()))));
                    }
                }

                private void validateField(String fieldName, ObjectFieldType field, Class<?> declaringClass, NullSafeTypeAnnotation nullSafeTypeAnnotation) {
                    Class<?> nullSafeType = nullSafeTypeAnnotation.getType();
                    Class<?> fieldType = getType(field.getValue());
                    boolean hasDefaultOverride = nullSafeTypeAnnotation.hasDefaultOverride();
                    field.getValue().accept(new BasicTypeMetadataVisitor() {

                        @Override
                        protected void visitBasicType(MetadataType metadataType) {
                            problemsReporter.addError(new Problem(extensionModel, format("Field '%s' in class '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex types (Pojos, Lists, Maps)", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                        }

                        @Override
                        public void visitArrayType(ArrayType arrayType) {
                            if (hasDefaultOverride) {
                                problemsReporter.addError(new Problem(extensionModel, format("Field '%s' in class '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Collections", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                            }
                        }

                        @Override
                        public void visitObject(ObjectType objectType) {
                            String requiredFields = objectType.getFields().stream().filter(f -> f.isRequired() && !isFlattenedParameterGroup(f)).map(MetadataTypeUtils::getLocalPart).collect(joining(", "));
                            if (!isBlank(requiredFields) && isCompiletime(extensionModel)) {
                                problemsReporter.addError(new Problem(model, format("Class '%s' cannot be used with '@%s' parameter since it contains non optional fields: [%s]", getId(objectType).orElse(""), NullSafe.class.getSimpleName(), requiredFields)));
                            }
                            if (objectType.isOpen()) {
                                if (hasDefaultOverride) {
                                    problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Maps", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                                }
                                return;
                            }
                            if (hasDefaultOverride && isInstantiable(fieldType, reflectionCache)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' is of concrete type '%s'," + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for concrete types", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                            }
                            if (!isInstantiable(nullSafeType, reflectionCache)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex instantiable types (Pojos, Lists, Maps)", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), nullSafeType.getName())));
                            }
                            if (hasDefaultOverride && !fieldType.isAssignableFrom(nullSafeType)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' of type '%s', but provided type '%s" + " is not a subtype of the parameter's type", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName(), nullSafeType.getName())));
                            }
                        }
                    });
                }
            });
        }
    }.walk(extensionModel);
}
Also used : ExtensionMetadataTypeUtils.getId(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getId) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) MetadataTypeUtils(org.mule.metadata.api.utils.MetadataTypeUtils) ExtensionMetadataTypeUtils.isFlattenedParameterGroup(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isFlattenedParameterGroup) ModelValidationUtils.isCompiletime(org.mule.runtime.module.extension.internal.loader.validation.ModelValidationUtils.isCompiletime) ArrayType(org.mule.metadata.api.model.ArrayType) BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) IntrospectionUtils.isInstantiable(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.isInstantiable) Problem(org.mule.runtime.extension.api.loader.Problem) MetadataTypeUtils.getLocalPart(org.mule.metadata.api.utils.MetadataTypeUtils.getLocalPart) TypeIdAnnotation(org.mule.metadata.api.annotation.TypeIdAnnotation) ExtensionModelValidator(org.mule.runtime.extension.api.loader.ExtensionModelValidator) ExtensionMetadataTypeUtils.isMap(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ProblemsReporter(org.mule.runtime.extension.api.loader.ProblemsReporter) NullSafeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.NullSafeTypeAnnotation) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) TypeLoader(org.mule.metadata.api.TypeLoader) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) NullSafe(org.mule.runtime.extension.api.annotation.param.NullSafe) MetadataType(org.mule.metadata.api.model.MetadataType) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) MetadataType(org.mule.metadata.api.model.MetadataType) TypeLoader(org.mule.metadata.api.TypeLoader) NullSafeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.NullSafeTypeAnnotation) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) MetadataTypeUtils(org.mule.metadata.api.utils.MetadataTypeUtils) BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) Problem(org.mule.runtime.extension.api.loader.Problem) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType)

Aggregations

ParameterGroupModel (org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)24 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)11 ParameterizedModel (org.mule.runtime.api.meta.model.parameter.ParameterizedModel)6 ExtensionWalker (org.mule.runtime.api.meta.model.util.ExtensionWalker)5 List (java.util.List)4 Optional (java.util.Optional)4 MetadataType (org.mule.metadata.api.model.MetadataType)4 ObjectType (org.mule.metadata.api.model.ObjectType)4 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)4 String.format (java.lang.String.format)3 Field (java.lang.reflect.Field)3 Set (java.util.Set)3 Test (org.junit.Test)3 MetadataTypeUtils.getLocalPart (org.mule.metadata.api.utils.MetadataTypeUtils.getLocalPart)3 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)3 JavaTypeUtils.getType (org.mule.metadata.java.api.utils.JavaTypeUtils.getType)3 NestableElementModel (org.mule.runtime.api.meta.model.nested.NestableElementModel)3 SourceModel (org.mule.runtime.api.meta.model.source.SourceModel)3 DslElementModel (org.mule.runtime.config.api.dsl.model.DslElementModel)3 MuleContext (org.mule.runtime.core.api.MuleContext)3