Skip to content

Commit d5b2f67

Browse files
committed
Merge pull request #6543 from vpavic:database-initializers
* pr/6543: Polish contribution Improve database initializers
2 parents d2d911b + 3e1425e commit d5b2f67

File tree

6 files changed

+169
-131
lines changed

6 files changed

+169
-131
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2012-2016 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.boot.autoconfigure;
18+
19+
import javax.annotation.PostConstruct;
20+
import javax.sql.DataSource;
21+
22+
import org.springframework.boot.jdbc.DatabaseDriver;
23+
import org.springframework.core.io.ResourceLoader;
24+
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
25+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
26+
import org.springframework.jdbc.support.JdbcUtils;
27+
import org.springframework.jdbc.support.MetaDataAccessException;
28+
import org.springframework.util.Assert;
29+
30+
/**
31+
* Base class used for database initialization.
32+
*
33+
* @author Vedran Pavic
34+
* @author Stephane Nicoll
35+
* @since 1.5.0
36+
*/
37+
public abstract class AbstractDatabaseInitializer {
38+
39+
private static final String PLATFORM_PLACEHOLDER = "@@platform@@";
40+
41+
private final DataSource dataSource;
42+
43+
private final ResourceLoader resourceLoader;
44+
45+
protected AbstractDatabaseInitializer(DataSource dataSource, ResourceLoader resourceLoader) {
46+
Assert.notNull(dataSource, "DataSource must not be null");
47+
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
48+
this.dataSource = dataSource;
49+
this.resourceLoader = resourceLoader;
50+
}
51+
52+
@PostConstruct
53+
protected void initialize() {
54+
if (isEnabled()) {
55+
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
56+
String schemaLocation = getSchemaLocation();
57+
if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) {
58+
String platform = getDatabaseName();
59+
schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform);
60+
}
61+
populator.addScript(this.resourceLoader.getResource(schemaLocation));
62+
populator.setContinueOnError(true);
63+
DatabasePopulatorUtils.execute(populator, this.dataSource);
64+
}
65+
}
66+
67+
protected abstract boolean isEnabled();
68+
69+
protected abstract String getSchemaLocation();
70+
71+
protected String getDatabaseName() {
72+
try {
73+
String databaseProductName = JdbcUtils.extractDatabaseMetaData(
74+
this.dataSource, "getDatabaseProductName").toString();
75+
databaseProductName = JdbcUtils.commonDatabaseName(databaseProductName);
76+
DatabaseDriver databaseDriver = DatabaseDriver.fromProductName(
77+
databaseProductName);
78+
if (databaseDriver == DatabaseDriver.UNKNOWN) {
79+
throw new IllegalStateException("Unable to detect database type");
80+
}
81+
return databaseDriver.getId();
82+
}
83+
catch (MetaDataAccessException ex) {
84+
throw new IllegalStateException("Unable to detect database type", ex);
85+
}
86+
}
87+
88+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4141
import org.springframework.context.annotation.Bean;
4242
import org.springframework.context.annotation.Configuration;
43+
import org.springframework.core.io.ResourceLoader;
4344
import org.springframework.jdbc.core.JdbcOperations;
4445
import org.springframework.util.StringUtils;
4546

