Search in sources :

Example 1 with DescriptiveResource

use of org.springframework.core.io.DescriptiveResource in project spring-framework by spring-projects.

the class ConfigurationClassPostProcessorTests method postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed.

@Test
void postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed() {
    RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
    rbd.setResource(new DescriptiveResource("XML or something"));
    beanFactory.registerBeanDefinition("bar", rbd);
    beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
    beanFactory.setAllowBeanDefinitionOverriding(false);
    ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
    assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> pp.postProcessBeanFactory(beanFactory)).withMessageContaining("bar").withMessageContaining("SingletonBeanConfig").withMessageContaining(TestBean.class.getName());
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DescriptiveResource(org.springframework.core.io.DescriptiveResource) Test(org.junit.jupiter.api.Test)

Example 2 with DescriptiveResource

use of org.springframework.core.io.DescriptiveResource in project spring-framework by spring-projects.

the class GroovyBeanDefinitionReader method createDynamicElementReader.

private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
    XmlReaderContext readerContext = this.groovyDslXmlBeanDefinitionReader.createReaderContext(new DescriptiveResource("Groovy"));
    BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
    boolean decorating = (this.currentBeanDefinition != null);
    if (!decorating) {
        this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);
    }
    return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) {

        @Override
        protected void afterInvocation() {
            if (!this.decorating) {
                currentBeanDefinition = null;
            }
        }
    };
}
Also used : BeanDefinitionParserDelegate(org.springframework.beans.factory.xml.BeanDefinitionParserDelegate) XmlReaderContext(org.springframework.beans.factory.xml.XmlReaderContext) DescriptiveResource(org.springframework.core.io.DescriptiveResource)

Example 3 with DescriptiveResource

use of org.springframework.core.io.DescriptiveResource in project spring-framework by spring-projects.

the class GroovyBeanDefinitionReader method xmlns.

/**
	 * Define a Spring XML namespace definition to use.
	 * @param definition the namespace definition
	 */
public void xmlns(Map<String, String> definition) {
    if (!definition.isEmpty()) {
        for (Map.Entry<String, String> entry : definition.entrySet()) {
            String namespace = entry.getKey();
            String uri = entry.getValue();
            if (uri == null) {
                throw new IllegalArgumentException("Namespace definition must supply a non-null URI");
            }
            NamespaceHandler namespaceHandler = this.groovyDslXmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve(uri);
            if (namespaceHandler == null) {
                throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri, new Location(new DescriptiveResource(("Groovy")))));
            }
            this.namespaces.put(namespace, uri);
        }
    }
}
Also used : BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) NamespaceHandler(org.springframework.beans.factory.xml.NamespaceHandler) DescriptiveResource(org.springframework.core.io.DescriptiveResource) Problem(org.springframework.beans.factory.parsing.Problem) GString(groovy.lang.GString) HashMap(java.util.HashMap) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap) Location(org.springframework.beans.factory.parsing.Location)

Example 4 with DescriptiveResource

use of org.springframework.core.io.DescriptiveResource in project spring-framework by spring-projects.

the class FailFastProblemReporterTests method testError.

@Test(expected = BeanDefinitionParsingException.class)
public void testError() throws Exception {
    FailFastProblemReporter reporter = new FailFastProblemReporter();
    reporter.error(new Problem("VGER", new Location(new DescriptiveResource("here")), null, new IllegalArgumentException()));
}
Also used : DescriptiveResource(org.springframework.core.io.DescriptiveResource) Test(org.junit.Test)

Example 5 with DescriptiveResource

use of org.springframework.core.io.DescriptiveResource in project spring-integration by spring-projects.

the class IntegrationFlowBeanPostProcessor method processStandardIntegrationFlow.

