Search in sources :

Example 1 with ExtensionWalker

use of org.mule.runtime.api.meta.model.util.ExtensionWalker in project mule by mulesoft.

the class ConnectionProviderModelValidator method validate.

@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
    Set<ConnectionProviderModel> globalConnectionProviders = new HashSet<>();
    Multimap<ConfigurationModel, ConnectionProviderModel> configLevelConnectionProviders = HashMultimap.create();
    new ExtensionWalker() {

        @Override
        public void onConnectionProvider(HasConnectionProviderModels owner, ConnectionProviderModel model) {
            if (owner instanceof ConfigurationModel) {
                configLevelConnectionProviders.put((ConfigurationModel) owner, model);
            } else {
                globalConnectionProviders.add(model);
            }
            validateConfigOverrideParametersNotAllowed(model, problemsReporter, "Connection");
        }
    }.walk(extensionModel);
    validateGlobalConnectionTypes(extensionModel, globalConnectionProviders, problemsReporter);
    validateConfigLevelConnectionTypes(configLevelConnectionProviders, problemsReporter);
}
Also used : ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) IdempotentExtensionWalker(org.mule.runtime.api.meta.model.util.IdempotentExtensionWalker) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) HasConnectionProviderModels(org.mule.runtime.api.meta.model.connection.HasConnectionProviderModels) HashSet(java.util.HashSet)

Example 2 with ExtensionWalker

use of org.mule.runtime.api.meta.model.util.ExtensionWalker in project mule by mulesoft.

the class InjectedFieldsModelValidator method validate.

