Search in sources :

Example 1 with Problem

use of org.mule.runtime.extension.api.loader.Problem in project mule by mulesoft.

the class ExportedPackagesValidator method validate.

@Override
public void validate(ExtensionModel model, ProblemsReporter problemsReporter) {
    if (shouldValidate()) {
        ExportedArtifactsCollector exportedArtifactsCollector = getExportedArtifactsCollector(model);
        Map<String, Collection<String>> exportedPackages = exportedArtifactsCollector.getDetailedExportedPackages();
        Map<String, Collection<String>> internalPackages = new HashMap<>();
        Map<String, Collection<String>> noVisibilityDeclaredPackages = new HashMap<>();
        exportedPackages.forEach((packageName, classes) -> {
            if (packageName.contains(".api.") || packageName.endsWith(".api")) {
            // valid package
            } else if (packageName.contains(".internal.") || packageName.endsWith(".internal")) {
                internalPackages.put(packageName, classes);
            } else {
                noVisibilityDeclaredPackages.put(packageName, classes);
            }
        });
        if (!internalPackages.isEmpty()) {
            Problem problem = new Problem(model, getErrorMessage(model, internalPackages, "exports the following internal packages"));
            if (strictValidation()) {
                problemsReporter.addError(problem);
            } else {
                problemsReporter.addWarning(problem);
            }
        }
        if (!noVisibilityDeclaredPackages.isEmpty()) {
            problemsReporter.addWarning(new Problem(model, getErrorMessage(model, noVisibilityDeclaredPackages, "exports packages which doesn't have a defined visibility, 'api' or 'internal'")));
        }
    }
}
Also used : ExportedArtifactsCollector(org.mule.runtime.module.extension.internal.resources.manifest.ExportedArtifactsCollector) HashMap(java.util.HashMap) Collection(java.util.Collection) Problem(org.mule.runtime.extension.api.loader.Problem)

Example 2 with Problem

use of org.mule.runtime.extension.api.loader.Problem 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 Problem

use of org.mule.runtime.extension.api.loader.Problem in project mule by mulesoft.

the class MetadataComponentModelValidator method validateResolversName.

