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

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

Introduction

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

Prototype

boolean containsBean(String name);

Source Link

Document

Does this bean factory contain a bean definition or externally registered singleton instance with the given name?

Usage

From source file:org.codehaus.groovy.grails.web.context.GrailsConfigUtils.java

public static WebApplicationContext configureWebApplicationContext(ServletContext servletContext,
        WebApplicationContext parent) {
    ServletContextHolder.setServletContext(servletContext);
    GrailsApplication application = (GrailsApplication) parent.getBean(GrailsApplication.APPLICATION_ID);

    if (LOG.isDebugEnabled()) {
        LOG.debug("[GrailsContextLoader] Configuring Grails Application");
    }// w  w w. j a v a2 s.  c  om

    if (application.getParentContext() == null) {
        application.setApplicationContext(parent);
    }

    GrailsRuntimeConfigurator configurator = null;
    if (parent.containsBean(GrailsRuntimeConfigurator.BEAN_ID)) {
        // get configurator from parent application context
        configurator = (GrailsRuntimeConfigurator) parent.getBean(GrailsRuntimeConfigurator.BEAN_ID);
    } else {
        // get configurator from servlet context
        configurator = determineGrailsRuntimeConfiguratorFromServletContext(application, servletContext,
                parent);
    }

    if (configurator == null) {
        // no configurator, use default
        configurator = new GrailsRuntimeConfigurator(application, parent);
        if (parent.containsBean(GrailsPluginManager.BEAN_NAME)) {
            GrailsPluginManager pluginManager = (GrailsPluginManager) parent
                    .getBean(GrailsPluginManager.BEAN_NAME);
            configurator.setPluginManager(pluginManager);
        }
    }

    final GrailsPluginManager pluginManager = configurator.getPluginManager();

    // return a context that obeys grails' settings
    WebApplicationContext webContext = configurator.configure(servletContext);
    pluginManager.setApplicationContext(webContext);

    configureServletContextAttributes(servletContext, application, pluginManager, webContext);
    LOG.info("[GrailsContextLoader] Grails application loaded.");
    return webContext;
}

From source file:org.codehaus.groovy.grails.web.context.GrailsContextLoader.java

@Override
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
    // disable annoying ehcache up-to-date check
    System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
    ExpandoMetaClass.enableGlobally();/*from  www.  j a va2s . com*/
    Metadata metadata = Metadata.getCurrent();
    if (metadata != null && metadata.isWarDeployed()) {
        Grape.setEnableAutoDownload(false);
        Grape.setEnableGrapes(false);
        Environment.cacheCurrentEnvironment();
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("[GrailsContextLoader] Loading context. Creating parent application context");
    }

    WebApplicationContext ctx;
    try {
        ctx = super.initWebApplicationContext(servletContext);

        if (LOG.isDebugEnabled()) {
            LOG.debug("[GrailsContextLoader] Created parent application context");
        }

        application = ctx.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class);

        final WebApplicationContext finalCtx = ctx;
        ShutdownOperations.addOperation(new Runnable() {
            public void run() {
                if (application != null) {
                    ClassLoader classLoader = application.getClassLoader();
                    if (classLoader instanceof GroovyClassLoader) {
                        MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
                        Class<?>[] loadedClasses = ((GroovyClassLoader) classLoader).getLoadedClasses();
                        for (Class<?> loadedClass : loadedClasses) {
                            metaClassRegistry.removeMetaClass(loadedClass);
                        }
                    }
                }

                GrailsPluginManager pluginManager = null;
                if (finalCtx.containsBean(GrailsPluginManager.BEAN_NAME)) {
                    pluginManager = finalCtx.getBean(GrailsPluginManager.BEAN_NAME, GrailsPluginManager.class);
                }

                if (pluginManager != null) {
                    try {
                        pluginManager.shutdown();
                    } catch (Exception e) {
                        new DefaultStackTraceFilterer().filter(e);
                        LOG.error("Error occurred shutting down plug-in manager: " + e.getMessage(), e);
                    }
                }
            }
        });
        ctx = GrailsConfigUtils.configureWebApplicationContext(servletContext, ctx);
        GrailsConfigUtils.executeGrailsBootstraps(application, ctx, servletContext);
    } catch (Throwable e) {
        GrailsUtil.deepSanitize(e);
        LOG.error("Error initializing the application: " + e.getMessage(), e);

        if (Environment.isDevelopmentMode() && !Environment.isWarDeployed()) {
            // bail out early in order to show appropriate error
            if (System.getProperty("grails.disable.exit") == null) {
                System.exit(1);
            }
        }

        LOG.error("Error initializing Grails: " + e.getMessage(), e);
        if (e instanceof BeansException)
            throw (BeansException) e;

        throw new BootstrapException("Error executing bootstraps", e);
    }
    return ctx;
}

From source file:org.grails.web.servlet.context.GrailsConfigUtils.java

