Search in sources :

Example 1 with Source

use of org.mule.runtime.extension.api.runtime.source.Source in project mule by mulesoft.

the class SourceConfigurer method configure.

/**
 * Performs the configuration of the given {@code source} and returns the result
 *
 * @param source a {@link Source}
 * @param config the {@link ConfigurationInstance config instance} associated to {@code this} source object.
 * @return the configured instance
 * @throws MuleException
 */
public Source configure(Source source, Optional<ConfigurationInstance> config) throws MuleException {
    ResolverSetBasedObjectBuilder<Source> builder = new ResolverSetBasedObjectBuilder<Source>(source.getClass(), model, resolverSet) {

        @Override
        protected Source instantiateObject() {
            return source;
        }

        @Override
        public Source build(ValueResolvingContext context) throws MuleException {
            Source source = build(resolverSet.resolve(context));
            injectDefaultEncoding(model, source, muleContext.getConfiguration().getDefaultEncoding());
            injectComponentLocation(source, componentLocation);
            config.ifPresent(c -> injectRefName(source, c.getName(), getReflectionCache()));
            return source;
        }
    };
    CoreEvent initialiserEvent = null;
    try {
        initialiserEvent = getInitialiserEvent(muleContext);
        Source configuredSource = builder.build(from(initialiserEvent, config));
        if (configuredSource instanceof PollingSource) {
            Scheduler scheduler = (Scheduler) resolverSet.getResolvers().get(SCHEDULING_STRATEGY_PARAMETER_NAME).resolve(ValueResolvingContext.from(initialiserEvent));
            configuredSource = new PollingSourceWrapper((PollingSource) configuredSource, scheduler);
        }
        return configuredSource;
    } catch (Exception e) {
        throw new MuleRuntimeException(createStaticMessage("Exception was found trying to configure source of type " + source.getClass().getName()), e);
    } finally {
        if (initialiserEvent != null) {
            ((BaseEventContext) initialiserEvent.getContext()).success();
        }
    }
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Scheduler(org.mule.runtime.core.api.source.scheduler.Scheduler) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ResolverSetBasedObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ResolverSetBasedObjectBuilder) ValueResolvingContext(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) Source(org.mule.runtime.extension.api.runtime.source.Source) PollingSourceWrapper(org.mule.runtime.module.extension.internal.runtime.source.poll.PollingSourceWrapper) MuleException(org.mule.runtime.api.exception.MuleException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 2 with Source

use of org.mule.runtime.extension.api.runtime.source.Source in project mule by mulesoft.

the class SourceAdapter method doCreateCompletionHandler.

private SourceCompletionHandlerFactory doCreateCompletionHandler(SourceCallbackModelProperty modelProperty) {
    SourceCallbackExecutor onSuccessExecutor;
    SourceCallbackExecutor onErrorExecutor;
    SourceCallbackExecutor onTerminateExecutor;
    SourceCallbackExecutor onBackPressureExecutor;
    if (source instanceof SourceWrapper) {
        SourceWrapper wrapper = (SourceWrapper) source;
        onSuccessExecutor = getMethodExecutor(modelProperty.getOnSuccessMethod(), modelProperty, wrapper::onSuccess);
        onErrorExecutor = getMethodExecutor(modelProperty.getOnErrorMethod(), modelProperty, wrapper::onError);
        onTerminateExecutor = getMethodExecutor(modelProperty.getOnTerminateMethod(), modelProperty, wrapper::onTerminate);
        onBackPressureExecutor = getMethodExecutor(modelProperty.getOnBackPressureMethod(), modelProperty, wrapper::onBackPressure);
    } else {
        onSuccessExecutor = getMethodExecutor(modelProperty.getOnSuccessMethod(), modelProperty);
        onErrorExecutor = getMethodExecutor(modelProperty.getOnErrorMethod(), modelProperty);
        onTerminateExecutor = getMethodExecutor(modelProperty.getOnTerminateMethod(), modelProperty);
        onBackPressureExecutor = getMethodExecutor(modelProperty.getOnBackPressureMethod(), modelProperty);
    }
    return context -> new DefaultSourceCompletionHandler(onSuccessExecutor, onErrorExecutor, onTerminateExecutor, onBackPressureExecutor, context);
}
Also used : SourceTransactionalAction(org.mule.runtime.extension.api.tx.SourceTransactionalAction) TransactionalActionModelProperty(org.mule.runtime.extension.internal.property.TransactionalActionModelProperty) Mono.create(reactor.core.publisher.Mono.create) ReflectionUtils.withAnnotation(org.reflections.ReflectionUtils.withAnnotation) Optional.of(java.util.Optional.of) FAIL(org.mule.runtime.extension.api.runtime.source.BackPressureAction.FAIL) Source(org.mule.runtime.extension.api.runtime.source.Source) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) PollingSourceWrapper(org.mule.runtime.module.extension.internal.runtime.source.poll.PollingSourceWrapper) ReactiveReconnectionCallback(org.mule.runtime.module.extension.internal.runtime.connectivity.ReactiveReconnectionCallback) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) FLOW_BACK_PRESSURE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Unhandleable.FLOW_BACK_PRESSURE) Map(java.util.Map) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ModelProperty(org.mule.runtime.api.meta.model.ModelProperty) Method(java.lang.reflect.Method) TRANSACTIONAL_TYPE_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TRANSACTIONAL_TYPE_PARAMETER_NAME) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) Startable(org.mule.runtime.api.lifecycle.Startable) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) Reconnectable(org.mule.runtime.extension.api.runtime.connectivity.Reconnectable) Connection(org.mule.runtime.extension.api.annotation.param.Connection) Set(java.util.Set) TRANSACTIONAL_ACTION_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TRANSACTIONAL_ACTION_PARAMETER_NAME) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigurationInstance(org.mule.runtime.extension.api.runtime.config.ConfigurationInstance) String.format(java.lang.String.format) SourceCallbackModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.SourceCallbackModelProperty) MuleExtensionUtils.getInitialiserEvent(org.mule.runtime.module.extension.api.util.MuleExtensionUtils.getInitialiserEvent) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) TransactionException(org.mule.runtime.api.tx.TransactionException) ErrorType(org.mule.runtime.api.message.ErrorType) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) NONE(org.mule.runtime.extension.api.tx.SourceTransactionalAction.NONE) CursorProviderFactory(org.mule.runtime.core.api.streaming.CursorProviderFactory) BackPressureAction(org.mule.runtime.extension.api.runtime.source.BackPressureAction) ValueResolvingContext.from(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext.from) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) Optional.empty(java.util.Optional.empty) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) LOCAL(org.mule.runtime.api.tx.TransactionType.LOCAL) TransactionalTypeModelProperty(org.mule.runtime.extension.internal.property.TransactionalTypeModelProperty) ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) StreamingManager(org.mule.runtime.core.api.streaming.StreamingManager) Supplier(java.util.function.Supplier) BACK_PRESSURE_ACTION_CONTEXT_PARAM(org.mule.runtime.module.extension.internal.ExtensionProperties.BACK_PRESSURE_ACTION_CONTEXT_PARAM) Config(org.mule.runtime.extension.api.annotation.param.Config) Inject(javax.inject.Inject) IntrospectionUtils.getSourceName(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getSourceName) MuleContext(org.mule.runtime.core.api.MuleContext) FieldSetter(org.mule.runtime.module.extension.internal.util.FieldSetter) MuleException(org.mule.runtime.api.exception.MuleException) CollectionUtils(org.apache.commons.collections.CollectionUtils) Component(org.mule.runtime.api.component.Component) Mono.from(reactor.core.publisher.Mono.from) Collections.emptyMap(java.util.Collections.emptyMap) SourceCallback(org.mule.runtime.extension.api.runtime.source.SourceCallback) Logger(org.slf4j.Logger) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Field(java.lang.reflect.Field) TransactionType(org.mule.runtime.api.tx.TransactionType) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) Either(org.mule.runtime.core.api.functional.Either) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) ReflectionUtils.getAllFields(org.reflections.ReflectionUtils.getAllFields) ResolverSetResult(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSetResult) IntrospectionUtils.getFieldsOfType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getFieldsOfType) PollingSourceWrapper(org.mule.runtime.module.extension.internal.runtime.source.poll.PollingSourceWrapper)

Aggregations

MuleException (org.mule.runtime.api.exception.MuleException)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)2 Source (org.mule.runtime.extension.api.runtime.source.Source)2 PollingSourceWrapper (org.mule.runtime.module.extension.internal.runtime.source.poll.PollingSourceWrapper)2 String.format (java.lang.String.format)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collections.emptyMap (java.util.Collections.emptyMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 Optional.of (java.util.Optional.of)1 Set (java.util.Set)1 Supplier (java.util.function.Supplier)1 Inject (javax.inject.Inject)1 CollectionUtils (org.apache.commons.collections.CollectionUtils)1