Search in sources :

Example 21 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class MetadataOutputDelegate method getOutputMetadataDescriptor.

/**
 * Creates an {@link OutputMetadataDescriptor} representing the Component's output metadata using the
 * {@link OutputTypeResolver}, if one is available to resolve the output {@link MetadataType}.
 *
 * @param context current {@link MetadataContext} that will be used by the {@link InputTypeResolver}
 * @param key     {@link MetadataKey} of the type which's structure has to be resolved
 * @return Success with an {@link OutputMetadataDescriptor} representing the Component's output metadata, resolved using the
 * {@link OutputTypeResolver} if one is available to resolve its {@link MetadataType}. Failure if the dynamic
 * resolution fails for any reason.
 */
MetadataResult<OutputMetadataDescriptor> getOutputMetadataDescriptor(MetadataContext context, Object key) {
    if (!(component instanceof HasOutputModel)) {
        return failure(MetadataFailure.Builder.newFailure().withMessage("The given component has not output definition to be described").onComponent());
    }
    MetadataResult<MetadataType> output = getOutputMetadata(context, key);
    MetadataResult<MetadataType> attributes = getOutputAttributesMetadata(context, key);
    HasOutputModel componentWithOutput = (HasOutputModel) this.component;
    MetadataResult<TypeMetadataDescriptor> outputDescriptor = toMetadataDescriptorResult(componentWithOutput.getOutput().getType(), componentWithOutput.getOutput().hasDynamicType(), output);
    MetadataResult<TypeMetadataDescriptor> attributesDescriptor = toMetadataDescriptorResult(componentWithOutput.getOutputAttributes().getType(), false, attributes);
    OutputMetadataDescriptor descriptor = OutputMetadataDescriptor.builder().withReturnType(outputDescriptor.get()).withAttributesType(attributesDescriptor.get()).build();
    if (!output.isSuccess() || !attributes.isSuccess()) {
        List<MetadataFailure> failures = ImmutableList.<MetadataFailure>builder().addAll(output.getFailures()).addAll(attributes.getFailures()).build();
        return failure(descriptor, failures);
    }
    return success(descriptor);
}
Also used : TypeMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.TypeMetadataDescriptor) OutputMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.OutputMetadataDescriptor) HasOutputModel(org.mule.runtime.api.meta.model.HasOutputModel) MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure)

Example 22 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class MetadataOutputDelegate method getOutputMetadata.

/**
 * Given a {@link MetadataKey} of a type and a {@link MetadataContext}, resolves the {@link MetadataType} of the Components's
 * output using the {@link OutputTypeResolver} associated to the current component.
 *
 * @param context {@link MetadataContext} of the Metadata resolution
 * @param key     {@link MetadataKey} of the type which's structure has to be resolved
 * @return a {@link MetadataResult} with the {@link MetadataType} of the component's output
 */
private MetadataResult<MetadataType> getOutputMetadata(final MetadataContext context, final Object key) {
    OutputModel output = ((HasOutputModel) component).getOutput();
    if (isVoid(output.getType()) || !output.hasDynamicType()) {
        return success(output.getType());
    }
    try {
        MetadataType metadata = resolverFactory.getOutputResolver().getOutputType(context, key);
        if (isMetadataResolvedCorrectly(metadata, true)) {
            return success(adaptToListIfNecessary(metadata, key, context));
        }
        MetadataFailure failure = newFailure().withMessage("Error resolving Output Payload metadata").withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE).withReason(NULL_TYPE_ERROR).onOutputPayload();
        return failure(output.getType(), failure);
    } catch (Exception e) {
        return failure(output.getType(), newFailure(e).onOutputAttributes());
    }
}
Also used : HasOutputModel(org.mule.runtime.api.meta.model.HasOutputModel) MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) OutputModel(org.mule.runtime.api.meta.model.OutputModel) HasOutputModel(org.mule.runtime.api.meta.model.HasOutputModel) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException)

Example 23 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class MetadataOutputDelegate method wrapInMessageType.