private void validateResolversName(ComponentModel model, MetadataResolverFactory resolverFactory, Table<String, String, Class<?>> names, ProblemsReporter problemsReporter) {
    List<NamedTypeResolver> resolvers = new LinkedList<>();
    resolvers.addAll(getAllInputResolvers(model, resolverFactory));
    resolvers.add(resolverFactory.getOutputResolver());
    resolvers.stream().filter(r -> !r.getClass().equals(NullMetadataResolver.class)).forEach(r -> {
        if (isBlank(r.getResolverName())) {
            problemsReporter.addError(new Problem(model, format(EMPTY_RESOLVER_NAME, getComponentModelTypeName(model), model.getName(), r.getClass().getSimpleName(), "resolver")));
        } else {
            if (names.get(r.getCategoryName(), r.getResolverName()) != null && names.get(r.getCategoryName(), r.getResolverName()) != r.getClass()) {
                problemsReporter.addError(new Problem(model, format("%s [%s] specifies metadata resolvers with repeated name [%s] for the same category [%s]. Resolver names should be unique for a given category. Affected resolvers are '%s' and '%s'", getComponentModelTypeName(model), model.getName(), r.getResolverName(), r.getCategoryName(), names.get(r.getCategoryName(), r.getResolverName()).getSimpleName(), r.getClass().getSimpleName())));
            }
            names.put(r.getCategoryName(), r.getResolverName(), r.getClass());
        }
    });
}
Also used : OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) Message(org.mule.runtime.api.message.Message) NamedObject(org.mule.runtime.api.meta.NamedObject) HashBasedTable(com.google.common.collect.HashBasedTable) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) ModelLoaderUtils.isScope(org.mule.runtime.module.extension.internal.loader.utils.ModelLoaderUtils.isScope) ArrayType(org.mule.metadata.api.model.ArrayType) MetadataKeyPartModelProperty(org.mule.runtime.extension.api.property.MetadataKeyPartModelProperty) Map(java.util.Map) StringUtils(org.mule.runtime.core.api.util.StringUtils) Collectors.toSet(java.util.stream.Collectors.toSet) ConnectableComponentModel(org.mule.runtime.api.meta.model.ConnectableComponentModel) ExtensionMetadataTypeUtils.isMap(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap) ObjectType(org.mule.metadata.api.model.ObjectType) OutputTypeResolver(org.mule.runtime.api.metadata.resolving.OutputTypeResolver) ProblemsReporter(org.mule.runtime.extension.api.loader.ProblemsReporter) InputTypeResolver(org.mule.runtime.api.metadata.resolving.InputTypeResolver) Set(java.util.Set) String.format(java.lang.String.format) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) MuleExtensionUtils(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils) Serializable(java.io.Serializable) List(java.util.List) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) NameUtils.getComponentModelTypeName(org.mule.runtime.extension.api.util.NameUtils.getComponentModelTypeName) MetadataType(org.mule.metadata.api.model.MetadataType) Optional(java.util.Optional) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) CustomDefinedStaticTypeAnnotation(org.mule.runtime.module.extension.internal.loader.annotations.CustomDefinedStaticTypeAnnotation) StringUtils.join(org.apache.commons.lang3.StringUtils.join) NullMetadataResolver(org.mule.runtime.extension.api.metadata.NullMetadataResolver) Problem(org.mule.runtime.extension.api.loader.Problem) LinkedList(java.util.LinkedList) MetadataResolverUtils.isNullResolver(org.mule.runtime.extension.api.metadata.MetadataResolverUtils.isNullResolver) ExtensionModelValidator(org.mule.runtime.extension.api.loader.ExtensionModelValidator) MetadataTypeUtils.isVoid(org.mule.metadata.api.utils.MetadataTypeUtils.isVoid) MetadataResolverUtils.getAllResolvers(org.mule.runtime.extension.api.metadata.MetadataResolverUtils.getAllResolvers) MetadataTypeUtils.isCollection(org.mule.metadata.api.utils.MetadataTypeUtils.isCollection) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ModelLoaderUtils.isRouter(org.mule.runtime.module.extension.internal.loader.utils.ModelLoaderUtils.isRouter) Collectors.toList(java.util.stream.Collectors.toList) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) ExtensionMetadataTypeUtils(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) MetadataResolverFactory(org.mule.runtime.extension.api.metadata.MetadataResolverFactory) NamedTypeResolver(org.mule.runtime.api.metadata.resolving.NamedTypeResolver) Table(com.google.common.collect.Table) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) NullMetadataResolver(org.mule.runtime.extension.api.metadata.NullMetadataResolver) NamedTypeResolver(org.mule.runtime.api.metadata.resolving.NamedTypeResolver) Problem(org.mule.runtime.extension.api.loader.Problem) LinkedList(java.util.LinkedList)

Example 4 with Problem

use of org.mule.runtime.extension.api.loader.Problem in project mule by mulesoft.

the class MetadataComponentModelValidator method validateCategoryNames.

private void validateCategoryNames(ComponentModel componentModel, ProblemsReporter problemsReporter, List<NamedTypeResolver> resolvers) {
    resolvers.stream().filter(r -> StringUtils.isBlank(r.getCategoryName())).findFirst().ifPresent(r -> problemsReporter.addError(new Problem(componentModel, String.format(EMPTY_RESOLVER_NAME, getComponentModelTypeName(componentModel), componentModel.getName(), r.getClass().getSimpleName(), "category"))));
    Set<String> names = resolvers.stream().filter(r -> !isNullResolver(r)).map(NamedTypeResolver::getCategoryName).collect(toSet());
    if (names.size() > 1) {
        problemsReporter.addError(new Problem(componentModel, String.format("%s '%s' specifies metadata resolvers that doesn't belong to the same category. The following categories were the ones found [%s]", getComponentModelTypeName(componentModel), componentModel.getName(), join(names, ","))));
    }
}
Also used : Problem(org.mule.runtime.extension.api.loader.Problem)

Example 5 with Problem

use of org.mule.runtime.extension.api.loader.Problem in project mule by mulesoft.

the class ValueProviderModelValidator method validateOptionsResolver.

