Skip to content

Commit 442e734

Browse files
author
Keith Donald
committed
polish
1 parent d159195 commit 442e734

File tree

8 files changed

+75
-18
lines changed

8 files changed

+75
-18
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
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+
*/
116
package org.springframework.jdbc.datasource.embedded;
217

318
import org.springframework.core.io.support.EncodedResource;
419

20+
/**
21+
* Thrown by {@link ResourceDatabasePopulator} if one of its SQL scripts could not be read during population.
22+
* @author Keith Donald
23+
*/
524
public class CannotReadScriptException extends RuntimeException {
625

26+
/**
27+
* Creates a new cannot read script exception.
28+
* @param resource the resource that could not be read from
29+
* @param cause the underlying cause of the resource access failure
30+
*/
731
public CannotReadScriptException(EncodedResource resource, Throwable cause) {
832
super("Cannot read SQL script from " + resource, cause);
933
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
2121

2222
/**
23-
* A factory for a kind of DataSource, such as a {@link SimpleDriverDataSource} or connection pool such as Apache DBCP or c3p0.
23+
* A factory for DataSource implementation, such as a {@link SimpleDriverDataSource} or connection pool such as Apache DBCP or c3p0.
2424
* Call {@link #getConnectionProperties()} to configure normalized DataSource properties before calling {@link #getDataSource()} to actually get the configured DataSource instance.
2525
* @author Keith Donald
2626
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/**
2121
* Encapsulates the configuration required to create, connect to, and shutdown a specific type of embedded database such as HSQLdb or H2.
22-
* Create a implementation for each database type we wish to support.
22+
* Create a implementation for each database type you wish to support; for example HSQL, H2, or some other type.
2323
* @author Keith Donald
2424
*/
2525
public interface EmbeddedDatabaseConfigurer {

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,27 @@
1818
import org.springframework.util.Assert;
1919

2020
/**
21-
* Package private factory for mapping well-known embedded database types to database configurer strategies.
21+
* Package private factory for mapping well-known {@link EmbeddedDatabaseType embedded database types} to
22+
* {@link EmbeddedDatabaseConfigurer} strategies.
2223
* @author Keith Donald
2324
*/
24-
class EmbeddedDatabaseConfigurerFactory {
25+
final class EmbeddedDatabaseConfigurerFactory {
2526

27+
private EmbeddedDatabaseConfigurerFactory() {
28+
}
29+
2630
public static EmbeddedDatabaseConfigurer getConfigurer(EmbeddedDatabaseType type) throws IllegalStateException {
2731
Assert.notNull(type, "The EmbeddedDatabaseType is required");
2832
try {
2933
if (type == EmbeddedDatabaseType.HSQL) {
3034
return HsqlEmbeddedDatabaseConfigurer.getInstance();
3135
} else {
32-
throw new UnsupportedOperationException("Other types not yet supported");
36+
throw new UnsupportedOperationException("Other embedded database types not yet supported");
3337
}
3438
} catch (ClassNotFoundException e) {
35-
throw new IllegalStateException("Drivers for test database type [" + type + "] are not available in the classpath", e);
39+
throw new IllegalStateException("Drivers for test database type [" + type
40+
+ "] are not available in the classpath", e);
3641
}
3742
}
38-
43+
3944
}

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,19 @@ public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
116116
*/
117117
public EmbeddedDatabase getDatabase() {
118118
if (dataSource == null) {
119-
initDataSource();
119+
initDatabase();
120120
}
121121
return new EmbeddedDataSourceProxy(dataSource);
122122
}
123123

124124
// subclassing hooks
125125

126-
protected void initDataSource() {
126+
/**
127+
* Hook to initialize the embedded database.
128+
* Subclasses may call to force initialization.
129+
* After calling this method, {@link #getDataSource()} returns the DataSource providing connectivity to the db.
130+
*/
131+
protected void initDatabase() {
127132
// create the embedded database source first
128133
if (logger.isInfoEnabled()) {
129134
logger.info("Created embedded database '" + databaseName + "'");
@@ -136,11 +141,23 @@ protected void initDataSource() {
136141
}
137142
}
138143

144+
/**
145+
* Hook that gets the datasource that provides the connectivity to the embedded database.
146+
* Returns null if the datasource has not been initialized or the database has been shutdown.
147+
* Subclasses may call to access the datasource instance directly.
148+
* @return the datasource
149+
*/
139150
protected DataSource getDataSource() {
140151
return dataSource;
141152
}
142153

143-
protected void shutdownDataSource() {
154+
/**
155+
* Hook to shutdown the embedded database.
156+
* Subclasses may call to force shutdown.
157+
* After calling, {@link #getDataSource()} returns null.
158+
* Does nothing if no embedded database has been initialized.
159+
*/
160+
protected void shutdownDatabase() {
144161
if (dataSource != null) {
145162
databaseConfigurer.shutdown(dataSource);
146163
dataSource = null;
@@ -200,7 +217,7 @@ public <T> T unwrap(Class<T> iface) throws SQLException {
200217
}
201218

202219
public void shutdown() {
203-
shutdownDataSource();
220+
shutdownDatabase();
204221
}
205222

206223
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class EmbeddedDatabaseFactoryBean extends EmbeddedDatabaseFactory implements FactoryBean<DataSource>, InitializingBean, DisposableBean {
3333

3434
public void afterPropertiesSet() throws Exception {
35-
initDataSource();
35+
initDatabase();
3636
}
3737

3838
public DataSource getObject() throws Exception {
@@ -48,7 +48,7 @@ public boolean isSingleton() {
4848
}
4949

5050
public void destroy() throws Exception {
51-
shutdownDataSource();
51+
shutdownDatabase();
5252
}
5353

5454
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@
2525
import org.apache.commons.logging.LogFactory;
2626
import org.springframework.util.ClassUtils;
2727

28-
public class HsqlEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigurer {
28+
/**
29+
* Initializes a HSQL embedded database instance.
30+
* Call {@link #getInstance()} to get the singleton instance of this class.
31+
*
32+
* @author Keith Donald
33+
*/
34+
final class HsqlEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigurer {
2935

3036
private static final Log logger = LogFactory.getLog(HsqlEmbeddedDatabaseConfigurer.class);
3137

3238
private static HsqlEmbeddedDatabaseConfigurer INSTANCE;
3339

40+
private HsqlEmbeddedDatabaseConfigurer() {
41+
}
42+
43+
/**
44+
* Get the singleton {@link HsqlEmbeddedDatabaseConfigurer} instance.
45+
* @return the configurer
46+
* @throws ClassNotFoundException if HSQL is not on the classpath
47+
*/
3448
public static synchronized HsqlEmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
3549
if (INSTANCE == null) {
3650
ClassUtils.forName("org.hsqldb.jdbcDriver", HsqlEmbeddedDatabaseConfigurer.class.getClassLoader());

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
66

7-
public class SimpleDriverDataSourceFactory implements DataSourceFactory {
7+
final class SimpleDriverDataSourceFactory implements DataSourceFactory {
88

99
private SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
1010

1111
public ConnectionProperties getConnectionProperties() {
1212
return new ConnectionProperties() {
13-
1413
public void setDriverClass(Class<?> driverClass) {
1514
dataSource.setDriverClass(driverClass);
1615
}
@@ -26,13 +25,11 @@ public void setUsername(String username) {
2625
public void setPassword(String password) {
2726
dataSource.setPassword(password);
2827
}
29-
3028
};
3129
}
3230

3331
public DataSource getDataSource() {
3432
return dataSource;
3533
}
36-
3734

3835
}

0 commit comments

Comments
 (0)