diff --git a/gateway-server/pom.xml b/gateway-server/pom.xml index 88495cbcb8..67e79dc935 100644 --- a/gateway-server/pom.xml +++ b/gateway-server/pom.xml @@ -422,6 +422,10 @@ mysql mysql-connector-java + + + org.mariadb.jdbc + mariadb-java-client de.thetaphi diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/util/JDBCUtils.java b/gateway-server/src/main/java/org/apache/knox/gateway/util/JDBCUtils.java index 10115692f4..8253f4c5b2 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/util/JDBCUtils.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/util/JDBCUtils.java @@ -36,6 +36,7 @@ import org.apache.knox.gateway.services.security.AliasService; import org.apache.knox.gateway.services.security.AliasServiceException; import org.hsqldb.jdbc.JDBCDataSource; +import org.mariadb.jdbc.MariaDbDataSource; import org.postgresql.ds.PGSimpleDataSource; import org.postgresql.jdbc.SslMode; import org.postgresql.ssl.NonValidatingFactory; @@ -46,6 +47,7 @@ public class JDBCUtils { public static final String POSTGRESQL_DB_TYPE = "postgresql"; public static final String MYSQL_DB_TYPE = "mysql"; + public static final String MARIA_DB_TYPE = "mariadb"; public static final String DERBY_DB_TYPE = "derbydb"; public static final String HSQL = "hsql"; public static final String DATABASE_USER_ALIAS_NAME = "gateway_database_user"; @@ -61,6 +63,8 @@ public static DataSource getDataSource(GatewayConfig gatewayConfig, AliasService return createHsqlDatasource(gatewayConfig, aliasService); } else if (MYSQL_DB_TYPE.equalsIgnoreCase(gatewayConfig.getDatabaseType())) { return createMySqlDataSource(gatewayConfig, aliasService); + } else if (MARIA_DB_TYPE.equalsIgnoreCase(gatewayConfig.getDatabaseType())) { + return createMariaDbDataSource(gatewayConfig); } throw new IllegalArgumentException("Invalid database type: " + gatewayConfig.getDatabaseType()); } @@ -155,6 +159,14 @@ private static void configureMysqlSsl(GatewayConfig gatewayConfig, AliasService } } + private static DataSource createMariaDbDataSource(GatewayConfig gatewayConfig) throws SQLException { + if (gatewayConfig.getDatabaseConnectionUrl() != null) { + return new MariaDbDataSource(gatewayConfig.getDatabaseConnectionUrl()); + } else { + throw new IllegalArgumentException("MariaDB Java Datasource requires a connection string!"); + } + } + private static String getDatabaseUser(AliasService aliasService) throws AliasServiceException { return getDatabaseAlias(aliasService, DATABASE_USER_ALIAS_NAME); } diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/util/JDBCUtilsTest.java b/gateway-server/src/test/java/org/apache/knox/gateway/util/JDBCUtilsTest.java index 799ae77fe4..f6bca16228 100644 --- a/gateway-server/src/test/java/org/apache/knox/gateway/util/JDBCUtilsTest.java +++ b/gateway-server/src/test/java/org/apache/knox/gateway/util/JDBCUtilsTest.java @@ -30,6 +30,7 @@ import org.apache.knox.gateway.services.security.AliasServiceException; import org.easymock.EasyMock; import org.junit.Test; +import org.mariadb.jdbc.MariaDbDataSource; import org.postgresql.ds.PGSimpleDataSource; import org.postgresql.ssl.NonValidatingFactory; @@ -198,4 +199,42 @@ public void testGetMySqlDatasourceFromJdbcConnectionUrl() throws Exception { assertEquals(connectionUrl, dataSource.getUrl()); EasyMock.verify(gatewayConfig); } + + @Test + public void shouldReturnMariaDbDatasource() throws Exception { + final GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class); + EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.MARIA_DB_TYPE).anyTimes(); + EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn("jdbc:mariadb://localhost:1234").anyTimes(); + EasyMock.replay(gatewayConfig); + assertTrue(JDBCUtils.getDataSource(gatewayConfig, null) instanceof MariaDbDataSource); + } + + @Test + public void shoulFailToReturnMariaDbDatasourceIfConnectionUrlIsMissing() throws Exception { + testMariaDbFailure("MariaDB Java Datasource requires a connection string!", null); + } + + @Test + public void shoulFailToReturnMariaDbDatasourceIfConnectionUrlIsWrong() throws Exception { + final String wrongConnectionUrl = "jdbc:mariadbb://localhost:1234"; // typo in the protocol + testMariaDbFailure("Wrong mariaDB url: " + wrongConnectionUrl, wrongConnectionUrl); + } + + private void testMariaDbFailure(String expectedError, String connectionUrl) { + boolean error = false; + try { + final GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class); + EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.MARIA_DB_TYPE).anyTimes(); + if (connectionUrl != null) { + EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn(connectionUrl).anyTimes(); + } + EasyMock.replay(gatewayConfig); + JDBCUtils.getDataSource(gatewayConfig, null); + } catch (Exception e) { + error = true; + assertEquals(expectedError, e.getMessage()); + } + assertTrue(error); + } + } diff --git a/pom.xml b/pom.xml index 07927d0ecf..4bc5a1fa7a 100644 --- a/pom.xml +++ b/pom.xml @@ -257,6 +257,7 @@ 4.5.6 42.4.1 8.0.28 + 3.3.0 3.16.3 2.0.9 0.0.11.1 @@ -2207,6 +2208,18 @@ mysql-connector-java ${mysql.version} + + org.mariadb.jdbc + mariadb-java-client + 3.3.0 + + + com.github.waffle + waffle-jna + + + + org.jetbrains.pty4j