private MetadataType wrapInMessageType(MetadataType type, Object key, MetadataContext context, Optional<MetadataType> staticAttributes) throws MetadataResolvingException {
    MessageMetadataTypeBuilder message = new MessageMetadataTypeBuilder().payload(type);
    staticAttributes.ifPresent(message::attributes);
    if (((HasOutputModel) component).getOutputAttributes().hasDynamicType()) {
        MetadataResult<MetadataType> attributes = resolveOutputAttributesMetadata(context, key, Objects::nonNull);
        if (!attributes.isSuccess()) {
            throw new MetadataResolvingException("Could not resolve attributes of List<Message> output", attributes.getFailures().stream().map(MetadataFailure::getFailureCode).findFirst().orElse(UNKNOWN));
        }
        message.attributes(attributes.get());
    }
    return message.build();
}
Also used : MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MetadataType(org.mule.metadata.api.model.MetadataType) Objects(java.util.Objects) MessageMetadataTypeBuilder(org.mule.metadata.message.api.MessageMetadataTypeBuilder)

Example 24 with MetadataType

use of org.mule.metadata.api.model.MetadataType 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)

Example 25 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class IntrospectionUtils method getReturnType.

private static MetadataType getReturnType(Type returnType) {
    Type type = returnType;
    if (returnType.isAssignableTo(Result.class)) {
        List<TypeGeneric> generics = returnType.getGenerics();
        if (generics.isEmpty()) {
            return typeBuilder().anyType().build();
        }
        Type payloadType = generics.get(0).getConcreteType();
        if (!payloadType.isAnyType()) {
            type = payloadType;
        } else {
            type = null;
        }
    }
    if (isPagingProvider(returnType)) {
        Type itemType = getPagingProviderTypes(returnType).getSecond();
        if (itemType.isSameType(Result.class)) {
            return returnListOfMessagesType(returnType, itemType);
        } else {
            return typeBuilder().arrayType().of(itemType.asMetadataType()).with(returnType.getClassInformation()).build();
        }
    }
    if (returnType.isAssignableTo(ParameterResolver.class) || returnType.isAssignableTo(TypedValue.class) || returnType.isAssignableTo(Literal.class)) {
        type = returnType.getGenerics().get(0).getConcreteType();
    }
    if (isCollection(returnType) && !returnType.getGenerics().isEmpty()) {
        Type itemType = returnType.getGenerics().get(0).getConcreteType();
        if (itemType.isAssignableTo(Result.class)) {
            return returnListOfMessagesType(returnType, itemType);
        }
    }
    return type != null ? type.asMetadataType() : typeBuilder().anyType().build();
}
Also used : Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) StringType(org.mule.metadata.api.model.StringType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeUtils.isObjectType(org.mule.metadata.api.utils.MetadataTypeUtils.isObjectType) AnyType(org.mule.metadata.api.model.AnyType) DeclaredType(javax.lang.model.type.DeclaredType) DataType(org.mule.runtime.api.metadata.DataType) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) ParameterizedType(java.lang.reflect.ParameterizedType) VoidType(org.mule.metadata.api.model.VoidType) ResolvableType(org.springframework.core.ResolvableType) TypeGeneric(org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric) Literal(org.mule.runtime.extension.api.runtime.parameter.Literal)

Aggregations

MetadataType (org.mule.metadata.api.model.MetadataType)99 ObjectType (org.mule.metadata.api.model.ObjectType)36 ArrayType (org.mule.metadata.api.model.ArrayType)28 Test (org.junit.Test)26 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)21 Optional (java.util.Optional)20 MessageMetadataType (org.mule.metadata.message.api.MessageMetadataType)18 String.format (java.lang.String.format)17 List (java.util.List)17 ObjectFieldType (org.mule.metadata.api.model.ObjectFieldType)17 StringType (org.mule.metadata.api.model.StringType)15 Map (java.util.Map)14 Collectors.toList (java.util.stream.Collectors.toList)14 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)14 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)14 Reference (org.mule.runtime.api.util.Reference)14 DslElementSyntax (org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax)14 ParameterGroupModel (org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)13 Set (java.util.Set)11 ParameterizedModel (org.mule.runtime.api.meta.model.parameter.ParameterizedModel)11