Search in sources :

Example 1 with MetadataContext

use of org.mule.runtime.api.metadata.MetadataContext in project mule by mulesoft.

the class ExtensionComponent method runWithMetadataContext.

protected <R> R runWithMetadataContext(Function<MetadataContext, R> metadataContextFunction) throws MetadataResolvingException, ConnectionException {
    MetadataContext context = null;
    R result;
    try {
        context = withContextClassLoader(getClassLoader(this.extensionModel), this::getMetadataContext);
        result = metadataContextFunction.apply(context);
    } catch (MuleRuntimeException e) {
        // TODO(MULE-13621) this should be deleted once the configuration is created lazily in the getMetadataContext method.
        try {
            throw e.getCause();
        } catch (MetadataResolvingException | ConnectionException cause) {
            throw cause;
        } catch (Throwable t) {
            throw e;
        }
    } finally {
        if (context != null) {
            context.dispose();
        }
    }
    return result;
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) DefaultMetadataContext(org.mule.runtime.module.extension.internal.metadata.DefaultMetadataContext) MetadataContext(org.mule.runtime.api.metadata.MetadataContext)

Example 2 with MetadataContext

use of org.mule.runtime.api.metadata.MetadataContext in project mule by mulesoft.

the class ConfigurationProviderToolingAdapter method getMetadataKeys.

/**
 * {@inheritDoc}
 */
@Override
public MetadataResult<MetadataKeysContainer> getMetadataKeys() throws MetadataResolvingException {
    MetadataKeysContainerBuilder keysBuilder = MetadataKeysContainerBuilder.getInstance();
    try {
        MetadataContext metadataContext = getMetadataContext();
        addComponentKeys(getConfigurationModel().getOperationModels(), metadataContext, keysBuilder);
        addComponentKeys(getConfigurationModel().getSourceModels(), metadataContext, keysBuilder);
        metadataContext.dispose();
    } catch (Exception e) {
        return failure(newFailure(e).onKeys());
    }
    return success(keysBuilder.build());
}
Also used : MetadataKeysContainerBuilder(org.mule.runtime.api.metadata.MetadataKeysContainerBuilder) DefaultMetadataContext(org.mule.runtime.module.extension.internal.metadata.DefaultMetadataContext) MetadataContext(org.mule.runtime.api.metadata.MetadataContext) ValueResolvingException(org.mule.runtime.extension.api.values.ValueResolvingException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException)

Example 3 with MetadataContext

use of org.mule.runtime.api.metadata.MetadataContext in project mule by mulesoft.

the class DsqlQueryMetadataResolver method getOutputType.

/**
 * Automatically resolves the output metadata for the {@link DsqlQuery}.
 * <p>
 * The base entity is resolved using the component {@link QueryEntityResolver} and assuming the key of the entity is the DSQL
 * {@link DsqlQuery#getType() type}.
 *
 * @param context {@link MetadataContext} of the MetaData resolution
 * @param query the {@link DsqlQuery} to resolve the output metadata from.
 */
@Override
public MetadataType getOutputType(MetadataContext context, Object query) throws MetadataResolvingException, ConnectionException {
    if (query instanceof DsqlQuery) {
        DsqlQuery dsqlQuery = (DsqlQuery) query;
        MetadataType entityMetadata = entityResolver.getEntityMetadata(context, dsqlQuery.getType().getName());
        BaseTypeBuilder builder = context.getTypeBuilder();
        final List<Field> fields = dsqlQuery.getFields();
        if (fields.size() == 1 && fields.get(0).getName().equals("*")) {
            return entityMetadata;
        }
        entityMetadata.accept(new MetadataTypeVisitor() {

            @Override
            public void visitObject(ObjectType objectType) {
                ObjectTypeBuilder objectTypeBuilder = builder.objectType();
                objectType.getFields().stream().filter(p -> fields.stream().anyMatch(f -> f.getName().equalsIgnoreCase(p.getKey().getName().getLocalPart()))).forEach(p -> {
                    ObjectFieldTypeBuilder field = objectTypeBuilder.addField();
                    field.key(p.getKey().getName());
                    field.value(p.getValue());
                });
            }
        });
        return builder.build();
    } else {
        return nativeOutputResolver.getOutputType(context, query);
    }
}
Also used : ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) ObjectType(org.mule.metadata.api.model.ObjectType) OutputTypeResolver(org.mule.runtime.api.metadata.resolving.OutputTypeResolver) Field(org.mule.runtime.extension.api.dsql.Field) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) MetadataContext(org.mule.runtime.api.metadata.MetadataContext) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder) QueryEntityResolver(org.mule.runtime.api.metadata.resolving.QueryEntityResolver) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) Field(org.mule.runtime.extension.api.dsql.Field) ObjectType(org.mule.metadata.api.model.ObjectType) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder)

Example 4 with MetadataContext

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

Aggregations

MetadataContext (org.mule.runtime.api.metadata.MetadataContext)4 MetadataResolvingException (org.mule.runtime.api.metadata.MetadataResolvingException)3 List (java.util.List)2 MetadataType (org.mule.metadata.api.model.MetadataType)2 ConnectionException (org.mule.runtime.api.connection.ConnectionException)2 DefaultMetadataContext (org.mule.runtime.module.extension.internal.metadata.DefaultMetadataContext)2 String.format (java.lang.String.format)1 LinkedList (java.util.LinkedList)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 BaseTypeBuilder (org.mule.metadata.api.builder.BaseTypeBuilder)1 ObjectFieldTypeBuilder (org.mule.metadata.api.builder.ObjectFieldTypeBuilder)1 ObjectTypeBuilder (org.mule.metadata.api.builder.ObjectTypeBuilder)1 ObjectType (org.mule.metadata.api.model.ObjectType)1 MetadataTypeUtils.isCollection (org.mule.metadata.api.utils.MetadataTypeUtils.isCollection)1 MetadataTypeUtils.isNullType (org.mule.metadata.api.utils.MetadataTypeUtils.isNullType)1 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)1 ClassInformationAnnotation (org.mule.metadata.java.api.annotation.ClassInformationAnnotation)1 JavaTypeUtils.getType (org.mule.metadata.java.api.utils.JavaTypeUtils.getType)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1