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);
}
}
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"));
}
}
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
}
}
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);
}
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);
}
}
Aggregations