Search in sources :

Example 1 with MetadataResolvingException

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

the class MetadataKeyIdObjectResolver method reconstructKeyFromType.

/**
 * Given a {@link Object} representing the resolved value for a {@link MetadataKey}, generates the {@link MetadataKey} object.
 *
 * @param resolvedKey
 * @return {@link MetadataKey} reconstructed from the resolved object key
 * @throws MetadataResolvingException
 */
MetadataKey reconstructKeyFromType(Object resolvedKey, ReflectionCache reflectionCache) throws MetadataResolvingException {
    if (isKeyLess() || resolvedKey == null) {
        return new NullMetadataKey();
    }
    if (keyParts.size() == 1) {
        return newKey(valueOf(resolvedKey)).build();
    }
    MetadataKeyBuilder rootBuilder = null;
    MetadataKeyBuilder childBuilder = null;
    for (ParameterModel p : keyParts) {
        try {
            if (p.getModelProperty(DeclaringMemberModelProperty.class).isPresent()) {
                MetadataKeyBuilder fieldBuilder = getKeyFromField(resolvedKey, p.getModelProperty(DeclaringMemberModelProperty.class).get(), reflectionCache);
                if (rootBuilder == null) {
                    rootBuilder = fieldBuilder;
                    childBuilder = rootBuilder;
                } else {
                    childBuilder.withChild(fieldBuilder);
                    childBuilder = fieldBuilder;
                }
            }
        } catch (Exception e) {
            throw new MetadataResolvingException("Could not construct Metadata Key part for parameter " + p.getName(), FailureCode.INVALID_METADATA_KEY, e);
        }
    }
    return rootBuilder != null ? rootBuilder.build() : new NullMetadataKey();
}
Also used : DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) NullMetadataKey(org.mule.runtime.extension.api.metadata.NullMetadataKey) MetadataKeyBuilder(org.mule.runtime.api.metadata.MetadataKeyBuilder) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException)

Example 2 with MetadataResolvingException

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

the class MetadataMediator method getMetadata.

/**
 * Resolves the {@link ComponentMetadataDescriptor} for the associated {@code context} using the component
 * {@link ParameterValueResolver} to resolve an object with the value of the {@link MetadataKey}.
 *
 * @param context current {@link MetadataContext} that will be used by the metadata resolvers.
 * @return Successful {@link MetadataResult} if the MetadataTypes are resolved without errors Failure {@link MetadataResult}
 *         when the Metadata retrieval of any element fails for any reason
 */
public MetadataResult<ComponentMetadataDescriptor<T>> getMetadata(MetadataContext context, ParameterValueResolver metadataKeyResolver, ReflectionCache reflectionCache) {
    try {
        Object keyValue;
        MetadataResult keyValueResult = getMetadataKeyObjectValue(metadataKeyResolver);
        if (!keyValueResult.isSuccess()) {
            return keyValueResult;
        }
        keyValue = keyValueResult.get();
        if (keyValue == null && !keyIdObjectResolver.isKeyLess()) {
            return failure(newFailure().withFailureCode(INVALID_METADATA_KEY).withMessage("MetadataKey resolved to null").onComponent());
        }
        MetadataAttributes.MetadataAttributesBuilder builder = MetadataAttributes.builder();
        if (!keyIdObjectResolver.isKeyLess()) {
            builder.withKey(keyIdObjectResolver.reconstructKeyFromType(keyValue, reflectionCache));
        }
        return getMetadata(context, keyValue, builder);
    } catch (MetadataResolvingException e) {
        return failure(newFailure(e).onComponent());
    }
}
Also used : MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) MetadataAttributes(org.mule.runtime.api.metadata.MetadataAttributes) MetadataResult(org.mule.runtime.api.metadata.resolving.MetadataResult)

Example 3 with MetadataResolvingException

use of org.mule.runtime.api.metadata.MetadataResolvingException 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 4 with MetadataResolvingException

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

the class ExtensionComponent method getMetadataContext.

private MetadataContext getMetadataContext() throws MetadataResolvingException {
    CoreEvent fakeEvent = null;
    try {
        fakeEvent = getInitialiserEvent(muleContext);
        Optional<ConfigurationInstance> configuration = getConfiguration(fakeEvent);
        if (configuration.isPresent()) {
            ConfigurationProvider configurationProvider = findConfigurationProvider().orElseThrow(() -> new MetadataResolvingException("Failed to create the required configuration for Metadata retrieval", INVALID_CONFIGURATION));
            if (configurationProvider instanceof DynamicConfigurationProvider) {
                throw new MetadataResolvingException("Configuration used for Metadata fetch cannot be dynamic", INVALID_CONFIGURATION);
            }
        }
        String cacheId = configuration.map(ConfigurationInstance::getName).orElseGet(() -> extensionModel.getName() + "|" + componentModel.getName());
        return new DefaultMetadataContext(() -> configuration, connectionManager, metadataService.getMetadataCache(cacheId), typeLoader);
    } finally {
        if (fakeEvent != null) {
            ((BaseEventContext) fakeEvent.getContext()).success();
        }
    }
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ConfigurationProvider(org.mule.runtime.extension.api.runtime.config.ConfigurationProvider) DynamicConfigurationProvider(org.mule.runtime.module.extension.internal.runtime.config.DynamicConfigurationProvider) DynamicConfigurationProvider(org.mule.runtime.module.extension.internal.runtime.config.DynamicConfigurationProvider) DefaultMetadataContext(org.mule.runtime.module.extension.internal.metadata.DefaultMetadataContext) ConfigurationInstance(org.mule.runtime.extension.api.runtime.config.ConfigurationInstance)

Example 5 with MetadataResolvingException

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

Aggregations

MetadataResolvingException (org.mule.runtime.api.metadata.MetadataResolvingException)10 MetadataType (org.mule.metadata.api.model.MetadataType)3 List (java.util.List)2 ObjectTypeBuilder (org.mule.metadata.api.builder.ObjectTypeBuilder)2 ObjectType (org.mule.metadata.api.model.ObjectType)2 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)2 ConnectionException (org.mule.runtime.api.connection.ConnectionException)2 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)2 MetadataContext (org.mule.runtime.api.metadata.MetadataContext)2 MetadataKeyBuilder (org.mule.runtime.api.metadata.MetadataKeyBuilder)2 DsqlQuery (org.mule.runtime.extension.api.dsql.DsqlQuery)2 DefaultMetadataContext (org.mule.runtime.module.extension.internal.metadata.DefaultMetadataContext)2 DefaultObjectBuilder (org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder)2 StaticValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver)2 String.format (java.lang.String.format)1 String.valueOf (java.lang.String.valueOf)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1