Search in sources :

Example 36 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class MuleArtifactContext method createApplicationComponents.

/**
 * Creates te definition for all the objects to be created form the enabled components in the {@code applicationModel}.
 *
 * @param beanFactory the bean factory in which definition must be created.
 * @param applicationModel the artifact application model.
 * @param mustBeRoot if the component must be root to be created.
 * @return an order list of the created bean names. The order must be respected for the creation of the objects.
 */
protected List<String> createApplicationComponents(DefaultListableBeanFactory beanFactory, ApplicationModel applicationModel, boolean mustBeRoot) {
    // This should only be done once at the initial application model creation, called from Spring
    List<Pair<ComponentModel, Optional<String>>> objectProvidersByName = lookObjectProvidersComponentModels(applicationModel);
    List<String> createdComponentModels = new ArrayList<>();
    applicationModel.executeOnEveryMuleComponentTree(cm -> {
        SpringComponentModel componentModel = (SpringComponentModel) cm;
        if (!mustBeRoot || componentModel.isRoot()) {
            if (componentModel.getIdentifier().equals(MULE_IDENTIFIER) || componentModel.getIdentifier().equals(MULE_DOMAIN_IDENTIFIER) || componentModel.getIdentifier().equals(MULE_EE_DOMAIN_IDENTIFIER)) {
                return;
            }
            SpringComponentModel parentComponentModel = componentModel.getParent() != null ? (SpringComponentModel) componentModel.getParent() : (SpringComponentModel) applicationModel.getRootComponentModel();
            if (componentModel.isEnabled()) {
                if (componentModel.getNameAttribute() != null && componentModel.isRoot()) {
                    createdComponentModels.add(componentModel.getNameAttribute());
                }
                beanDefinitionFactory.resolveComponentRecursively(parentComponentModel, componentModel, beanFactory, (resolvedComponentModel, registry) -> {
                    SpringComponentModel resolvedSpringComponentModel = (SpringComponentModel) resolvedComponentModel;
                    if (resolvedComponentModel.isRoot()) {
                        String nameAttribute = resolvedComponentModel.getNameAttribute();
                        if (resolvedComponentModel.getIdentifier().equals(CONFIGURATION_IDENTIFIER)) {
                            nameAttribute = OBJECT_MULE_CONFIGURATION;
                        } else if (nameAttribute == null) {
                            // This may be a configuration that does not requires a name.
                            nameAttribute = uniqueValue(resolvedSpringComponentModel.getBeanDefinition().getBeanClassName());
                        }
                        registry.registerBeanDefinition(nameAttribute, resolvedSpringComponentModel.getBeanDefinition());
                        postProcessBeanDefinition(componentModel, registry, nameAttribute);
                    }
                }, null, componentLocator);
            } else {
                beanDefinitionFactory.resolveComponentRecursively(parentComponentModel, componentModel, beanFactory, null, null, componentLocator);
            }
            componentLocator.addComponentLocation(cm.getComponentLocation());
        }
    });
    this.objectProviders.addAll(objectProvidersByName.stream().map(pair -> (ConfigurableObjectProvider) pair.getFirst().getObjectInstance()).collect(toList()));
    registerObjectFromObjectProviders(beanFactory);
    Set<String> alwaysEnabledComponents = dependencyResolver.resolveAlwaysEnabledComponents().stream().map(dependencyNode -> dependencyNode.getComponentName()).collect(toSet());
    Set<String> objectProviderNames = objectProvidersByName.stream().map(Pair::getSecond).filter(Optional::isPresent).map(Optional::get).collect(toSet());
    // Put object providers first, then always enabled components, then the rest
    createdComponentModels.sort(Comparator.comparing(beanName -> {
        if (objectProviderNames.contains(beanName)) {
            return 1;
        } else if (alwaysEnabledComponents.contains(beanName)) {
            return 2;
        } else {
            return 3;
        }
    }));
    return createdComponentModels;
}
Also used : ClassLoaderResourceProvider(org.mule.runtime.config.internal.dsl.model.ClassLoaderResourceProvider) CORE_PREFIX(org.mule.runtime.internal.dsl.DslConstants.CORE_PREFIX) SimpleConfigAttribute(org.mule.runtime.config.api.dsl.processor.SimpleConfigAttribute) Optional.of(java.util.Optional.of) Thread.currentThread(java.lang.Thread.currentThread) ContextAnnotationAutowireCandidateResolver(org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MuleInjectorProcessor(org.mule.runtime.config.internal.processor.MuleInjectorProcessor) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) BeanDefinitionFactory(org.mule.runtime.config.internal.dsl.spring.BeanDefinitionFactory) Arrays.asList(java.util.Arrays.asList) Document(org.w3c.dom.Document) Map(java.util.Map) MULE_EE_DOMAIN_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_EE_DOMAIN_IDENTIFIER) PostRegistrationActionsPostProcessor(org.mule.runtime.config.internal.processor.PostRegistrationActionsPostProcessor) ComponentBuildingDefinitionUtils.registerComponentBuildingDefinitions(org.mule.runtime.config.internal.util.ComponentBuildingDefinitionUtils.registerComponentBuildingDefinitions) Resource(org.springframework.core.io.Resource) ConfigurableObjectProvider(org.mule.runtime.api.ioc.ConfigurableObjectProvider) Set(java.util.Set) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME(org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME) ComponentLocatorCreatePostProcessor(org.mule.runtime.config.internal.processor.ComponentLocatorCreatePostProcessor) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Optional.empty(java.util.Optional.empty) MulePropertyEditorRegistrar(org.mule.runtime.config.internal.editors.MulePropertyEditorRegistrar) BeanDefinitionBuilder.genericBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) TransformerResolver(org.mule.runtime.core.internal.registry.TransformerResolver) DefaultRegistry(org.mule.runtime.core.internal.registry.DefaultRegistry) ArrayList(java.util.ArrayList) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MULE_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_IDENTIFIER) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Component(org.mule.runtime.api.component.Component) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) XmlConfigurationDocumentLoader.noValidationDocumentLoader(org.mule.runtime.config.api.XmlConfigurationDocumentLoader.noValidationDocumentLoader) Registry(org.mule.runtime.api.artifact.Registry) ConfigurationDependencyResolver(org.mule.runtime.config.internal.dsl.model.ConfigurationDependencyResolver) ConfigFile(org.mule.runtime.config.api.dsl.processor.ConfigFile) REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME(org.springframework.context.annotation.AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME) LaxInstantiationStrategyWrapper(org.mule.runtime.config.internal.util.LaxInstantiationStrategyWrapper) ManagedMap(org.springframework.beans.factory.support.ManagedMap) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) ComponentModelHelper.updateAnnotationValue(org.mule.runtime.config.internal.dsl.spring.ComponentModelHelper.updateAnnotationValue) ApplicationModel(org.mule.runtime.config.internal.model.ApplicationModel) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) ArtifactConfig(org.mule.runtime.config.api.dsl.processor.ArtifactConfig) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) LifecycleUtils.disposeIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.disposeIfNeeded) SPRING_SINGLETON_OBJECT(org.mule.runtime.config.internal.dsl.spring.BeanDefinitionFactory.SPRING_SINGLETON_OBJECT) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ResourceProvider(org.mule.runtime.config.api.dsl.model.ResourceProvider) CglibSubclassingInstantiationStrategy(org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy) LoggerFactory(org.slf4j.LoggerFactory) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ManagedList(org.springframework.beans.factory.support.ManagedList) XmlApplicationParser(org.mule.runtime.config.api.dsl.processor.xml.XmlApplicationParser) OBJECT_REGISTRY(org.mule.runtime.core.api.config.MuleProperties.OBJECT_REGISTRY) DslResolvingContext(org.mule.runtime.api.dsl.DslResolvingContext) Preconditions.checkState(org.mule.runtime.api.util.Preconditions.checkState) ServiceRegistry(org.mule.runtime.core.api.registry.ServiceRegistry) Pair(org.mule.runtime.api.util.Pair) DOMAIN(org.mule.runtime.core.api.config.bootstrap.ArtifactType.DOMAIN) Collectors.toSet(java.util.stream.Collectors.toSet) XmlConfigurationDocumentLoader(org.mule.runtime.config.api.XmlConfigurationDocumentLoader) EnvironmentPropertiesConfigurationProvider(org.mule.runtime.config.internal.dsl.model.config.EnvironmentPropertiesConfigurationProvider) MULE_DOMAIN_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_DOMAIN_IDENTIFIER) POLICY(org.mule.runtime.core.api.config.bootstrap.ArtifactType.POLICY) ImmutableSet(com.google.common.collect.ImmutableSet) IMPORT_ELEMENT(org.mule.runtime.config.api.dsl.CoreDslConstants.IMPORT_ELEMENT) ObjectProvider(org.mule.runtime.api.ioc.ObjectProvider) OBJECT_MULE_CONTEXT(org.mule.runtime.core.api.config.MuleProperties.OBJECT_MULE_CONTEXT) String.format(java.lang.String.format) AbstractRefreshableConfigApplicationContext(org.springframework.context.support.AbstractRefreshableConfigApplicationContext) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) List(java.util.List) APP(org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP) AutoIdUtils.uniqueValue(org.mule.runtime.config.internal.parsers.generic.AutoIdUtils.uniqueValue) Optional(java.util.Optional) OBJECT_MULE_CONFIGURATION(org.mule.runtime.core.api.config.MuleProperties.OBJECT_MULE_CONFIGURATION) MuleRegistryHelper(org.mule.runtime.core.internal.registry.MuleRegistryHelper) Converter(org.mule.runtime.core.api.transformer.Converter) ComponentBuildingDefinitionRegistry(org.mule.runtime.config.api.dsl.model.ComponentBuildingDefinitionRegistry) UrlResource(org.springframework.core.io.UrlResource) ConfigurationProperties(org.mule.runtime.api.component.ConfigurationProperties) MuleContext(org.mule.runtime.core.api.MuleContext) ImmutableList(com.google.common.collect.ImmutableList) ArtifactDeclaration(org.mule.runtime.app.declaration.api.ArtifactDeclaration) XmlApplicationServiceRegistry(org.mule.runtime.config.api.dsl.processor.xml.XmlApplicationServiceRegistry) DiscardedOptionalBeanPostProcessor(org.mule.runtime.config.internal.processor.DiscardedOptionalBeanPostProcessor) RuntimeConfigurationException(org.mule.runtime.config.internal.dsl.model.config.RuntimeConfigurationException) ConfigLine(org.mule.runtime.config.api.dsl.processor.ConfigLine) IOUtils(org.mule.runtime.core.api.util.IOUtils) ROOT_CONTAINER_NAME_KEY(org.mule.runtime.api.component.AbstractComponent.ROOT_CONTAINER_NAME_KEY) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) Optional.ofNullable(java.util.Optional.ofNullable) PropertyValue(org.springframework.beans.PropertyValue) ConfigResource(org.mule.runtime.core.api.config.ConfigResource) DefaultConfigurationPropertiesResolver(org.mule.runtime.config.internal.dsl.model.config.DefaultConfigurationPropertiesResolver) XmlConfigurationDocumentLoader.schemaValidatingDocumentLoader(org.mule.runtime.config.api.XmlConfigurationDocumentLoader.schemaValidatingDocumentLoader) LifecycleStatePostProcessor(org.mule.runtime.config.internal.processor.LifecycleStatePostProcessor) Collectors.toList(java.util.stream.Collectors.toList) ArtifactType(org.mule.runtime.core.api.config.bootstrap.ArtifactType) ConfigurationClassPostProcessor(org.springframework.context.annotation.ConfigurationClassPostProcessor) ExtensionManager(org.mule.runtime.core.api.extension.ExtensionManager) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) RequiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) CONFIGURATION_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.CONFIGURATION_IDENTIFIER) Comparator(java.util.Comparator) InputStream(java.io.InputStream) Optional(java.util.Optional) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ArrayList(java.util.ArrayList) Pair(org.mule.runtime.api.util.Pair)

