Example usage for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext

List of usage examples for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext

Introduction

In this page you can find the example usage for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext.

Prototype

@Nullable
public static WebApplicationContext getWebApplicationContext(ServletContext sc) 

Source Link

Document

Find the root WebApplicationContext for this web app, typically loaded via org.springframework.web.context.ContextLoaderListener .

Usage

From source file:de.itsvs.cwtrpc.controller.RemoteServiceControllerServlet.java

@Override
public void init(ServletConfig config) throws ServletException {
    final WebApplicationContext applicationContext;
    final RemoteServiceControllerConfig controllerConfig;
    String configBeanName;//from  ww w.  ja v a2 s  .  c om
    String initParam;

    super.init(config);

    initParam = config.getInitParameter(SPRING_REQUEST_CONTEXT_ENABLED_INIT_PARAM);
    if (initParam != null) {
        setSpringRequestContextEnabled(Boolean.valueOf(initParam));
    } else {
        setSpringRequestContextEnabled(DEFAULT_SPRING_REQUEST_CONTEXT_ENABLED);
    }
    if (log.isInfoEnabled()) {
        log.info("Spring request context initialization on every request: " + isSpringRequestContextEnabled());
    }

    configBeanName = config.getInitParameter(CONFIG_BEAN_NAME_INIT_PARAM);
    if (configBeanName == null) {
        configBeanName = RemoteServiceControllerConfig.DEFAULT_BEAN_ID;
    }

    applicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
    if (log.isDebugEnabled()) {
        log.debug(
                "Resolving controller config with bean name '" + configBeanName + "' from application context");
    }
    controllerConfig = applicationContext.getBean(configBeanName, RemoteServiceControllerConfig.class);

    setApplicationContext(applicationContext);
    setSerializationPolicyProvider(controllerConfig.getSerializationPolicyProvider());
    setServiceConfigsByUri(
            Collections.unmodifiableMap(createPreparedRemoteServiceConfigBuilder().build(controllerConfig)));

    setUncaughtExceptions(getUncaughtExceptions(controllerConfig));
}

From source file:com.ewcms.web.pubsub.MessageSender.java

private NotesFacable getNotesFac(ServletContext context) {
    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    Assert.notNull(applicationContext, "Can not get spring's context");

    NotesFacable fac = applicationContext.getBean("notesFac", NotesFacable.class);
    Assert.notNull(fac, "Can not get notesFac");

    return fac;/*from  w ww.  j  ava2  s  .co m*/
}

From source file:se.vgregion.javg.web.filter.WebErrorHandlingFilter.java

public void init(FilterConfig arg0) throws ServletException {
    tyckTillErrorFormURL = arg0.getInitParameter("TyckTillErrorFormURL");
    // String ldapContextConfigLocation =
    // arg0.getServletContext().getInitParameter("ldapContextConfigLocation");
    ac = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());

    contextName = arg0.getServletContext().getServletContextName();
    reportMethod = arg0.getInitParameter("TyckTillReportMethod");
    reportEmail = arg0.getInitParameter("TyckTillReportEmail");
    getLdapService(); // Test Before use
}

From source file:net.danielkvasnicka.flower.FlowerDispatcherServlet.java

/**
 * Setup Spring managed resources on me/* w  ww  . j a va 2s .  c o m*/
 */
