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

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

Introduction

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

Prototype

public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc)
        throws IllegalStateException 

Source Link

Document

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

Usage

From source file:jp.terasoluna.fw.web.taglib.WriteCodeValueTag.java

/**
 * ^O]Jn?\bh?B//from  w ww.j  a va2s  . c om
 *
 * <p>
 * T?[ubgReLXg?AApplicationContext?A
 * &quot;codeList&quot; ?w id
 *  CodeListLoader ?AR?[hXg
 * l?A?o?B
 * &quot;key&quot; ?w?AL?[l?A
 * w?A&quot;name&quot; ?wbean
 * L?[p?B
 *
 * R?[hXg???A
 * L?[????A?o?B
 * </p>
 *
 * @return ???w?B? EVAL_BODY_INCLUDE
 * @throws JspException JSP O
 */
@Override
public int doStartTag() throws JspException {
    if (log.isDebugEnabled()) {
        log.debug("doStartTag() called.");
    }

    if ("".equals(codeList) || codeList == null) {
        // codeListw??
        log.error("codeList is required.");
        throw new JspTagException("codeList is required.");
    }

    if (key == null && name == null) {
        // keynamew??
        log.error("key and name is required.");
        throw new JspTagException("key and name is required.");
    }

    String codeKey = null;

    if ((key == null) && (name != null)) {
        if (TagUtil.lookup(pageContext, name, scope) == null) {
            log.error("bean id:" + name + " is not defined.");
            throw new JspTagException("bean id:" + name + " is not defined.");
        }
        Object bean = null;
        try {
            bean = TagUtil.lookup(pageContext, name, property, scope);
        } catch (JspException e) {
            // 
        }
        if (bean == null) {
            log.error("Cannot get property[" + name + "." + property + "].");
            throw new JspTagException("Cannot get property[" + name + "." + property + "].");
        }
        codeKey = bean.toString();
    } else {
        codeKey = key;
    }

    // T?[ubgReLXg?AApplicationContext?B
    ServletContext sc = pageContext.getServletContext();
    ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

    CodeListLoader loader = null;

    if (context.containsBean(codeList)) {
        try {
            loader = (CodeListLoader) context.getBean(codeList);
        } catch (ClassCastException e) {
            // BeanCodeListLoaderOX??[
            String errorMessage = "bean id:" + codeList + " is not instance of CodeListLoader.";
            log.error(errorMessage);
            throw new JspTagException(errorMessage, e);
        }
    }

    JspWriter out = pageContext.getOut();

    if (loader == null) {
        log.error("CodeListLoader:" + codeList + " is not defined.");
        throw new JspTagException("CodeListLoader:" + codeList + " is not defined.");
    }

    // ?P?[
    Locale locale = RequestUtils.getUserLocale((HttpServletRequest) pageContext.getRequest(),
            Globals.LOCALE_KEY);

    CodeBean[] codeBeanArray = loader.getCodeBeans(locale);
    if (codeBeanArray == null) {
        // codeBeanListnull??
        if (log.isWarnEnabled()) {
            log.warn("Codebean is null. CodeListLoader(bean id:" + codeList + ")");
        }
        // 
    } else {
        try {
            // ????R?[hXgl?o?B
            for (int i = 0; i < codeBeanArray.length; i++) {
                if (codeKey.equals(codeBeanArray[i].getId())) {
                    out.print(codeBeanArray[i].getName());
                    break;
                }
            }
            // 
        } catch (IOException ioe) {
            log.error("", ioe);
            throw new JspTagException(ioe);
        }

    }

    return EVAL_BODY_INCLUDE;
}

From source file:org.socialsignin.springsocial.security.signin.SpringSocialSecurityAccessDeniedHandler.java

