Skip to content

Commit e9823b5

Browse files
committed
revised embedded database support
1 parent 3ac3a72 commit e9823b5

20 files changed

+365
-308
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.
15+
*/
16+
17+
package org.springframework.core.io;
18+
19+
import org.springframework.util.Assert;
20+
import org.springframework.util.StringUtils;
21+
22+
/**
23+
* {@link ResourceLoader} implementation that interprets plain resource paths
24+
* as relative to a given <code>java.lang.Class</code>.
25+
*
26+
* @author Juergen Hoeller
27+
* @since 3.0
28+
* @see java.lang.Class#getResource(String)
29+
* @see ClassPathResource#ClassPathResource(String, Class)
30+
*/
31+
public class ClassRelativeResourceLoader extends DefaultResourceLoader {
32+
33+
private final Class clazz;
34+
35+
36+
/**
37+
* Create a new ClassRelativeResourceLoader for the given class.
38+
* @param clazz the class to load resources through
39+
*/
40+
public ClassRelativeResourceLoader(Class clazz) {
41+
Assert.notNull(clazz, "Class must not be null");
42+
this.clazz = clazz;
43+
setClassLoader(clazz.getClassLoader());
44+
}
45+
46+
protected Resource getResourceByPath(String path) {
47+
return new ClassRelativeContextResource(path, this.clazz);
48+
}
49+
50+
51+
/**
52+
* ClassPathResource that explicitly expresses a context-relative path
53+
* through implementing the ContextResource interface.
54+
*/
55+
private static class ClassRelativeContextResource extends ClassPathResource implements ContextResource {
56+
57+
private final Class clazz;
58+
59+
public ClassRelativeContextResource(String path, Class clazz) {
60+
super(path, clazz);
61+
this.clazz = clazz;
62+
}
63+
64+
public String getPathWithinContext() {
65+
return getPath();
66+
}
67+
68+
@Override
69+
public Resource createRelative(String relativePath) {
70+
String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);
71+
return new ClassRelativeContextResource(pathToUse, this.clazz);
72+
}
73+
}
74+
75+
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/SimpleDriverDataSource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -107,8 +107,8 @@ public SimpleDriverDataSource(Driver driver, String url, Properties conProps) {
107107
* within the SimpleDriverDataSource.
108108
* @see #setDriver
109109
*/
110-
public void setDriverClass(Class driverClass) {
111-
this.driver = (Driver) BeanUtils.instantiateClass(driverClass);
110+
public void setDriverClass(Class<? extends Driver> driverClass) {
111+
this.driver = BeanUtils.instantiateClass(driverClass);
112112
}
113113

114114
/**

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/AbstractEmbeddedDatabaseConfigurer.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,39 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.jdbc.datasource.embedded;
1718

1819
import java.sql.Connection;
1920
import java.sql.SQLException;
2021
import java.sql.Statement;
21-
2222
import javax.sql.DataSource;
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626

2727
/**
28-
* Base class for {@link EmbeddedDatabaseConfigurer} implementations providing common shutdown behaviour.
28+
* Base class for {@link EmbeddedDatabaseConfigurer} implementations providing common shutdown behavior.
29+
*
2930
* @author Oliver Gierke
31+
* @author Juergen Hoeller
3032
* @since 3.0
3133
*/
3234
abstract class AbstractEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigurer {
3335

34-
private static final Log logger = LogFactory.getLog(AbstractEmbeddedDatabaseConfigurer.class);
36+
protected final Log logger = LogFactory.getLog(getClass());
3537

3638
public void shutdown(DataSource dataSource, String databaseName) {
37-
Connection connection = JdbcUtils.getConnection(dataSource);
38-
Statement stmt = null;
3939
try {
40-
stmt = connection.createStatement();
40+
Connection connection = dataSource.getConnection();
41+
Statement stmt = connection.createStatement();
4142
stmt.execute("SHUTDOWN");
42-
} catch (SQLException e) {
43+
}
44+
catch (SQLException ex) {
4345
if (logger.isWarnEnabled()) {
44-
logger.warn("Could not shutdown embedded database", e);
46+
logger.warn("Could not shutdown embedded database", ex);
4547
}
46-
} finally {
47-
JdbcUtils.closeStatement(stmt);
4848
}
4949
}
50+
5051
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/CannotReadScriptException.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.jdbc.datasource.embedded;
1718

1819
import org.springframework.core.io.support.EncodedResource;
1920

2021
/**
2122
* Thrown by {@link ResourceDatabasePopulator} if one of its SQL scripts could
2223
* not be read during population.
24+
*
2325
* @author Keith Donald
2426
* @since 3.0
2527
*/
26-
@SuppressWarnings("serial")
2728
public class CannotReadScriptException extends RuntimeException {
2829

2930
/**
30-
* Creates a new cannot read script exception.
31-
*
31+
* Constructor a new CannotReadScriptException.
3232
* @param resource the resource that could not be read from
3333
* @param cause the underlying cause of the resource access failure
3434
*/
3535
public CannotReadScriptException(EncodedResource resource, Throwable cause) {
3636
super("Cannot read SQL script from " + resource, cause);
3737
}
38+
3839
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ConnectionProperties.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,43 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.jdbc.datasource.embedded;
1718

19+
import java.sql.Driver;
20+
1821
/**
1922
* DataSourceFactory helper that allows essential JDBC connection properties to be configured consistently,
2023
* independent of the actual DataSource implementation.
24+
*
2125
* @author Keith Donald
2226
* @since 3.0
2327
* @see DataSourceFactory
2428
*/
2529
public interface ConnectionProperties {
26-
30+
2731
/**
2832
* Set the JDBC driver to use to connect to the database.
2933
* @param driverClass the jdbc driver class
3034
*/
31-
void setDriverClass(Class<?> driverClass);
32-
35+
void setDriverClass(Class driverClass);
36+
3337
/**
3438
* Sets the JDBC connection URL of the database.
3539
* @param url the connection url
3640
*/
3741
void setUrl(String url);
38-
42+
3943
/**
4044
* Sets the username to use to connect to the database.
4145
* @param username the username
4246
*/
4347
void setUsername(String username);
44-
48+
4549
/**
4650
* Sets the password to use to connect to the database.
4751
* @param password the password
4852
*/
4953
void setPassword(String password);
54+
5055
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/DataSourceFactory.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,33 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.jdbc.datasource.embedded;
1718

1819
import javax.sql.DataSource;
1920

2021
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
2122

2223
/**
23-
* Encapsulates the creation of a particular DataSource implementation, such as a {@link SimpleDriverDataSource} or connection pool such as Apache DBCP or c3p0.
24-
* Call {@link #getConnectionProperties()} to configure normalized DataSource properties before calling {@link #getDataSource()} to actually get the configured DataSource instance.
24+
* Encapsulates the creation of a particular DataSource implementation, such as a
25+
* {@link SimpleDriverDataSource} or connection pool such as Apache DBCP or C3P0.
26+
*
27+
* <p>Call {@link #getConnectionProperties()} to configure normalized DataSource properties
28+
* before calling {@link #getDataSource()} to actually get the configured DataSource instance.
29+
*
2530
* @author Keith Donald
2631
* @since 3.0
2732
*/
2833
public interface DataSourceFactory {
29-
34+
3035
/**
3136
* Allows properties of the DataSource to be configured.
3237
*/
3338
ConnectionProperties getConnectionProperties();
34-
39+
3540
/**
3641
* Returns the DataSource with the connection properties applied.
3742
*/
3843
DataSource getDataSource();
39-
44+
4045
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/DatabasePopulator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.jdbc.datasource.embedded;
1718

1819
import java.sql.Connection;
1920
import java.sql.SQLException;
2021

2122
/**
2223
* Strategy used to populate an embedded database during initialization.
24+
*
2325
* @author Keith Donald
2426
* @since 3.0
2527
* @see ResourceDatabasePopulator
@@ -32,4 +34,5 @@ public interface DatabasePopulator {
3234
* @throws SQLException if an unrecoverable data access exception occurs during database population
3335
*/
3436
void populate(Connection connection) throws SQLException;
35-
}
37+
38+
}

0 commit comments

Comments
 (0)