Search in sources :

Example 1 with CursorProvider

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);
        }
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Reference(org.mule.runtime.api.util.Reference) Cursor(org.mule.runtime.api.streaming.Cursor) CheckedFunction(org.mule.runtime.core.api.util.func.CheckedFunction)

Example 2 with CursorProvider

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);
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultExpressionLanguageFactoryService(org.mule.runtime.api.el.DefaultExpressionLanguageFactoryService) Registry(org.mule.runtime.api.artifact.Registry) MVELExpressionLanguage(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage) ExpressionLanguage(org.mule.runtime.api.el.ExpressionLanguage) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 3 with CursorProvider

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;
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 4 with CursorProvider

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;
        }
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) CursorStream(org.mule.runtime.api.streaming.bytes.CursorStream) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Aggregations

CursorProvider (org.mule.runtime.api.streaming.CursorProvider)4 TypedValue (org.mule.runtime.api.metadata.TypedValue)3 CollectionDataType (org.mule.runtime.api.metadata.CollectionDataType)2 DataType (org.mule.runtime.api.metadata.DataType)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 Test (org.junit.Test)1 Registry (org.mule.runtime.api.artifact.Registry)1 DefaultExpressionLanguageFactoryService (org.mule.runtime.api.el.DefaultExpressionLanguageFactoryService)1 ExpressionLanguage (org.mule.runtime.api.el.ExpressionLanguage)1 Cursor (org.mule.runtime.api.streaming.Cursor)1 CursorStream (org.mule.runtime.api.streaming.bytes.CursorStream)1 Reference (org.mule.runtime.api.util.Reference)1 CheckedFunction (org.mule.runtime.core.api.util.func.CheckedFunction)1 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)1