protected Collection<WebInvocationPrivilegeEvaluator> getPrivilegeEvaluators(HttpServletRequest request)
        throws ServletException {
    ServletContext servletContext = request.getSession().getServletContext();
    ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    Map<String, WebInvocationPrivilegeEvaluator> wipes = ctx
            .getBeansOfType(WebInvocationPrivilegeEvaluator.class);

    if (wipes.size() == 0) {
        throw new ServletException(
                "No visible WebInvocationPrivilegeEvaluator instance could be found in the application "
                        + "context. There must be at least one in order to support the use of URL access checks in this AccessDeniedHandler.");
    }//from w w w .j  a v a 2  s  .c  o m

    return wipes.values();
}

From source file:jp.co.opentone.bsol.linkbinder.view.filter.ProjectSwitchFilter.java

private ApplicationContext createApplicationContext(HttpSession session) {
    ServletContext context = session.getServletContext();
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(context);

    return applicationContext;
}

From source file:org.openmrs.contrib.metadatarepository.webapp.taglib.LabelTag.java

/**
 * Get the validator resources from a ValidatorFactory defined in the
 * web application context or one of its parent contexts.
 * The bean is resolved by type (org.springframework.validation.commons.ValidatorFactory).
 *
 * @return ValidatorResources from a ValidatorFactory.
 *//*from   w w  w .  j av a2 s  .  c o m*/
private ValidatorResources getValidatorResources() {
    // look in servlet beans definition (i.e. action-servlet.xml)
    WebApplicationContext ctx = (WebApplicationContext) pageContext.getRequest()
            .getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    ValidatorFactory factory = null;
    try {
        factory = (ValidatorFactory) BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, ValidatorFactory.class,
                true, true);
    } catch (NoSuchBeanDefinitionException e) {
        // look in main application context (i.e. applicationContext.xml)
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());
        factory = (ValidatorFactory) BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, ValidatorFactory.class,
                true, true);
    }
    return factory.getValidatorResources();
}

From source file:com.someco.servlets.AuthenticationFilter.java

public void init(FilterConfig config) throws ServletException {
    this.context = config.getServletContext();
    WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    transactionService = serviceRegistry.getTransactionService();
    nodeService = serviceRegistry.getNodeService();

    authComponent = (AuthenticationComponent) ctx.getBean("AuthenticationComponent");
    authService = (AuthenticationService) ctx.getBean("AuthenticationService");
    personService = (PersonService) ctx.getBean("personService");

    // Get a list of the available locales
    ConfigService configServiceService = (ConfigService) ctx.getBean("webClientConfigService");
    LanguagesConfigElement configElement = (LanguagesConfigElement) configServiceService.getConfig("Languages")
            .getConfigElement(LanguagesConfigElement.CONFIG_ELEMENT_ID);

    m_languages = configElement.getLanguages();

    casUserSessionAttributeName = config.getInitParameter(CAS_USER_INIT_PARAM_NAME);
    if (casUserSessionAttributeName == null) {
        logger.error("CAS : Filter init-param named " + CAS_USER_INIT_PARAM_NAME + " not found in web.xml");
        Enumeration enumNames = context.getInitParameterNames();
        while (enumNames.hasMoreElements()) {
            String name = enumNames.nextElement().toString();
            logger.error("init param " + name + ": " + context.getInitParameter(name));
        }//from  w  ww  .  j a v a 2s. co m
        // last resort - hack in the default CAS attribute name. At least it prevents 
        // NPEs later.
        casUserSessionAttributeName = "edu.yale.its.tp.cas.client.filter.user";
    }
}

From source file:com.qut.middleware.esoe.authn.servlet.AuthnServlet.java

