Example usage for org.springframework.web.context WebApplicationContext getBean

List of usage examples for org.springframework.web.context WebApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.web.context WebApplicationContext getBean.

Prototype

Object getBean(String name) throws BeansException;

Source Link

Document

Return an instance, which may be shared or independent, of the specified bean.

Usage

From source file:org.LexGrid.LexBIG.caCore.web.util.LexEVSHTTPQuery.java

/**
 * Initialize the servlet// w  w  w.j ava2s  .  c o m
 */
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    context = config.getServletContext();

    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
    Properties systemProperties = (Properties) ctx.getBean("WebSystemProperties");

    cacoreStyleSheet = systemProperties.getProperty("resultOutputFormatter");
    jsonStyleSheet = systemProperties.getProperty("jsonOutputFormatter");

    log.debug("cacoreStylesheet: " + cacoreStyleSheet);

    try {
        String pageCount = systemProperties.getProperty("rowCounter");
        log.debug("rowCounter: " + pageCount);
        if (pageCount != null) {
            pageSize = Integer.parseInt(pageCount);
        }
    } catch (Exception ex) {
        log.error("Exception: ", ex);
    }
}

From source file:org.wallride.job.UpdatePostViewsItemWriter.java

@Override
public void write(List<? extends List> items) throws Exception {
    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext,
            "org.springframework.web.servlet.FrameworkServlet.CONTEXT.guestServlet");
    if (context == null) {
        throw new ServiceException("GuestServlet is not ready yet");
    }/*from  ww  w . j a v  a 2 s  . co  m*/

    final RequestMappingHandlerMapping mapping = context.getBean(RequestMappingHandlerMapping.class);

    for (List item : items) {
        UriComponents uriComponents = UriComponentsBuilder.fromUriString((String) item.get(0)).build();
        logger.info("Processing [{}]", uriComponents.toString());

        MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
        request.setMethod("GET");
        request.setRequestURI(uriComponents.getPath());
        request.setQueryString(uriComponents.getQuery());
        MockHttpServletResponse response = new MockHttpServletResponse();

        BlogLanguageRewriteRule rewriteRule = new BlogLanguageRewriteRule(blogService);
        BlogLanguageRewriteMatch rewriteMatch = (BlogLanguageRewriteMatch) rewriteRule.matches(request,
                response);
        try {
            rewriteMatch.execute(request, response);
        } catch (ServletException e) {
            throw new ServiceException(e);
        } catch (IOException e) {
            throw new ServiceException(e);
        }

        request.setRequestURI(rewriteMatch.getMatchingUrl());

        HandlerExecutionChain handler;
        try {
            handler = mapping.getHandler(request);
        } catch (Exception e) {
            throw new ServiceException(e);
        }

        if (!(handler.getHandler() instanceof HandlerMethod)) {
            continue;
        }

        HandlerMethod method = (HandlerMethod) handler.getHandler();
        if (!method.getBeanType().equals(ArticleDescribeController.class)
                && !method.getBeanType().equals(PageDescribeController.class)) {
            continue;
        }

        // Last path mean code of post
        String code = uriComponents.getPathSegments().get(uriComponents.getPathSegments().size() - 1);
        Post post = postRepository.findOneByCodeAndLanguage(code, rewriteMatch.getBlogLanguage().getLanguage());
        if (post == null) {
            logger.debug("Post not found [{}]", code);
            continue;
        }

        logger.info("Update the PageView. Post ID [{}]: {} -> {}", post.getId(), post.getViews(), item.get(1));
        post.setViews(Long.parseLong((String) item.get(1)));
        postRepository.saveAndFlush(post);
    }
}

From source file:org.keysupport.shibboleth.idp.x509.X509AuthServlet.java

/** {@inheritDoc} */
@Override/*from w  w  w  .ja  v  a2  s  .com*/
public void init(final ServletConfig config) throws ServletException {
    super.init(config);

    final WebApplicationContext springContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());

    final String param = config.getInitParameter(TRUST_ENGINE_PARAM);
    if (param != null) {
        log.debug("Looking up TrustEngine bean: {}", param);
        final Object bean = springContext.getBean(param);
        if (bean instanceof TrustEngine) {
            trustEngine = (TrustEngine) bean;
        } else {
            throw new ServletException("Bean " + param + " was missing, or not a TrustManager");
        }
    }
}

From source file:net.sourceforge.vulcan.web.ProjectFileServlet.java

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);

    final WebApplicationContext wac = WebApplicationContextUtils
            .getRequiredWebApplicationContext(config.getServletContext());

    projectManager = (ProjectManager) wac.getBean(Keys.PROJECT_MANAGER);
    buildManager = (BuildManager) wac.getBean(Keys.BUILD_MANAGER);
}

From source file:org.ahp.core.bootstrap.AhpBootstrapStrutsPlugin.java

/**
 * /*w  w w  .  j  a  v  a2  s.co m*/
 */
