From 53165979204ea403840e7941f47b9616fbf9a5b9 Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Fri, 30 Jun 2017 14:51:26 +0200 Subject: [PATCH] Fixed MID-4010: startup failure when using datasource having no driverClassName in config.xml --- .../repo/sql/SqlRepositoryConfiguration.java | 128 ++++++++++++------ 1 file changed, 89 insertions(+), 39 deletions(-) diff --git a/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/SqlRepositoryConfiguration.java b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/SqlRepositoryConfiguration.java index 31d007c7938..47d7bc8add7 100644 --- a/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/SqlRepositoryConfiguration.java +++ b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/SqlRepositoryConfiguration.java @@ -28,6 +28,13 @@ import org.apache.commons.lang.StringUtils; import org.h2.Driver; import org.hibernate.dialect.*; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static com.evolveum.midpoint.repo.sql.SqlRepositoryConfiguration.Database.*; /** * This class is used for SQL repository configuration. It reads values from Apache configuration object (xml). @@ -39,8 +46,52 @@ public class SqlRepositoryConfiguration { private static final Trace LOGGER = TraceManager.getTrace(SqlRepositoryConfiguration.class); enum Database { - H2, MYSQL, POSTGRESQL, SQLSERVER, ORACLE, MARIADB - } + + // we might include other dialects if needed (but the ones listed here are the recommended ones) + H2(DRIVER_H2, MidPointMySQLDialect.class.getName()), + MYSQL(DRIVER_MYSQL, MidPointMySQLDialect.class.getName()), + POSTGRESQL(DRIVER_POSTGRESQL, MidPointPostgreSQLDialect.class.getName()), + SQLSERVER(DRIVER_SQLSERVER, UnicodeSQLServer2008Dialect.class.getName()), + ORACLE(DRIVER_ORACLE, Oracle10gDialect.class.getName()), + MARIADB(DRIVER_MARIADB, MidPointMySQLDialect.class.getName()); + + // order is important! (the first value is the default) + @NotNull List drivers; + @NotNull List dialects; + + Database(String driver, String... dialects) { + this.drivers = Collections.singletonList(driver); + this.dialects = Arrays.asList(dialects); + } + + public static Database findDatabase(String databaseName) { + if (StringUtils.isBlank(databaseName)) { + return null; + } + for (Database database : values()) { + if (database.name().equalsIgnoreCase(databaseName)) { + return database; + } + } + throw new IllegalArgumentException("Unsupported database type: " + databaseName); + } + + public String getDefaultHibernateDialect() { + return dialects.get(0); + } + + public String getDefaultDriverClassName() { + return drivers.get(0); + } + + public boolean containsDriver(String driverClassName) { + return drivers.contains(driverClassName); + } + + public boolean containsDialect(String hibernateDialect) { + return dialects.contains(hibernateDialect); + } + } public static final String PROPERTY_DATABASE = "database"; public static final String PROPERTY_BASE_DIR = "baseDir"; @@ -86,7 +137,7 @@ enum Database { private static final String DRIVER_POSTGRESQL = "org.postgresql.Driver"; private static final String DRIVER_ORACLE = "oracle.jdbc.OracleDriver"; - private String database = Database.H2.name(); + private String database = null; //embedded configuration private boolean embedded = true; @@ -125,8 +176,9 @@ enum Database { public SqlRepositoryConfiguration(Configuration configuration) { setDatabase(configuration.getString(PROPERTY_DATABASE, database)); + setDataSource(configuration.getString(PROPERTY_DATASOURCE, null)); - computeDefaultDatabaseParameters(); + computeDefaultDatabaseParameters(); setAsServer(configuration.getBoolean(PROPERTY_AS_SERVER, embedded)); setBaseDir(configuration.getString(PROPERTY_BASE_DIR, baseDir)); @@ -141,7 +193,6 @@ public SqlRepositoryConfiguration(Configuration configuration) { setTcpSSL(configuration.getBoolean(PROPERTY_TCP_SSL, tcpSSL)); setFileName(configuration.getString(PROPERTY_FILE_NAME, fileName)); setDropIfExists(configuration.getBoolean(PROPERTY_DROP_IF_EXISTS, dropIfExists)); - setDataSource(configuration.getString(PROPERTY_DATASOURCE, null)); setMinPoolSize(configuration.getInt(PROPERTY_MIN_POOL_SIZE, minPoolSize)); setMaxPoolSize(configuration.getInt(PROPERTY_MAX_POOL_SIZE, maxPoolSize)); setUseZip(configuration.getBoolean(PROPERTY_USE_ZIP, useZip)); @@ -167,33 +218,23 @@ public SqlRepositoryConfiguration(Configuration configuration) { } private void computeDefaultDatabaseParameters() { - if (Database.H2.name().equalsIgnoreCase(getDatabase())) { - embedded = true; - hibernateHbm2ddl = "update"; - - hibernateDialect = H2Dialect.class.getName(); - driverClassName = DRIVER_H2; - } else { - embedded = false; - hibernateHbm2ddl = "validate"; - - if (Database.MYSQL.name().equalsIgnoreCase(getDatabase())) { - hibernateDialect = MidPointMySQLDialect.class.getName(); - driverClassName = DRIVER_MYSQL; - } else if (Database.POSTGRESQL.name().equalsIgnoreCase(getDatabase())) { - hibernateDialect = MidPointPostgreSQLDialect.class.getName(); - driverClassName = DRIVER_POSTGRESQL; - } else if (Database.ORACLE.name().equalsIgnoreCase(getDatabase())) { - hibernateDialect = Oracle10gDialect.class.getName(); - driverClassName = DRIVER_ORACLE; - } else if (Database.SQLSERVER.name().equalsIgnoreCase(getDatabase())) { - hibernateDialect = UnicodeSQLServer2008Dialect.class.getName(); - driverClassName = DRIVER_SQLSERVER; - } else if (Database.MARIADB.name().equalsIgnoreCase(getDatabase())) { - hibernateDialect = MidPointMySQLDialect.class.getName(); - driverClassName = DRIVER_MARIADB; - } - } + Database db = Database.findDatabase(getDatabase()); + if (db == null) { + if (dataSource != null) { + return; // no defaults in this case + } else { + db = H2; + } + } + if (db == H2) { + embedded = true; + hibernateHbm2ddl = "update"; + } else { + embedded = false; + hibernateHbm2ddl = "validate"; + } + hibernateDialect = db.getDefaultHibernateDialect(); + driverClassName = db.getDefaultDriverClassName(); } private void computeDefaultConcurrencyParameters() { @@ -555,27 +596,36 @@ public void setOrgClosureStartupAction(String orgClosureStartupAction) { } public boolean isUsingH2() { - return DRIVER_H2.equals(driverClassName); + return isUsing(H2); } - public boolean isUsingOracle() { - return DRIVER_ORACLE.equals(driverClassName); + private boolean isUsing(Database db) { + return db.name().equalsIgnoreCase(database) + || db.containsDriver(driverClassName) + || db.containsDialect(hibernateDialect); + } + + public boolean isUsingOracle() { + return isUsing(ORACLE); } public boolean isUsingMySQL() { - return DRIVER_MYSQL.equals(driverClassName); + return isUsing(MYSQL); } public boolean isUsingMariaDB() { - return DRIVER_MARIADB.equals(driverClassName); + // Note that MySQL and MariaDB share the same hibernateDialect. + // So if this is the only information (e.g. when using tomcat datasource configuration), + // we want only isUsingMySQL to return 'true'. + return isUsing(MARIADB) && !isUsing(MYSQL); } public boolean isUsingPostgreSQL() { - return DRIVER_POSTGRESQL.equals(driverClassName); + return isUsing(POSTGRESQL); } public boolean isUsingSQLServer() { - return DRIVER_SQLSERVER.equals(driverClassName); + return isUsing(SQLSERVER); } public void setStopOnOrgClosureStartupFailure(boolean stopOnOrgClosureStartupFailure) {