Example usage for org.springframework.web.context ConfigurableWebApplicationContext setServletContext

List of usage examples for org.springframework.web.context ConfigurableWebApplicationContext setServletContext

Introduction

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

Prototype

void setServletContext(@Nullable ServletContext servletContext);

Source Link

Document

Set the ServletContext for this web application context.

Usage

From source file:org.springside.modules.test.spring.SpringWebTestHelper.java

/**
 * ServletContext?Spring WebApplicationContext.
 * //  ww  w.java2  s  .c o  m
 * @param applicationContext ApplicationContext.
 */
public static void initWebApplicationContext(MockServletContext servletContext,
        ApplicationContext applicationContext) {
    ConfigurableWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setParent(applicationContext);
    wac.setServletContext(servletContext);
    wac.setConfigLocation("");
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    wac.refresh();
}

From source file:nz.co.senanque.vaadinsupport.application.SpringApplicationLoader.java

public static ApplicationContext loadContext(Application application, HttpServletRequest request) {

    // Logging/*from   w  w w  .  ja  v  a2s.c  o  m*/
    log.info("loading application context for Vaadin application " + application.getClass().getSimpleName());
    CURRENT_APPLICATION.set(application);

    // Find the application context associated with the servlet; it will be the parent
    ServletContext servletContext;
    try {
        // getServletContext() is a servlet AIP 3.0 method, so don't freak out if it's not there
        servletContext = (ServletContext) HttpServletRequest.class.getMethod("getServletContext")
                .invoke(request);
    } catch (Exception e) {
        servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
    }
    WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(servletContext);

    // Create and configure a new application context for this Application instance
    ConfigurableWebApplicationContext context = new XmlWebApplicationContext();
    context.setId(
            ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContext.getContextPath()
                    + "/" + application.getClass().getSimpleName() + "-" + UNIQUE_INDEX.incrementAndGet());
    context.setParent(parent);
    context.setServletContext(servletContext);
    //context.setServletConfig(??);
    context.setNamespace(application.getClass().getSimpleName());

    // Refresh context
    context.refresh();

    CURRENT_APPLICATION.set(null);
    return context;
}

From source file:com.j2trp.test.util.jersey.spi.spring.container.servlet.SpringServlet.java

private ConfigurableApplicationContext getChildContext(String contextConfigLocation) {
    final ConfigurableWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setParent(getDefaultContext());//w ww .  j a va  2  s .co  m
    ctx.setServletContext(getServletContext());
    ctx.setConfigLocation(contextConfigLocation);

    ctx.refresh();
    return ctx;
}

From source file:net.sf.sail.webapp.spring.impl.CustomContextLoader.java

/**
 * The behaviour of this method is the same as the superclass except for
 * setting of the config locations.//from  www . ja  va  2s .co  m
 * 
 * @throws ClassNotFoundException
 * 
 * @see org.springframework.web.context.ContextLoader#createWebApplicationContext(javax.servlet.ServletContext,
 *      org.springframework.context.ApplicationContext)
 */
@Override
protected WebApplicationContext createWebApplicationContext(ServletContext servletContext,
        ApplicationContext parent) throws BeansException {

    Class<?> contextClass = determineContextClass(servletContext);
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
        throw new ApplicationContextException("Custom context class [" + contextClass.getName()
                + "] is not of type ConfigurableWebApplicationContext");
    }

    ConfigurableWebApplicationContext webApplicationContext = (ConfigurableWebApplicationContext) BeanUtils
            .instantiateClass(contextClass);
    webApplicationContext.setParent(parent);
    webApplicationContext.setServletContext(servletContext);

    String configClass = servletContext.getInitParameter(CONFIG_CLASS_PARAM);
    if (configClass != null) {
        try {
            SpringConfiguration springConfig = (SpringConfiguration) BeanUtils
                    .instantiateClass(Class.forName(configClass));
            webApplicationContext.setConfigLocations(springConfig.getRootApplicationContextConfigLocations());
        } catch (ClassNotFoundException e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error(CONFIG_CLASS_PARAM + " <" + configClass + "> not found.", e);
            }
            throw new InvalidParameterException("ClassNotFoundException: " + configClass);
        }
    } else {
        throw new InvalidParameterException("No value defined for the required: " + CONFIG_CLASS_PARAM);
    }

    webApplicationContext.refresh();
    return webApplicationContext;
}

From source file:com.katsu.dwm.springframework.Loader.java

