Example usage for org.springframework.context.support ClassPathXmlApplicationContext close

List of usage examples for org.springframework.context.support ClassPathXmlApplicationContext close

Introduction

In this page you can find the example usage for org.springframework.context.support ClassPathXmlApplicationContext close.

Prototype

@Override
public void close() 

Source Link

Document

Close this application context, destroying all beans in its bean factory.

Usage

From source file:org.agiso.tempel.support.test.AbstractTemplateTest.java

protected ITempel createTempelInstance() {
    ClassPathXmlApplicationContext ctx = null;
    try {/*from   w w w.ja  v  a 2 s .  co  m*/
        ctx = new ClassPathXmlApplicationContext(getContextConfigLocations());

        File tempelInf = new File("src/main/resources/TEMPEL-INF");
        if (tempelInf.exists() && tempelInf.isDirectory()) {
            Archive<?> archive = createTemplateArchive();

            // Mamy do czynienia z testem projektu szablonu:
            IArchiveTemplateProviderElement atp = ctx.getBean(IArchiveTemplateProviderElement.class);
            atp.addArchive(groupId + ":" + templateId + ":" + version, archive);
        }

        return ctx.getBean(ITempel.class);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:org.cometd.annotation.spring.SpringAnnotationTest.java

@Test
public void testSpringWiringOfCometDServices() throws Exception {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();
    applicationContext.setConfigLocation("classpath:applicationContext.xml");
    applicationContext.refresh();/*from   w  w w  .  j a v a2  s . co m*/

    String beanName = Introspector.decapitalize(SpringBayeuxService.class.getSimpleName());

    String[] beanNames = applicationContext.getBeanDefinitionNames();
    assertTrue(Arrays.asList(beanNames).contains(beanName));

    SpringBayeuxService service = (SpringBayeuxService) applicationContext.getBean(beanName);
    assertNotNull(service);
    assertNotNull(service.dependency);
    assertNotNull(service.bayeuxServer);
    assertNotNull(service.serverSession);
    assertTrue(service.active);
    assertEquals(1, service.bayeuxServer.getChannel(SpringBayeuxService.CHANNEL).getSubscribers().size());

    applicationContext.close();

    assertFalse(service.active);
}

From source file:gr.seab.r2rml.test.ComplianceTests.java

@Test
public void testSingle() {
    log.info("test single. Careful, database 'test' will be erased and re-created!");
    String folder = "src/test/resources/postgres/D014-3tables1primarykey1foreignkey/";
    initialiseSourceDatabase(folder + "create.sql");

    //Load property file
    try {/*from w  ww  . java  2 s .c o  m*/
        properties.load(new FileInputStream("src/test/resources/test.properties"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    //Override certain properties
    properties.setProperty("mapping.file", folder + "r2rmla.ttl");
    properties.setProperty("jena.destinationFileName", folder + "r2rmla.nq");

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test-context.xml");
    Database db = (Database) context.getBean("db");
    db.setProperties(properties);

    Parser parser = (Parser) context.getBean("parser");
    parser.setProperties(properties);
    MappingDocument mappingDocument = parser.parse();

    Generator generator = (Generator) context.getBean("generator");
    generator.setProperties(parser.getProperties());
    generator.setResultModel(parser.getResultModel());
    generator.createTriples(mappingDocument);

    context.close();
}

From source file:egovframework.rte.bat.core.launch.support.EgovSchedulerRunner.java

/**
 * Scheduler .//from   ww  w .  j  av a 2 s . c  o m
 * <br>
 * Thread.sleep()? ?, Scheduler schedulerJob?   
 * ApplicationContext  ?? delayTime ? .
 * ApplicationContext ?  ? Scheduler   Batch Job? .
 * :  10 Batch Job (Cron ?: 0/10 * * * * ?)
 * ?? xml? : /egovframework/batch/batch-scheduler-runner-context.xml
 */
public void start() {
    List<String> paths = new ArrayList<String>();

    paths.add(contextPath);
    paths.add(schedulerJobPath);

    //  XML ?? ContextPath? ?.
    for (int index = 0; index < jobPaths.size(); index++) {
        paths.add(jobPaths.get(index));
    }

    String[] locations = paths.toArray(new String[paths.size()]);

    // ApplicationContext ?.
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(locations, false);
    context.refresh();

    logger.warn("ApplicationContext Running Time: " + delayTime / 1000 + " seconds");

    boolean mustContinue = false;
    long realDelayTIme = delayTime;

    if (delayTime < 0) {
        mustContinue = true;
        realDelayTIme = -1 * delayTime;
    }

    if (mustContinue) {
        logger.info("Continue...");
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(realDelayTIme);
            } catch (InterruptedException ie) {
                break;
            }
        }
        logger.info("Sleeping ends...");
        context.close();
    } else {
        try {
            //  ? ,  ?? Scheduler ?.
            logger.info("Sleeping Time: " + realDelayTIme / 1000 + " seconds");
            Thread.sleep(realDelayTIme);
            logger.info("Sleeping ends...");
        } catch (InterruptedException ie) {
            /* interrupted by other thread */
        }
        context.close();
    }
}

From source file:org.echocat.jemoni.carbon.spring.WriterDefinitionParserUnitTest.java

@Test
public void test() throws Exception {
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("writerTestBeans.xml",
            WriterDefinitionParserUnitTest.class);
    try {//  w  ww.j a va  2s  .  c  o  m
        final CarbonWriter defaultWriter = context.getBean(CarbonWriter.class.getName(), CarbonWriter.class);
        assertThat(defaultWriter.getAddress(), is(new InetSocketAddress("localhost", 667)));
        assertThat(defaultWriter.getMaxBufferLifetime(), is(DEFAULT_MAX_BUFFER_LIFETIME));
        assertThat(defaultWriter.getCharset(), is(DEFAULT_CHARSET));

        final CarbonWriter xxxWriter = context.getBean("xxx", CarbonWriter.class);
        assertThat(xxxWriter.getAddress(), is(new InetSocketAddress("localhost", 666)));
        assertThat(xxxWriter.getMaxBufferLifetime(), is(new Duration("666h")));
        assertThat(xxxWriter.getCharset(), is(forName("ISO-8859-15")));
    } finally {
        context.close();
    }
}

From source file:net.sf.oval.test.integration.spring.SpringAOPAllianceTest.java

public void testJDKProxying() {
    final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
            "SpringAOPAllianceTestJDKProxy.xml", SpringAOPAllianceTest.class);

    try {/*from   w  ww .j  a v  a2 s  .com*/
        final TestServiceInterface testServiceWithInterface = ctx.getBean("testServiceWithInterface",
                TestServiceInterface.class);

        try {
            testServiceWithInterface.getSomething(null);
            fail();
        } catch (final ConstraintsViolatedException ex) {
            assertEquals("NOT_NULL", ex.getConstraintViolations()[0].getMessage());
        }

        try {
            testServiceWithInterface.getSomething("123456");
            fail();
        } catch (final ConstraintsViolatedException ex) {
            assertEquals("MAX_LENGTH", ex.getConstraintViolations()[0].getMessage());
        }
    } finally {
        ctx.close();
    }
}

From source file:ch.ralscha.extdirectspring.util.MethodInfoCacheTest.java

@Test
public void testFindMethodWithAnnotation() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/testApplicationContextB.xml");
    MethodInfoCache methodInfoCache = context.getBean(MethodInfoCache.class);

    MethodInfo methodBInfo = methodInfoCache.get("springManagedBean", "methodB");
    Method methodBWithAnnotation = MethodInfo.findMethodWithAnnotation(methodBInfo.getMethod(),
            ExtDirectMethod.class);
    assertThat(methodBWithAnnotation).isEqualTo(methodBInfo.getMethod());

    MethodInfo methodSubBInfo = methodInfoCache.get("springManagedSubBean", "methodB");
    methodBWithAnnotation = MethodInfo.findMethodWithAnnotation(methodSubBInfo.getMethod(),
            ExtDirectMethod.class);
    assertThat(methodSubBInfo.getMethod().equals(methodBWithAnnotation)).isFalse();
    assertThat(methodBInfo.getMethod().equals(methodBWithAnnotation)).isTrue();
    context.close();
}

From source file:gr.seab.r2rml.test.ComplianceTests.java

@Test
public void testSparqlQuery() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test-context.xml");
    Util util = (Util) context.getBean("util");

    Model model = ModelFactory.createDefaultModel();
    String modelFilename = "dump1-epersons.rdf";
    InputStream isMap = FileManager.get().open(modelFilename);
    try {//from  w w w  .ja  v  a 2s  .  c  o m
        model.read(isMap, null, "N3");
    } catch (Exception e) {
        log.error("Error reading model.");
        System.exit(0);
    }
    String query = "SELECT ?x ?z WHERE {?x dc:source ?z} ";
    LocalResultSet rs = util.sparql(model, query);
    log.info("found " + String.valueOf(rs.getRows().size()));

    context.close();
}

