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:org.openmrs.contrib.metadatarepository.webapp.listener.StartupListenerTest.java

protected void setUp() throws Exception {
    super.setUp();
    sc = new MockServletContext("");
    sc.addInitParameter(Constants.CSS_THEME, "simplicity");

    // initialize Spring
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "classpath:/applicationContext-dao.xml, "
            + "classpath:/applicationContext-service.xml, " + "classpath:/applicationContext-resources.xml");

    springListener = new ContextLoaderListener();
    springListener.contextInitialized(new ServletContextEvent(sc));
    listener = new StartupListener();
}

From source file:com.counter.IntegrationTest.java

@BeforeClass
public void setup() throws Exception {
    server = new Server(55555);

    Context context = new Context();

    ServletHolder servletHolder = new ServletHolder();

    servletHolder.setInitOrder(1);/*w  w  w  .j a v a  2s. c om*/
    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:org.excalibur.service.deployment.server.WebServer.java

/**
 * Starts the this server and its services.
 **///from  w ww  . j a v a 2 s  . c om
public void start() throws Exception {
    if (this.started.compareAndSet(false, true)) {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        context.addEventListener(new ContextLoaderListener());
        context.setInitParameter("contextConfigLocation", "classpath*:META-INF/applicationContext.xml");
        context.setContextPath(contextPath);

        ServletHolder sh = new ServletHolder(new org.glassfish.jersey.servlet.ServletContainer());
        sh.setInitParameter("javax.ws.rs.Application", ApplicationConfig.class.getName());
        sh.setInitOrder(1);
        context.addServlet(sh, "/*");

        context.addEventListener(new ApplicationServletContextListener());

        server.setHandler(context);
        server.start();

        NodeManagerFactory.getManagerReference().start();
        server.join();
    }

    LOG.debug("Server already started!");
}

From source file:org.apache.servicemix.http.HttpManagedTest.java

public void test() throws Exception {
    ContextHandler context = new ContextHandler();
    context.setContextPath("/test");
    context.setEventListeners(new EventListener[] { new ContextLoaderListener() });
    Map initParams = new HashMap();
    initParams.put("contextConfigLocation", "classpath:org/apache/servicemix/http/spring-web.xml");
    initParams.put("contextClass", XmlWebApplicationContext.class.getName());
    context.setInitParams(initParams);//w  w  w.  j ava  2  s.c o  m
    ServletHolder holder = new ServletHolder();
    holder.setName("jbiServlet");
    holder.setClassName(HttpManagedServlet.class.getName());
    ServletHandler handler = new ServletHandler();
    handler.setServlets(new ServletHolder[] { holder });
    ServletMapping mapping = new ServletMapping();
    mapping.setServletName("jbiServlet");
    mapping.setPathSpec("/*");
    handler.setServletMappings(new ServletMapping[] { mapping });
    context.setHandler(handler);

    ContextHandlerCollection contexts = new ContextHandlerCollection();
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { contexts });
    contexts.addHandler(context);

    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setHost("localhost");
    connector.setPort(8190);

    server = new Server();
    server.setConnectors(new Connector[] { connector });
    server.setHandler(handlers);
    server.start();

    logger.info("Started");

    PostMethod post = new PostMethod("http://localhost:8190/test/jbi/Service/");
    post.setRequestEntity(
            new StringRequestEntity("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>"
                    + "<soap:Body><hello>world</hello></soap:Body>" + "</soap:Envelope>"));
    new HttpClient().executeMethod(post);
    if (post.getStatusCode() != 200) {
        throw new InvalidStatusResponseException(post.getStatusCode());
    }
    logger.info(post.getResponseBodyAsString());

}

From source file:com.iflytek.edu.cloud.frame.web.RestServiceWebApplicationInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setInitParameter("contextConfigLocation", "classpath*:META-INF/spring/*-context.xml");
    servletContext.setInitParameter("contextInitializerClasses",
            ProfileApplicationContextInitializer.class.getName());
    servletContext.addListener(new LogBackLoadConfigureListener());
    servletContext.addListener(new ContextLoaderListener());

    FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter",
            new CharacterEncodingFilter());
    EnumSet<DispatcherType> characterEncodingFilterDispatcherTypes = EnumSet.of(DispatcherType.REQUEST,
            DispatcherType.FORWARD);//from   w  w w.java2  s.c om
    characterEncodingFilter.setInitParameter("encoding", "UTF-8");
    characterEncodingFilter.setInitParameter("forceEncoding", "true");
    characterEncodingFilter.addMappingForUrlPatterns(characterEncodingFilterDispatcherTypes, true, "/*");

    FilterRegistration.Dynamic openServiceFilter = servletContext.addFilter("openServiceFilter",
            new DelegatingFilterProxy());
    EnumSet<DispatcherType> openServiceFilterDispatcherTypes = EnumSet.of(DispatcherType.REQUEST,
            DispatcherType.FORWARD);
    openServiceFilter.addMappingForUrlPatterns(openServiceFilterDispatcherTypes, true, "/api");

    if (EnvUtil.jdbcEnabled()) {
        FilterRegistration.Dynamic serviceMetricsFilter = servletContext.addFilter("serviceMetricsFilter",
                new DelegatingFilterProxy());
        EnumSet<DispatcherType> serviceMetricsFilterDispatcherTypes = EnumSet.of(DispatcherType.REQUEST,
                DispatcherType.FORWARD);
        serviceMetricsFilter.addMappingForUrlPatterns(serviceMetricsFilterDispatcherTypes, true, "/api");
    }

    FilterRegistration.Dynamic CORSFilter = servletContext.addFilter("CORSFilter", new DelegatingFilterProxy());
    EnumSet<DispatcherType> CORSFilterDispatcherTypes = EnumSet.of(DispatcherType.REQUEST,
            DispatcherType.FORWARD);
    CORSFilter.addMappingForUrlPatterns(CORSFilterDispatcherTypes, true, "/api");

    if (EnvUtil.oauthEnabled()) {
        FilterRegistration.Dynamic springSecurityFilterChain = servletContext
                .addFilter("springSecurityFilterChain", new DelegatingFilterProxyExt());
        EnumSet<DispatcherType> springSecurityFilterChainDispatcherTypes = EnumSet.of(DispatcherType.REQUEST,
                DispatcherType.FORWARD);
        springSecurityFilterChain.addMappingForUrlPatterns(springSecurityFilterChainDispatcherTypes, true,
                "/api");
    } else {
        logger.info(
                "?oauth2???META-INF/res/profile.propertiesoauth2 profile");
    }

    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("rest", new DispatcherServlet());
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
    dispatcherServlet.setInitParameter("contextConfigLocation", "org.spring.rest");
    dispatcherServlet.setMultipartConfig(getMultiPartConfig());
    dispatcherServlet.addMapping("/api");

    ServletRegistration.Dynamic printProjectVersionServlet = servletContext
            .addServlet("printProjectVersionServlet", new PrintProjectVersionServlet());
    printProjectVersionServlet.setLoadOnStartup(Integer.MAX_VALUE);
}

