Example usage for org.springframework.web.context ContextLoader initWebApplicationContext

List of usage examples for org.springframework.web.context ContextLoader initWebApplicationContext

Introduction

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

Prototype

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) 

Source Link

Document

Initialize Spring's web application context for the given servlet context, using the application context provided at construction time, or creating a new one according to the " #CONTEXT_CLASS_PARAM contextClass " and " #CONFIG_LOCATION_PARAM contextConfigLocation " context-params.

Usage

From source file:dk.frankbille.scoreboard.test.WicketSpringTestCase.java

@BeforeClass
public static void setupSpring() {
    if (applicationContext == null) {
        MockServletContext servletContext = new MockServletContext(new ScoreBoardApplication(),
                "src/main/webapp");
        servletContext.addInitParameter("contextConfigLocation", "classpath:applicationContext-test.xml");
        ContextLoader loader = new ContextLoader();
        applicationContext = loader.initWebApplicationContext(servletContext);
        WicketSpringTestCase.servletContext = servletContext;
    }//from   www.j a v  a  2s. c om
}

From source file:org.parancoe.web.test.BaseTest.java

@Override
protected ConfigurableApplicationContext createApplicationContext(String[] locations) {
    FileSystemResourceLoader rl = new FileSystemResourceLoader();
    MockServletContext servletContext = new MockServletContext(rl);
    servletContext.setMinorVersion(4);//from  w w  w. j a  v  a  2s. co m
    servletContext.registerContext("/test", servletContext);
    servletContext.setServletContextName("/test");
    servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, arrayToString(locations));
    ContextLoader loader = new ContextLoader();
    WebApplicationContext context = loader.initWebApplicationContext(servletContext);
    return (ConfigurableApplicationContext) context;
}

From source file:net.jawr.web.bundle.processor.spring.SpringControllerBundleProcessor.java

/**
 * Initialize the servlets which will handle the request to the JawrSpringController 
 * @param servletContext the servlet context
 * @return the list of servlet definition for the JawrSpringControllers
 * @throws ServletException if a servlet exception occurs
 *///from  ww  w.  j  a  va2s  .  c o m
@SuppressWarnings("rawtypes")
public List<ServletDefinition> initJawrSpringServlets(ServletContext servletContext) throws ServletException {

    List<ServletDefinition> jawrServletDefinitions = new ArrayList<ServletDefinition>();
    ContextLoader contextLoader = new ContextLoader();
    WebApplicationContext applicationCtx = contextLoader.initWebApplicationContext(servletContext);
    Map<?, ?> jawrControllersMap = applicationCtx.getBeansOfType(JawrSpringController.class);

    Iterator<?> entrySetIterator = jawrControllersMap.entrySet().iterator();
    while (entrySetIterator.hasNext()) {

        JawrSpringController jawrController = (JawrSpringController) ((Map.Entry) entrySetIterator.next())
                .getValue();
        Map<String, Object> initParams = new HashMap<String, Object>();
        initParams.putAll(jawrController.getInitParams());
        ServletConfig servletConfig = new MockServletConfig(SPRING_DISPATCHER_SERVLET, servletContext,
                initParams);
        MockJawrSpringServlet servlet = new MockJawrSpringServlet(jawrController, servletConfig);
        ServletDefinition servletDefinition = new ServletDefinition(servlet, servletConfig);
        jawrServletDefinitions.add(servletDefinition);
    }

    contextLoader.closeWebApplicationContext(servletContext);
    return jawrServletDefinitions;
}

From source file:architecture.ee.component.core.lifecycle.BootstrapImpl.java

public void boot(ServletContext servletContext) {
    lock.lock();//from w ww. ja  v a  2 s. c om
    try {
        log.debug("checking repository state:" + getState());
        if (repository.getState() != State.INITIALIZED) {
            ((RepositoryImpl) repository).setServletContext(servletContext);
        }

        // 1. admin service  bootstrap ?   : DOTO
        AdminService adminService = getBootstrapComponent(AdminService.class);
        bootstrapAdminServiceEnabled.set(true);
        if (adminService instanceof SpringAdminService) {
            ((SpringAdminService) adminService).setServletContext(servletContext);
        }
        adminService.start();
    } catch (ComponentNotFoundException e) {
        log.debug("no adminservice.");
        ContextLoader contextLoader = getBootstrapComponent(ContextLoader.class);
        contextLoader.initWebApplicationContext(servletContext);
    } finally {
        lock.unlock();
    }
}