@Override
public void init() throws ServletException {
    FileInputStream configFile;//from  w ww.j  ava 2 s. c  om
    Properties props;
    WebApplicationContext webAppContext;

    try {
        configFile = new FileInputStream(System.getProperty("esoe.data") + ConfigurationConstants.ESOE_CONFIG);

        props = new java.util.Properties();

        props.load(configFile);

        /* Spring integration to make our servlet aware of IoC */
        webAppContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

        this.authnProcessor = (AuthnProcessor) webAppContext.getBean(ConfigurationConstants.AUTHN_PROCESSOR,
                com.qut.middleware.esoe.authn.AuthnProcessor.class);

        if (this.authnProcessor == null) {
            this.logger.error(MessageFormat.format(Messages.getString("AuthnServlet.1"), //$NON-NLS-1$
                    ConfigurationConstants.AUTHN_PROCESSOR));
            throw new IllegalArgumentException(
                    Messages.getString("AuthnServlet.1") + ConfigurationConstants.AUTHN_PROCESSOR); //$NON-NLS-1$
        }

        this.authnDynamicURLParam = props.getProperty(ConfigurationConstants.AUTHN_DYNAMIC_URL_PARAM);
        this.sessionTokenName = props.getProperty(ConfigurationConstants.ESOE_SESSION_TOKEN_NAME);
        this.sessionDomain = props.getProperty(ConfigurationConstants.ESOE_SESSION_DOMAIN);
        this.disableSSOTokenName = props.getProperty(ConfigurationConstants.DISABLE_SSO_TOKEN_NAME);

        if (this.authnDynamicURLParam == null) {
            this.logger.error(Messages.getString("AuthnServlet.10") //$NON-NLS-1$
                    + ConfigurationConstants.AUTHN_DYNAMIC_URL_PARAM + Messages.getString("AuthnServlet.11")); //$NON-NLS-1$
            throw new IllegalArgumentException(Messages.getString("AuthnServlet.10") //$NON-NLS-1$
                    + ConfigurationConstants.AUTHN_DYNAMIC_URL_PARAM + Messages.getString("AuthnServlet.11")); //$NON-NLS-1$
        }
        if (this.sessionTokenName == null) {
            this.logger.error(Messages.getString("AuthnServlet.10") //$NON-NLS-1$
                    + ConfigurationConstants.ESOE_SESSION_TOKEN_NAME + Messages.getString("AuthnServlet.11")); //$NON-NLS-1$
            throw new IllegalArgumentException(Messages.getString("AuthnServlet.10") //$NON-NLS-1$
                    + ConfigurationConstants.ESOE_SESSION_TOKEN_NAME + Messages.getString("AuthnServlet.11")); //$NON-NLS-1$
        }
        if (this.sessionDomain == null) {
            this.logger.error(Messages.getString("AuthnServlet.10") //$NON-NLS-1$
                    + ConfigurationConstants.ESOE_SESSION_DOMAIN + Messages.getString("AuthnServlet.11")); //$NON-NLS-1$
            throw new IllegalArgumentException(Messages.getString("AuthnServlet.10") //$NON-NLS-1$
                    + ConfigurationConstants.ESOE_SESSION_DOMAIN + Messages.getString("AuthnServlet.11")); //$NON-NLS-1$
        }
        if (this.disableSSOTokenName == null) {
            this.logger.error(Messages.getString("AuthnServlet.10") //$NON-NLS-1$
                    + ConfigurationConstants.DISABLE_SSO_TOKEN_NAME + Messages.getString("AuthnServlet.11")); //$NON-NLS-1$
            throw new IllegalArgumentException(Messages.getString("AuthnServlet.10") //$NON-NLS-1$
                    + ConfigurationConstants.DISABLE_SSO_TOKEN_NAME + Messages.getString("AuthnServlet.11")); //$NON-NLS-1$
        }

        this.logger.info(Messages.getString("AuthnServlet.33")); //$NON-NLS-1$
    } catch (BeansException e) {
        this.logger.error(MessageFormat.format(Messages.getString("AuthnServlet.12"), //$NON-NLS-1$
                ConfigurationConstants.AUTHN_PROCESSOR, e.getLocalizedMessage()));
        throw new ServletException(MessageFormat.format(Messages.getString("AuthnServlet.12"), //$NON-NLS-1$
                ConfigurationConstants.AUTHN_PROCESSOR, e.getLocalizedMessage()));
    } catch (MalformedURLException e) {
        this.logger.error(Messages.getString("AuthnServlet.14") //$NON-NLS-1$
                + ConfigurationConstants.ESOE_CONFIG + Messages.getString("AuthnServlet.15") //$NON-NLS-1$
                + e.getLocalizedMessage());
        throw new ServletException(Messages.getString("AuthnServlet.14") //$NON-NLS-1$
                + ConfigurationConstants.ESOE_CONFIG + Messages.getString("AuthnServlet.15")); //$NON-NLS-1$
    } catch (IllegalStateException e) {
        this.logger.error(Messages.getString("AuthnServlet.16") + e.getLocalizedMessage()); //$NON-NLS-1$
        throw new ServletException(Messages.getString("AuthnServlet.16")); //$NON-NLS-1$
    } catch (IOException e) {
        this.logger.error(Messages.getString("AuthnServlet.17") //$NON-NLS-1$
                + ConfigurationConstants.ESOE_CONFIG + Messages.getString("AuthnServlet.18") //$NON-NLS-1$
                + e.getLocalizedMessage());
        throw new ServletException(Messages.getString("AuthnServlet.17") //$NON-NLS-1$
                + ConfigurationConstants.ESOE_CONFIG + Messages.getString("AuthnServlet.18")); //$NON-NLS-1$
    }
}

