From f208988563b2f5592f16fc8b5285fe12c935e9e2 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 15 Dec 2009 21:37:59 +0000 Subject: [PATCH] polishing --- .../EmbeddedDatabaseBeanDefinitionParser.java | 16 ++--- .../config/SortedResourcesFactoryBean.java | 70 ++++++++++--------- .../init/ResourceDatabasePopulator.java | 33 ++++++--- 3 files changed, 66 insertions(+), 53 deletions(-) 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 1cdbc9b266e..e366ff87579 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 @@ -19,6 +19,8 @@ package org.springframework.jdbc.config; import java.util.ArrayList; import java.util.List; +import org.w3c.dom.Element; + import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -28,7 +30,6 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; -import org.w3c.dom.Element; /** * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses {@code embedded-database} element and @@ -36,6 +37,7 @@ import org.w3c.dom.Element; * configures a {@link ResourceDatabasePopulator} for them. * * @author Oliver Gierke + * @since 3.0 */ public class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser { @@ -62,15 +64,11 @@ public class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinition } private BeanDefinition createDatabasePopulator(List scripts, ParserContext context) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class); - List locations = new ArrayList(); for (Element scriptElement : scripts) { - String location = scriptElement.getAttribute("location"); - locations.add(location); + locations.add(scriptElement.getAttribute("location")); } - // 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); @@ -79,11 +77,11 @@ public class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinition builder.addPropertyValue("scripts", resourcesFactory.getBeanDefinition()); return builder.getBeanDefinition(); - } - private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source, - ParserContext context) { + private AbstractBeanDefinition getSourcedBeanDefinition( + BeanDefinitionBuilder builder, Element source, ParserContext context) { + AbstractBeanDefinition definition = builder.getBeanDefinition(); definition.setSource(context.extractSource(source)); return definition; 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 index 46da80fe48e..1e0802dcdb3 100644 --- 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 @@ -1,6 +1,19 @@ -/** - * +/* + * 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; @@ -10,42 +23,32 @@ 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; +/** + * @author Dave Syer + * @author Juergen Hoeller + * @since 3.0 + */ public class SortedResourcesFactoryBean implements FactoryBean { - private static final Log logger = LogFactory.getLog(SortedResourcesFactoryBean.class); - - private ResourceLoader resourceLoader; - private List locations; + private final Resource[] resources; - public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List locations) { - super(); - this.resourceLoader = resourceLoader; - this.locations = locations; - } - - public Resource[] getObject() throws Exception { + public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List locations) throws IOException { 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() { + 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) { + } + catch (IOException ex) { return 0; } } @@ -53,23 +56,24 @@ public class SortedResourcesFactoryBean implements FactoryBean { for (Resource resource : resources) { scripts.add(resource); } - - } else { + } + else { scripts.add(resourceLoader.getResource(location)); } - } - return scripts.toArray(new Resource[scripts.size()]); + this.resources = scripts.toArray(new Resource[scripts.size()]); + } + + public Resource[] getObject() { + return this.resources; } public Class getObjectType() { - // TODO Auto-generated method stub - return null; + return Resource[].class; } public boolean isSingleton() { - // TODO Auto-generated method stub - return false; + return true; } -} \ No newline at end of file +} diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java index 2e8e9070288..d5f716700be 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java @@ -28,6 +28,7 @@ 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; @@ -44,8 +45,11 @@ import org.springframework.util.StringUtils; */ public class ResourceDatabasePopulator implements DatabasePopulator { + private static String COMMENT_PREFIX = "--"; + private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class); + private List scripts = new ArrayList(); private String sqlScriptEncoding; @@ -54,14 +58,13 @@ public class ResourceDatabasePopulator implements DatabasePopulator { private boolean ignoreFailedDrops = false; - private static String COMMENT_PREFIX = "--"; /** * Add a script to execute to populate the database. * @param script the path to a SQL script */ public void addScript(Resource script) { - scripts.add(script); + this.scripts.add(script); } /** @@ -100,7 +103,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator { public void setIgnoreFailedDrops(boolean ignoreFailedDrops) { this.ignoreFailedDrops = ignoreFailedDrops; } - + + public void populate(Connection connection) throws SQLException { for (Resource script : this.scripts) { executeSqlScript(connection, applyEncodingIfNecessary(script), this.continueOnError, this.ignoreFailedDrops); @@ -110,7 +114,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator { private EncodedResource applyEncodingIfNecessary(Resource script) { if (script instanceof EncodedResource) { return (EncodedResource) script; - } else { + } + else { return new EncodedResource(script, this.sqlScriptEncoding); } } @@ -134,8 +139,9 @@ public class ResourceDatabasePopulator implements DatabasePopulator { String script; try { script = readScript(resource); - } catch (IOException e) { - throw new CannotReadScriptException(resource, e); + } + catch (IOException ex) { + throw new CannotReadScriptException(resource, ex); } char delimiter = ';'; if (!containsSqlScriptDelimiters(script, delimiter)) { @@ -152,21 +158,25 @@ public class ResourceDatabasePopulator implements DatabasePopulator { if (logger.isDebugEnabled()) { logger.debug(rowsAffected + " rows affected by SQL: " + statement); } - } catch (SQLException ex) { + } + catch (SQLException ex) { boolean dropStatement = statement.trim().toLowerCase().startsWith("drop"); if (continueOnError || (dropStatement && ignoreFailedDrops)) { if (logger.isDebugEnabled()) { logger.debug("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); } } @@ -237,7 +247,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator { statements.add(sb.toString()); sb = new StringBuilder(); } - } else { + } + else { sb.append(content[i]); } }