@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
    final Set<Class<?>> validatedTypes = new HashSet<>();
    // TODO - MULE-14401 - Make InjectedFieldsModelValidator work in AST Mode
    Boolean isASTMode = !extensionModel.getModelProperty(ExtensionTypeDescriptorModelProperty.class).map(mp -> mp.getType().getDeclaringClass().isPresent()).orElse(false);
    if (!isASTMode) {
        extensionModel.getModelProperty(ClassLoaderModelProperty.class).ifPresent(classLoaderModelProperty -> {
            new ExtensionWalker() {

                @Override
                protected void onSource(HasSourceModels owner, SourceModel model) {
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
                }

                @Override
                protected void onConfiguration(ConfigurationModel model) {
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), RefName.class);
                }

                @Override
                protected void onOperation(HasOperationModels owner, OperationModel model) {
                    validateArguments(model, model.getModelProperty(ExtensionOperationDescriptorModelProperty.class), DefaultEncoding.class);
                }

                @Override
                protected void onConnectionProvider(HasConnectionProviderModels owner, ConnectionProviderModel model) {
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), RefName.class);
                }

                @Override
                protected void onParameter(ParameterizedModel owner, ParameterGroupModel groupModel, ParameterModel model) {
                    if (model.getType().getMetadataFormat().equals(JAVA)) {
                        model.getType().accept(new MetadataTypeVisitor() {

                            @Override
                            public void visitObject(ObjectType objectType) {
                                if (!objectType.getAnnotation(InfrastructureTypeAnnotation.class).isPresent()) {
                                    try {
                                        Class<?> type = getType(objectType, classLoaderModelProperty.getClassLoader());
                                        if (validatedTypes.add(type)) {
                                            validateType(model, type, DefaultEncoding.class);
                                        }
                                    } catch (Exception e) {
                                        problemsReporter.addWarning(new Problem(model, "Could not validate Class: " + e.getMessage()));
                                    }
                                }
                            }
                        });
                    }
                }

                private void validateArguments(NamedObject model, Optional<ExtensionOperationDescriptorModelProperty> modelProperty, Class<? extends Annotation> annotationClass) {
                    modelProperty.ifPresent(operationDescriptorModelProperty -> {
                        MethodElement operation = operationDescriptorModelProperty.getOperationMethod();
                        int size = operation.getParametersAnnotatedWith(annotationClass).size();
                        if (size == 0) {
                            return;
                        } else if (size > 1) {
                            problemsReporter.addError(new Problem(model, format("Operation method '%s' has %d arguments annotated with @%s. Only one argument may carry that annotation", operation.getName(), size, annotationClass.getSimpleName())));
                        }
                        ExtensionParameter argument = operation.getParametersAnnotatedWith(annotationClass).get(0);
                        if (!argument.getType().isSameType(String.class)) {
                            problemsReporter.addError(new Problem(model, format("Operation method '%s' declares an argument '%s' which is annotated with @%s and is of type '%s'. Only " + "arguments of type String are allowed to carry such annotation", operation.getName(), argument.getName(), annotationClass.getSimpleName(), argument.getType().getName())));
                        }
                    });
                }

                private void validateFields(NamedObject model, Optional<ImplementingTypeModelProperty> modelProperty, Class<? extends Annotation> annotationClass) {
                    modelProperty.ifPresent(implementingTypeModelProperty -> {
                        validateType(model, implementingTypeModelProperty.getType(), annotationClass);
                    });
                }

                private void validateType(NamedObject model, Class<?> type, Class<? extends Annotation> annotationClass) {
                    List<Field> fields = getAnnotatedFields(type, annotationClass);
                    if (fields.isEmpty()) {
                        return;
                    } else if (fields.size() > 1) {
                        problemsReporter.addError(new Problem(model, format("Class '%s' has %d fields annotated with @%s. Only one field may carry that annotation", type.getName(), fields.size(), annotationClass.getSimpleName())));
                    }
                    Field field = fields.get(0);
                    if (!String.class.equals(field.getType())) {
                        problemsReporter.addError(new Problem(model, format("Class '%s' declares the field '%s' which is annotated with @%s and is of type '%s'. Only " + "fields of type String are allowed to carry such annotation", type.getName(), field.getName(), annotationClass.getSimpleName(), field.getType().getName())));
                    }
                }
            }.walk(extensionModel);
        });
    }
}
Also used : ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) IntrospectionUtils.getAnnotatedFields(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getAnnotatedFields) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) NamedObject(org.mule.runtime.api.meta.NamedObject) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) HashSet(java.util.HashSet) InfrastructureTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.InfrastructureTypeAnnotation) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) Problem(org.mule.runtime.extension.api.loader.Problem) HasConnectionProviderModels(org.mule.runtime.api.meta.model.connection.HasConnectionProviderModels) ExtensionModelValidator(org.mule.runtime.extension.api.loader.ExtensionModelValidator) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ProblemsReporter(org.mule.runtime.extension.api.loader.ProblemsReporter) Set(java.util.Set) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) ClassLoaderModelProperty(org.mule.runtime.extension.api.property.ClassLoaderModelProperty) Field(java.lang.reflect.Field) String.format(java.lang.String.format) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) DefaultEncoding(org.mule.runtime.extension.api.annotation.param.DefaultEncoding) List(java.util.List) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) RefName(org.mule.runtime.extension.api.annotation.param.RefName) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) ExtensionParameter(org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) ExtensionParameter(org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter) RefName(org.mule.runtime.extension.api.annotation.param.RefName) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ObjectType(org.mule.metadata.api.model.ObjectType) Field(java.lang.reflect.Field) List(java.util.List) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) HashSet(java.util.HashSet) ClassLoaderModelProperty(org.mule.runtime.extension.api.property.ClassLoaderModelProperty) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) NamedObject(org.mule.runtime.api.meta.NamedObject) HasConnectionProviderModels(org.mule.runtime.api.meta.model.connection.HasConnectionProviderModels) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) 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) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) DefaultEncoding(org.mule.runtime.extension.api.annotation.param.DefaultEncoding) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 3 with ExtensionWalker

use of org.mule.runtime.api.meta.model.util.ExtensionWalker in project mule by mulesoft.

the class DefaultExtensionsClient method findOperation.

