Example usage for org.springframework.web.context.support XmlWebApplicationContext registerShutdownHook

List of usage examples for org.springframework.web.context.support XmlWebApplicationContext registerShutdownHook

Introduction

In this page you can find the example usage for org.springframework.web.context.support XmlWebApplicationContext 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:com.cisco.oss.foundation.tools.simulator.rest.startup.SingleRestSimulatorStartup.java

public static void startServer() {
    try {//ww w . j  a  v  a  2 s. co  m

        ListMultimap<String, Servlet> servlets = ArrayListMultimap.create();
        ListMultimap<String, Filter> filters = ArrayListMultimap.create();

        // Set the init params
        Map<String, String> initParams = new HashMap<String, String>();
        initParams.put("com.sun.jersey.config.property.packages", "com.cisco.oss.foundation.tools");

        // Create the servlet
        ResourceConfig resourceConfig = new ResourceConfig();
        resourceConfig.packages("com.cisco.oss.foundation.tools");
        ServletContainer resourceServlet = new ServletContainer(resourceConfig);
        servlets.put("/*", resourceServlet);

        XmlWebApplicationContext webConfig = new XmlWebApplicationContext();
        webConfig.setConfigLocation("classpath:META-INF/restSimulatorContext.xml");
        webConfig.registerShutdownHook();

        List<EventListener> eventListeners = new ArrayList<EventListener>();
        eventListeners.add(new ContextLoaderListener(webConfig));
        eventListeners.add(new RequestContextListener());

        JettyHttpServerFactory.INSTANCE.startHttpServer(SINGLE_REST_SIMULATOR, servlets, filters,
                eventListeners, initParams);

        //         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        //               "META-INF/applicationContext.xml");
        //         server = (Server) applicationContext.getBean("restSimJettyServer");
        //
        //         server.start();
    } catch (Exception ex) {
        logger.error("Error Starting REST Simulator" + ex);
    }
}

From source file:org.springsource.sinspctr.AdminMain.java

/**
* Launch stream server with the given home and transport
 * @throws IOException /*from   ww w . j a  va2s  . c  o  m*/
*/
static InspctrServer launchStreamServer() throws IOException {
    XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocation("classpath:" + CONFIG_ROOT + "sinspctr-server.xml");

    // Not making StreamServer a spring bean eases move to .war file if
    // needed
    final InspctrServer server = new InspctrServer(context, DEFAULT_PORT);
    server.afterPropertiesSet();
    server.start();
    StringBuilder runtimeInfo = new StringBuilder(
            String.format("Running in Local Mode on port: %s ", server.getPort()));
    System.out.println(BannerUtils.displayBanner(null, runtimeInfo.toString()));
    context.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
        @Override
        public void onApplicationEvent(ContextClosedEvent event) {
            server.stop();
        }
    });
    context.registerShutdownHook();
    return server;
}

From source file:org.alfresco.bm.web.WebApp.java

@Override
public void onStartup(ServletContext container) {
    // Grab the server capabilities, otherwise just use the java version
    String javaVersion = System.getProperty("java.version");
    String systemCapabilities = System.getProperty(PROP_SYSTEM_CAPABILITIES, javaVersion);

    String appDir = new File(".").getAbsolutePath();
    String appContext = container.getContextPath();
    String appName = container.getContextPath().replace(SEPARATOR, "");

    // Create an application context (don't start, yet)
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocations(new String[] { "classpath:config/spring/app-context.xml" });

    // Pass our properties to the new context
    Properties ctxProperties = new Properties();
    {/*from w  w w  .  ja  v  a  2  s . com*/
        ctxProperties.put(PROP_SYSTEM_CAPABILITIES, systemCapabilities);
        ctxProperties.put(PROP_APP_CONTEXT_PATH, appContext);
        ctxProperties.put(PROP_APP_DIR, appDir);
    }
    ConfigurableEnvironment ctxEnv = ctx.getEnvironment();
    ctxEnv.getPropertySources().addFirst(new PropertiesPropertySource(appName, ctxProperties));
    // Override all properties with system properties
    ctxEnv.getPropertySources().addFirst(new PropertiesPropertySource("system", System.getProperties()));
    // Bind to shutdown
    ctx.registerShutdownHook();

    ContextLoaderListener ctxLoaderListener = new ContextLoaderListener(ctx);
    container.addListener(ctxLoaderListener);

    ServletRegistration.Dynamic jerseyServlet = container.addServlet("jersey-serlvet", SpringServlet.class);
    jerseyServlet.setInitParameter("com.sun.jersey.config.property.packages", "org.alfresco.bm.rest");
    jerseyServlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    jerseyServlet.addMapping("/api/*");
}