Search in sources :

Example 71 with MetadataType

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

the class MetadataInputDelegate method getInputMetadataDescriptors.

/**
 * For each of the Component's {@link ParameterModel} creates the corresponding {@link TypeMetadataDescriptor} using only its
 * static {@link MetadataType} and ignoring if any parameter has a dynamic type.
 *
 * @return A {@link List} containing a {@link MetadataResult} of {@link TypeMetadataDescriptor} for each input parameter using
 * only its static {@link MetadataType} and ignoring if any parameter has a dynamic type.
 */
MetadataResult<InputMetadataDescriptor> getInputMetadataDescriptors(MetadataContext context, Object key) {
    InputMetadataDescriptor.InputMetadataDescriptorBuilder input = InputMetadataDescriptor.builder();
    List<MetadataResult<ParameterMetadataDescriptor>> results = new LinkedList<>();
    for (ParameterModel parameter : component.getAllParameterModels()) {
        MetadataResult<ParameterMetadataDescriptor> result = getParameterMetadataDescriptor(parameter, context, key);
        input.withParameter(parameter.getName(), result.get());
        results.add(result);
    }
    List<MetadataFailure> failures = results.stream().flatMap(e -> e.getFailures().stream()).collect(toList());
    return failures.isEmpty() ? success(input.build()) : failure(input.build(), failures);
}
Also used : InputMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.InputMetadataDescriptor) ParameterMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.ParameterMetadataDescriptor) MetadataResult.success(org.mule.runtime.api.metadata.resolving.MetadataResult.success) MetadataResult(org.mule.runtime.api.metadata.resolving.MetadataResult) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) InputMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.InputMetadataDescriptor) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) MetadataTypeUtils.isNullType(org.mule.metadata.api.utils.MetadataTypeUtils.isNullType) TypeMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.TypeMetadataDescriptor) MetadataKey(org.mule.runtime.api.metadata.MetadataKey) NO_DYNAMIC_TYPE_AVAILABLE(org.mule.runtime.api.metadata.resolving.FailureCode.NO_DYNAMIC_TYPE_AVAILABLE) LinkedList(java.util.LinkedList) InputTypeResolver(org.mule.runtime.api.metadata.resolving.InputTypeResolver) MetadataTypeUtils.isCollection(org.mule.metadata.api.utils.MetadataTypeUtils.isCollection) ClassInformationAnnotation(org.mule.metadata.java.api.annotation.ClassInformationAnnotation) String.format(java.lang.String.format) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) MetadataContext(org.mule.runtime.api.metadata.MetadataContext) Builder.newFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure.Builder.newFailure) MetadataType(org.mule.metadata.api.model.MetadataType) Optional(java.util.Optional) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) ParameterMetadataDescriptorBuilder(org.mule.runtime.api.metadata.descriptor.ParameterMetadataDescriptor.ParameterMetadataDescriptorBuilder) NamedTypeResolver(org.mule.runtime.api.metadata.resolving.NamedTypeResolver) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) MetadataResult.failure(org.mule.runtime.api.metadata.resolving.MetadataResult.failure) ParameterMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.ParameterMetadataDescriptor) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) LinkedList(java.util.LinkedList) MetadataResult(org.mule.runtime.api.metadata.resolving.MetadataResult)

Example 72 with MetadataType

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

the class MetadataInputDelegate method getParameterMetadata.

/**
 * Given a {@link MetadataKey} of a type and a {@link MetadataContext}, resolves the {@link MetadataType} of the
 * {@code parameter} using the {@link InputTypeResolver} 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 {@code parameter}.
 */
private MetadataResult<MetadataType> getParameterMetadata(ParameterModel parameter, MetadataContext context, Object key) {
    try {
        boolean allowsNullType = !parameter.isRequired() && (parameter.getDefaultValue() == null);
        MetadataType metadata = resolverFactory.getInputResolver(parameter.getName()).getInputMetadata(context, key);
        if (isMetadataResolvedCorrectly(metadata, allowsNullType)) {
            return success(adaptToListIfNecessary(metadata, parameter, context));
        }
        MetadataFailure failure = newFailure().withMessage(format("Error resolving metadata for the [%s] input parameter", parameter.getName())).withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE).withReason(NULL_TYPE_ERROR).onParameter(parameter.getName());
        return failure(parameter.getType(), failure);
    } catch (Exception e) {
        return failure(parameter.getType(), newFailure(e).onParameter(parameter.getName()));
    }
}
Also used : MetadataType(org.mule.metadata.api.model.MetadataType) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException)

Example 73 with MetadataType

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

the class MetadataKeyIdObjectResolver method resolve.

/**
 * Returns the populated key in the Type that the component parameter requires by looking for default values, if no
 * {@link MetadataKeyId} is present an empty value is returned since is a key less component.
 * <p>
 * If a key should be built and there is at least one default value missing an {@link IllegalArgumentException} is thrown.
 *
 * @return a new instance of the {@link MetadataKeyId} parameter {@code type}.
 * @throws MetadataResolvingException if the Parameter type is not instantiable.
 * @throws IllegalArgumentException if cannot found the required default values for an specified key.
 */