public void load(File jar, Properties properties) throws MalformedURLException {
    ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils
            .instantiateClass(XmlWebApplicationContext.class);
    //FIXME/*  www  .  ja v a  2  s  . c  o m*/
    wac.setId("test-context");
    wac.setParent(applicationContext);
    wac.setServletContext(((XmlWebApplicationContext) applicationContext).getServletContext());
    //wac.setServletConfig(((XmlWebApplicationContext) applicationContext).getServletConfig());
    //wac.setNamespace(((XmlWebApplicationContext) applicationContext).getNamespace());
    wac.setConfigLocation(properties.getProperty(LoaderConst.CONTEXT_LOCATION.getValue()));
    //wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
    wac.refresh();

    wac.getServletContext().setAttribute("test-context", wac);
}

From source file:org.sarons.spring4me.web.filter.PageFilter.java

protected ConfigurableWebApplicationContext initPageApplicationContext() {
    WebApplicationContext root = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    ////  w ww  .  java2 s . c  o m
    ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils
            .instantiateClass(contextClass);
    wac.setParent(root);
    wac.setServletContext(getServletContext());
    wac.setServletConfig(new DelegatingServletConfig(getServletContext()));
    wac.setNamespace(getNamespace());
    wac.setConfigLocation(getWidgetConfigLocation());
    wac.refresh();
    //
    return wac;
}

From source file:com.workingmouse.webservice.axis.SpringAxisServlet.java

/**
 * Instantiate the WebApplicationContext for the SpringAxisServlet, either a default
 * XmlWebApplicationContext or a custom context class if set. This implementation
 * expects custom contexts to implement ConfigurableWebApplicationContext.
 * Can be overridden in subclasses.//from  w  w w.  jav  a2 s  .  c o  m
 *
 * @throws org.springframework.beans.BeansException
 *      if the context couldn't be initialized
 *
 * @see #setContextClass
 *
 * @see org.springframework.web.context.support.XmlWebApplicationContext
 */
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
        throws BeansException {

    if (LOG.isDebugEnabled()) {
        LOG.debug("Servlet with name '" + getServletName()
                + "' will try to create custom WebApplicationContext context of class '"
                + getContextClass().getName() + "'" + " using parent context [" + parent + "]");
    }
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) {
        throw new ApplicationContextException("Fatal initialization error in servlet with name '"
                + getServletName() + "': custom WebApplicationContext class [" + getContextClass().getName()
                + "] is not of type ConfigurableWebApplicationContext");
    }

    ConfigurableWebApplicationContext wac = createContextInstance();
    wac.setParent(parent);
    wac.setServletContext(getServletContext());
    wac.setNamespace(getNamespace());

    if (this.contextConfigLocation != null) {
        wac.setConfigLocations(StringUtils.tokenizeToStringArray(this.contextConfigLocation,
                ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS, true, true));
    }
    wac.refresh();
    return wac;
}

From source file:com.easyjf.container.impl.SpringContainer.java

public void init() {
    logger.info(I18n.getLocaleMessage("ext.began.pring.containers.initialization"));
    if (factory == null) {
        if (this.configLocation != null && !"".equals(this.configLocation)) {
            this.factory = new ClassPathXmlApplicationContext(this.configLocation.split(";"));
        } else {//from   ww  w . jav  a 2  s .c om
            this.factory = new ClassPathXmlApplicationContext("classpath: applicationContext.xml");
        }
    }

    if (factory instanceof AbstractApplicationContext) {
        AbstractApplicationContext context = (AbstractApplicationContext) factory;
        if (factory instanceof ConfigurableWebApplicationContext) {
            // ??
            ConfigurableWebApplicationContext webContext = (ConfigurableWebApplicationContext) factory;
            if (parent != null && parent instanceof WebContextContainer) {
                webContext.setServletContext(((WebContextContainer) parent).getServletContext());
            }
            if (!haveStart) {
                context.refresh();
                haveStart = true;
            }
        }
        if (parent != null && parent instanceof WebContextContainer) {
            this.registerWebContext(((WebContextContainer) parent).getServletContext());
        }
    }
    logger.info(I18n.getLocaleMessage("ext.Spring.container.initialized"));
}

From source file:com.xinlv.test.MockContextLoader.java

@Override
public ConfigurableApplicationContext loadContext(String... locations) throws Exception {

    ConfigurableWebApplicationContext context = new XmlWebApplicationContext();
    MockServletContext servletContext = new MockServletContext("") {
        @Override//from   w ww  . j a va2s.c  o m
        public String getRealPath(String arg0) {
            return System.getProperty("java.io.tmpdir");
        }
    };
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    context.setServletContext(servletContext);
    String configLocation = "/xinlv-test-context.xml";
    context.setConfigLocation(configLocation);
    /*String[] configLocations = PortalContextLoaderListener.locateConfigLocations(locations);
    context.setConfigLocations(configLocations);*/
    context.refresh();
    context.registerShutdownHook();
    return context;
}