Example usage for org.springframework.web.context ContextLoaderListener ContextLoaderListener

List of usage examples for org.springframework.web.context ContextLoaderListener ContextLoaderListener

Introduction

In this page you can find the example usage for org.springframework.web.context ContextLoaderListener ContextLoaderListener.

Prototype

public ContextLoaderListener() 

Source Link

Document

Create a new ContextLoaderListener that will create a web application context based on the "contextClass" and "contextConfigLocation" servlet context-params.

Usage

From source file:com.counter.TestServer.java

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);

    Context context = new Context();

    ServletHolder servletHolder = new ServletHolder();

    servletHolder.setInitOrder(1);/*from  w  w  w. j a  v  a  2  s. c  o m*/
    servletHolder.setServlet(new CXFServlet());
    servletHolder.setName("CXFServlet");
    servletHolder.setDisplayName("CXF Servlet");
    context.addServlet(servletHolder, "/*");
    context.addEventListener(new ContextLoaderListener());
    Properties initParams = new Properties();
    initParams.put("contextConfigLocation", "classpath:/beans.xml,classpath:/factorybeans.xml");
    context.setInitParams(initParams);
    server.addHandler(context);
    server.start();
}

From source file:de.alpharogroup.scheduler.system.ApplicationJettyRunner.java

/**
 * The main method starts a jetty server with the rest services for the resource-bundle-data.
 *
 * @param args//from   ww  w .  j ava2  s. c  o  m
 *            the arguments
 * @throws Exception
 *             the exception
 */
public static void main(final String[] args) throws Exception {
    final int sessionTimeout = 1800;// set timeout to 30min(60sec * 30min=1800sec)...
    final String projectname = getProjectName();
    final File projectDirectory = PathFinder.getProjectDirectory();
    final File webapp = PathFinder.getRelativePath(projectDirectory, projectname, "src", "main", "webapp");

    final String filterPath = "/*";

    final File logfile = new File(projectDirectory, "application.log");
    if (logfile.exists()) {
        try {
            DeleteFileExtensions.delete(logfile);
        } catch (final IOException e) {
            Logger.getRootLogger().error("logfile could not deleted.", e);
        }
    }
    // Add a file appender to the logger programatically
    LoggerExtensions.addFileAppender(Logger.getRootLogger(),
            LoggerExtensions.newFileAppender(logfile.getAbsolutePath()));

    final ServletContextHandler servletContextHandler = ServletContextHandlerFactory
            .getNewServletContextHandler(ServletContextHandlerConfiguration.builder()
                    .servletHolderConfiguration(ServletHolderConfiguration.builder()
                            .servletClass(CXFServlet.class).pathSpec(filterPath).build())
                    .contextPath("/").webapp(webapp).maxInactiveInterval(sessionTimeout).filterPath(filterPath)
                    .initParameter("contextConfigLocation", "classpath:application-context.xml").build());
    servletContextHandler.addEventListener(new ContextLoaderListener());
    final Jetty9RunConfiguration configuration = Jetty9RunConfiguration.builder()
            .servletContextHandler(servletContextHandler).httpPort(8080).httpsPort(8443).build();
    final Server server = new Server();
    Jetty9Runner.runServletContextHandler(server, configuration);

}

From source file:de.alpharogroup.message.system.ApplicationJettyRunner.java

/**
 * The main method starts a jetty server with the rest services for the resource-bundle-data.
 *
 * @param args/*  w w w .  ja  v a 2  s.c o m*/
 *            the arguments
 * @throws Exception
 *             the exception
 */
