RESOLVED - issue SPR-5917: DataSourceInitializer and namespace support for creating and populating databases
Moved populator to init package and added namespace features
This commit is contained in:
@@ -24,9 +24,9 @@ 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.DatabasePopulator;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean;
|
||||
import org.springframework.jdbc.datasource.embedded.ResourceDatabasePopulator;
|
||||
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;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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.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.DatabasePopulator;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses {@code initialize-database} element and
|
||||
* creates a {@link BeanDefinition} for {@link DataSourceInitializer}. Picks up nested {@code script} elements and
|
||||
* configures a {@link ResourceDatabasePopulator} for them.
|
||||
@author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class InitializeDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser {
|
||||
|
||||
@Override
|
||||
protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DataSourceInitializer.class);
|
||||
builder.addPropertyReference("dataSource", element.getAttribute("data-source"));
|
||||
builder.addPropertyValue("enabled", element.getAttribute("enabled"));
|
||||
setDatabasePopulator(element, context, builder);
|
||||
return getSourcedBeanDefinition(builder, element, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setDatabasePopulator(Element element, ParserContext context, BeanDefinitionBuilder builder) {
|
||||
List<Element> scripts = DomUtils.getChildElementsByTagName(element, "script");
|
||||
if (scripts.size() > 0) {
|
||||
builder.addPropertyValue("databasePopulator", createDatabasePopulator(element, scripts, context));
|
||||
}
|
||||
}
|
||||
|
||||
private DatabasePopulator createDatabasePopulator(Element element, List<Element> scripts, ParserContext context) {
|
||||
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||
populator.setIgnoreFailedDrops(element.getAttribute("ignore-failures").equals("DROPS"));
|
||||
populator.setContinueOnError(element.getAttribute("ignore-failures").equals("ALL"));
|
||||
for (Element scriptElement : scripts) {
|
||||
String location = scriptElement.getAttribute("location");
|
||||
ResourceLoader resourceLoader = context.getReaderContext().getResourceLoader();
|
||||
if (resourceLoader instanceof ResourcePatternResolver) {
|
||||
try {
|
||||
List<Resource> resources = new ArrayList<Resource>(Arrays.asList(((ResourcePatternResolver)resourceLoader).getResources(location)));
|
||||
Collections.<Resource>sort(resources, new Comparator<Resource>() {
|
||||
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) {
|
||||
populator.addScript(resource);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
context.getReaderContext().error("Cannot locate resources for script from location="+location, scriptElement);
|
||||
}
|
||||
} else {
|
||||
populator.addScript(resourceLoader.getResource(location));
|
||||
}
|
||||
}
|
||||
return populator;
|
||||
}
|
||||
|
||||
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
|
||||
ParserContext context) {
|
||||
AbstractBeanDefinition definition = builder.getBeanDefinition();
|
||||
definition.setSource(context.extractSource(source));
|
||||
return definition;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,10 +22,12 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||
/**
|
||||
* {@link NamespaceHandler} for JDBC configuration namespace.
|
||||
* @author Oliver Gierke
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class JdbcNamespaceHandler extends NamespaceHandlerSupport {
|
||||
|
||||
public void init() {
|
||||
registerBeanDefinitionParser("embedded-database", new EmbeddedDatabaseBeanDefinitionParser());
|
||||
registerBeanDefinitionParser("initialize-database", new InitializeDatabaseBeanDefinitionParser());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.jdbc.datasource.embedded;
|
||||
import org.springframework.core.io.ClassRelativeResourceLoader;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
|
||||
/**
|
||||
* A builder that provides a fluent API for constructing an embedded database.
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.jdbc.datasource.init.DatabasePopulator;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jdbc.datasource.embedded;
|
||||
package org.springframework.jdbc.datasource.init;
|
||||
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.jdbc.datasource.init;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Used to populate a database during initialization.
|
||||
* @author Dave Syer
|
||||
* @since 3.0
|
||||
* @see DatabasePopulator
|
||||
*/
|
||||
public class DataSourceInitializer implements InitializingBean {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private DatabasePopulator databasePopulator;
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* Flag to explicitly enable or disable the database populator.
|
||||
*
|
||||
* @param enabled true if the database populator will be called on startup
|
||||
*/
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link DatabasePopulator} to use to populate the data source. Mandatory with no default.
|
||||
*
|
||||
* @param databasePopulator the database populator to use.
|
||||
*/
|
||||
public void setDatabasePopulator(DatabasePopulator databasePopulator) {
|
||||
this.databasePopulator = databasePopulator;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link DataSource} to populate when this component is initialized. Mandatory with no default.
|
||||
*
|
||||
* @param dataSource the DataSource
|
||||
*/
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the populator to set up data in the data source. Both properties are mandatory with no defaults.
|
||||
*
|
||||
* @see InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if (enabled) {
|
||||
Assert.state(dataSource != null, "DataSource must be provided");
|
||||
Assert.state(databasePopulator != null, "DatabasePopulator must be provided");
|
||||
databasePopulator.populate(dataSource.getConnection());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jdbc.datasource.embedded;
|
||||
package org.springframework.jdbc.datasource.init;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jdbc.datasource.embedded;
|
||||
package org.springframework.jdbc.datasource.init;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
@@ -28,7 +28,6 @@ import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -40,6 +39,7 @@ import org.springframework.util.StringUtils;
|
||||
* Call {@link #setSqlScriptEncoding(String)} to set the encoding for all added scripts.<br>
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Dave Syer
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ResourceDatabasePopulator implements DatabasePopulator {
|
||||
@@ -50,6 +50,9 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
|
||||
|
||||
private String sqlScriptEncoding;
|
||||
|
||||
private boolean ignoreFailedDrops = false;
|
||||
|
||||
private boolean continueOnError = false;
|
||||
|
||||
/**
|
||||
* Add a script to execute to populate the database.
|
||||
@@ -66,6 +69,29 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
|
||||
public void setScripts(Resource[] scripts) {
|
||||
this.scripts = Arrays.asList(scripts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that all failures in SQL should be logged but not cause a
|
||||
* failure. Defaults to false.
|
||||
*
|
||||
* @param continueOnError the flag value to set
|
||||
*/
|
||||
public void setContinueOnError(boolean continueOnError) {
|
||||
this.continueOnError = continueOnError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that a failed SQL <code>DROP</code> statement can be ignored.
|
||||
* This is useful for non-embedded databases whose SQL dialect does not support
|
||||
* an <code>IF EXISTS</code> clause in a <code>DROP</code>. The default is false
|
||||
* so that if it the populator runs accidentally against an existing database it
|
||||
* will fail fast when the script starts with a <code>DROP</code>.
|
||||
*
|
||||
* @param ignoreFailedDrops the flag value to set
|
||||
*/
|
||||
public void setIgnoreFailedDrops(boolean ignoreFailedDrops) {
|
||||
this.ignoreFailedDrops = ignoreFailedDrops;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the encoding for SQL scripts, if different from the platform encoding.
|
||||
@@ -77,30 +103,29 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
|
||||
this.sqlScriptEncoding = sqlScriptEncoding;
|
||||
}
|
||||
|
||||
|
||||
public void populate(Connection connection) throws SQLException {
|
||||
for (Resource script : this.scripts) {
|
||||
executeSqlScript(connection, applyEncodingIfNecessary(script), false);
|
||||
executeSqlScript(connection, applyEncodingIfNecessary(script), continueOnError, ignoreFailedDrops);
|
||||
}
|
||||
}
|
||||
|
||||
private EncodedResource applyEncodingIfNecessary(Resource script) {
|
||||
if (script instanceof EncodedResource) {
|
||||
return (EncodedResource) script;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return new EncodedResource(script, this.sqlScriptEncoding);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Execute the given SQL script. <p>The script will normally be loaded by classpath. There should be one statement
|
||||
* per line. Any semicolons will be removed. <b>Do not use this method to execute DDL if you expect rollback.</b>
|
||||
* @param template the SimpleJdbcTemplate with which to perform JDBC operations
|
||||
* @param resource the resource (potentially associated with a specific encoding) to load the SQL script from.
|
||||
* @param continueOnError whether or not to continue without throwing an exception in the event of an error.
|
||||
* @param ignoreFailedDrops whether of not to continue in thw event of specifically an error on a <code>DROP</code>.
|
||||
*/
|
||||
private void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError)
|
||||
private void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops)
|
||||
throws SQLException {
|
||||
|
||||
if (logger.isInfoEnabled()) {
|
||||
@@ -129,24 +154,21 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(rowsAffected + " rows affected by SQL: " + statement);
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
if (continueOnError) {
|
||||
} catch (SQLException ex) {
|
||||
boolean dropStatement = statement.trim().toLowerCase().startsWith("drop");
|
||||
if (continueOnError || (dropStatement && ignoreFailedDrops)) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Line " + lineNumber + " statement failed: " + statement, ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
try {
|
||||
stmt.close();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
} catch (Throwable ex) {
|
||||
logger.debug("Could not close JDBC Statement", ex);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user