From source file:ch.ralscha.extdirectspring.util.MethodInfoCacheTest.java

@Test
public void testFindMethodAndInvoke()
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/testApplicationContextB.xml");
    MethodInfoCache methodInfoCache = context.getBean(MethodInfoCache.class);

    MethodInfo infoB = methodInfoCache.get("springManagedBean", "methodB");

    assertThat((Boolean) ExtDirectSpringUtil.invoke(context, "springManagedBean", infoB, null)).isFalse();
    assertThat((Boolean) ExtDirectSpringUtil.invoke(context, "springManagedBean", infoB, null)).isFalse();

    MethodInfo infoSum = methodInfoCache.get("springManagedBean", "sum");

    assertThat(ExtDirectSpringUtil.invoke(context, "springManagedBean", infoSum, new Object[] { 1, 2 }))
            .isEqualTo(Integer.valueOf(3));
    assertThat(ExtDirectSpringUtil.invoke(context, "springManagedBean", infoSum, new Object[] { 6, 3 }))
            .isEqualTo(Integer.valueOf(9));

    assertThat(methodInfoCache.get("springManagedBean", "methodC")).isNull();
    context.close();
}

From source file:com.brienwheeler.apps.schematool.SchemaToolBean.java

public void afterPropertiesSet() throws Exception {
    try {//from   ww  w  .  jav a 2s  .  c  o m
        // set based on default values in schematool properties file if not already set
        PropertyPlaceholderConfigurer.setProperty(emfPersistenceLocationsPropName,
                emfPersistenceLocationsPropValue);

        Configuration configuration = determineHibernateConfiguration();

        Settings settings = null;
        if (exec || mode == Mode.UPDATE) {
            ClassPathXmlApplicationContext newContext = new SmartClassPathXmlApplicationContext(
                    emfContextLocation);
            try {
                // get a reference to the factory bean, don't have it create a new EntityManager
                LocalContainerEntityManagerFactoryBean factoryBean = newContext
                        .getBean("&" + emfContextBeanName, LocalContainerEntityManagerFactoryBean.class);
                SettingsFactory settingsFactory = new InjectedDataSourceSettingsFactory(
                        factoryBean.getDataSource());
                settings = settingsFactory.buildSettings(new Properties());
            } finally {
                newContext.close();
            }
        }

        if (mode == Mode.UPDATE) {
            SchemaUpdate update = new SchemaUpdate(configuration, settings);
            update.execute(true, exec);
        } else {
            SchemaExport export = exec ? new SchemaExport(configuration, settings)
                    : new SchemaExport(configuration);
            export.create(true, exec);
        }
    } catch (Exception e) {
        log.error("Error running SchemaTool", e);
        throw e;
    }
}