public static WebApplicationContext configureWebApplicationContext(final ServletContext servletContext,
        WebApplicationContext parent) {
    Holders.setServletContext(servletContext);
    Holders.addApplicationDiscoveryStrategy(
            new ServletEnvironmentGrailsApplicationDiscoveryStrategy(servletContext));
    GrailsApplication application = (GrailsApplication) parent.getBean(GrailsApplication.APPLICATION_ID);

    if (LOG.isDebugEnabled()) {
        LOG.debug("[GrailsContextLoader] Configuring Grails Application");
    }/*from w w w .  j  a  va 2s  .  c  o m*/

    if (application.getParentContext() == null) {
        application.setApplicationContext(parent);
    }

    GrailsRuntimeConfigurator configurator = null;
    if (parent.containsBean(GrailsRuntimeConfigurator.BEAN_ID)) {
        // get configurator from parent application context
        configurator = (GrailsRuntimeConfigurator) parent.getBean(GrailsRuntimeConfigurator.BEAN_ID);
    } else {
        // get configurator from servlet context
        configurator = determineGrailsRuntimeConfiguratorFromServletContext(application, servletContext,
                parent);
    }

    if (configurator == null) {
        // no configurator, use default
        configurator = new GrailsRuntimeConfigurator(application, parent);
        if (parent.containsBean(GrailsPluginManager.BEAN_NAME)) {
            GrailsPluginManager pluginManager = (GrailsPluginManager) parent
                    .getBean(GrailsPluginManager.BEAN_NAME);
            configurator.setPluginManager(pluginManager);
        }
    }

    final GrailsPluginManager pluginManager = configurator.getPluginManager();

    // return a context that obeys grails' settings
    WebApplicationContext webContext = configurator.configure(servletContext);
    pluginManager.setApplicationContext(webContext);

    configureServletContextAttributes(servletContext, application, pluginManager, webContext);
    LOG.info("[GrailsContextLoader] Grails application loaded.");
    return webContext;
}

From source file:org.sakaiproject.login.springframework.SafeDelegatingFilterProxy.java

protected void initFilterBean() throws ServletException {
    // If no target bean name specified, use filter name.
    if (getTargetBeanName() == null) {
        setTargetBeanName(getFilterName());
    }/*from  ww  w .ja  va  2s  . c om*/

    // make sure context is valid and bean exists before enabling this filter
    synchronized (this.delegateMonitor) {
        WebApplicationContext wac = findWebApplicationContext();
        if (wac != null) {
            if (wac.containsBean(getTargetBeanName())) {
                super.initFilterBean();
                enabled = true;
            } else {
                log.info("Can't find a bean with name: " + getTargetBeanName() + ", safely disable proxying");
            }
        } else {
            log.warn("Can't find web application context");
        }
    }
}

From source file:org.springframework.web.jsf.el.WebApplicationContextFacesELResolver.java

@Override
@Nullable//from   ww w.j av  a 2s.co m
public Object getValue(ELContext elContext, @Nullable Object base, Object property) throws ELException {
    if (base != null) {
        if (base instanceof WebApplicationContext) {
            WebApplicationContext wac = (WebApplicationContext) base;
            String beanName = property.toString();
            if (logger.isTraceEnabled()) {
                logger.trace("Attempting to resolve property '" + beanName + "' in root WebApplicationContext");
            }
            if (wac.containsBean(beanName)) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "Successfully resolved property '" + beanName + "' in root WebApplicationContext");
                }
                elContext.setPropertyResolved(true);
                try {
                    return wac.getBean(beanName);
                } catch (BeansException ex) {
                    throw new ELException(ex);
                }
            } else {
                // Mimic standard JSF/JSP behavior when base is a Map by returning null.
                return null;
            }
        }
    } else {
        if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(property)) {
            elContext.setPropertyResolved(true);
            return getWebApplicationContext(elContext);
        }
    }

    return null;
}

From source file:org.springframework.web.jsf.el.WebApplicationContextFacesELResolver.java

@Override
@Nullable/* www . j av  a2  s.  c om*/
public Class<?> getType(ELContext elContext, @Nullable Object base, Object property) throws ELException {
    if (base != null) {
        if (base instanceof WebApplicationContext) {
            WebApplicationContext wac = (WebApplicationContext) base;
            String beanName = property.toString();
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to resolve property '" + beanName + "' in root WebApplicationContext");
            }
            if (wac.containsBean(beanName)) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "Successfully resolved property '" + beanName + "' in root WebApplicationContext");
                }
                elContext.setPropertyResolved(true);
                try {
                    return wac.getType(beanName);
                } catch (BeansException ex) {
                    throw new ELException(ex);
                }
            } else {
                // Mimic standard JSF/JSP behavior when base is a Map by returning null.
                return null;
            }
        }
    } else {
        if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(property)) {
            elContext.setPropertyResolved(true);
            return WebApplicationContext.class;
        }
    }

    return null;
}

From source file:org.springframework.web.socket.server.standard.SpringConfigurator.java

@SuppressWarnings("unchecked")
@Override//w  ww. j  a  va2s  .c  om
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    if (wac == null) {
        String message = "Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?";
        logger.error(message);
        throw new IllegalStateException(message);
    }

    String beanName = ClassUtils.getShortNameAsProperty(endpointClass);
    if (wac.containsBean(beanName)) {
        T endpoint = wac.getBean(beanName, endpointClass);
        if (logger.isTraceEnabled()) {
            logger.trace("Using @ServerEndpoint singleton " + endpoint);
        }
        return endpoint;
    }

    Component ann = AnnotationUtils.findAnnotation(endpointClass, Component.class);
    if (ann != null && wac.containsBean(ann.value())) {
        T endpoint = wac.getBean(ann.value(), endpointClass);
        if (logger.isTraceEnabled()) {
            logger.trace("Using @ServerEndpoint singleton " + endpoint);
        }
        return endpoint;
    }

    beanName = getBeanNameByType(wac, endpointClass);
    if (beanName != null) {
        return (T) wac.getBean(beanName);
    }

    if (logger.isTraceEnabled()) {
        logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass);
    }
    return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
}