public void init(ActionServlet pActionServlet, ModuleConfig pModuleConfig) throws ServletException {
    System.out.println("Initializing AhpBootstrapStrutsPlugin");
    try {
        String lRealPath = pActionServlet.getServletContext().getRealPath("/");
        System.out.println("ConfigurationFiles - " + this.getAhpConfigurationLocation());
        WebApplicationContext lWebApplicationContext = WebApplicationContextUtils
                .getWebApplicationContext(pActionServlet.getServletContext());
        String lBeanName = StringUtils.trimToEmpty(this.getAhpBootstrapManagerBeanId());
        if (lBeanName == null) {
            lBeanName = DEFAULT_AHP_BOOTSTRAP_MANAGER_BEAN_ID;
        }
        IAhpBootstrapManager lAhpBootstrapManager = (IAhpBootstrapManager) lWebApplicationContext
                .getBean(lBeanName);
        if (lAhpBootstrapManager == null)
            lAhpBootstrapManager = lWebApplicationContext.getBean(IAhpBootstrapManager.class);
        lAhpBootstrapManager.bootstrap(this.getAhpConfigurationLocation().split(","), lRealPath);
    } catch (AhpRuntimeException exAhpRuntime) {
        throw exAhpRuntime;
    } catch (Exception ex) {
        throw new AhpRuntimeException("AHP.001.0001", ex);
    }
}

From source file:org.malaguna.cmdit.bbeans.AbstractBean.java

/**
* Resuelve dinmicamente el ServiceDelegate
*///from w w  w  .j  a va2  s  .c o  m
public AbstractBean() {
    WebApplicationContext wac = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    super.setService((ServiceDelegate) wac.getBean(BeanNames.SERVICE));
}

From source file:org.grails.plugin.batch.Launcher.java

private void executeMainClass(GrailsApplication application, DefaultGrailsMainClass main) {
    Map<String, Object> context = new LinkedHashMap<String, Object>();
    for (@SuppressWarnings("rawtypes")
    Enumeration e = servletContext.getAttributeNames(); e.hasMoreElements();) {
        String key = (String) e.nextElement();
        Object value = servletContext.getAttribute(key);
        context.put(key, value);//from  www  .  j  a v  a  2s  .c  o  m
    }

    WebApplicationContext webContext = (WebApplicationContext) application.getMainContext();

    PersistenceContextInterceptor interceptor = null;
    String[] beanNames = webContext.getBeanNamesForType(PersistenceContextInterceptor.class);
    if (beanNames.length > 0) {
        interceptor = (PersistenceContextInterceptor) webContext.getBean(beanNames[0]);
    }

    if (interceptor != null) {
        interceptor.init();
    }

    try {
        main.callRun(context);

        if (interceptor != null) {
            interceptor.flush();
        }
    } finally {
        if (interceptor != null) {
            interceptor.destroy();
        }
    }
}

From source file:org.sakaiproject.jsf.app.SakaiVariableResolver.java

/**
 * @inheritDoc/*from  w  ww  .j  a va  2s  . co m*/
 */
public Object resolveVariable(FacesContext context, String name) throws EvaluationException {
    if (M_log.isDebugEnabled())
        M_log.debug("resolving: " + name);

    Object rv = null;

    // first, give the other a shot
    if (m_resolver != null) {
        rv = m_resolver.resolveVariable(context, name);
        if (rv != null) {
            if (M_log.isDebugEnabled())
                M_log.debug("resolving: " + name + " with other to: " + rv);
            return rv;
        }
    }

    // now, we extend. Check for a component/bean, using the local Spring method to pick up local and global definitions.
    WebApplicationContext wac = null;

    try {
        wac = WebApplicationContextUtils
                .getWebApplicationContext((ServletContext) context.getExternalContext().getContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (wac != null) {
        // try the name as given
        try {
            rv = wac.getBean(name);
            if (rv != null) {
                if (M_log.isDebugEnabled())
                    M_log.debug("resolving: " + name + " via spring to : " + rv);
                return rv;
            }
        } catch (NoSuchBeanDefinitionException ignore) {
            // the bean doesn't exist, but its not necessarily an error
        }

        // since the jsf environment does not allow a dot in the name, convert from underbar to dot and try again
        if (name.indexOf('_') != -1) {
            String alternate = name.replace('_', '.');
            try {
                rv = wac.getBean(alternate);

                if (rv != null) {
                    if (M_log.isDebugEnabled())
                        M_log.debug("resolving: " + alternate + " via spring to : " + rv);
                    return rv;
                }
            } catch (NoSuchBeanDefinitionException ignore) {
                // the bean doesn't exist, but its not necessarily an error
            }
        }
    }

    // if no wac, try using the component manager
    else
        rv = ComponentManager.get(name);
    {
        if (rv != null) {
            if (M_log.isDebugEnabled())
                M_log.debug("resolving: " + name + " via component manager to : " + rv);
            return rv;
        }

        // since the jsf environment does not allow a dot in the name, convert from underbar to dot and try again
        if (name.indexOf('_') != -1) {
            String alternate = name.replace('_', '.');
            rv = ComponentManager.get(alternate);

            if (rv != null) {
                if (M_log.isDebugEnabled())
                    M_log.debug("resolving: " + alternate + " via component manager to : " + rv);
                return rv;
            }
        }
    }

    if (M_log.isDebugEnabled())
        M_log.debug("resolving: " + name + " unresolved!");
    return null;
}

From source file:org.spring4gwt.server.SpringGwtRemoteServiceServlet.java

/**
 * Look up a spring bean with the specified name in the current web
 * application context.// w  ww.  java2s  .  c  om
 * 
 * @param name
 *            bean name
 * @return the bean
 */
protected Object getBean(String name) {
    WebApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(getServletContext());
    if (applicationContext == null) {
        throw new IllegalStateException("No Spring web application context found");
    }
    if (!applicationContext.containsBean(name)) {
        {
            throw new IllegalArgumentException("Spring bean not found: " + name);
        }
    }
    return applicationContext.getBean(name);
}