private Object processStandardIntegrationFlow(StandardIntegrationFlow flow, String flowBeanName) {
    String flowNamePrefix = flowBeanName + ".";
    int subFlowNameIndex = 0;
    int channelNameIndex = 0;
    Map<Object, String> integrationComponents = flow.getIntegrationComponents();
    Map<Object, String> targetIntegrationComponents = new LinkedHashMap<>(integrationComponents.size());
    for (Map.Entry<Object, String> entry : integrationComponents.entrySet()) {
        Object component = entry.getKey();
        if (component instanceof ConsumerEndpointSpec) {
            ConsumerEndpointSpec<?, ?> endpointSpec = (ConsumerEndpointSpec<?, ?>) component;
            MessageHandler messageHandler = endpointSpec.get().getT2();
            ConsumerEndpointFactoryBean endpoint = endpointSpec.get().getT1();
            String id = endpointSpec.getId();
            if (id == null) {
                id = generateBeanName(endpoint, entry.getValue());
            }
            Collection<?> messageHandlers = this.beanFactory.getBeansOfType(messageHandler.getClass(), false, false).values();
            if (!messageHandlers.contains(messageHandler)) {
                String handlerBeanName = generateBeanName(messageHandler);
                String[] handlerAlias = new String[] { id + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX };
                registerComponent(messageHandler, handlerBeanName, flowBeanName);
                for (String alias : handlerAlias) {
                    this.beanFactory.registerAlias(handlerBeanName, alias);
                }
            }
            registerComponent(endpoint, id, flowBeanName);
            targetIntegrationComponents.put(endpoint, id);
        } else {
            Collection<?> values = this.beanFactory.getBeansOfType(component.getClass(), false, false).values();
            if (!values.contains(component)) {
                if (component instanceof AbstractMessageChannel) {
                    String channelBeanName = ((AbstractMessageChannel) component).getComponentName();
                    if (channelBeanName == null) {
                        channelBeanName = entry.getValue();
                        if (channelBeanName == null) {
                            channelBeanName = flowNamePrefix + "channel" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + channelNameIndex++;
                        }
                    }
                    registerComponent(component, channelBeanName, flowBeanName);
                    targetIntegrationComponents.put(component, channelBeanName);
                } else if (component instanceof MessageChannelReference) {
                    String channelBeanName = ((MessageChannelReference) component).getName();
                    if (!this.beanFactory.containsBean(channelBeanName)) {
                        DirectChannel directChannel = new DirectChannel();
                        registerComponent(directChannel, channelBeanName, flowBeanName);
                        targetIntegrationComponents.put(directChannel, channelBeanName);
                    }
                } else if (component instanceof FixedSubscriberChannel) {
                    FixedSubscriberChannel fixedSubscriberChannel = (FixedSubscriberChannel) component;
                    String channelBeanName = fixedSubscriberChannel.getComponentName();
                    if ("Unnamed fixed subscriber channel".equals(channelBeanName)) {
                        channelBeanName = flowNamePrefix + "channel" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + channelNameIndex++;
                    }
                    registerComponent(component, channelBeanName, flowBeanName);
                    targetIntegrationComponents.put(component, channelBeanName);
                } else if (component instanceof SourcePollingChannelAdapterSpec) {
                    SourcePollingChannelAdapterSpec spec = (SourcePollingChannelAdapterSpec) component;
                    Map<Object, String> componentsToRegister = spec.getComponentsToRegister();
                    if (!CollectionUtils.isEmpty(componentsToRegister)) {
                        componentsToRegister.entrySet().stream().filter(o -> !this.beanFactory.getBeansOfType(o.getKey().getClass(), false, false).values().contains(o.getKey())).forEach(o -> registerComponent(o.getKey(), generateBeanName(o.getKey(), o.getValue())));
                    }
                    SourcePollingChannelAdapterFactoryBean pollingChannelAdapterFactoryBean = spec.get().getT1();
                    String id = spec.getId();
                    if (!StringUtils.hasText(id)) {
                        id = generateBeanName(pollingChannelAdapterFactoryBean, entry.getValue());
                    }
                    registerComponent(pollingChannelAdapterFactoryBean, id, flowBeanName);
                    targetIntegrationComponents.put(pollingChannelAdapterFactoryBean, id);
                    MessageSource<?> messageSource = spec.get().getT2();
                    if (!this.beanFactory.getBeansOfType(messageSource.getClass(), false, false).values().contains(messageSource)) {
                        String messageSourceId = id + ".source";
                        if (messageSource instanceof NamedComponent && ((NamedComponent) messageSource).getComponentName() != null) {
                            messageSourceId = ((NamedComponent) messageSource).getComponentName();
                        }
                        registerComponent(messageSource, messageSourceId, flowBeanName);
                    }
                } else if (component instanceof StandardIntegrationFlow) {
                    String subFlowBeanName = entry.getValue() != null ? entry.getValue() : flowNamePrefix + "subFlow" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + subFlowNameIndex++;
                    registerComponent(component, subFlowBeanName, flowBeanName);
                    targetIntegrationComponents.put(component, subFlowBeanName);
                } else if (component instanceof AnnotationGatewayProxyFactoryBean) {
                    AnnotationGatewayProxyFactoryBean gateway = (AnnotationGatewayProxyFactoryBean) component;
                    String gatewayId = entry.getValue();
                    if (gatewayId == null) {
                        gatewayId = gateway.getComponentName();
                    }
                    if (gatewayId == null) {
                        gatewayId = flowNamePrefix + "gateway";
                    }
                    registerComponent(gateway, gatewayId, flowBeanName, beanDefinition -> {
                        ((AbstractBeanDefinition) beanDefinition).setSource(new DescriptiveResource(gateway.getObjectType().getName()));
                    });
                    targetIntegrationComponents.put(component, gatewayId);
                } else {
                    String generatedBeanName = generateBeanName(component, entry.getValue());
                    registerComponent(component, generatedBeanName, flowBeanName);
                    targetIntegrationComponents.put(component, generatedBeanName);
                }
            } else {
                targetIntegrationComponents.put(entry.getKey(), entry.getValue());
            }
        }
    }
    flow.setIntegrationComponents(targetIntegrationComponents);
    return flow;
}
Also used : DescriptiveResource(org.springframework.core.io.DescriptiveResource) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) NamedComponent(org.springframework.integration.support.context.NamedComponent) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageSource(org.springframework.integration.core.MessageSource) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) LinkedHashMap(java.util.LinkedHashMap) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) Map(java.util.Map) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanDefinitionCustomizer(org.springframework.beans.factory.config.BeanDefinitionCustomizer) MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) Collection(java.util.Collection) BeansException(org.springframework.beans.BeansException) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) SmartInitializingSingleton(org.springframework.beans.factory.SmartInitializingSingleton) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) IntegrationConfigUtils(org.springframework.integration.config.IntegrationConfigUtils) MessageHandler(org.springframework.messaging.MessageHandler) CollectionUtils(org.springframework.util.CollectionUtils) BeanFactory(org.springframework.beans.factory.BeanFactory) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) AnnotationGatewayProxyFactoryBean(org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean) DirectChannel(org.springframework.integration.channel.DirectChannel) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) LinkedHashMap(java.util.LinkedHashMap) MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) AnnotationGatewayProxyFactoryBean(org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) MessageSource(org.springframework.integration.core.MessageSource) DescriptiveResource(org.springframework.core.io.DescriptiveResource) NamedComponent(org.springframework.integration.support.context.NamedComponent) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Aggregations

DescriptiveResource (org.springframework.core.io.DescriptiveResource)8 Test (org.junit.jupiter.api.Test)4 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 Map (java.util.Map)2 GString (groovy.lang.GString)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Log (org.apache.commons.logging.Log)1 Test (org.junit.Test)1 BeansException (org.springframework.beans.BeansException)1 BeanCreationNotAllowedException (org.springframework.beans.factory.BeanCreationNotAllowedException)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)1 BeanFactoryUtils (org.springframework.beans.factory.BeanFactoryUtils)1 SmartInitializingSingleton (org.springframework.beans.factory.SmartInitializingSingleton)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanDefinitionCustomizer (org.springframework.beans.factory.config.BeanDefinitionCustomizer)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1