From source file:org.apache.wink.test.mock.SpringAwareTestCase.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    ContextLoader contextLoader = new ContextLoader();
    servletContext = new MockServletContext();
    servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, getApplicationContextPath());
    applicationContext = contextLoader.initWebApplicationContext(servletContext);
}

From source file:org.red5.server.war.RootContextLoaderServlet.java

public void registerSubContext(String webAppKey) {
    // get the sub contexts - servlet context
    ServletContext ctx = servletContext.getContext(webAppKey);
    logger.info("Registering subcontext for servlet context: " + ctx.getContextPath());
    if (registeredContexts.contains(ctx)) {
        logger.debug("Context is already registered: " + webAppKey);
        return;/*from   www  .  j ava 2  s . com*/
    }

    ContextLoader loader = new ContextLoader();

    ConfigurableWebApplicationContext appCtx = (ConfigurableWebApplicationContext) loader
            .initWebApplicationContext(ctx);
    appCtx.setParent(applicationContext);
    appCtx.refresh();

    ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);

    ConfigurableBeanFactory appFactory = appCtx.getBeanFactory();

    logger.debug("About to grab Webcontext bean for " + webAppKey);
    Context webContext = (Context) appCtx.getBean("web.context");
    webContext.setCoreBeanFactory(parentFactory);
    webContext.setClientRegistry(clientRegistry);
    webContext.setServiceInvoker(globalInvoker);
    webContext.setScopeResolver(globalResolver);
    webContext.setMappingStrategy(globalStrategy);

    WebScope scope = (WebScope) appFactory.getBean("web.scope");
    scope.setServer(server);
    scope.setParent(global);
    scope.register();
    scope.start();

    // register the context so we dont try to reinitialize it
    registeredContexts.add(ctx);

}

From source file:org.red5.server.war.RootContextLoaderServlet.java

/**
 * Main entry point for the Red5 Server as a war
 *//*from   www.  j av  a 2 s . c om*/
