Search in sources :

Example 16 with BeanDefinitionStoreException

use of org.springframework.beans.factory.BeanDefinitionStoreException in project spring-security by spring-projects.

the class UserServiceBeanDefinitionParser method doParse.

@SuppressWarnings("unchecked")
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    String userProperties = element.getAttribute(ATT_PROPERTIES);
    List<Element> userElts = DomUtils.getChildElementsByTagName(element, ELT_USER);
    if (StringUtils.hasText(userProperties)) {
        if (!CollectionUtils.isEmpty(userElts)) {
            throw new BeanDefinitionStoreException("Use of a properties file and user elements are mutually exclusive");
        }
        BeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
        bd.getPropertyValues().addPropertyValue("location", userProperties);
        builder.addConstructorArgValue(bd);
        return;
    }
    if (CollectionUtils.isEmpty(userElts)) {
        throw new BeanDefinitionStoreException("You must supply user definitions, either with <" + ELT_USER + "> child elements or a " + "properties file (using the '" + ATT_PROPERTIES + "' attribute)");
    }
    ManagedList<BeanDefinition> users = new ManagedList<BeanDefinition>();
    for (Object elt : userElts) {
        Element userElt = (Element) elt;
        String userName = userElt.getAttribute(ATT_NAME);
        String password = userElt.getAttribute(ATT_PASSWORD);
        if (!StringUtils.hasLength(password)) {
            password = generateRandomPassword();
        }
        boolean locked = "true".equals(userElt.getAttribute(ATT_LOCKED));
        boolean disabled = "true".equals(userElt.getAttribute(ATT_DISABLED));
        BeanDefinitionBuilder authorities = BeanDefinitionBuilder.rootBeanDefinition(AuthorityUtils.class);
        authorities.addConstructorArgValue(userElt.getAttribute(ATT_AUTHORITIES));
        authorities.setFactoryMethod("commaSeparatedStringToAuthorityList");
        BeanDefinitionBuilder user = BeanDefinitionBuilder.rootBeanDefinition(User.class);
        user.addConstructorArgValue(userName);
        user.addConstructorArgValue(password);
        user.addConstructorArgValue(!disabled);
        user.addConstructorArgValue(true);
        user.addConstructorArgValue(true);
        user.addConstructorArgValue(!locked);
        user.addConstructorArgValue(authorities.getBeanDefinition());
        users.add(user.getBeanDefinition());
    }
    builder.addConstructorArgValue(users);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 17 with BeanDefinitionStoreException

use of org.springframework.beans.factory.BeanDefinitionStoreException in project grails-core by grails.

the class GrailsPlaceholderConfigurer method doProcessProperties.

@Override
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver) {
    BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver) {

        @Override
        protected void visitMap(Map<?, ?> mapVal) {
            if (mapVal instanceof Config)
                return;
            super.visitMap(mapVal);
        }
    };
    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (String curName : beanNames) {
        // to avoid failing on unresolvable placeholders in properties file locations.
        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (Exception ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex);
            }
        }
    }
    // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
    beanFactoryToProcess.resolveAliases(valueResolver);
    // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
    beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
}
Also used : BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) Config(grails.config.Config) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Map(java.util.Map) IOException(java.io.IOException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Example 18 with BeanDefinitionStoreException

use of org.springframework.beans.factory.BeanDefinitionStoreException in project spring-framework by spring-projects.

the class ContextLoaderTests method testFrameworkServletWithDefaultLocation.

@Test
public void testFrameworkServletWithDefaultLocation() throws Exception {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setContextClass(XmlWebApplicationContext.class);
    try {
        servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
        assertTrue(ex.getCause().getMessage().contains("/WEB-INF/test-servlet.xml"));
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) IOException(java.io.IOException) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 19 with BeanDefinitionStoreException

use of org.springframework.beans.factory.BeanDefinitionStoreException 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 20 with BeanDefinitionStoreException

use of org.springframework.beans.factory.BeanDefinitionStoreException in project spring-framework by spring-projects.

the class ViewResolverTests method testXmlViewResolverDefaultLocation.

@Test
public void testXmlViewResolverDefaultLocation() {
    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();
    try {
        vr.setApplicationContext(wac);
        vr.afterPropertiesSet();
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
    // expected
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)39 Test (org.junit.Test)14 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)11 IOException (java.io.IOException)8 LinkedHashSet (java.util.LinkedHashSet)6 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)5 MockServletContext (org.springframework.mock.web.test.MockServletContext)5 ArrayList (java.util.ArrayList)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)4 Map (java.util.Map)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)3 BeanCreationException (org.springframework.beans.factory.BeanCreationException)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)3 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)3 Resource (org.springframework.core.io.Resource)3 Method (java.lang.reflect.Method)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2