@@ -77,8 +78,9 @@ public BatchAutoConfiguration(BatchProperties properties,
7778
@Bean
7879
@ConditionalOnMissingBean
7980
@ConditionalOnBean(DataSource.class)
80-
public BatchDatabaseInitializer batchDatabaseInitializer() {
81-
return new BatchDatabaseInitializer();
81+
public BatchDatabaseInitializer batchDatabaseInitializer(DataSource dataSource,
82+
ResourceLoader resourceLoader) {
83+
return new BatchDatabaseInitializer(dataSource, resourceLoader, this.properties);
8284
}
8385

8486
@Bean
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -16,63 +16,46 @@
1616

1717
package org.springframework.boot.autoconfigure.batch;
1818

19-
import javax.annotation.PostConstruct;
2019
import javax.sql.DataSource;
2120

22-
import org.springframework.batch.support.DatabaseType;
23-
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer;
2422
import org.springframework.core.io.ResourceLoader;
25-
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
26-
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
27-
import org.springframework.jdbc.support.MetaDataAccessException;
28-
import org.springframework.stereotype.Component;
23+
import org.springframework.util.Assert;
2924

3025
/**
3126
* Initialize the Spring Batch schema (ignoring errors, so should be idempotent).
3227
*
3328
* @author Dave Syer
29+
* @author Vedran Pavic
3430
*/
35-
@Component
36-
public class BatchDatabaseInitializer {
31+
public class BatchDatabaseInitializer extends AbstractDatabaseInitializer {
3732

38-
@Autowired
39-
private BatchProperties properties;
33+
private final BatchProperties properties;
4034

41-
@Autowired
42-
private DataSource dataSource;
35+
public BatchDatabaseInitializer(DataSource dataSource,
36+
ResourceLoader resourceLoader, BatchProperties properties) {
37+
super(dataSource, resourceLoader);
38+
Assert.notNull(properties, "BatchProperties must not be null");
39+
this.properties = properties;
40+
}
4341

44-
@Autowired
45-
private ResourceLoader resourceLoader;
42+
@Override
43+
protected boolean isEnabled() {
44+
return this.properties.getInitializer().isEnabled();
45+
}
4646

47-
@PostConstruct
48-
protected void initialize() {
49-
if (this.properties.getInitializer().isEnabled()) {
50-
String platform = getDatabaseType();
51-
if ("hsql".equals(platform)) {
52-
platform = "hsqldb";
53-
}
54-
if ("postgres".equals(platform)) {
55-
platform = "postgresql";
56-
}
57-
if ("oracle".equals(platform)) {
58-
platform = "oracle10g";
59-
}
60-
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
61-
String schemaLocation = this.properties.getSchema();
62-
schemaLocation = schemaLocation.replace("@@platform@@", platform);
63-
populator.addScript(this.resourceLoader.getResource(schemaLocation));
64-
populator.setContinueOnError(true);
65-
DatabasePopulatorUtils.execute(populator, this.dataSource);
66-
}
47+
@Override
48+
protected String getSchemaLocation() {
49+
return this.properties.getSchema();
6750
}
6851

69-
private String getDatabaseType() {
70-
try {
71-
return DatabaseType.fromMetaData(this.dataSource).toString().toLowerCase();
72-
}
73-
catch (MetaDataAccessException ex) {
74-
throw new IllegalStateException("Unable to detect database type", ex);
52+
@Override
53+
protected String getDatabaseName() {
54+
String databaseName = super.getDatabaseName();
55+
if ("oracle".equals(databaseName)) {
56+
return "oracle10g";
7557
}
58+
return databaseName;
7659
}
7760

7861
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class JdbcSessionConfiguration {
4747
@Bean
4848
@ConditionalOnMissingBean
4949
public JdbcSessionDatabaseInitializer jdbcSessionDatabaseInitializer(
50-
SessionProperties properties, DataSource dataSource,
51-
ResourceLoader resourceLoader) {
52-
return new JdbcSessionDatabaseInitializer(properties, dataSource, resourceLoader);
50+
DataSource dataSource, ResourceLoader resourceLoader,
51+
SessionProperties properties) {
52+
return new JdbcSessionDatabaseInitializer(dataSource, resourceLoader, properties);
5353
}
5454

5555
@Configuration

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,10 @@
1616

1717
package org.springframework.boot.autoconfigure.session;
1818

19-
import java.util.Collections;
20-
import java.util.HashMap;
21-
import java.util.Map;
22-
23-
import javax.annotation.PostConstruct;
2419
import javax.sql.DataSource;
2520

21+
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer;
2622
import org.springframework.core.io.ResourceLoader;
27-
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
28-
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
29-
import org.springframework.jdbc.support.JdbcUtils;
30-
import org.springframework.jdbc.support.MetaDataAccessException;
3123
import org.springframework.util.Assert;
3224

3325
/**
@@ -36,64 +28,25 @@
3628
* @author Vedran Pavic
3729
* @since 1.4.0
3830
*/
39-
public class JdbcSessionDatabaseInitializer {
40-
41-
private static Map<String, String> ALIASES;
42-
43-
static {
44-
Map<String, String> aliases = new HashMap<String, String>();
45-
aliases.put("apache derby", "derby");
46-
aliases.put("hsql database engine", "hsqldb");
47-
aliases.put("microsoft sql server", "sqlserver");
48-
ALIASES = Collections.unmodifiableMap(aliases);
49-
}
31+
public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer {
5032

51-
private SessionProperties properties;
33+
private final SessionProperties.Jdbc properties;
5234

53-
private DataSource dataSource;
54-
55-
private ResourceLoader resourceLoader;
56-
57-
public JdbcSessionDatabaseInitializer(SessionProperties properties,
58-
DataSource dataSource, ResourceLoader resourceLoader) {
35+
public JdbcSessionDatabaseInitializer(DataSource dataSource,
36+
ResourceLoader resourceLoader, SessionProperties properties) {
37+
super(dataSource, resourceLoader);
5938
Assert.notNull(properties, "SessionProperties must not be null");
60-
Assert.notNull(dataSource, "DataSource must not be null");
61-
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
62-
this.properties = properties;
63-
this.dataSource = dataSource;
64-
this.resourceLoader = resourceLoader;
65-
}
66-
67-
@PostConstruct
68-
protected void initialize() {
69-
if (this.properties.getJdbc().getInitializer().isEnabled()) {
70-
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
71-
String schemaLocation = this.properties.getJdbc().getSchema();
72-
schemaLocation = schemaLocation.replace("@@platform@@", getPlatform());
73-
populator.addScript(this.resourceLoader.getResource(schemaLocation));
74-
populator.setContinueOnError(true);
75-
DatabasePopulatorUtils.execute(populator, this.dataSource);
76-
}
39+
this.properties = properties.getJdbc();
7740
}
7841

79-
private String getPlatform() {
80-
String databaseName = getDatabaseName();
81-
if (ALIASES.containsKey(databaseName)) {
82-
return ALIASES.get(databaseName);
83-
}
84-
return databaseName;
42+
@Override
43+
protected boolean isEnabled() {
44+
return this.properties.getInitializer().isEnabled();
8545
}
8646

87-
private String getDatabaseName() {
88-
try {
89-
String databaseProductName = JdbcUtils
90-
.extractDatabaseMetaData(this.dataSource, "getDatabaseProductName")
91-
.toString();
92-
return JdbcUtils.commonDatabaseName(databaseProductName).toLowerCase();
93-
}
94-
catch (MetaDataAccessException ex) {
95-
throw new IllegalStateException("Unable to detect database type", ex);
96-
}
47+
@Override
48+
protected String getSchemaLocation() {
49+
return this.properties.getSchema();
9750
}
9851

9952
}

0 commit comments

Comments
 (0)