From source file:alpha.portal.webapp.taglib.LabelTag.java

/**
 * Get the validator resources from a ValidatorFactory defined in the web
 * application context or one of its parent contexts. The bean is resolved
 * by type (org.springframework.validation.commons.ValidatorFactory).
 * /*w  w w . j a  v  a  2s. com*/
 * @return ValidatorResources from a ValidatorFactory.
 */
private ValidatorResources getValidatorResources() {
    // look in servlet beans definition (i.e. action-servlet.xml)
    WebApplicationContext ctx = (WebApplicationContext) this.pageContext.getRequest()
            .getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    ValidatorFactory factory = null;
    try {
        factory = BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, ValidatorFactory.class, true, true);
    } catch (final NoSuchBeanDefinitionException e) {
        // look in main application context (i.e. applicationContext.xml)
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.pageContext.getServletContext());
        factory = BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, ValidatorFactory.class, true, true);
    }
    return factory.getValidatorResources();
}

From source file:com.openmeap.services.ApplicationManagementServlet.java

/**
 * Pulls parameters out of the request and passes them to the ApplicationManagementPortType bean pulled from the WebApplicationContext
 * /*from  w  w w .j a  va 2  s.co  m*/
 * @param req
 * @return
 */
public Result connectionOpenRequest(HttpServletRequest req) {

    WebApplicationContext context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());

    ConnectionOpenRequest request = createConnectionOpenRequest(req);

    Result result = new Result();

    try {
        ConnectionOpenResponse response = ((ApplicationManagementService) context
                .getBean("applicationManagementService")).connectionOpen(request);
        result.setConnectionOpenResponse(response);
    } catch (WebServiceException wse) {
        Error err = new Error();
        err.setCode(wse.getType().asErrorCode());
        err.setMessage(wse.getMessage());
        result.setError(err);
    }

    return result;
}

From source file:org.lamsfoundation.lams.admin.web.ToolContentListAction.java

private ILearningDesignService getLearningDesignService() {
    if (ToolContentListAction.learningDesignService == null) {
        WebApplicationContext ctx = WebApplicationContextUtils
                .getRequiredWebApplicationContext(getServlet().getServletContext());
        ToolContentListAction.learningDesignService = (ILearningDesignService) ctx
                .getBean("learningDesignService");
    }//w w  w . jav a  2  s . c  o  m
    return ToolContentListAction.learningDesignService;
}

From source file:org.lamsfoundation.lams.admin.web.ToolContentListAction.java

private IUserManagementService getUserManagementService() {
    if (ToolContentListAction.userManagementService == null) {
        WebApplicationContext ctx = WebApplicationContextUtils
                .getRequiredWebApplicationContext(getServlet().getServletContext());
        ToolContentListAction.userManagementService = (IUserManagementService) ctx
                .getBean("userManagementService");
    }//from   w  w w . ja  va2  s  .c  o  m
    return ToolContentListAction.userManagementService;
}