use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver in project mule by mulesoft.
the class ExtensionDefinitionParser method doParseDate.
private ValueResolver doParseDate(Object value, Class<?> type) {
if (value instanceof String) {
Object constructedValue = null;
DateTime dateTime = getParsedDateTime((String) value);
if (type.equals(LocalDate.class)) {
constructedValue = LocalDate.of(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth());
} else if (type.equals(Date.class)) {
constructedValue = dateTime.toDate();
} else if (type.equals(LocalDateTime.class)) {
Instant instant = ofEpochMilli(dateTime.getMillis());
constructedValue = LocalDateTime.ofInstant(instant, ZoneId.of(dateTime.getZone().getID()));
} else if (type.equals(Calendar.class)) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime.toDate());
constructedValue = calendar;
}
if (constructedValue == null) {
throw new IllegalArgumentException(format("Could not construct value of type '%s' from String '%s'", type.getName(), value));
} else {
value = constructedValue;
}
}
if (value instanceof Date || value instanceof LocalDate || value instanceof LocalDateTime || value instanceof Calendar) {
return new StaticValueResolver<>(value);
}
throw new IllegalArgumentException(format("Could not transform value of type '%s' to a valid date type", value != null ? value.getClass().getName() : "null"));
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver in project mule by mulesoft.
the class DefaultExtensionsClient method resolveParameters.
private Map<String, ValueResolver> resolveParameters(Map<String, Object> parameters, CoreEvent event) {
LinkedHashMap<String, ValueResolver> values = new LinkedHashMap<>();
parameters.forEach((name, value) -> {
if (value instanceof ComplexParameter) {
ComplexParameter complex = (ComplexParameter) value;
DefaultObjectBuilder<?> builder = new DefaultObjectBuilder<>(complex.getType());
resolveParameters(complex.getParameters(), event).forEach((propertyName, valueResolver) -> {
try {
initialiseIfNeeded(valueResolver, true, muleContext);
builder.addPropertyResolver(propertyName, valueResolver);
} catch (InitialisationException e) {
throw new MuleRuntimeException(e);
}
});
try {
values.put(name, new StaticValueResolver<>(builder.build(from(event))));
} catch (MuleException e) {
throw new MuleRuntimeException(createStaticMessage(format("Could not construct parameter [%s]", name)), e);
}
} else {
if (value instanceof String && parser.isContainsTemplate((String) value)) {
values.put(name, new ExpressionValueResolver((String) value));
} else {
values.put(name, new StaticValueResolver<>(value));
}
}
});
return values;
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver in project mule by mulesoft.
the class ReflectiveOperationExecutorFactory method createExecutor.
@Override
public ComponentExecutor<M> createExecutor(M operationModel, Map<String, Object> parameters) {
DefaultObjectBuilder objectBuilder = new DefaultObjectBuilder(implementationClass);
parameters.forEach((k, v) -> objectBuilder.addPropertyResolver(k, new StaticValueResolver<>(v)));
Object delegate;
CoreEvent initialiserEvent = null;
try {
initialiserEvent = getInitialiserEvent();
delegate = objectBuilder.build(from(initialiserEvent));
} catch (Exception e) {
throw new MuleRuntimeException(createStaticMessage("Could not create instance of operation class " + implementationClass.getName()), e);
} finally {
if (initialiserEvent != null) {
((BaseEventContext) initialiserEvent.getContext()).success();
}
}
return new ReflectiveMethodOperationExecutor(operationModel, operationMethod, delegate);
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver in project mule by mulesoft.
the class ExtensionDefinitionParser method getStaticValueResolver.
/**
* Generates the {@link ValueResolver} for non expression based values
*/
private ValueResolver getStaticValueResolver(String parameterName, MetadataType expectedType, Object value, Object defaultValue, Set<ModelProperty> modelProperties, boolean acceptsReferences, Class<?> expectedClass) {
Optional<StackedTypesModelProperty> optionalStackedTypeModelProperty = getStackedTypesModelProperty(modelProperties);
if (optionalStackedTypeModelProperty.isPresent()) {
StackedTypesModelProperty property = optionalStackedTypeModelProperty.get();
Optional<ValueResolver> optionalResolver = property.getValueResolverFactory().getStaticValueResolver(value, Literal.class);
if (optionalResolver.isPresent()) {
return optionalResolver.get();
}
}
if (isLiteral(expectedType)) {
return new StaticLiteralValueResolver<>(value != null ? value.toString() : null, expectedClass);
}
ValueResolver resolver;
resolver = value != null ? getValueResolverFromMetadataType(parameterName, expectedType, value, defaultValue, acceptsReferences, expectedClass) : new StaticValueResolver<>(defaultValue);
if (optionalStackedTypeModelProperty.isPresent()) {
resolver = optionalStackedTypeModelProperty.get().getValueResolverFactory().getWrapperValueResolver(resolver);
} else if (isParameterResolver(expectedType)) {
resolver = new ParameterResolverValueResolverWrapper(resolver);
} else if (isTypedValue(expectedType)) {
resolver = new TypedValueValueResolverWrapper(resolver);
}
return resolver;
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver in project mule by mulesoft.
the class ExtensionDefinitionParser method getValueResolverFromMetadataType.
private ValueResolver getValueResolverFromMetadataType(final String parameterName, MetadataType expectedType, final Object value, final Object defaultValue, final boolean acceptsReferences, final Class<?> expectedClass) {
final Reference<ValueResolver> resolverValueHolder = new Reference<>();
expectedType.accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
if (conversionService.canConvert(value.getClass(), expectedClass)) {
resolverValueHolder.set(new StaticValueResolver<>(convertSimpleValue(value, expectedClass, parameterName)));
} else {
defaultVisit(metadataType);
}
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
resolverValueHolder.set(parseDate(value, dateTimeType, defaultValue));
}
@Override
public void visitDate(DateType dateType) {
resolverValueHolder.set(parseDate(value, dateType, defaultValue));
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
defaultVisit(objectType);
return;
}
ValueResolver valueResolver;
Optional<? extends ParsingDelegate> delegate = locateParsingDelegate(valueResolverParsingDelegates, objectType);
Optional<DslElementSyntax> typeDsl = dslResolver.resolve(objectType);
if (delegate.isPresent() && typeDsl.isPresent()) {
valueResolver = (ValueResolver) delegate.get().parse(value.toString(), objectType, typeDsl.get());
} else {
valueResolver = acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), objectType, null) : new StaticValueResolver<>(value);
}
resolverValueHolder.set(valueResolver);
}
@Override
protected void defaultVisit(MetadataType metadataType) {
ValueResolver delegateResolver = locateParsingDelegate(valueResolverParsingDelegates, metadataType).map(delegate -> delegate.parse(value.toString(), metadataType, null)).orElseGet(() -> acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), metadataType, null) : new TypeSafeValueResolverWrapper(new StaticValueResolver<>(value), expectedClass));
resolverValueHolder.set(delegateResolver);
}
});
return resolverValueHolder.get();
}
Aggregations