use of org.mule.runtime.api.component.execution.ExecutableComponent in project mule by mulesoft.
the class LookupFunction method call.
@Override
public Object call(Object[] parameters, BindingContext context) {
String flowName = (String) parameters[0];
Object payload = parameters[1];
Location componentLocation = Location.builder().globalName(flowName).build();
Component component = componentLocator.find(componentLocation).orElseThrow(() -> new IllegalArgumentException(format("There is no component named '%s'.", flowName)));
if (component instanceof Flow) {
try {
Message incomingMessage = lookupValue(context, MESSAGE, Message.builder().nullValue().build());
Map<String, ?> incomingVariables = lookupValue(context, VARS, EMPTY_MAP);
Error incomingError = lookupValue(context, ERROR, null);
Message message = Message.builder(incomingMessage).value(payload).mediaType(APPLICATION_JAVA).build();
CoreEvent event = CoreEvent.builder(PrivilegedEvent.getCurrentEvent().getContext()).variables(incomingVariables).error(incomingError).message(message).build();
return ((ExecutableComponent) component).execute(event).get().getMessage().getPayload();
} catch (ExecutionException e) {
ComponentExecutionException componentExecutionException = (ComponentExecutionException) e.getCause();
Error error = componentExecutionException.getEvent().getError().get();
throw new MuleRuntimeException(createStaticMessage(format("Flow '%s' has failed with error '%s' (%s)", flowName, error.getErrorType(), error.getDescription())), error.getCause());
} catch (InterruptedException e) {
throw new MuleRuntimeException(e);
}
} else {
throw new IllegalArgumentException(format("Component '%s' is not a flow.", flowName));
}
}
Aggregations