private void validateOptionsResolver(ParameterModel param, ValueProviderFactoryModelProperty modelProperty, ParameterizedModel model, ProblemsReporter problemsReporter, boolean supportsConnectionsAndConfigs, ReflectionCache reflectionCache) {
    Class<? extends ValueProvider> valueProvider = modelProperty.getValueProvider();
    String providerName = valueProvider.getSimpleName();
    Map<String, MetadataType> allParameters = model.getAllParameterModels().stream().collect(toMap(IntrospectionUtils::getImplementingName, ParameterModel::getType));
    String modelName = NameUtils.getModelName(model);
    String modelTypeName = NameUtils.getComponentModelTypeName(model);
    if (!isInstantiable(valueProvider, reflectionCache)) {
        problemsReporter.addError(new Problem(model, format("The Value Provider [%s] is not instantiable but it should", providerName)));
    }
    if (!(param.getType() instanceof StringType)) {
        problemsReporter.addError(new Problem(model, format("The parameter [%s] of the %s '%s' is not of String type. Parameters that provides Values should be of String type.", param.getName(), modelTypeName, modelName)));
    }
    for (InjectableParameterInfo parameterInfo : modelProperty.getInjectableParameters()) {
        if (!allParameters.containsKey(parameterInfo.getParameterName())) {
            problemsReporter.addError(new Problem(model, format("The Value Provider [%s] declares a parameter '%s' which doesn't exist in the %s '%s'", providerName, parameterInfo.getParameterName(), modelTypeName, modelName)));
        } else {
            MetadataType metadataType = allParameters.get(parameterInfo.getParameterName());
            Class<?> expectedType = getType(metadataType).orElseThrow(() -> new IllegalStateException(format("Unable to get Class for parameter: %s", parameterInfo.getParameterName())));
            Class<?> gotType = getType(parameterInfo.getType()).orElseThrow(() -> new IllegalStateException(format("Unable to get Class for parameter: %s", parameterInfo.getParameterName())));
            if (!expectedType.equals(gotType)) {
                problemsReporter.addError(new Problem(model, format("The Value Provider [%s] defines a parameter '%s' of type '%s' but in the %s '%s' is of type '%s'", providerName, parameterInfo.getParameterName(), gotType, modelTypeName, modelName, expectedType)));
            }
        }
    }
    if (supportsConnectionsAndConfigs && modelProperty.usesConnection() && model instanceof ConnectableComponentModel) {
        boolean requiresConnection = ((ConnectableComponentModel) model).requiresConnection();
        if (requiresConnection != modelProperty.usesConnection()) {
            problemsReporter.addError(new Problem(model, format("The Value Provider [%s] defines that requires a connection, but is used in the %s '%s' which is connection less", providerName, modelTypeName, modelName)));
        }
    }
    if (!supportsConnectionsAndConfigs) {
        if (modelProperty.usesConnection()) {
            problemsReporter.addError(new Problem(model, format("The Value Provider [%s] defines that requires a connection which is not allowed for a Value Provider of a %s's parameter [%s]", providerName, modelTypeName, modelName)));
        }
        if (modelProperty.usesConfig()) {
            problemsReporter.addError(new Problem(model, format("The Value Provider [%s] defines that requires a configuration which is not allowed for a Value Provider of a %s's parameter [%s]", providerName, modelTypeName, modelName)));
        }
    }
}
Also used : StringType(org.mule.metadata.api.model.StringType) InjectableParameterInfo(org.mule.runtime.module.extension.internal.loader.java.property.ValueProviderFactoryModelProperty.InjectableParameterInfo) ConnectableComponentModel(org.mule.runtime.api.meta.model.ConnectableComponentModel) MetadataType(org.mule.metadata.api.model.MetadataType) Problem(org.mule.runtime.extension.api.loader.Problem)

Aggregations

Problem (org.mule.runtime.extension.api.loader.Problem)13 ProblemsReporter (org.mule.runtime.extension.api.loader.ProblemsReporter)5 String.format (java.lang.String.format)4 MetadataType (org.mule.metadata.api.model.MetadataType)4 ObjectType (org.mule.metadata.api.model.ObjectType)4 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)4 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)4 ExtensionModelValidator (org.mule.runtime.extension.api.loader.ExtensionModelValidator)4 Optional (java.util.Optional)3 Set (java.util.Set)3 Test (org.junit.Test)3 ArrayType (org.mule.metadata.api.model.ArrayType)3 JAVA (org.mule.metadata.api.model.MetadataFormat.JAVA)3 NamedObject (org.mule.runtime.api.meta.NamedObject)3 OperationModel (org.mule.runtime.api.meta.model.operation.OperationModel)3 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)3 SourceModel (org.mule.runtime.api.meta.model.source.SourceModel)3 ExtensionWalker (org.mule.runtime.api.meta.model.util.ExtensionWalker)3 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2