Example 37 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method interceptorApplied.

@Test
public void interceptorApplied() throws Exception {
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {
    });
    startFlowWithInterceptors(interceptor);
    CoreEvent result = process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    assertThat(result.getMessage().getPayload().getValue(), is(""));
    assertThat(result.getError().isPresent(), is(false));
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor);
        inOrder.verify(interceptor).before(eq(((Component) processor).getLocation()), mapArgWithEntry("param", ""), any());
        inOrder.verify(interceptor).around(eq(((Component) processor).getLocation()), mapArgWithEntry("param", ""), any(), any());
        inOrder.verify(processor).process(argThat(hasPayloadValue("")));
        inOrder.verify(interceptor).after(eq(((Component) processor).getLocation()), any(), eq(empty()));
        assertThat(((InternalEvent) result).getInternalParameters().entrySet(), hasSize(0));
        verifyParametersResolvedAndDisposed(times(1));
    }
}
Also used : InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Component(org.mule.runtime.api.component.Component) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 38 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method paramWithErrorExpression.

@Test
public void paramWithErrorExpression() throws Exception {
    Component annotatedProcessor = (Component) processor;
    Map<QName, Object> annotations = new HashMap<>(annotatedProcessor.getAnnotations());
    Map<String, String> params = new HashMap<>((Map<String, String>) annotations.get(ANNOTATION_PARAMETERS));
    params.put("errorExpr", "#[notAnExpression]");
    annotations.put(ANNOTATION_PARAMETERS, params);
    ((Component) processor).setAnnotations(annotations);
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {
    });
    startFlowWithInterceptors(interceptor);
    process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor);
        inOrder.verify(interceptor).before(any(), mapArgWithErrorEntry("errorExpr", instanceOf(ExpressionRuntimeException.class)), any());
        inOrder.verify(interceptor).around(any(), mapArgWithErrorEntry("errorExpr", instanceOf(ExpressionRuntimeException.class)), any(), any());
        inOrder.verify(processor).process(any());
        inOrder.verify(interceptor).after(any(), any(), any());
        verifyParametersResolvedAndDisposed(times(1));
    }
}
Also used : InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Component(org.mule.runtime.api.component.Component) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 39 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class LoggerMessageProcessorTestCase method buildExpressionManager.