public static void main(final String[] args) throws Exception {
    final int sessionTimeout = 1800;// set timeout to 30min(60sec *
    // 30min=1800sec)...
    final String projectname = getProjectName();
    final File projectDirectory = PathFinder.getProjectDirectory();
    final File webapp = PathFinder.getRelativePath(projectDirectory, projectname, "src", "main", "webapp");

    final String filterPath = "/*";

    final File logfile = new File(projectDirectory, "application.log");
    if (logfile.exists()) {
        try {
            DeleteFileExtensions.delete(logfile);
        } catch (final IOException e) {
            Logger.getRootLogger().error("logfile could not deleted.", e);
        }
    }
    // Add a file appender to the logger programatically
    LoggerExtensions.addFileAppender(Logger.getRootLogger(),
            LoggerExtensions.newFileAppender(logfile.getAbsolutePath()));

    final ServletContextHandler servletContextHandler = ServletContextHandlerFactory
            .getNewServletContextHandler(ServletContextHandlerConfiguration.builder()
                    .servletHolderConfiguration(ServletHolderConfiguration.builder()
                            .servletClass(CXFServlet.class).pathSpec(filterPath).build())
                    .contextPath("/").webapp(webapp).maxInactiveInterval(sessionTimeout).filterPath(filterPath)
                    .initParameter("contextConfigLocation", "classpath:application-context.xml").build());
    servletContextHandler.addEventListener(new ContextLoaderListener());
    final Jetty9RunConfiguration configuration = Jetty9RunConfiguration.builder()
            .servletContextHandler(servletContextHandler).httpPort(8080).httpsPort(8443).build();
    final Server server = new Server();
    Jetty9Runner.runServletContextHandler(server, configuration);

}

From source file:de.alpharogroup.phone.data.management.system.ApplicationJettyRunner.java

/**
 * The main method starts a jetty server with the rest services for the resource-bundle-data.
 *
 * @param args//from  w  ww.  ja  va  2  s .c  o  m
 *            the arguments
 * @throws Exception
 *             the exception
 */
public static void main(final String[] args) throws Exception {
    final int sessionTimeout = 1800;// set timeout to 30min(60sec *
    // 30min=1800sec)...
    final String projectname = getProjectName();
    final File projectDirectory = PathFinder.getProjectDirectory();
    final File webapp = PathFinder.getRelativePath(projectDirectory, projectname, "src", "main", "webapp");

    final String filterPath = "/*";

    final File logfile = new File(projectDirectory, "application.log");
    if (logfile.exists()) {
        try {
            DeleteFileExtensions.delete(logfile);
        } catch (final IOException e) {
            Logger.getRootLogger().error("logfile could not deleted.", e);
        }
    }
    // Add a file appender to the logger programatically
    LoggerExtensions.addFileAppender(Logger.getRootLogger(),
            LoggerExtensions.newFileAppender(logfile.getAbsolutePath()));

    final ServletContextHandler servletContextHandler = ServletContextHandlerFactory
            .getNewServletContextHandler(ServletContextHandlerConfiguration.builder()
                    .servletHolderConfiguration(ServletHolderConfiguration.builder()
                            .servletClass(CXFServlet.class).pathSpec(filterPath).build())
                    .contextPath("/").webapp(webapp).maxInactiveInterval(sessionTimeout).filterPath(filterPath)
                    .initParameter("contextConfigLocation", "classpath:application-context.xml").build());
    servletContextHandler.addEventListener(new ContextLoaderListener());
    final Jetty9RunConfiguration configuration = Jetty9RunConfiguration.builder()
            .servletContextHandler(servletContextHandler).httpPort(11080).httpsPort(11443).build();
    final Server server = new Server();
    Jetty9Runner.runServletContextHandler(server, configuration);

}

From source file:com.bekk.boss.pluto.embedded.jetty.util.OverrideContextLoaderListener.java

public OverrideContextLoaderListener() {
    this.contextLoaderListener = new ContextLoaderListener();
}

From source file:org.cometd.websocket.SpringFrameworkConfigurationTest.java

@Test
public void testXMLSpringConfigurationWithWebSocket() throws Exception {
    prepareServer(0, null, false);/*  ww  w  . java 2 s.  c o  m*/
    // Add Spring listener
    context.addEventListener(new ContextLoaderListener());
    String config = WEBSOCKET_JSR_356.equals(wsTransportType) ? "applicationContext-javax-websocket.xml"
            : "applicationContext-jetty-websocket.xml";
    context.getInitParams().put(ContextLoader.CONFIG_LOCATION_PARAM, "classpath:/" + config);
    startServer();

    prepareClient();
    startClient();

    WebApplicationContext applicationContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(context.getServletContext());
    Assert.assertNotNull(applicationContext);

    BayeuxServerImpl bayeuxServer = (BayeuxServerImpl) applicationContext.getBean("bayeux");
    Assert.assertNotNull(bayeuxServer);
    Assert.assertTrue(bayeuxServer.isStarted());

    Assert.assertSame(bayeuxServer, bayeux);

    ServerTransport transport = bayeuxServer.getTransport("websocket");
    Assert.assertNotNull(transport);

    final BayeuxClient client = newBayeuxClient();
    client.handshake();
    Assert.assertTrue(client.waitFor(5000, BayeuxClient.State.CONNECTED));

    // Wait for connect to establish
    Thread.sleep(1000);

    ClientTransport clientTransport = client.getTransport();
    Assert.assertEquals("websocket", clientTransport.getName());

    disconnectBayeuxClient(client);
}

