diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java index e18b4adab2b..1cdbc9b266e 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java @@ -16,6 +16,7 @@ package org.springframework.jdbc.config; +import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.config.BeanDefinition; @@ -23,9 +24,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.core.io.Resource; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean; -import org.springframework.jdbc.datasource.init.DatabasePopulator; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; @@ -62,13 +61,25 @@ public class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinition } } - private DatabasePopulator createDatabasePopulator(List scripts, ParserContext context) { - ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); + private BeanDefinition createDatabasePopulator(List scripts, ParserContext context) { + + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class); + + List locations = new ArrayList(); for (Element scriptElement : scripts) { - Resource script = context.getReaderContext().getResourceLoader().getResource(scriptElement.getAttribute("location")); - populator.addScript(script); + String location = scriptElement.getAttribute("location"); + locations.add(location); } - return populator; + + // Use a factory bean for the resources so they can be given an order if a pattern is used + BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder + .genericBeanDefinition(SortedResourcesFactoryBean.class); + resourcesFactory.addConstructorArgValue(context.getReaderContext().getResourceLoader()); + resourcesFactory.addConstructorArgValue(locations); + builder.addPropertyValue("scripts", resourcesFactory.getBeanDefinition()); + + return builder.getBeanDefinition(); + } private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source, diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.java index f714084c1ca..3037fabc0f3 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.java @@ -16,24 +16,14 @@ package org.springframework.jdbc.config; -import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; import java.util.List; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.core.io.Resource; -import org.springframework.core.io.ResourceLoader; -import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.jdbc.datasource.init.DataSourceInitializer; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.util.xml.DomUtils; @@ -80,7 +70,7 @@ public class InitializeDatabaseBeanDefinitionParser extends AbstractBeanDefiniti locations.add(location); } - // Use a factor bean for the resources so they can be given an order if a pattern is used + // Use a factory bean for the resources so they can be given an order if a pattern is used BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder .genericBeanDefinition(SortedResourcesFactoryBean.class); resourcesFactory.addConstructorArgValue(context.getReaderContext().getResourceLoader()); @@ -97,62 +87,4 @@ public class InitializeDatabaseBeanDefinitionParser extends AbstractBeanDefiniti return definition; } - public static class SortedResourcesFactoryBean implements FactoryBean { - - private static final Log logger = LogFactory.getLog(SortedResourcesFactoryBean.class); - - private ResourceLoader resourceLoader; - - private List locations; - - public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List locations) { - super(); - this.resourceLoader = resourceLoader; - this.locations = locations; - } - - public Resource[] getObject() throws Exception { - List scripts = new ArrayList(); - for (String location : locations) { - - if (logger.isDebugEnabled()) { - logger.debug("Adding resources from pattern: " + location); - } - - if (resourceLoader instanceof ResourcePatternResolver) { - List resources = new ArrayList(Arrays - .asList(((ResourcePatternResolver) resourceLoader).getResources(location))); - Collections. sort(resources, new Comparator() { - public int compare(Resource o1, Resource o2) { - try { - return o1.getURL().toString().compareTo(o2.getURL().toString()); - } catch (IOException e) { - return 0; - } - } - }); - for (Resource resource : resources) { - scripts.add(resource); - } - - } else { - scripts.add(resourceLoader.getResource(location)); - } - - } - return scripts.toArray(new Resource[scripts.size()]); - } - - public Class getObjectType() { - // TODO Auto-generated method stub - return null; - } - - public boolean isSingleton() { - // TODO Auto-generated method stub - return false; - } - - } - } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java new file mode 100644 index 00000000000..46da80fe48e --- /dev/null +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java @@ -0,0 +1,75 @@ +/** + * + */ +package org.springframework.jdbc.config; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.core.io.support.ResourcePatternResolver; + +public class SortedResourcesFactoryBean implements FactoryBean { + + private static final Log logger = LogFactory.getLog(SortedResourcesFactoryBean.class); + + private ResourceLoader resourceLoader; + private List locations; + + public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List locations) { + super(); + this.resourceLoader = resourceLoader; + this.locations = locations; + } + + public Resource[] getObject() throws Exception { + List scripts = new ArrayList(); + for (String location : locations) { + + if (logger.isDebugEnabled()) { + logger.debug("Adding resources from pattern: "+location); + } + + if (resourceLoader instanceof ResourcePatternResolver) { + List resources = new ArrayList(Arrays + .asList(((ResourcePatternResolver) resourceLoader).getResources(location))); + Collections. sort(resources, new Comparator() { + public int compare(Resource o1, Resource o2) { + try { + return o1.getURL().toString().compareTo(o2.getURL().toString()); + } catch (IOException e) { + return 0; + } + } + }); + for (Resource resource : resources) { + scripts.add(resource); + } + + } else { + scripts.add(resourceLoader.getResource(location)); + } + + } + return scripts.toArray(new Resource[scripts.size()]); + } + + public Class getObjectType() { + // TODO Auto-generated method stub + return null; + } + + public boolean isSingleton() { + // TODO Auto-generated method stub + return false; + } + +} \ No newline at end of file diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java index 47493609f7e..f7fbffb32e4 100644 --- a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java @@ -20,6 +20,14 @@ public class JdbcNamespaceIntegrationTest { context.close(); } + @Test + public void testCreateWithResourcePattern() throws Exception { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "org/springframework/jdbc/config/jdbc-config-pattern.xml"); + assertCorrectSetup(context.getBean("dataSource", DataSource.class)); + context.close(); + } + private void assertCorrectSetup(DataSource dataSource) { JdbcTemplate t = new JdbcTemplate(dataSource); assertEquals(1, t.queryForInt("select count(*) from T_TEST")); diff --git a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-pattern.xml b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-pattern.xml new file mode 100644 index 00000000000..876334b1e90 --- /dev/null +++ b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-pattern.xml @@ -0,0 +1,13 @@ + + + + + + + + +