private ExtendedExpressionManager buildExpressionManager() {
    ExtendedExpressionManager expressionLanguage = mock(ExtendedExpressionManager.class);
    when(expressionLanguage.parseLogTemplate(anyString(), any(CoreEvent.class), eq(((Component) flow).getLocation()), any(BindingContext.class))).thenReturn("text to log");
    return expressionLanguage;
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ExtendedExpressionManager(org.mule.runtime.core.api.el.ExtendedExpressionManager) Component(org.mule.runtime.api.component.Component) BindingContext(org.mule.runtime.api.el.BindingContext)

Aggregations

Component (org.mule.runtime.api.component.Component)39 Test (org.junit.Test)15 Map (java.util.Map)12 AbstractComponent (org.mule.runtime.api.component.AbstractComponent)12 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)12 SmallTest (org.mule.tck.size.SmallTest)11 Optional (java.util.Optional)9 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)9 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)9 List (java.util.List)8 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)8 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)7 HashMap (java.util.HashMap)6 String.format (java.lang.String.format)5 Optional.empty (java.util.Optional.empty)5 Optional.of (java.util.Optional.of)5 Set (java.util.Set)5 Inject (javax.inject.Inject)5 Arrays.asList (java.util.Arrays.asList)4 Collection (java.util.Collection)4