Search in sources :

Example 11 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project spring-framework by spring-projects.

the class FrameworkServlet method loadInitializer.

@SuppressWarnings("unchecked")
private ApplicationContextInitializer<ConfigurableApplicationContext> loadInitializer(String className, ConfigurableApplicationContext wac) {
    try {
        Class<?> initializerClass = ClassUtils.forName(className, wac.getClassLoader());
        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 " + "framework servlet: [%s]", initializerClass.getName(), initializerContextClass.getName(), wac.getClass().getName()));
        }
        return BeanUtils.instantiateClass(initializerClass, ApplicationContextInitializer.class);
    } catch (ClassNotFoundException ex) {
        throw new ApplicationContextException(String.format("Could not load class [%s] specified " + "via 'contextInitializerClasses' init-param", className), ex);
    }
}
Also used : ApplicationContextException(org.springframework.context.ApplicationContextException)

Example 12 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderListenerWithUnknownContextInitializer.

@Test
public void testContextLoaderListenerWithUnknownContextInitializer() {
    MockServletContext sc = new MockServletContext("");
    // config file doesn't matter.  just a placeholder
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/empty-context.xml");
    sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(new Object[] { UnknownContextInitializer.class.getName() }));
    ContextLoaderListener listener = new ContextLoaderListener();
    try {
        listener.contextInitialized(new ServletContextEvent(sc));
        fail("expected exception");
    } catch (ApplicationContextException ex) {
        assertTrue(ex.getMessage().contains("not assignable"));
    }
}
Also used : ApplicationContextException(org.springframework.context.ApplicationContextException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 13 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project spring-framework by spring-projects.

the class ViewResolverTests method testXmlViewResolverWithoutCache.

@Test
public void testXmlViewResolverWithoutCache() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext() {

        @Override
        protected Resource getResourceByPath(String path) {
            assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
            return super.getResourceByPath(path);
        }
    };
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    XmlViewResolver vr = new XmlViewResolver();
    vr.setCache(false);
    try {
        vr.setApplicationContext(wac);
    } catch (ApplicationContextException ex) {
        fail("Should not have thrown ApplicationContextException: " + ex.getMessage());
    }
    try {
        vr.resolveViewName("example1", Locale.getDefault());
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
    // expected
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) ApplicationContextException(org.springframework.context.ApplicationContextException) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 14 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project spring-security by spring-projects.

the class UserDetailsServiceFactoryBean method authenticationUserDetailsService.

@SuppressWarnings("unchecked")
AuthenticationUserDetailsService authenticationUserDetailsService(String name) {
    UserDetailsService uds;
    if (!StringUtils.hasText(name)) {
        Map<String, ?> beans = getBeansOfType(AuthenticationUserDetailsService.class);
        if (!beans.isEmpty()) {
            if (beans.size() > 1) {
                throw new ApplicationContextException("More than one AuthenticationUserDetailsService registered." + " Please use a specific Id reference.");
            }
            return (AuthenticationUserDetailsService) beans.values().toArray()[0];
        }
        uds = getUserDetailsService();
    } else {
        Object bean = beanFactory.getBean(name);
        if (bean instanceof AuthenticationUserDetailsService) {
            return (AuthenticationUserDetailsService) bean;
        } else if (bean instanceof UserDetailsService) {
            uds = cachingUserDetailsService(name);
            if (uds == null) {
                uds = (UserDetailsService) bean;
            }
        } else {
            throw new ApplicationContextException("Bean '" + name + "' must be a UserDetailsService or an" + " AuthenticationUserDetailsService");
        }
    }
    return new UserDetailsByNameServiceWrapper(uds);
}
Also used : CachingUserDetailsService(org.springframework.security.config.authentication.CachingUserDetailsService) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) AuthenticationUserDetailsService(org.springframework.security.core.userdetails.AuthenticationUserDetailsService) AuthenticationUserDetailsService(org.springframework.security.core.userdetails.AuthenticationUserDetailsService) UserDetailsByNameServiceWrapper(org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper) ApplicationContextException(org.springframework.context.ApplicationContextException)

Example 15 with ApplicationContextException

use of org.springframework.context.ApplicationContextException in project spring-security by spring-projects.

the class ContextSourceSettingPostProcessor method postProcessBeanFactory.

public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {
    Class<?> contextSourceClass;
    try {
        contextSourceClass = ClassUtils.forName(REQUIRED_CONTEXT_SOURCE_CLASS_NAME, ClassUtils.getDefaultClassLoader());
    } catch (ClassNotFoundException e) {
        throw new ApplicationContextException("Couldn't locate: " + REQUIRED_CONTEXT_SOURCE_CLASS_NAME + ". " + " If you are using LDAP with Spring Security, please ensure that you include the spring-ldap " + "jar file in your application", e);
    }
    String[] sources = bf.getBeanNamesForType(contextSourceClass, false, false);
    if (sources.length == 0) {
        throw new ApplicationContextException("No BaseLdapPathContextSource instances found. Have you " + "added an <" + Elements.LDAP_SERVER + " /> element to your application context? If you have " + "declared an explicit bean, do not use lazy-init");
    }
    if (!bf.containsBean(BeanIds.CONTEXT_SOURCE) && defaultNameRequired) {
        if (sources.length > 1) {
            throw new ApplicationContextException("More than one BaseLdapPathContextSource instance found. " + "Please specify a specific server id using the 'server-ref' attribute when configuring your <" + Elements.LDAP_PROVIDER + "> " + "or <" + Elements.LDAP_USER_SERVICE + ">.");
        }
        bf.registerAlias(sources[0], BeanIds.CONTEXT_SOURCE);
    }
}
Also used : ApplicationContextException(org.springframework.context.ApplicationContextException)

Aggregations

ApplicationContextException (org.springframework.context.ApplicationContextException)17 Test (org.junit.Test)4 MockServletContext (org.springframework.mock.web.test.MockServletContext)3 IOException (java.io.IOException)2 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 ServletContextEvent (javax.servlet.ServletContextEvent)2 DirectoryContent (com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent)1 File (com.github.hakko.musiccabinet.domain.model.library.File)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DirectoryIteratorException (java.nio.file.DirectoryIteratorException)1 Path (java.nio.file.Path)1 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1 ServletContextListener (javax.servlet.ServletContextListener)1 ServletException (javax.servlet.ServletException)1