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();
}
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());
}
}
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();
}
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();
}
}
}
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());
}
Aggregations