Example usage for org.springframework.context ConfigurableApplicationContext isActive

List of usage examples for org.springframework.context ConfigurableApplicationContext isActive

Introduction

In this page you can find the example usage for org.springframework.context ConfigurableApplicationContext isActive.

Prototype

boolean isActive();

Source Link

Document

Determine whether this application context is active, that is, whether it has been refreshed at least once and has not been closed yet.

Usage

From source file:app.core.ApplicationContextTest.java

public void testLoadOk() {
    ConfigurableApplicationContext appCtx = super.getApplicationContext();
    assertTrue(appCtx.isActive());
    assertTrue(Arrays.toString(appCtx.getBeanDefinitionNames()), appCtx.getBeanDefinitionCount() > 9);
    for (String beanDefinitionName : appCtx.getBeanDefinitionNames()) {
        // Load all beans to make sure no initialization fails
        assertNotNull(appCtx.getBean(beanDefinitionName));
    }/*from   w w w. j a  v  a  2 s.co  m*/

    assertEquals(appCtx, ((ApplicationContextProvider) appCtx.getBean("applicationContextProvider")).getCtx());
    //        assertNotNull(((Dao) appCtx.getBean("dao")).getSessionFactory());
    //        ((Db) appCtx.getBean("db")).getSessionFactory();
    assertNotNull(Db.getSession());
}

From source file:org.alfresco.repo.management.subsystems.test.SubsystemsTest.java

/**
 * Test subsystems./*from  w  w w. j  a  v  a2 s  .c o  m*/
 * 
 * @throws Exception
 *             the exception
 */
public void testSubsystems() throws Exception {
    ApplicationContextFactory subsystem = (ApplicationContextFactory) getApplicationContext()
            .getBean("testsubsystem");
    ConfigurableApplicationContext childContext = (ConfigurableApplicationContext) subsystem
            .getApplicationContext();
    assertTrue("Subsystem not started", childContext.isActive());
    TestService testService = (TestService) childContext.getBean("testService");
    // Make sure subsystem defaults work
    assertEquals("Subsystem Default1", testService.getSimpleProp1());
    // Make sure global property overrides work
    assertEquals(true, testService.getSimpleProp2().booleanValue());
    // Make sure extension classpath property overrides work
    assertEquals("Instance Override3", testService.getSimpleProp3());
    // Make sure extension classpath Spring overrides work
    assertEquals("An extra bean I changed", childContext.getBean("anotherBean"));
    // Make sure composite properties and their defaults work
    TestBean[] testBeans = testService.getTestBeans();
    assertNotNull("Composite property not set", testBeans);
    assertEquals(3, testBeans.length);
    assertEquals("inst1", testBeans[0].getId());
    assertEquals(false, testBeans[0].isBoolProperty());
    assertEquals(123456789123456789L, testBeans[0].getLongProperty());
    assertEquals("Global Default", testBeans[0].getAnotherStringProperty());
    assertEquals("inst2", testBeans[1].getId());
    assertEquals(true, testBeans[1].isBoolProperty());
    assertEquals(123456789123456789L, testBeans[1].getLongProperty());
    assertEquals("Global Default", testBeans[1].getAnotherStringProperty());
    assertEquals("inst3", testBeans[2].getId());
    assertEquals(false, testBeans[2].isBoolProperty());
    assertEquals(123456789123456789L, testBeans[2].getLongProperty());
    assertEquals("Global Instance Default", testBeans[2].getAnotherStringProperty());
}

From source file:org.openspaces.pu.container.jee.context.BootstrapWebApplicationContextListener.java

public void contextDestroyed(ServletContextEvent servletContextEvent) {
    if (jeeContainerContextListener != null) {
        jeeContainerContextListener.contextDestroyed(servletContextEvent);
    }/*w  w w .  j a v a  2  s. co m*/
    ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) servletContextEvent
            .getServletContext().getAttribute(JeeProcessingUnitContainerProvider.APPLICATION_CONTEXT_CONTEXT);
    if (applicationContext != null && applicationContext.isActive()) {
        applicationContext.close();
    }
}

From source file:org.springframework.batch.core.configuration.support.DefaultJobLoader.java

