Search in sources :

Example 76 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project com.revolsys.open by revolsys.

the class JndiObjectFactory method createInstance.

@Override
protected Object createInstance() throws Exception {
    final Hashtable<Object, Object> initialEnvironment = new Hashtable<>();
    final String initialFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
    final BeanFactory beanFactory = getBeanFactory();
    if (initialFactory != null) {
        initialEnvironment.put(Context.INITIAL_CONTEXT_FACTORY, initialFactory);
    }
    Object savedObject;
    if (initialFactory == null && !NamingManager.hasInitialContextFactoryBuilder()) {
        final SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
        synchronized (this.beanName.intern()) {
            savedObject = beanFactory.getBean(this.beanName);
            builder.bind(this.jndiUrl, savedObject);
        }
    } else {
        Context context;
        if (NamingManager.hasInitialContextFactoryBuilder()) {
            context = NamingManager.getInitialContext(initialEnvironment);
        } else {
            context = new InitialContext();
        }
        try {
            savedObject = context.lookup(this.jndiUrl);
        } catch (final NameNotFoundException e) {
            synchronized (this.beanName.intern()) {
                savedObject = beanFactory.getBean(this.beanName);
                try {
                    context.bind(this.jndiUrl, savedObject);
                } catch (final NameNotFoundException e2) {
                }
            }
        }
        if (savedObject == null) {
            synchronized (this.beanName.intern()) {
                Logs.error(this, "JNDI data source was null " + this.jndiUrl);
                savedObject = beanFactory.getBean(this.beanName);
            }
        }
    }
    return null;
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Hashtable(java.util.Hashtable) BeanFactory(org.springframework.beans.factory.BeanFactory) InitialContext(javax.naming.InitialContext)

Example 77 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class MapToObjectTransformerTests method testMapToObjectTransformationWithConversionService.

@Test
public void testMapToObjectTransformationWithConversionService() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("fname", "Justin");
    map.put("lname", "Case");
    map.put("address", "1123 Main st");
    Message<?> message = MessageBuilder.withPayload(map).build();
    MapToObjectTransformer transformer = new MapToObjectTransformer(Person.class);
    BeanFactory beanFactory = this.getBeanFactory();
    ConverterRegistry conversionService = beanFactory.getBean(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME, ConverterRegistry.class);
    conversionService.addConverter(new StringToAddressConverter());
    transformer.setBeanFactory(beanFactory);
    Message<?> newMessage = transformer.transform(message);
    Person person = (Person) newMessage.getPayload();
    assertNotNull(person);
    assertEquals("Justin", person.getFname());
    assertEquals("Case", person.getLname());
    assertNotNull(person.getAddress());
    assertEquals("1123 Main st", person.getAddress().getStreet());
}
Also used : ConverterRegistry(org.springframework.core.convert.converter.ConverterRegistry) HashMap(java.util.HashMap) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 78 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class AbstractCorrelatingMessageHandler method onInit.

@Override
protected void onInit() throws Exception {
    super.onInit();
    Assert.state(!(this.discardChannelName != null && this.discardChannel != null), "'discardChannelName' and 'discardChannel' are mutually exclusive.");
    BeanFactory beanFactory = this.getBeanFactory();
    if (beanFactory != null) {
        if (this.outputProcessor instanceof BeanFactoryAware) {
            ((BeanFactoryAware) this.outputProcessor).setBeanFactory(beanFactory);
        }
        if (this.correlationStrategy instanceof BeanFactoryAware) {
            ((BeanFactoryAware) this.correlationStrategy).setBeanFactory(beanFactory);
        }
        if (this.releaseStrategy instanceof BeanFactoryAware) {
            ((BeanFactoryAware) this.releaseStrategy).setBeanFactory(beanFactory);
        }
    }
    if (this.discardChannel == null) {
        this.discardChannel = new NullChannel();
    }
    if (this.releasePartialSequences) {
        Assert.isInstanceOf(SequenceSizeReleaseStrategy.class, this.releaseStrategy, "Release strategy of type [" + this.releaseStrategy.getClass().getSimpleName() + "] cannot release partial sequences. Use a SequenceSizeReleaseStrategy instead.");
        ((SequenceSizeReleaseStrategy) this.releaseStrategy).setReleasePartialSequences(this.releasePartialSequences);
    }
    if (this.evaluationContext == null) {
        this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
    }
    if (this.sequenceAware) {
        this.logger.warn("Using a SequenceSizeReleaseStrategy with large groups may not perform well, consider " + "using a SimpleSequenceSizeReleaseStrategy");
    }
    /*
		 * Disallow any further changes to the lock registry
		 * (checked in the setter).
		 */
    this.lockRegistrySet = true;
    this.forceReleaseProcessor = createGroupTimeoutProcessor();
}
Also used : BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) BeanFactory(org.springframework.beans.factory.BeanFactory) NullChannel(org.springframework.integration.channel.NullChannel)

Example 79 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class HeaderChannelRegistryTests method testBFCRNoRegistry.

@Test
public void testBFCRNoRegistry() {
    BeanFactoryChannelResolver resolver = new BeanFactoryChannelResolver();
    BeanFactory beanFactory = mock(BeanFactory.class);
    doAnswer(invocation -> {
        throw new NoSuchBeanDefinitionException("bar");
    }).when(beanFactory).getBean("foo", MessageChannel.class);
    resolver.setBeanFactory(beanFactory);
    try {
        resolver.resolveDestination("foo");
        fail("Expected exception");
    } catch (DestinationResolutionException e) {
        assertThat(e.getMessage(), Matchers.containsString("failed to look up MessageChannel with name 'foo' in the BeanFactory " + "(and there is no HeaderChannelRegistry present)."));
    }
}
Also used : DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) BeanFactory(org.springframework.beans.factory.BeanFactory) BeanFactoryChannelResolver(org.springframework.integration.support.channel.BeanFactoryChannelResolver) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 80 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class HeaderChannelRegistryTests method testBFCRWithRegistry.

@Test
public void testBFCRWithRegistry() {
    BeanFactoryChannelResolver resolver = new BeanFactoryChannelResolver();
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.getBean(IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME, HeaderChannelRegistry.class)).thenReturn(mock(HeaderChannelRegistry.class));
    doAnswer(invocation -> {
        throw new NoSuchBeanDefinitionException("bar");
    }).when(beanFactory).getBean("foo", MessageChannel.class);
    resolver.setBeanFactory(beanFactory);
    try {
        resolver.resolveDestination("foo");
        fail("Expected exception");
    } catch (DestinationResolutionException e) {
        assertThat(e.getMessage(), Matchers.containsString("failed to look up MessageChannel with name 'foo' in the BeanFactory."));
    }
}
Also used : DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) BeanFactory(org.springframework.beans.factory.BeanFactory) BeanFactoryChannelResolver(org.springframework.integration.support.channel.BeanFactoryChannelResolver) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) DefaultHeaderChannelRegistry(org.springframework.integration.channel.DefaultHeaderChannelRegistry) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)121 Test (org.junit.jupiter.api.Test)30 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)25 Test (org.junit.Test)20 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)16 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)15 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ExecutorService (java.util.concurrent.ExecutorService)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 JmsTemplate (org.springframework.jms.core.JmsTemplate)6 Bucket4JAutoConfigurationServletFilter (com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter)4 Bucket4JAutoConfigurationZuul (com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul)4