// Notification that the web application is ready to process requests
@Override
public void contextInitialized(ServletContextEvent sce) {
    if (null != servletContext) {
        return;
    }
    instance = this;
    System.setProperty("red5.deployment.type", "war");

    myClassloader = getClass().getClassLoader();

    servletContext = sce.getServletContext();
    String prefix = servletContext.getRealPath("/");

    servletContext.setAttribute("root.classloader", myClassloader);

    initRegistry(servletContext);

    long time = System.currentTimeMillis();

    logger.info("RED5 Server (http://www.osflash.org/red5)");
    logger.info("Root context loader");
    logger.debug("Path: " + prefix);

    try {
        // instance the context loader
        ContextLoader loader = createContextLoader();
        applicationContext = (ConfigurableWebApplicationContext) loader
                .initWebApplicationContext(servletContext);
        logger.debug("Root context path: " + applicationContext.getServletContext().getContextPath());

        ConfigurableBeanFactory factory = applicationContext.getBeanFactory();

        // register default
        factory.registerSingleton("default.context", applicationContext);

        // get the main factory
        parentFactory = (DefaultListableBeanFactory) factory.getParentBeanFactory();

        // create a wrapper around our primary context
        BeanFactoryReference beanfactoryRef = new ContextBeanFactoryReference(applicationContext);

        // set it in the root servlet context
        servletContext.setAttribute("bean.factory.ref", beanfactoryRef);

        // set a remoting codec factory for AMF use
        servletContext.setAttribute("remoting.codec.factory", parentFactory.getBean("remotingCodecFactory"));

        server = (Server) parentFactory.getBean("red5.server");

        clientRegistry = (ClientRegistry) factory.getBean("global.clientRegistry");

        globalInvoker = (ServiceInvoker) factory.getBean("global.serviceInvoker");

        globalStrategy = (MappingStrategy) factory.getBean("global.mappingStrategy");

        global = (GlobalScope) factory.getBean("global.scope");
        logger.debug("GlobalScope: " + global);
        global.setServer(server);
        global.register();
        global.start();

        globalResolver = new ScopeResolver();
        globalResolver.setGlobalScope(global);

        logger.debug("About to grab Webcontext bean for Global");
        Context globalContext = (Context) factory.getBean("global.context");
        globalContext.setCoreBeanFactory(parentFactory);
        globalContext.setClientRegistry(clientRegistry);
        globalContext.setServiceInvoker(globalInvoker);
        globalContext.setScopeResolver(globalResolver);
        globalContext.setMappingStrategy(globalStrategy);

        logger.debug("About to grab Webcontext bean for ROOT");
        Context webContext = (Context) factory.getBean("web.context");
        webContext.setCoreBeanFactory(parentFactory);
        webContext.setClientRegistry(clientRegistry);
        webContext.setServiceInvoker(globalInvoker);
        webContext.setScopeResolver(globalResolver);
        webContext.setMappingStrategy(globalStrategy);

        WebScope scope = (WebScope) factory.getBean("web.scope");
        scope.setServer(server);
        scope.setParent(global);
        scope.register();
        scope.start();

        // grab the scope list (other war/webapps)
        IRemotableList remote = (IRemotableList) Naming
                .lookup("rmi://localhost:" + rmiPort + "/subContextList");
        logger.debug("Children: " + remote.numChildren());
        if (remote.hasChildren()) {
            logger.debug("Children were detected");
            for (int i = 0; i < remote.numChildren(); i++) {
                logger.debug("Enumerating children");
                WebSettings settings = remote.getAt(i);
                registerSubContext(settings.getWebAppKey());
            }
            logger.debug("End of children...");
        }

    } catch (Throwable t) {
        logger.error(t);
    } finally {
        timer = new Timer();
        checkScopeList = new CheckScopeListTask();
        timer.scheduleAtFixedRate(checkScopeList, 1000, 30000);
    }

    long startupIn = System.currentTimeMillis() - time;
    logger.info("Startup done in: " + startupIn + " ms");

}

From source file:org.openmrs.web.filter.update.UpdateFilter.java

/**``
 * Do everything to get openmrs going./*from   ww  w  . j  a va2 s.co m*/
 *
 * @param servletContext the servletContext from the filterconfig
 * @see Listener#startOpenmrs(ServletContext)
 */
private void startOpenmrs(ServletContext servletContext) throws Exception {
    // start spring
    // after this point, all errors need to also call: contextLoader.closeWebApplicationContext(event.getServletContext())
    // logic copied from org.springframework.web.context.ContextLoaderListener
    ContextLoader contextLoader = new ContextLoader();
    contextLoader.initWebApplicationContext(servletContext);

    try {
        WebDaemon.startOpenmrs(servletContext);
    } catch (Exception exception) {
        contextLoader.closeWebApplicationContext(servletContext);
        throw exception;
    }
}

From source file:org.red5.server.war.WarLoaderServlet.java

public void registerSubContext(String webAppKey) {
    // get the sub contexts - servlet context
    ServletContext ctx = servletContext.getContext(webAppKey);
    if (ctx == null) {
        ctx = servletContext;// w  w w . ja  v  a 2  s  .c o m
    }
    ContextLoader loader = new ContextLoader();
    ConfigurableWebApplicationContext appCtx = (ConfigurableWebApplicationContext) loader
            .initWebApplicationContext(ctx);
    appCtx.setParent(applicationContext);
    appCtx.refresh();

    ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);

    ConfigurableBeanFactory appFactory = appCtx.getBeanFactory();

    logger.debug("About to grab Webcontext bean for " + webAppKey);
    Context webContext = (Context) appCtx.getBean("web.context");
    webContext.setCoreBeanFactory(parentFactory);
    webContext.setClientRegistry(clientRegistry);
    webContext.setServiceInvoker(globalInvoker);
    webContext.setScopeResolver(globalResolver);
    webContext.setMappingStrategy(globalStrategy);

    WebScope scope = (WebScope) appFactory.getBean("web.scope");
    scope.setServer(server);
    scope.setParent(global);
    scope.register();
    scope.start();

    // register the context so we dont try to reinitialize it
    registeredContexts.add(ctx);

}