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

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

Introduction

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

Prototype

void refresh() throws BeansException, IllegalStateException;

Source Link

Document

Load or refresh the persistent representation of the configuration, which might an XML file, properties file, or relational database schema.

Usage

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

/**
 * ServletContext?Spring WebApplicationContext.
 * /*from   w  w w  .  j  a  v a  2 s.c om*/
 * @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// w  w w .  j a v  a  2s. 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());/*from  w  w  w .  ja v  a  2  s  .c o  m*/
    ctx.setServletContext(getServletContext());
    ctx.setConfigLocation(contextConfigLocation);

    ctx.refresh();
    return ctx;
}

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

protected ConfigurableWebApplicationContext initPageApplicationContext() {
    WebApplicationContext root = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    ///*from   ww  w .  j  a v a2  s.co  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: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  a v a2  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);

}

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.//ww  w.j  av a  2 s  . co  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.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 va2 s  .  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;
}

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//from  w  ww . ja  v a2s . c om
    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);
}