use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class AbstractForkJoinStrategyFactory method mergeVariables.
private Consumer<List<CoreEvent>> mergeVariables(CoreEvent original, CoreEvent.Builder result) {
return list -> {
Map<String, TypedValue> routeVars = new HashMap<>();
list.forEach(event -> event.getVariables().forEach((key, value) -> {
// Only merge variables that have been added or mutated in routes
if (!value.equals(original.getVariables().get(key))) {
if (!routeVars.containsKey(key)) {
// A new variable that hasn't already been set by another route is added as a simple entry.
routeVars.put(key, value);
} else {
// If a variable already exists from before route, or was set in a previous route, then it's added to a list of 1.
if (!(routeVars.get(key).getValue() instanceof List)) {
List newList = new ArrayList();
newList.add(routeVars.get(key).getValue());
routeVars.put(key, new TypedValue(newList, DataType.builder().collectionType(List.class).itemType(routeVars.get(key).getDataType().getType()).build()));
}
List valueList = (List) routeVars.get(key).getValue();
valueList.add(value.getValue());
if (((CollectionDataType) routeVars.get(key).getDataType()).getItemDataType().isCompatibleWith(value.getDataType())) {
// If item types are compatible then data type is conserved
routeVars.put(key, new TypedValue(valueList, routeVars.get(key).getDataType()));
} else {
// Else Object item type is used.
routeVars.put(key, new TypedValue(valueList, DataType.builder().collectionType(List.class).build()));
}
}
}
}));
routeVars.forEach((s, typedValue) -> result.addVariable(s, typedValue.getValue(), typedValue.getDataType()));
};
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class Foreach method splitAndProcess.
private Publisher<CoreEvent> splitAndProcess(CoreEvent request) {
AtomicInteger count = new AtomicInteger();
final AtomicReference<CoreEvent> currentEvent = new AtomicReference<>(request);
// Split into sequence of TypedValue
return fromIterable(() -> splitRequest(request)).onErrorMap(throwable -> new MessagingException(request, throwable, Foreach.this)).transform(p -> batchSize > 1 ? from(p).buffer(batchSize).map(list -> new TypedValue<>(list, fromObject(list))) : p).flatMapSequential(typedValue -> {
EventContext parentContext = currentEvent.get().getContext();
BaseEventContext childContext = newChildContext(currentEvent.get(), ofNullable(getLocation()));
Builder partEventBuilder = builder(childContext, currentEvent.get());
if (typedValue.getValue() instanceof EventBuilderConfigurer) {
// Support EventBuilderConfigurer currently used by Batch Module
EventBuilderConfigurer configurer = (EventBuilderConfigurer) typedValue.getValue();
configurer.configure(partEventBuilder);
childContext.onResponse((e, t) -> {
configurer.eventCompleted();
});
} else if (typedValue.getValue() instanceof Message) {
// If value is a Message then use it directly conserving attributes and properties.
partEventBuilder.message((Message) typedValue.getValue());
} else {
// Otherwise create a new message
partEventBuilder.message(Message.builder().payload(typedValue).build());
}
return Mono.from(just(partEventBuilder.addVariable(counterVariableName, count.incrementAndGet()).build()).transform(nestedChain).doOnNext(completeSuccessIfNeeded(childContext, true)).switchIfEmpty(Mono.from(childContext.getResponsePublisher())).map(result -> builder(parentContext, result).build()).doOnNext(result -> currentEvent.set(CoreEvent.builder(result).build())).doOnError(MessagingException.class, me -> me.setProcessedEvent(builder(parentContext, me.getEvent()).build())).doOnSuccess(result -> {
if (result == null) {
childContext.success();
}
}));
}, // Force sequential execution of the chain for each element
1).switchIfEmpty(defer(() -> {
if (count.get() == 0) {
logger.warn("Split expression returned no results. If this is not expected please check your expression");
return just(request);
} else {
return empty();
}
})).takeLast(1).map(s -> CoreEvent.builder(currentEvent.get()).message(request.getMessage()).build()).errorStrategyStop();
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class DefaultMuleSession method getProperties.
public Map<String, Object> getProperties() {
Map<String, Object> result = new HashMap<>();
for (String key : properties.keySet()) {
TypedValue typedValue = properties.get(key);
result.put(key, typedValue.getValue());
}
return result;
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class SoapOperationExecutor method toSoapAttachments.
private Map<String, SoapAttachment> toSoapAttachments(Map<String, TypedValue<?>> attachments) throws MessageTransformerException, TransformerException {
Map<String, SoapAttachment> soapAttachmentMap = new HashMap<>();
for (Map.Entry<String, TypedValue<?>> attachment : attachments.entrySet()) {
SoapAttachment soapAttachment = new SoapAttachment(toInputStream(attachment.getValue()), attachment.getValue().getDataType().getMediaType());
soapAttachmentMap.put(attachment.getKey(), soapAttachment);
}
return soapAttachmentMap;
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class FunctionExecutionTestCase method echoFromManager.
@Test
public void echoFromManager() {
TypedValue result = expressionManager.evaluate("Fn::defaultPrimitives()");
assertThat(result.getValue(), is("SUCCESS"));
}
Aggregations