/**
 * Unregister all the jobs and close all the contexts created by this
 * loader./*  w  ww  .ja v  a 2  s.  c o m*/
 *
 * @see JobLoader#clear()
 */
@Override
public void clear() {
    for (ConfigurableApplicationContext context : contexts.values()) {
        if (context.isActive()) {
            context.close();
        }
    }
    for (String jobName : jobRegistry.getJobNames()) {
        doUnregister(jobName);
    }
    contexts.clear();
    contextToJobNames.clear();
}

From source file:org.springframework.boot.context.event.EventPublishingRunListener.java

@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
    ApplicationFailedEvent event = new ApplicationFailedEvent(this.application, this.args, context, exception);
    if (context != null && context.isActive()) {
        // Listeners have been registered to the application context so we should
        // use it at this point if we can
        context.publishEvent(event);/*from w w  w  .j a  v a  2 s. c  om*/
    } else {
        // An inactive context may not have a multicaster so we use our multicaster to
        // call all of the context's listeners instead
        if (context instanceof AbstractApplicationContext) {
            for (ApplicationListener<?> listener : ((AbstractApplicationContext) context)
                    .getApplicationListeners()) {
                this.initialMulticaster.addApplicationListener(listener);
            }
        }
        this.initialMulticaster.setErrorHandler(new LoggingErrorHandler());
        this.initialMulticaster.multicastEvent(event);
    }
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test
public void testSetBeanClassLoaderWhenApplicationContextIsInactive() {
    ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
            "testSetBeanClassLoaderWhenApplicationContextIsInactive.MockApplicationContext");

    when(mockApplicationContext.isActive()).thenReturn(false);

    SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
    SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());

    verify(mockApplicationContext, times(1)).isActive();
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test(expected = IllegalStateException.class)
public void testSetBeanClassLoaderWhenApplicationContextIsActive() {
    ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
            "testSetBeanClassLoaderWhenApplicationContextIsActive.MockApplicationContext");

    when(mockApplicationContext.isActive()).thenReturn(true);

    try {//  ww  w  .j a  va2  s .  com
        SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
        SpringContextBootstrappingInitializer
                .setBeanClassLoader(Thread.currentThread().getContextClassLoader());
    } catch (IllegalStateException expected) {
        assertEquals("The Spring ApplicationContext has already been initialized!", expected.getMessage());
        throw expected;
    }
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test
public void testInitWithExistingApplicationContext() {
    ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
            "testInitWithExistingApplicationContext");

    when(mockApplicationContext.isActive()).thenReturn(true);
    when(mockApplicationContext.getId()).thenReturn("testInitWithExistingApplicationContext");

    SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;

    assertSame(mockApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer();

    initializer.init(createParameters("test", "test"));

    verify(mockApplicationContext, never())
            .addApplicationListener(any(SpringContextBootstrappingInitializer.class));
    verify(mockApplicationContext, never()).registerShutdownHook();
    verify(mockApplicationContext, never()).refresh();

    assertSame(mockApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test
public void testInitWhenApplicationContextIsInactive() {
    ConfigurableApplicationContext mockInactiveApplicationContext = mock(ConfigurableApplicationContext.class,
            "testInitWhenApplicationContextIsInactive.Inactive");

    when(mockInactiveApplicationContext.isActive()).thenReturn(false);

    SpringContextBootstrappingInitializer.applicationContext = mockInactiveApplicationContext;

    assertSame(mockInactiveApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());

    final ConfigurableApplicationContext mockNewApplicationContext = mock(ConfigurableApplicationContext.class,
            "testInitWhenApplicationContextIsInactive.New");

    when(mockNewApplicationContext.getId()).thenReturn("testInitWhenApplicationContextIsInactive.New");
    when(mockNewApplicationContext.isRunning()).thenReturn(true);

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
        @Override/*w  w  w.  j ava 2s .  c om*/
        protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
                final String[] configLocations) {
            return mockNewApplicationContext;
        }
    };

    initializer.init(
            createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER, "org.example.app"));

    verify(mockNewApplicationContext, times(1)).addApplicationListener(same(initializer));
    verify(mockNewApplicationContext, times(1)).registerShutdownHook();
    verify(mockNewApplicationContext, times(1)).refresh();

    assertSame(mockNewApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
}