@Override
public void init(final ServletConfig servletConfig) throws ServletException {
    this.servletConfig = servletConfig;
    this.appCtx = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
    this.appCtx.getAutowireCapableBeanFactory().autowireBeanProperties(this,
            AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
}

From source file:org.mfr.web.PrivateContentAccessManager.java

@Override
public void init(FilterConfig config) throws ServletException {
    springContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
    siteDao = (SiteDao) springContext.getBean("siteDao");
    userManager = (UserManager) springContext.getBean("userManager");
    permissionDao = (PermissionDao) springContext.getBean("permissionDao");
}

From source file:com.tecapro.inventory.common.servlet.BaseServlet.java

/**
 * Error log output/*from   w  w  w .  j a v  a2  s  .  com*/
 * 
 * @param zform
 * @param request
 * @param e
 * @param method
 */
protected void errorLog(BaseForm zform, HttpServletRequest request, Throwable e, String method) {

    InfoValue info = null;

    if (zform != null) {
        info = zform.getValue().getInfo();
    } else {
        info = new InfoValue();
    }

    LogUtil logUtil = (LogUtil) WebApplicationContextUtils.getWebApplicationContext(getServletContext())
            .getBean(BEAN_NAME_LOG_UTIL);

    logUtil.errorLog(LOG, BaseServlet.class.getSimpleName(), method, info, "------ Error information ------");
    logUtil.errorLog(LOG, BaseServlet.class.getSimpleName(), method, info, e);

    if (request != null) {
        logUtil.errorLog(LOG, BaseServlet.class.getSimpleName(), method, info,
                "------ Input information ------");
        logUtil.errorLog(LOG, BaseServlet.class.getSimpleName(), method, info,
                logUtil.dump(request.getParameterMap()));
    }

    if (zform != null) {
        logUtil.errorLog(LOG, BaseServlet.class.getSimpleName(), method, info,
                "------ Output information ------");
        logUtil.errorLog(LOG, BaseServlet.class.getSimpleName(), method, info, logUtil.dump(zform));
    }

}

From source file:com.ewcms.web.pubsub.MessageSender.java

private DocumentFacable getDocumentFac(ServletContext context) {
    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    Assert.notNull(applicationContext, "Can not get spring's context");

    DocumentFacable fac = applicationContext.getBean("documentFac", DocumentFacable.class);
    Assert.notNull(fac, "Can not get documentFac");

    return fac;/* w  w w .j a  v  a  2  s  .  c o  m*/
}

From source file:org.jasig.portlet.announcements.spring.PortletApplicationContextLocator.java

/**
 * If running in a web application the existing {@link org.springframework.web.context.WebApplicationContext} will be returned. if
 * not a singleton {@link org.springframework.context.ApplicationContext} is created if needed and returned. Unless a {@link org.springframework.web.context.WebApplicationContext}
 * is specifically needed this method should be used as it will work both when running in and out
 * of a web application/*from  www  . j a  v a2  s  .  com*/
 *
 * @return The {@link org.springframework.context.ApplicationContext} for the portal.
 */
public static ApplicationContext getApplicationContext(String importExportContextFile) {
    final ServletContext context = servletContext;

    if (context != null) {
        LOGGER.debug("Using WebApplicationContext");

        if (applicationContextCreator.isCreated()) {
            final IllegalStateException createException = new IllegalStateException(
                    "A portal managed ApplicationContext has already been created but now a ServletContext is available and a WebApplicationContext will be returned. "
                            + "This situation should be resolved by delaying calls to this class until after the web-application has completely initialized.");
            LOGGER.error(createException, createException);
            LOGGER.error("Stack trace of original ApplicationContext creator", directCreatorThrowable);
            throw createException;
        }

        final WebApplicationContext webApplicationContext = WebApplicationContextUtils
                .getWebApplicationContext(context);
        if (webApplicationContext == null) {
            throw new IllegalStateException(
                    "ServletContext is available but WebApplicationContextUtils.getWebApplicationContext(ServletContext) returned null. Either the application context failed to load or is not yet done loading.");
        }
        return webApplicationContext;
    }

    return applicationContextCreator.get(importExportContextFile);
}

From source file:io.milton.servlet.SpringMiltonFilter.java

@SuppressWarnings("resource")
protected void initSpringApplicationContext(FilterConfig fc) {

    final WebApplicationContext rootContext = WebApplicationContextUtils
            .getWebApplicationContext(fc.getServletContext());

    StaticApplicationContext parent;//from w ww  . j  a  v  a2s .c  o m
    if (rootContext != null) {
        log.info("Found a root spring context, and using it");
        parent = new StaticApplicationContext(rootContext);
    } else {
        log.info("No root spring context");
        parent = new StaticApplicationContext();
    }

    final FilterConfigWrapper configWrapper = new FilterConfigWrapper(fc);
    parent.getBeanFactory().registerSingleton("config", configWrapper);
    parent.getBeanFactory().registerSingleton("servletContext", fc.getServletContext());
    File webRoot = new File(fc.getServletContext().getRealPath("/"));
    parent.getBeanFactory().registerSingleton("webRoot", webRoot);
    log.info("Registered root webapp path in: webroot=" + webRoot.getAbsolutePath());
    parent.refresh();

    final String configClass = fc.getInitParameter("contextConfigClass");
    final String sFiles = fc.getInitParameter("contextConfigLocation");

    ConfigurableApplicationContext ctx = null;
    if (StringUtils.isNotBlank(configClass)) {
        try {
            Class<?> clazz = Class.forName(configClass);
            final AnnotationConfigApplicationContext annotationCtx = new AnnotationConfigApplicationContext();
            annotationCtx.setParent(parent);
            annotationCtx.register(clazz);
            annotationCtx.refresh();
            ctx = annotationCtx;
        } catch (ClassNotFoundException e) {
            ctx = null;
            log.error("Unable to create a child context for Milton", e);
        }
    } else {
        String[] contextFiles;
        if (sFiles != null && sFiles.trim().length() > 0) {
            contextFiles = sFiles.split(" ");
        } else {
            contextFiles = new String[] { "applicationContext.xml" };
        }

        try {
            ctx = new ClassPathXmlApplicationContext(contextFiles, parent);
        } catch (BeansException e) {
            log.error("Unable to create a child context for Milton", e);
        }
    }

    if (ctx == null) {
        log.warn("No child context available, only using parent context");
        context = parent;
    } else {
        context = ctx;
    }

}

From source file:org.jbpm.formbuilder.server.EmbedingServlet.java

protected FormDefinitionService createFormService(HttpServletRequest request, String usr, String pwd) {
    GuvnorFormDefinitionService service = (GuvnorFormDefinitionService) WebApplicationContextUtils
            .getWebApplicationContext(request.getSession().getServletContext()).getBean("guvnorFormService");
    if (usr != null && pwd != null) {
        service.setUser(usr);//from  w w  w. ja  v  a 2 s . c o  m
        service.setPassword(pwd);
        try {
            service.afterPropertiesSet();
        } catch (Exception e) {
        }
    }
    return service;
}