public Object resolve() throws MetadataResolvingException {
    if (isKeyLess()) {
        return NullMetadataKey.ID;
    }
    if (!keyParts.stream().allMatch(p -> p.getDefaultValue() != null)) {
        throw new IllegalArgumentException("Could not build metadata key from an object that does" + " not have a default value for all it's components.");
    }
    String id = keyParts.get(0).getDefaultValue().toString();
    final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component);
    MetadataType type = keyIdModelProperty.getType();
    KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(id, getType(type)) {

        @Override
        protected Map<Field, String> getFieldValuesMap() {
            return keyParts.stream().filter(p -> p.getModelProperty(DeclaringMemberModelProperty.class).isPresent()).collect(toMap(p -> p.getModelProperty(DeclaringMemberModelProperty.class).get().getDeclaringField(), p -> p.getDefaultValue().toString()));
        }
    };
    type.accept(visitor);
    return visitor.getResultId();
}
Also used : StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) IntrospectionUtils.getFieldValue(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getFieldValue) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) MetadataKeyId(org.mule.runtime.extension.api.annotation.metadata.MetadataKeyId) HashMap(java.util.HashMap) DsqlParser.isDsqlQuery(org.mule.runtime.extension.api.dsql.DsqlParser.isDsqlQuery) MetadataKeyBuilder(org.mule.runtime.api.metadata.MetadataKeyBuilder) QueryParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.QueryParameterModelProperty) MetadataKey(org.mule.runtime.api.metadata.MetadataKey) DsqlParser(org.mule.runtime.extension.api.dsql.DsqlParser) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) Collectors.toMap(java.util.stream.Collectors.toMap) MetadataKeyPartModelProperty(org.mule.runtime.extension.api.property.MetadataKeyPartModelProperty) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) Map(java.util.Map) MetadataKeyBuilder.newKey(org.mule.runtime.api.metadata.MetadataKeyBuilder.newKey) INVALID_METADATA_KEY(org.mule.runtime.api.metadata.resolving.FailureCode.INVALID_METADATA_KEY) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) NullMetadataKey(org.mule.runtime.extension.api.metadata.NullMetadataKey) FailureCode(org.mule.runtime.api.metadata.resolving.FailureCode) ObjectType(org.mule.metadata.api.model.ObjectType) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) Field(java.lang.reflect.Field) String.format(java.lang.String.format) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) String.valueOf(java.lang.String.valueOf) StringType(org.mule.metadata.api.model.StringType) BooleanType(org.mule.metadata.api.model.BooleanType) Reference(org.mule.runtime.api.util.Reference) Function.identity(java.util.function.Function.identity) MetadataType(org.mule.metadata.api.model.MetadataType) Optional(java.util.Optional) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) Field(java.lang.reflect.Field) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType)

Example 74 with MetadataType

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

the class MetadataKeyIdObjectResolver method resolve.

/**
 * Given {@link MetadataKey}, return the populated key in the Type that the component parameter requires.
 *
 * @param key the {@link MetadataKey} associated to the {@link MetadataKeyId}
 * @return a new instance of the {@link MetadataKeyId} parameter {@code type} with the values of the passed {@link MetadataKey}
 * @throws MetadataResolvingException if:
 *         <ul>
 *         <li>Parameter types is not instantiable</li>
 *         <li>{@param key} does not provide the required levels</li>
 *         </ul>
 */
public Object resolve(MetadataKey key) throws MetadataResolvingException {
    if (isKeyLess()) {
        return NullMetadataKey.ID;
    }
    final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component);
    MetadataType type = keyIdModelProperty.getType();
    KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(key.getId(), getType(type)) {

        @Override
        protected Map<Field, String> getFieldValuesMap() throws MetadataResolvingException {
            return keyToFieldValueMap(key);
        }
    };
    type.accept(visitor);
    return visitor.getResultId();
}
Also used : Field(java.lang.reflect.Field) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType)

Example 75 with MetadataType

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

the class MetadataOutputDelegate method resolveOutputAttributesMetadata.

private MetadataResult<MetadataType> resolveOutputAttributesMetadata(MetadataContext context, Object key, Function<MetadataType, Boolean> metadataValidator) {
    try {
        MetadataType metadata = resolverFactory.getOutputAttributesResolver().getAttributesType(context, key);
        if (metadataValidator.apply(metadata)) {
            return success(metadata);
        }
        MetadataFailure failure = newFailure().withMessage("Error resolving Output Attributes metadata").withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE).withReason(NULL_TYPE_ERROR).onOutputAttributes();
        return failure(failure);
    } catch (Exception e) {
        return failure(newFailure(e).onOutputAttributes());
    }
}
Also used : MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException)

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