use of org.mule.runtime.api.streaming.CursorProvider in project mule by mulesoft.
the class StreamingUtils method withCursoredEvent.
/**
* Executes the given function {@code f} considering that the given {@code event} might have a {@link CursorProvider} as
* payload. In that case, this method obtains a cursor from the provider and executes the function.
* <p>
* Closing the opened cursor, handling exceptions and return values are all taken care of by this utility method.
*
* @param event an {@link CoreEvent}
* @param f the function to execute
* @return the output {@link CoreEvent}
* @throws MuleException
*/
public static CoreEvent withCursoredEvent(CoreEvent event, CheckedFunction<CoreEvent, CoreEvent> f) throws MuleException {
if (event.getMessage().getPayload() == null) {
return event;
}
Reference<Throwable> exception = new Reference<>();
CheckedFunction<CoreEvent, CoreEvent> function = new CheckedFunction<CoreEvent, CoreEvent>() {
@Override
public CoreEvent applyChecked(CoreEvent event) throws Throwable {
return f.apply(event);
}
@Override
public CoreEvent handleException(Throwable throwable) {
exception.set(unwrap(throwable));
return null;
}
};
Object payload = event.getMessage().getPayload().getValue();
CursorProvider cursorProvider = null;
Cursor cursor = null;
try {
if (payload instanceof CursorProvider) {
cursorProvider = (CursorProvider) payload;
cursor = cursorProvider.openCursor();
event = replacePayload(event, cursor);
}
CoreEvent value = function.apply(event);
if (value == null) {
handlePossibleException(exception);
} else if (value.getMessage().getPayload().getValue() == cursor) {
value = replacePayload(value, cursorProvider);
}
return value;
} finally {
if (cursor != null) {
closeQuietly(cursor);
}
}
}
use of org.mule.runtime.api.streaming.CursorProvider in project mule by mulesoft.
the class DefaultExpressionManagerTestCase method managedCursor.
@Test
public void managedCursor() throws Exception {
final DefaultExpressionLanguageFactoryService mockFactory = mock(DefaultExpressionLanguageFactoryService.class, RETURNS_DEEP_STUBS);
final ExpressionLanguage expressionLanguage = mock(ExpressionLanguage.class, RETURNS_DEEP_STUBS);
final CursorProvider cursorProvider = mock(CursorProvider.class);
Registry registry = mock(Registry.class);
when(registry.lookupByType(DefaultExpressionLanguageFactoryService.class)).thenReturn(of(mockFactory));
when(registry.lookupByName(OBJECT_EXPRESSION_LANGUAGE)).thenReturn(of(mock(MVELExpressionLanguage.class, RETURNS_DEEP_STUBS)));
when(registry.lookupByName(COMPATIBILITY_PLUGIN_INSTALLED)).thenReturn(empty());
TypedValue value = new TypedValue(cursorProvider, BYTE_ARRAY);
when(expressionLanguage.evaluate(anyString(), any())).thenReturn(value);
when(expressionLanguage.evaluate(anyString(), any(), any())).thenReturn(value);
when(mockFactory.create()).thenReturn(expressionLanguage);
expressionManager = new DefaultExpressionManager();
((DefaultExpressionManager) expressionManager).setRegistry(registry);
((DefaultExpressionManager) expressionManager).setStreamingManager(streamingManager);
initialiseIfNeeded(expressionManager, false, muleContext);
final CoreEvent event = testEvent();
when(streamingManager.manage(cursorProvider, event)).thenReturn(cursorProvider);
expressionManager.evaluate("someExpression", event);
verify(streamingManager).manage(cursorProvider, event);
}
use of org.mule.runtime.api.streaming.CursorProvider in project mule by mulesoft.
the class StreamingUtils method updateTypedValueForStreaming.
/**
* Updates the value a given {@link TypedValue} instance by replacing it with a {@link CursorProvider}.
*
* @param value the typed value to update
* @param event the current event
* @param streamingManager the streaming manager
* @return updated {@link TypedValue instance}
*/
public static TypedValue updateTypedValueForStreaming(final TypedValue value, final CoreEvent event, final StreamingManager streamingManager) {
if (event == null) {
return value;
} else {
Object payload = value.getValue();
if (payload instanceof CursorProvider) {
CursorProvider cursorProvider = streamingManager.manage((CursorProvider) payload, event);
DataType dataType = DataType.builder(value.getDataType()).type(cursorProvider.getClass()).build();
return new TypedValue<>(cursorProvider, dataType, value.getByteLength());
}
return value;
}
}
use of org.mule.runtime.api.streaming.CursorProvider in project mule by mulesoft.
the class StreamingUtils method updateTypedValueWithCursorProvider.
/**
* Updates the {@link Cursor} value a given {@link TypedValue} instance by replacing it with a {@link CursorProvider}.
*
* @param value the typed value to update
* @param event the current event
* @param streamingManager the streaming manager
* @return updated {@link TypedValue instance}
*/
public static TypedValue updateTypedValueWithCursorProvider(final TypedValue value, final CoreEvent event, final StreamingManager streamingManager) {
if (event == null) {
return value;
} else {
Object payload = value.getValue();
if (payload instanceof CursorStream) {
CursorProvider provider = ((CursorStream) value.getValue()).getProvider();
DataType dataType = DataType.builder(value.getDataType()).type(provider.getClass()).build();
return new TypedValue(provider, dataType, value.getByteLength());
} else {
return value;
}
}
}
Aggregations