From source file:org.jessma.ext.spring.SpringInjectFilter.java

@Override
public void init() {
    servletContext = HttpHelper.getServletContext();
    springMap = new HashMap<CoupleKey<Class<?>, Method>, SpringAttr[]>();
    context = WebApplicationContextUtils.getWebApplicationContext(servletContext);

    if (context == null) {
        listener = new ContextLoaderListener();
        listener.contextInitialized(new ServletContextEvent(servletContext));
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    }/*w  ww  . ja  va2  s.c  om*/
}

From source file:alpha.portal.webapp.listener.StartupListenerTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.sc = new MockServletContext("");
    this.sc.addInitParameter(Constants.CSS_THEME, "simplicity");

    // initialize Spring
    this.sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "classpath:/applicationContext-dao.xml, "
            + "classpath:/applicationContext-service.xml, " + "classpath:/applicationContext-resources.xml");

    this.springListener = new ContextLoaderListener();
    this.springListener.contextInitialized(new ServletContextEvent(this.sc));
    this.listener = new StartupListener();
}

From source file:io.gravitee.management.war.WebAppInitializer.java

@Override
public void onStartup(ServletContext context) throws ServletException {
    // initialize
    initialize();/*from   w  ww .  ja  v a  2s.  c  o  m*/
    Properties prop = propertiesLoader.load();

    // REST configuration
    ServletRegistration.Dynamic servletRegistration = context.addServlet("REST",
            ServletContainer.class.getName());
    servletRegistration.addMapping("/management/*");
    servletRegistration.setLoadOnStartup(1);
    servletRegistration.setInitParameter("javax.ws.rs.Application", GraviteeApplication.class.getName());

    // Spring configuration
    System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME,
            prop.getProperty("security.type", "basic-auth"));
    context.addListener(new ContextLoaderListener());
    context.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
    context.setInitParameter("contextConfigLocation", RestConfiguration.class.getName());

    // Spring Security filter
    context.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class)
            .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
}

From source file:org.excalibur.service.deployment.server.ApplicationServer.java

/**
 * Starts the this server and its services. 
 **///from  w  ww. j a v a 2  s  . co  m
public void start() throws Exception {
    if (this.started.compareAndSet(false, true)) {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        context.addEventListener(new ContextLoaderListener());
        context.setInitParameter("contextConfigLocation", "classpath*:META-INF/context.xml");
        context.setContextPath(contextPath);

        ServletHolder sh = new ServletHolder(new org.glassfish.jersey.servlet.ServletContainer());
        sh.setInitParameter("javax.ws.rs.Application", ApplicationConfig.class.getName());
        sh.setInitOrder(1);
        context.addServlet(sh, "/*");

        server.setHandler(context);
        server.start();

        ApplicationContext applicationContext = (ApplicationContext) context.getServletContext()
                .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

        Environment environment = applicationContext.getEnvironment();
        ApplicationConfig application = applicationContext.getBean(ApplicationConfig.class);

        for (Class<?> klass : application.getAvailableResources()) {
            registerThisInstanceServices(klass, environment, application);
        }

        this.serviceDiscovery.start();
        server.join();
    }
}

From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java

@Before
public void setUp() throws Throwable {
    beforeServletStart();//from   w ww  .ja va  2  s  . com

    setServletContext(new MockServletContext("file:src/main/webapp"));

    getServletContext().addInitParameter("contextConfigLocation",
            "file:src/main/resources/META-INF/opennms/component-service.xml");

    getServletContext().addInitParameter("parentContextKey", "testDaoContext");

    ServletContextEvent e = new ServletContextEvent(getServletContext());
    setContextListener(new ContextLoaderListener());
    getContextListener().contextInitialized(e);

    getServletContext().setContextPath(contextPath);
    setServletConfig(new MockServletConfig(getServletContext(), "dispatcher"));
    /*
    getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
    getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.netmgt.ncs.rest");
    */
    try {

        MockFilterConfig filterConfig = new MockFilterConfig(getServletContext(), "openSessionInViewFilter");
        setFilter(new OpenSessionInViewFilter());
        getFilter().init(filterConfig);

        setDispatcher(new SpringServlet());
        getDispatcher().init(getServletConfig());

    } catch (ServletException se) {
        throw se.getRootCause();
    }

    setWebAppContext(WebApplicationContextUtils.getWebApplicationContext(getServletContext()));
    afterServletStart();
    System.err.println("------------------------------------------------------------------------------");
}