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