private OperationModel findOperation(ExtensionModel extensionModel, String operationName) {
    Reference<OperationModel> operation = new Reference<>();
    ExtensionWalker walker = new IdempotentExtensionWalker() {

        @Override
        protected void onOperation(OperationModel operationModel) {
            if (operationName.equals(operationModel.getName())) {
                operation.set(operationModel);
                stop();
            }
        }
    };
    walker.walk(extensionModel);
    if (operation.get() == null) {
        throw new MuleRuntimeException(createStaticMessage("No Operation [" + operationName + "] Found"));
    }
    return operation.get();
}
Also used : Reference(org.mule.runtime.api.util.Reference) IdempotentExtensionWalker(org.mule.runtime.api.meta.model.util.IdempotentExtensionWalker) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IdempotentExtensionWalker(org.mule.runtime.api.meta.model.util.IdempotentExtensionWalker) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel)

Example 4 with ExtensionWalker

use of org.mule.runtime.api.meta.model.util.ExtensionWalker in project mule by mulesoft.

the class ConfigurationBasedElementModelFactory method createIdentifiedElement.

private DslElementModel createIdentifiedElement(ComponentConfiguration configuration) {
    final ComponentIdentifier identifier = configuration.getIdentifier();
    Optional<Map.Entry<ExtensionModel, DslSyntaxResolver>> entry = resolvers.entrySet().stream().filter(e -> e.getKey().getXmlDslModel().getPrefix().equals(identifier.getNamespace())).findFirst();
    if (!entry.isPresent()) {
        return null;
    }
    currentExtension = entry.get().getKey();
    dsl = entry.get().getValue();
    Reference<DslElementModel> elementModel = new Reference<>();
    new ExtensionWalker() {

        @Override
        protected void onConfiguration(ConfigurationModel model) {
            final DslElementSyntax elementDsl = dsl.resolve(model);
            getIdentifier(elementDsl).ifPresent(elementId -> {
                if (elementId.equals(identifier)) {
                    DslElementModel.Builder<ConfigurationModel> element = createElementModel(model, elementDsl, configuration);
                    addConnectionProvider(model, dsl, element, configuration);
                    elementModel.set(element.build());
                    stop();
                }
            });
        }

        @Override
        protected void onConstruct(HasConstructModels owner, ConstructModel model) {
            final DslElementSyntax elementDsl = dsl.resolve(model);
            getIdentifier(elementDsl).ifPresent(elementId -> {
                if (elementId.equals(identifier)) {
                    elementModel.set(createElementModel(model, elementDsl, configuration).build());
                    stop();
                }
            });
        }

        @Override
        protected void onOperation(HasOperationModels owner, OperationModel model) {
            final DslElementSyntax elementDsl = dsl.resolve(model);
            getIdentifier(elementDsl).ifPresent(elementId -> {
                if (elementId.equals(identifier)) {
                    elementModel.set(createElementModel(model, elementDsl, configuration).build());
                    stop();
                }
            });
        }

        @Override
        protected void onSource(HasSourceModels owner, SourceModel model) {
            final DslElementSyntax elementDsl = dsl.resolve(model);
            getIdentifier(elementDsl).ifPresent(elementId -> {
                if (elementId.equals(identifier)) {
                    elementModel.set(createElementModel(model, elementDsl, configuration).build());
                    stop();
                }
            });
        }
    }.walk(currentExtension);
    if (elementModel.get() == null) {
        resolveBasedOnTypes(configuration).ifPresent(elementModel::set);
    }
    return elementModel.get();
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ExtensionMetadataTypeUtils.getId(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getId) RECONNECT_FOREVER_ELEMENT_IDENTIFIER(org.mule.runtime.internal.dsl.DslConstants.RECONNECT_FOREVER_ELEMENT_IDENTIFIER) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) CORE_PREFIX(org.mule.runtime.internal.dsl.DslConstants.CORE_PREFIX) Optional.of(java.util.Optional.of) TLS_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TLS_PARAMETER_NAME) FlattenedTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.FlattenedTypeAnnotation) ComposableModel(org.mule.runtime.api.meta.model.ComposableModel) EXPIRATION_POLICY_ELEMENT_IDENTIFIER(org.mule.runtime.internal.dsl.DslConstants.EXPIRATION_POLICY_ELEMENT_IDENTIFIER) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) REDELIVERY_POLICY_ELEMENT_IDENTIFIER(org.mule.runtime.internal.dsl.DslConstants.REDELIVERY_POLICY_ELEMENT_IDENTIFIER) ArrayType(org.mule.metadata.api.model.ArrayType) KEY_ATTRIBUTE_NAME(org.mule.runtime.internal.dsl.DslConstants.KEY_ATTRIBUTE_NAME) Map(java.util.Map) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) REPEATABLE_IN_MEMORY_BYTES_STREAM_ALIAS(org.mule.runtime.extension.api.declaration.type.StreamingStrategyTypeBuilder.REPEATABLE_IN_MEMORY_BYTES_STREAM_ALIAS) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) REDELIVERY_POLICY_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.REDELIVERY_POLICY_PARAMETER_NAME) ConstructModel(org.mule.runtime.api.meta.model.construct.ConstructModel) DslElementModelFactory(org.mule.runtime.config.api.dsl.model.DslElementModelFactory) MetadataTypeUtils.getLocalPart(org.mule.metadata.api.utils.MetadataTypeUtils.getLocalPart) NestableElementModel(org.mule.runtime.api.meta.model.nested.NestableElementModel) ExtensionModelUtils.isContent(org.mule.runtime.extension.api.util.ExtensionModelUtils.isContent) RECONNECT_ELEMENT_IDENTIFIER(org.mule.runtime.internal.dsl.DslConstants.RECONNECT_ELEMENT_IDENTIFIER) ExtensionMetadataTypeUtils.isMap(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap) SCHEDULING_STRATEGY_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.SCHEDULING_STRATEGY_PARAMETER_NAME) ObjectType(org.mule.metadata.api.model.ObjectType) RECONNECTION_STRATEGY_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.RECONNECTION_STRATEGY_PARAMETER_NAME) Set(java.util.Set) ExtensionModelUtils.isText(org.mule.runtime.extension.api.util.ExtensionModelUtils.isText) InternalComponentConfiguration(org.mule.runtime.dsl.internal.component.config.InternalComponentConfiguration) POOLING_PROFILE_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.POOLING_PROFILE_PARAMETER_NAME) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) Objects(java.util.Objects) NestedRouteModel(org.mule.runtime.api.meta.model.nested.NestedRouteModel) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) NON_REPEATABLE_BYTE_STREAM_ALIAS(org.mule.runtime.extension.api.declaration.type.StreamingStrategyTypeBuilder.NON_REPEATABLE_BYTE_STREAM_ALIAS) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) DslElementSyntax(org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax) REPEATABLE_FILE_STORE_OBJECTS_STREAM_ALIAS(org.mule.runtime.extension.api.declaration.type.StreamingStrategyTypeBuilder.REPEATABLE_FILE_STORE_OBJECTS_STREAM_ALIAS) ExtensionModelUtils.isInfrastructure(org.mule.runtime.extension.api.util.ExtensionModelUtils.isInfrastructure) MetadataType(org.mule.metadata.api.model.MetadataType) DslSyntaxResolver(org.mule.runtime.extension.api.dsl.syntax.resolver.DslSyntaxResolver) Optional(java.util.Optional) EE_PREFIX(org.mule.runtime.internal.dsl.DslConstants.EE_PREFIX) ComponentIdentifier.builder(org.mule.runtime.api.component.ComponentIdentifier.builder) NON_REPEATABLE_OBJECTS_STREAM_ALIAS(org.mule.runtime.extension.api.declaration.type.StreamingStrategyTypeBuilder.NON_REPEATABLE_OBJECTS_STREAM_ALIAS) ExtensionModelUtils.isRequired(org.mule.runtime.extension.api.util.ExtensionModelUtils.isRequired) Optional.empty(java.util.Optional.empty) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) CronScheduler(org.mule.runtime.core.api.source.scheduler.CronScheduler) CRON_STRATEGY_ELEMENT_IDENTIFIER(org.mule.runtime.internal.dsl.DslConstants.CRON_STRATEGY_ELEMENT_IDENTIFIER) REPEATABLE_IN_MEMORY_OBJECTS_STREAM_ALIAS(org.mule.runtime.extension.api.declaration.type.StreamingStrategyTypeBuilder.REPEATABLE_IN_MEMORY_OBJECTS_STREAM_ALIAS) HasConstructModels(org.mule.runtime.api.meta.model.construct.HasConstructModels) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) REPEATABLE_FILE_STORE_BYTES_STREAM_ALIAS(org.mule.runtime.extension.api.declaration.type.StreamingStrategyTypeBuilder.REPEATABLE_FILE_STORE_BYTES_STREAM_ALIAS) VALUE_ATTRIBUTE_NAME(org.mule.runtime.internal.dsl.DslConstants.VALUE_ATTRIBUTE_NAME) ComponentConfiguration(org.mule.runtime.dsl.api.component.config.ComponentConfiguration) Multimap(com.google.common.collect.Multimap) Deque(java.util.Deque) EXPIRATION_POLICY_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.EXPIRATION_POLICY_PARAMETER_NAME) ExtensionModelUtils.getDefaultValue(org.mule.runtime.extension.api.util.ExtensionModelUtils.getDefaultValue) Stream.concat(java.util.stream.Stream.concat) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) Iterator(java.util.Iterator) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) RECONNECTION_CONFIG_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.RECONNECTION_CONFIG_PARAMETER_NAME) FixedFrequencyScheduler(org.mule.runtime.core.api.source.scheduler.FixedFrequencyScheduler) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) DslElementModel(org.mule.runtime.config.api.dsl.model.DslElementModel) Collectors.toList(java.util.stream.Collectors.toList) STREAMING_STRATEGY_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.STREAMING_STRATEGY_PARAMETER_NAME) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) Reference(org.mule.runtime.api.util.Reference) SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER(org.mule.runtime.internal.dsl.DslConstants.SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ArrayDeque(java.util.ArrayDeque) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) Reference(org.mule.runtime.api.util.Reference) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) ConstructModel(org.mule.runtime.api.meta.model.construct.ConstructModel) DslElementSyntax(org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax) HasConstructModels(org.mule.runtime.api.meta.model.construct.HasConstructModels) DslElementModel(org.mule.runtime.config.api.dsl.model.DslElementModel) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel)

