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

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

Introduction

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

Prototype

@Override
public void registerShutdownHook() 

Source Link

Document

Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

Usage

From source file:org.alfresco.bm.test.TestRunServicesCache.java

/**
 * Create an application context holding the services for the given test run
 *///  ww w. j  a  va  2  s.co m
private ClassPathXmlApplicationContext createContext(String test, String run) {
    String testRunFqn = test + "." + run;
    DBObject runObj;
    try {
        runObj = dao.getTestRun(test, run, true);
    } catch (ObjectNotFoundException e1) {
        logger.error("Test '" + test + "." + run + "' not found.", e1);
        return null;
    }

    // Dig the properties out of the test run
    Properties testRunProps = new Properties();
    {
        testRunProps.put(PROP_TEST_RUN_FQN, testRunFqn);

        BasicDBList propObjs = (BasicDBList) runObj.get(FIELD_PROPERTIES);
        for (Object obj : propObjs) {
            DBObject propObj = (DBObject) obj;
            String propName = (String) propObj.get(FIELD_NAME);
            String propDef = (String) propObj.get(FIELD_DEFAULT);
            String propValue = (String) propObj.get(FIELD_VALUE);
            if (propValue == null) {
                propValue = propDef;
            }
            testRunProps.put(propName, propValue);
        }
    }
    // Construct the properties
    ClassPathXmlApplicationContext testRunCtx = new ClassPathXmlApplicationContext(
            new String[] { PATH_TEST_SERVICES_CONTEXT }, false);
    ConfigurableEnvironment ctxEnv = testRunCtx.getEnvironment();
    ctxEnv.getPropertySources().addFirst(new PropertiesPropertySource("run-props", testRunProps));
    // Bind to shutdown
    testRunCtx.registerShutdownHook();

    // Attempt to start the context
    try {
        testRunCtx.refresh();
        testRunCtx.start();
        // Make sure that the required components are present
        testRunCtx.getBean(EventService.class);
        testRunCtx.getBean(ResultService.class);
        testRunCtx.getBean(SessionService.class);
    } catch (Exception e) {
        Throwable root = ExceptionUtils.getRootCause(e);
        if (root != null && root instanceof MongoSocketException) {
            // We deal with this specifically as it's a simple case of not finding the MongoDB
            logger.error("Failed to start test run services context '" + testRunFqn + "': "
                    + e.getCause().getMessage());
            logger.error(
                    "Set the test run property '" + PROP_MONGO_TEST_HOST + "' (<server>:<port>) as required.");
        } else if (root != null && root instanceof UnknownHostException) {
            // We deal with this specifically as it's a simple case of not finding the MongoDB
            logger.error("Failed to start test run services context '" + testRunFqn + "': "
                    + e.getCause().getCause().getMessage());
            logger.error(
                    "Set the test run property '" + PROP_MONGO_TEST_HOST + "' (<server>:<port>) as required.");
        } else {
            logger.error("Failed to start test run services context '" + testRunFqn + "': ", e);
        }
        testRunCtx = null;
    }
    // Done
    if (testRunCtx == null) {
        logger.warn("Failed to start test run services context: " + testRunFqn);
    } else if (logger.isDebugEnabled()) {
        logger.debug("Started test run services context: " + testRunFqn);
    }
    return testRunCtx;
}

From source file:org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProviderTests.java

License:asdf

@Test
public void javadocExample() {
    String resName = "/" + getClass().getName().replace('.', '/') + ".xml";
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(resName);
    context.registerShutdownHook();
    try {//  w  ww. j a va 2 s. com
        provider = context.getBean(DefaultJaasAuthenticationProvider.class);
        Authentication auth = provider.authenticate(token);
        assertThat(auth.isAuthenticated()).isEqualTo(true);
        assertThat(auth.getPrincipal()).isEqualTo(token.getPrincipal());
    } finally {
        context.close();
    }
}

From source file:org.springframework.xd.dirt.launcher.RedisContainerLauncher.java

/**
 * Create a container instance// w ww.j ava  2  s  .co m
 * @param options
 */
@SuppressWarnings("resource")
public static Container create(ContainerOptions options) {
    ClassPathXmlApplicationContext context = null;
    try {
        context = new ClassPathXmlApplicationContext();
        context.setConfigLocation(LAUNCHER_CONFIG_LOCATION);
        //TODO: Need to sort out how this will be handled consistently among launchers
        if (!options.isJmxDisabled()) {
            context.getEnvironment().addActiveProfile("xd.jmx.enabled");
            OptionUtils.setJmxProperties(options, context.getEnvironment());
        }
        context.refresh();
    } catch (BeanCreationException e) {
        if (e.getCause() instanceof RedisConnectionFailureException) {
            logger.fatal(e.getCause().getMessage());
            System.err.println("Redis does not seem to be running. Did you install and start Redis? "
                    + "Please see the Getting Started section of the guide for instructions.");
            System.exit(1);
        }
    }
    context.registerShutdownHook();
    ContainerLauncher launcher = context.getBean(ContainerLauncher.class);
    Container container = launcher.launch(options);
    return container;
}

From source file:org.springframework.xd.perftest.PerformanceTest.java

public static void main(String[] args) {
    updatePerfTestHome();//from www .  j  ava  2  s.c o m
    PerformanceTest perfTest = new PerformanceTest();
    CmdLineOptions options = perfTest.new CmdLineOptions();
    CmdLineParser parser = new CmdLineParser(options);
    parser.setUsageWidth(90);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        parser.printUsage(System.err);
        System.exit(1);
    }
    transport = options.getTransport();
    consumerType = options.getConsumerType();
    flow = options.getFlow();
    setConnectionFactoryType(options.getConnectionFactoryType());
    if (options.isShowHelp()) {
        parser.printUsage(System.err);
        System.exit(0);
    }
    logger.info("*******Starting Performance test********");
    logger.info("Transport: " + transport);
    if (flow.equals(Flow.inbound)) {
        logger.info("Consumer:  " + consumerType);
    }
    logger.info("ConnectionFactory: " + options.getConnectionFactoryType());
    logger.info("****************************************");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
    context.getEnvironment().setActiveProfiles(consumerType.name());
    context.setConfigLocation("META-INF/perftest/" + transport + "-perftest-" + options.getFlow() + ".xml");
    context.refresh();
    context.registerShutdownHook();

}