Skip to content

Commit f208988

Browse files
committed
polishing
1 parent fd1bfee commit f208988

File tree

3 files changed

+66
-53
lines changed

3 files changed

+66
-53
lines changed

org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.w3c.dom.Element;
23+
2224
import org.springframework.beans.factory.config.BeanDefinition;
2325
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2426
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -28,14 +30,14 @@
2830
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2931
import org.springframework.util.StringUtils;
3032
import org.springframework.util.xml.DomUtils;
31-
import org.w3c.dom.Element;
3233

3334
/**
3435
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses {@code embedded-database} element and
3536
* creates a {@link BeanDefinition} for {@link EmbeddedDatabaseFactoryBean}. Picks up nested {@code script} elements and
3637
* configures a {@link ResourceDatabasePopulator} for them.
3738
*
3839
* @author Oliver Gierke
40+
* @since 3.0
3941
*/
4042
public class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser {
4143

@@ -62,15 +64,11 @@ private void setDatabasePopulator(Element element, ParserContext context, BeanDe
6264
}
6365

6466
private BeanDefinition createDatabasePopulator(List<Element> scripts, ParserContext context) {
65-
6667
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class);
67-
6868
List<String> locations = new ArrayList<String>();
6969
for (Element scriptElement : scripts) {
70-
String location = scriptElement.getAttribute("location");
71-
locations.add(location);
70+
locations.add(scriptElement.getAttribute("location"));
7271
}
73-
7472
// Use a factory bean for the resources so they can be given an order if a pattern is used
7573
BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder
7674
.genericBeanDefinition(SortedResourcesFactoryBean.class);
@@ -79,11 +77,11 @@ private BeanDefinition createDatabasePopulator(List<Element> scripts, ParserCont
7977
builder.addPropertyValue("scripts", resourcesFactory.getBeanDefinition());
8078

8179
return builder.getBeanDefinition();
82-
8380
}
8481

85-
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
86-
ParserContext context) {
82+
private AbstractBeanDefinition getSourcedBeanDefinition(
83+
BeanDefinitionBuilder builder, Element source, ParserContext context) {
84+
8785
AbstractBeanDefinition definition = builder.getBeanDefinition();
8886
definition.setSource(context.extractSource(source));
8987
return definition;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
/**
2-
*
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
315
*/
16+
417
package org.springframework.jdbc.config;
518

619
import java.io.IOException;
@@ -10,66 +23,57 @@
1023
import java.util.Comparator;
1124
import java.util.List;
1225

13-
import org.apache.commons.logging.Log;
14-
import org.apache.commons.logging.LogFactory;
1526
import org.springframework.beans.factory.FactoryBean;
1627
import org.springframework.core.io.Resource;
1728
import org.springframework.core.io.ResourceLoader;
1829
import org.springframework.core.io.support.ResourcePatternResolver;
1930

31+
/**
32+
* @author Dave Syer
33+
* @author Juergen Hoeller
34+
* @since 3.0
35+
*/
2036
public class SortedResourcesFactoryBean implements FactoryBean<Resource[]> {
2137

22-
private static final Log logger = LogFactory.getLog(SortedResourcesFactoryBean.class);
23-
24-
private ResourceLoader resourceLoader;
25-
private List<String> locations;
38+
private final Resource[] resources;
2639

27-
public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List<String> locations) {
28-
super();
29-
this.resourceLoader = resourceLoader;
30-
this.locations = locations;
31-
}
32-
33-
public Resource[] getObject() throws Exception {
40+
public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List<String> locations) throws IOException {
3441
List<Resource> scripts = new ArrayList<Resource>();
3542
for (String location : locations) {
36-
37-
if (logger.isDebugEnabled()) {
38-
logger.debug("Adding resources from pattern: "+location);
39-
}
40-
4143
if (resourceLoader instanceof ResourcePatternResolver) {
42-
List<Resource> resources = new ArrayList<Resource>(Arrays
43-
.asList(((ResourcePatternResolver) resourceLoader).getResources(location)));
44-
Collections.<Resource> sort(resources, new Comparator<Resource>() {
44+
List<Resource> resources = new ArrayList<Resource>(
45+
Arrays.asList(((ResourcePatternResolver) resourceLoader).getResources(location)));
46+
Collections.sort(resources, new Comparator<Resource>() {
4547
public int compare(Resource o1, Resource o2) {
4648
try {
4749
return o1.getURL().toString().compareTo(o2.getURL().toString());
48-
} catch (IOException e) {
50+
}
51+
catch (IOException ex) {
4952
return 0;
5053
}
5154
}
5255
});
5356
for (Resource resource : resources) {
5457
scripts.add(resource);
5558
}
56-
57-
} else {
59+
}
60+
else {
5861
scripts.add(resourceLoader.getResource(location));
5962
}
60-
6163
}
62-
return scripts.toArray(new Resource[scripts.size()]);
64+
this.resources = scripts.toArray(new Resource[scripts.size()]);
65+
}
66+
67+
public Resource[] getObject() {
68+
return this.resources;
6369
}
6470

6571
public Class<? extends Resource[]> getObjectType() {
66-
// TODO Auto-generated method stub
67-
return null;
72+
return Resource[].class;
6873
}
6974

7075
public boolean isSingleton() {
71-
// TODO Auto-generated method stub
72-
return false;
76+
return true;
7377
}
7478

75-
}
79+
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.apache.commons.logging.Log;
3030
import org.apache.commons.logging.LogFactory;
31+
3132
import org.springframework.core.io.Resource;
3233
import org.springframework.core.io.support.EncodedResource;
3334
import org.springframework.util.StringUtils;
@@ -44,8 +45,11 @@
4445
*/
4546
public class ResourceDatabasePopulator implements DatabasePopulator {
4647

48+
private static String COMMENT_PREFIX = "--";
49+
4750
private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class);
4851

52+
4953
private List<Resource> scripts = new ArrayList<Resource>();
5054

5155
private String sqlScriptEncoding;
@@ -54,14 +58,13 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
5458

5559
private boolean ignoreFailedDrops = false;
5660

57-
private static String COMMENT_PREFIX = "--";
5861

5962
/**
6063
* Add a script to execute to populate the database.
6164
* @param script the path to a SQL script
6265
*/
6366
public void addScript(Resource script) {
64-
scripts.add(script);
67+
this.scripts.add(script);
6568
}
6669

6770
/**
@@ -100,7 +103,8 @@ public void setContinueOnError(boolean continueOnError) {
100103
public void setIgnoreFailedDrops(boolean ignoreFailedDrops) {
101104
this.ignoreFailedDrops = ignoreFailedDrops;
102105
}
103-
106+
107+
104108
public void populate(Connection connection) throws SQLException {
105109
for (Resource script : this.scripts) {
106110
executeSqlScript(connection, applyEncodingIfNecessary(script), this.continueOnError, this.ignoreFailedDrops);
@@ -110,7 +114,8 @@ public void populate(Connection connection) throws SQLException {
110114
private EncodedResource applyEncodingIfNecessary(Resource script) {
111115
if (script instanceof EncodedResource) {
112116
return (EncodedResource) script;
113-
} else {
117+
}
118+
else {
114119
return new EncodedResource(script, this.sqlScriptEncoding);
115120
}
116121
}
@@ -134,8 +139,9 @@ private void executeSqlScript(Connection connection, EncodedResource resource, b
134139
String script;
135140
try {
136141
script = readScript(resource);
137-
} catch (IOException e) {
138-
throw new CannotReadScriptException(resource, e);
142+
}
143+
catch (IOException ex) {
144+
throw new CannotReadScriptException(resource, ex);
139145
}
140146
char delimiter = ';';
141147
if (!containsSqlScriptDelimiters(script, delimiter)) {
@@ -152,21 +158,25 @@ private void executeSqlScript(Connection connection, EncodedResource resource, b
152158
if (logger.isDebugEnabled()) {
153159
logger.debug(rowsAffected + " rows affected by SQL: " + statement);
154160
}
155-
} catch (SQLException ex) {
161+
}
162+
catch (SQLException ex) {
156163
boolean dropStatement = statement.trim().toLowerCase().startsWith("drop");
157164
if (continueOnError || (dropStatement && ignoreFailedDrops)) {
158165
if (logger.isDebugEnabled()) {
159166
logger.debug("Line " + lineNumber + " statement failed: " + statement, ex);
160167
}
161-
} else {
168+
}
169+
else {
162170
throw ex;
163171
}
164172
}
165173
}
166-
} finally {
174+
}
175+
finally {
167176
try {
168177
stmt.close();
169-
} catch (Throwable ex) {
178+
}
179+
catch (Throwable ex) {
170180
logger.debug("Could not close JDBC Statement", ex);
171181
}
172182
}
@@ -237,7 +247,8 @@ private static void splitSqlScript(String script, char delim, List<String> state
237247
statements.add(sb.toString());
238248
sb = new StringBuilder();
239249
}
240-
} else {
250+
}
251+
else {
241252
sb.append(content[i]);
242253
}
243254
}

0 commit comments

Comments
 (0)