From source file:com.dm.platform.listener.base.MyServletContextListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    System.out.println("MyServletContextListener");
    ServletContext sc = sce.getServletContext();

    ServletRegistration sr = sc.addServlet("spring", DispatcherServlet.class);
    sr.setInitParameter("contextConfigLocation", "/WEB-INF/dispatcher-servlet.xml");
    sr.addMapping("/spring/*");

    FilterRegistration fr = sc.addFilter(DynamicRegistFilter.class.getSimpleName(), DynamicRegistFilter.class);
    fr.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/platform/*");
    //          fr = sc.addFilter(DelegatingFilterProxy.class.getSimpleName(), DelegatingFilterProxy.class);
    //        fr.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/platform/*");

    sc.addListener(new ContextLoaderListener());
}

From source file:org.cometd.server.SpringFrameworkConfigurationTest.java

@Test
public void testXMLSpringConfiguration() throws Exception {
    int port = this.port;
    server.stop();// w ww .  j ava 2 s . c  om
    // Add Spring listener
    context.addEventListener(new ContextLoaderListener());
    context.getInitParams().put(ContextLoader.CONFIG_LOCATION_PARAM, "classpath:/applicationContext.xml");
    connector.setPort(port);
    server.start();

    WebApplicationContext applicationContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(context.getServletContext());
    assertNotNull(applicationContext);

    int sweepPeriod = (Integer) applicationContext.getBean("sweepPeriod");

    BayeuxServerImpl bayeuxServer = (BayeuxServerImpl) applicationContext.getBean("bayeux");
    assertNotNull(bayeuxServer);
    assertTrue(bayeuxServer.isStarted());
    assertEquals(sweepPeriod, bayeuxServer.getOption("sweepPeriod"));

    assertSame(bayeuxServer, cometdServlet.getBayeux());

    Request handshake = newBayeuxRequest("" + "[{" + "\"channel\": \"/meta/handshake\","
            + "\"version\": \"1.0\"," + "\"minimumVersion\": \"1.0\","
            + "\"supportedConnectionTypes\": [\"long-polling\"]" + "}]");
    ContentResponse response = handshake.send();
    assertEquals(200, response.getStatus());
}

From source file:org.cometd.oort.spring.OortSpringAnnotationTest.java

@Test
public void testSpringWiringOfOort() throws Exception {
    Server server = new Server();
    ServletContextHandler context = new ServletContextHandler(server, "/");
    WebSocketServerContainerInitializer.configureContext(context);
    context.addEventListener(new ContextLoaderListener());
    context.getInitParams().put(ContextLoader.CONFIG_LOCATION_PARAM, "classpath:/applicationContext.xml");
    server.start();/*from ww  w.  j a  v a2s. c o m*/

    ApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(context.getServletContext());

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

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

    OortService service = (OortService) applicationContext.getBean(beanName);
    assertNotNull(service);
    Seti seti = service.seti;
    assertNotNull(seti);
    Oort oort = seti.getOort();
    assertNotNull(oort);
    BayeuxServer bayeux = oort.getBayeuxServer();
    assertNotNull(bayeux);

    SecurityPolicy policy = bayeux.getSecurityPolicy();
    assertNotNull(policy);
    assertTrue(policy instanceof OortSecurityPolicy);

    server.stop();
}

From source file:base.StripesWithoutMockTestFixture.java

private static void initServlet() {
    CTX.addInitParameter("contextConfigLocation", SPRING_APPLICATION_CONTEXT_XML);

    ContextLoaderListener springContextListener = new ContextLoaderListener();
    springContextListener.contextInitialized(new ServletContextEvent(CTX));

    CTX.setServlet(DispatcherServlet.class, "StripesDispatcher", null);
}