Example 5 with ExtensionWalker

use of org.mule.runtime.api.meta.model.util.ExtensionWalker in project mule by mulesoft.

the class DeclarationBasedElementModelFactory method create.

public <T> Optional<DslElementModel<T>> create(ElementDeclaration declaration) {
    setupCurrentExtensionContext(declaration.getDeclaringExtension());
    final Function<NamedObject, Boolean> equalsName = (named) -> named.getName().equals(declaration.getName());
    if (declaration instanceof TopLevelParameterDeclaration) {
        return createFromType((TopLevelParameterDeclaration) declaration);
    }
    Reference<DslElementModel> elementModel = new Reference<>();
    new ExtensionWalker() {

        @Override
        protected void onConfiguration(ConfigurationModel model) {
            if (equalsName.apply(model) && declaration instanceof ConfigurationElementDeclaration) {
                elementModel.set(createConfigurationElement(model, (ConfigurationElementDeclaration) declaration));
                stop();
            }
        }

        @Override
        protected void onOperation(HasOperationModels owner, OperationModel model) {
            if (equalsName.apply(model) && declaration instanceof OperationElementDeclaration) {
                elementModel.set(createComponentElement(model, (OperationElementDeclaration) declaration));
                stop();
            }
        }

        @Override
        protected void onConstruct(HasConstructModels owner, ConstructModel model) {
            if (equalsName.apply(model) && declaration instanceof ConstructElementDeclaration) {
                elementModel.set(createComponentElement(model, (ConstructElementDeclaration) declaration));
                stop();
            }
        }

        @Override
        protected void onSource(HasSourceModels owner, SourceModel model) {
            if (equalsName.apply(model) && declaration instanceof SourceElementDeclaration) {
                elementModel.set(createComponentElement(model, (SourceElementDeclaration) declaration));
                stop();
            }
        }
    }.walk(currentExtension);
    if (LOGGER.isDebugEnabled() && elementModel.get() == null) {
        LOGGER.debug(format("No model found with name [%s] of type [%s] for extension [%s]", declaration.getName(), declaration.getClass().getName(), declaration.getDeclaringExtension()));
    }
    return Optional.ofNullable(elementModel.get());
}
Also used : ExtensionMetadataTypeUtils.getId(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getId) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) NamedObject(org.mule.runtime.api.meta.NamedObject) LoggerFactory(org.slf4j.LoggerFactory) ParameterizedElementDeclaration(org.mule.runtime.app.declaration.api.ParameterizedElementDeclaration) ComposableModel(org.mule.runtime.api.meta.model.ComposableModel) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) ArrayType(org.mule.metadata.api.model.ArrayType) KEY_ATTRIBUTE_NAME(org.mule.runtime.internal.dsl.DslConstants.KEY_ATTRIBUTE_NAME) Map(java.util.Map) DslResolvingContext(org.mule.runtime.api.dsl.DslResolvingContext) ParameterSimpleValue(org.mule.runtime.app.declaration.api.fluent.ParameterSimpleValue) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) ParameterValueVisitor(org.mule.runtime.app.declaration.api.ParameterValueVisitor) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ConstructModel(org.mule.runtime.api.meta.model.construct.ConstructModel) DslElementModelFactory(org.mule.runtime.config.api.dsl.model.DslElementModelFactory) ParameterElementDeclaration(org.mule.runtime.app.declaration.api.ParameterElementDeclaration) ExtensionModelUtils.isContent(org.mule.runtime.extension.api.util.ExtensionModelUtils.isContent) ExtensionMetadataTypeUtils.isMap(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap) ObjectType(org.mule.metadata.api.model.ObjectType) ElementDeclarer.newObjectValue(org.mule.runtime.app.declaration.api.fluent.ElementDeclarer.newObjectValue) InternalComponentConfiguration(org.mule.runtime.dsl.internal.component.config.InternalComponentConfiguration) String.format(java.lang.String.format) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) NAME_ATTRIBUTE_NAME(org.mule.runtime.internal.dsl.DslConstants.NAME_ATTRIBUTE_NAME) NestedRouteModel(org.mule.runtime.api.meta.model.nested.NestedRouteModel) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) ConstructElementDeclaration(org.mule.runtime.app.declaration.api.ConstructElementDeclaration) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) DslElementSyntax(org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax) ElementDeclaration(org.mule.runtime.app.declaration.api.ElementDeclaration) LayoutModel(org.mule.runtime.api.meta.model.display.LayoutModel) ConfigurationElementDeclaration(org.mule.runtime.app.declaration.api.ConfigurationElementDeclaration) ExtensionModelUtils.isInfrastructure(org.mule.runtime.extension.api.util.ExtensionModelUtils.isInfrastructure) MetadataType(org.mule.metadata.api.model.MetadataType) DslSyntaxResolver(org.mule.runtime.extension.api.dsl.syntax.resolver.DslSyntaxResolver) Optional(java.util.Optional) ComponentIdentifier.builder(org.mule.runtime.api.component.ComponentIdentifier.builder) ReferableElementDeclaration(org.mule.runtime.app.declaration.api.ReferableElementDeclaration) ExtensionMetadataTypeUtils.getAlias(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getAlias) ExtensionModelUtils.isRequired(org.mule.runtime.extension.api.util.ExtensionModelUtils.isRequired) Optional.empty(java.util.Optional.empty) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) SourceElementDeclaration(org.mule.runtime.app.declaration.api.SourceElementDeclaration) CONFIG_ATTRIBUTE_NAME(org.mule.runtime.internal.dsl.DslConstants.CONFIG_ATTRIBUTE_NAME) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) HasConstructModels(org.mule.runtime.api.meta.model.construct.HasConstructModels) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) RouteElementDeclaration(org.mule.runtime.app.declaration.api.RouteElementDeclaration) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) VALUE_ATTRIBUTE_NAME(org.mule.runtime.internal.dsl.DslConstants.VALUE_ATTRIBUTE_NAME) ComponentConfiguration(org.mule.runtime.dsl.api.component.config.ComponentConfiguration) Function(java.util.function.Function) ExtensionMetadataTypeUtils.isFlattenedParameterGroup(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isFlattenedParameterGroup) ParameterListValue(org.mule.runtime.app.declaration.api.fluent.ParameterListValue) DslElementSyntaxBuilder(org.mule.runtime.extension.api.dsl.syntax.DslElementSyntaxBuilder) ExtensionModelUtils.getDefaultValue(org.mule.runtime.extension.api.util.ExtensionModelUtils.getDefaultValue) ParameterObjectValue(org.mule.runtime.app.declaration.api.fluent.ParameterObjectValue) Stream.concat(java.util.stream.Stream.concat) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) Logger(org.slf4j.Logger) ParameterValue(org.mule.runtime.app.declaration.api.ParameterValue) Stream.of(java.util.stream.Stream.of) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) ConnectionElementDeclaration(org.mule.runtime.app.declaration.api.ConnectionElementDeclaration) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) DslElementModel(org.mule.runtime.config.api.dsl.model.DslElementModel) Collectors.toList(java.util.stream.Collectors.toList) TopLevelParameterDeclaration(org.mule.runtime.app.declaration.api.TopLevelParameterDeclaration) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) Reference(org.mule.runtime.api.util.Reference) OperationElementDeclaration(org.mule.runtime.app.declaration.api.OperationElementDeclaration) IS_CDATA(org.mule.runtime.config.internal.dsl.processor.xml.XmlCustomAttributeHandler.IS_CDATA) ComponentElementDeclaration(org.mule.runtime.app.declaration.api.ComponentElementDeclaration) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ParameterGroupElementDeclaration(org.mule.runtime.app.declaration.api.ParameterGroupElementDeclaration) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) Reference(org.mule.runtime.api.util.Reference) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) NamedObject(org.mule.runtime.api.meta.NamedObject) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) ConstructModel(org.mule.runtime.api.meta.model.construct.ConstructModel) ConstructElementDeclaration(org.mule.runtime.app.declaration.api.ConstructElementDeclaration) ConfigurationElementDeclaration(org.mule.runtime.app.declaration.api.ConfigurationElementDeclaration) HasConstructModels(org.mule.runtime.api.meta.model.construct.HasConstructModels) SourceElementDeclaration(org.mule.runtime.app.declaration.api.SourceElementDeclaration) OperationElementDeclaration(org.mule.runtime.app.declaration.api.OperationElementDeclaration) DslElementModel(org.mule.runtime.config.api.dsl.model.DslElementModel) TopLevelParameterDeclaration(org.mule.runtime.app.declaration.api.TopLevelParameterDeclaration) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel)

