Polish
This commit is contained in:
parent
d124d68213
commit
ebdacfabc3
|
|
@ -16,6 +16,10 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.session;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
|
@ -34,6 +38,15 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public class JdbcSessionDatabaseInitializer {
|
||||
|
||||
private static Map<String, String> ALIASES;
|
||||
|
||||
static {
|
||||
Map<String, String> aliases = new HashMap<String, String>();
|
||||
aliases.put("hsql", "hsqldb");
|
||||
aliases.put("postgres", "postgresql");
|
||||
ALIASES = Collections.unmodifiableMap(aliases);
|
||||
}
|
||||
|
||||
private SessionProperties properties;
|
||||
|
||||
private DataSource dataSource;
|
||||
|
|
@ -53,26 +66,28 @@ public class JdbcSessionDatabaseInitializer {
|
|||
@PostConstruct
|
||||
protected void initialize() {
|
||||
if (this.properties.getJdbc().getInitializer().isEnabled()) {
|
||||
String platform = getDatabaseType();
|
||||
if ("hsql".equals(platform)) {
|
||||
platform = "hsqldb";
|
||||
}
|
||||
if ("postgres".equals(platform)) {
|
||||
platform = "postgresql";
|
||||
}
|
||||
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||
String schemaLocation = this.properties.getJdbc().getSchema();
|
||||
schemaLocation = schemaLocation.replace("@@platform@@", platform);
|
||||
schemaLocation = schemaLocation.replace("@@platform@@", getPlatform());
|
||||
populator.addScript(this.resourceLoader.getResource(schemaLocation));
|
||||
populator.setContinueOnError(true);
|
||||
DatabasePopulatorUtils.execute(populator, this.dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
private String getDatabaseType() {
|
||||
private String getPlatform() {
|
||||
String databaseName = getDatabaseName();
|
||||
if (ALIASES.containsKey(databaseName)) {
|
||||
return ALIASES.get(databaseName);
|
||||
}
|
||||
return databaseName;
|
||||
}
|
||||
|
||||
private String getDatabaseName() {
|
||||
try {
|
||||
String databaseProductName = JdbcUtils.extractDatabaseMetaData(
|
||||
this.dataSource, "getDatabaseProductName").toString();
|
||||
String databaseProductName = JdbcUtils
|
||||
.extractDatabaseMetaData(this.dataSource, "getDatabaseProductName")
|
||||
.toString();
|
||||
return JdbcUtils.commonDatabaseName(databaseProductName).toLowerCase();
|
||||
}
|
||||
catch (MetaDataAccessException ex) {
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ public class SessionProperties {
|
|||
}
|
||||
|
||||
public Initializer getInitializer() {
|
||||
return initializer;
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
public static class Initializer {
|
||||
|
|
|
|||
|
|
@ -37,7 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
* @author Vedran Pavic
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class SessionAutoConfigurationJdbcTests extends AbstractSessionAutoConfigurationTests {
|
||||
public class SessionAutoConfigurationJdbcTests
|
||||
extends AbstractSessionAutoConfigurationTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
|
|
|||
Loading…
Reference in New Issue