Example usage for org.springframework.context ApplicationContextException ApplicationContextException

List of usage examples for org.springframework.context ApplicationContextException ApplicationContextException

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContextException ApplicationContextException.

Prototype

public ApplicationContextException(String msg) 

Source Link

Document

Create a new ApplicationContextException with the specified detail message and no root cause.

Usage

From source file:org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.java

/**
 * Returns the {@link ServletWebServerFactory} that should be used to create the
 * embedded {@link WebServer}. By default this method searches for a suitable bean in
 * the context itself.//ww w  . j  a v a2s .c om
 * @return a {@link ServletWebServerFactory} (never {@code null})
 */
protected ServletWebServerFactory getWebServerFactory() {
    // Use bean names so that we don't consider the hierarchy
    String[] beanNames = getBeanFactory().getBeanNamesForType(ServletWebServerFactory.class);
    if (beanNames.length == 0) {
        throw new ApplicationContextException(
                "Unable to start ServletWebServerApplicationContext due to missing "
                        + "ServletWebServerFactory bean.");
    }
    if (beanNames.length > 1) {
        throw new ApplicationContextException(
                "Unable to start ServletWebServerApplicationContext due to multiple "
                        + "ServletWebServerFactory beans : "
                        + StringUtils.arrayToCommaDelimitedString(beanNames));
    }
    return getBeanFactory().getBean(beanNames[0], ServletWebServerFactory.class);
}

From source file:org.springframework.context.support.ApplicationObjectSupport.java

@Override
public final void setApplicationContext(@Nullable ApplicationContext context) throws BeansException {
    if (context == null && !isContextRequired()) {
        // Reset internal context state.
        this.applicationContext = null;
        this.messageSourceAccessor = null;
    } else if (this.applicationContext == null) {
        // Initialize with passed-in context.
        if (!requiredContextClass().isInstance(context)) {
            throw new ApplicationContextException("Invalid application context: needs to be of type ["
                    + requiredContextClass().getName() + "]");
        }/*from   w w w.java  2  s . co m*/
        this.applicationContext = context;
        this.messageSourceAccessor = new MessageSourceAccessor(context);
        initApplicationContext(context);
    } else {
        // Ignore reinitialization if same context passed in.
        if (this.applicationContext != context) {
            throw new ApplicationContextException(
                    "Cannot reinitialize with different application context: current one is ["
                            + this.applicationContext + "], passed-in one is [" + context + "]");
        }
    }
}

From source file:org.springframework.test.context.support.AbstractContextLoader.java

@SuppressWarnings("unchecked")
private void invokeApplicationContextInitializers(ConfigurableApplicationContext context,
        MergedContextConfiguration mergedConfig) {

    Set<Class<? extends ApplicationContextInitializer<?>>> initializerClasses = mergedConfig
            .getContextInitializerClasses();
    if (initializerClasses.isEmpty()) {
        // no ApplicationContextInitializers have been declared -> nothing to do
        return;/* www  .  j a v a2 s  . c om*/
    }

    List<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances = new ArrayList<>();
    Class<?> contextClass = context.getClass();

    for (Class<? extends ApplicationContextInitializer<?>> initializerClass : initializerClasses) {
        Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass,
                ApplicationContextInitializer.class);
        if (initializerContextClass != null && !initializerContextClass.isInstance(context)) {
            throw new ApplicationContextException(String.format(
                    "Could not apply context initializer [%s] since its generic parameter [%s] "
                            + "is not assignable from the type of application context used by this "
                            + "context loader: [%s]",
                    initializerClass.getName(), initializerContextClass.getName(), contextClass.getName()));
        }
        initializerInstances.add((ApplicationContextInitializer<ConfigurableApplicationContext>) BeanUtils
                .instantiateClass(initializerClass));
    }

    AnnotationAwareOrderComparator.sort(initializerInstances);
    for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
        initializer.initialize(context);
    }
}

From source file:org.springframework.web.context.ContextLoader.java

/**
 * Customize the {@link ConfigurableWebApplicationContext} created by this
 * ContextLoader after config locations have been supplied to the context
 * but before the context is <em>refreshed</em>.
 * <p>The default implementation {@linkplain #determineContextInitializerClasses(ServletContext)
 * determines} what (if any) context initializer classes have been specified through
 * {@linkplain #CONTEXT_INITIALIZER_CLASSES_PARAM context init parameters} and
 * {@linkplain ApplicationContextInitializer#initialize invokes each} with the
 * given web application context./*from  w  w w  .ja v a2s. c  o  m*/
 * <p>Any {@code ApplicationContextInitializers} implementing
 * {@link org.springframework.core.Ordered Ordered} or marked with @{@link
 * org.springframework.core.annotation.Order Order} will be sorted appropriately.
 * @param sc the current servlet context
 * @param wac the newly created application context
 * @see #CONTEXT_INITIALIZER_CLASSES_PARAM
 * @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
 */
protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) {
    List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses = determineContextInitializerClasses(
            sc);

    for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass : initializerClasses) {
        Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass,
                ApplicationContextInitializer.class);
        if (initializerContextClass != null && !initializerContextClass.isInstance(wac)) {
            throw new ApplicationContextException(String.format(
                    "Could not apply context initializer [%s] since its generic parameter [%s] "
                            + "is not assignable from the type of application context used by this "
                            + "context loader: [%s]",
                    initializerClass.getName(), initializerContextClass.getName(), wac.getClass().getName()));
        }
        this.contextInitializers.add(BeanUtils.instantiateClass(initializerClass));
    }

    AnnotationAwareOrderComparator.sort(this.contextInitializers);
    for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) {
        initializer.initialize(wac);
    }
}

From source file:org.springframework.web.context.ContextLoader.java

@SuppressWarnings("unchecked")
private Class<ApplicationContextInitializer<ConfigurableApplicationContext>> loadInitializerClass(
        String className) {//from w  w w .  j  a va 2 s .c  o m
    try {
        Class<?> clazz = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
        if (!ApplicationContextInitializer.class.isAssignableFrom(clazz)) {
            throw new ApplicationContextException(
                    "Initializer class does not implement ApplicationContextInitializer interface: " + clazz);
        }
        return (Class<ApplicationContextInitializer<ConfigurableApplicationContext>>) clazz;
    } catch (ClassNotFoundException ex) {
        throw new ApplicationContextException("Failed to load context initializer class [" + className + "]",
                ex);
    }
}

From source file:org.springframework.web.struts.ContextLoaderPlugIn.java

/**
 * Instantiate the WebApplicationContext for the ActionServlet, 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 a  v 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 (logger.isDebugEnabled()) {
        logger.debug("ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() + "', module '"
                + getModulePrefix() + "' 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 ContextLoaderPlugIn for Struts ActionServlet '"
                        + getServletName() + "', module '" + getModulePrefix()
                        + "': custom WebApplicationContext class [" + getContextClass().getName()
                        + "] is not of type ConfigurableWebApplicationContext");
    }

    ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils
            .instantiateClass(getContextClass());
    wac.setParent(parent);
    wac.setServletContext(getServletContext());
    wac.setNamespace(getNamespace());
    if (getContextConfigLocation() != null) {
        wac.setConfigLocations(StringUtils.tokenizeToStringArray(getContextConfigLocation(),
                ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
    }
    wac.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            beanFactory.addBeanPostProcessor(new ActionServletAwareProcessor(getActionServlet()));
        }
    });
    wac.refresh();
    return wac;
}

From source file:org.wise.portal.spring.impl.CustomContextLoaderListener.java

/**
 * The behaviour of this method is the same as the superclass except for
 * setting of the config locations.// w  w  w  . ja  v  a 2 s. c o m
 * 
 * @throws ClassNotFoundException
 * 
 * @see org.springframework.web.context.ContextLoader#createWebApplicationContext(javax.servlet.ServletContext,
 *      org.springframework.context.ApplicationContext)
 */
@Override
protected WebApplicationContext createWebApplicationContext(ServletContext servletContext)
        throws BeansException {

    Class<?> contextClass = determineContextClass(servletContext);
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
        throw new ApplicationContextException("Custom context class [" + contextClass.getName()
                + "] is not of type ConfigurableWebApplicationContext");
    }

    ConfigurableWebApplicationContext webApplicationContext = (ConfigurableWebApplicationContext) BeanUtils
            .instantiateClass(contextClass);
    webApplicationContext.setServletContext(servletContext);

    String configClass = servletContext.getInitParameter(CONFIG_CLASS_PARAM);
    if (configClass != null) {
        try {
            SpringConfiguration springConfig = (SpringConfiguration) BeanUtils
                    .instantiateClass(Class.forName(configClass));
            webApplicationContext.setConfigLocations(springConfig.getRootApplicationContextConfigLocations());
        } catch (ClassNotFoundException e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error(CONFIG_CLASS_PARAM + " <" + configClass + "> not found.", e);
            }
            throw new InvalidParameterException("ClassNotFoundException: " + configClass);
        }
    } else {
        throw new InvalidParameterException("No value defined for the required: " + CONFIG_CLASS_PARAM);
    }

    webApplicationContext.refresh();
    return webApplicationContext;
}