Aggregations

ExtensionWalker (org.mule.runtime.api.meta.model.util.ExtensionWalker)12 OperationModel (org.mule.runtime.api.meta.model.operation.OperationModel)8 ParameterGroupModel (org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)7 ParameterizedModel (org.mule.runtime.api.meta.model.parameter.ParameterizedModel)7 HasOperationModels (org.mule.runtime.api.meta.model.operation.HasOperationModels)6 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)6 HasSourceModels (org.mule.runtime.api.meta.model.source.HasSourceModels)6 SourceModel (org.mule.runtime.api.meta.model.source.SourceModel)6 ObjectType (org.mule.metadata.api.model.ObjectType)5 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)5 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)5 ConstructModel (org.mule.runtime.api.meta.model.construct.ConstructModel)5 DslSyntaxResolver (org.mule.runtime.extension.api.dsl.syntax.resolver.DslSyntaxResolver)5 List (java.util.List)4 Optional (java.util.Optional)4 ArrayType (org.mule.metadata.api.model.ArrayType)4 MetadataType (org.mule.metadata.api.model.MetadataType)4 ComponentModel (org.mule.runtime.api.meta.model.ComponentModel)4 ConfigurationModel (org.mule.runtime.api.meta.model.config.ConfigurationModel)4 String.format (java.lang.String.format)3