Added configuration parameter to @Enable annotation to make it possible to run db connection in a tenant-agnostic manner while still taking advantage of connection pooling/configuration code. This could be further improved to sandbox by app in the tenant-agnostic manner.
diff --git a/build.gradle b/build.gradle
index 152c6e5..666b501 100644
--- a/build.gradle
+++ b/build.gradle
@@ -59,6 +59,10 @@
)
}
+jar {
+ from sourceSets.main.allSource
+}
+
publishing {
publications {
mariadbPublication(MavenPublication) {
diff --git a/src/main/java/io/mifos/core/mariadb/config/EnableMariaDB.java b/src/main/java/io/mifos/core/mariadb/config/EnableMariaDB.java
index b756123..51d003a 100644
--- a/src/main/java/io/mifos/core/mariadb/config/EnableMariaDB.java
+++ b/src/main/java/io/mifos/core/mariadb/config/EnableMariaDB.java
@@ -24,11 +24,12 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+@SuppressWarnings({"WeakerAccess", "unused"})
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
-@Import({MariaDBJavaConfiguration.class})
+@Import({MariaDBJavaConfigurationImportSelector.class})
public @interface EnableMariaDB {
-
+ boolean forTenantContext() default true;
}
diff --git a/src/main/java/io/mifos/core/mariadb/config/MariaDBJavaConfiguration.java b/src/main/java/io/mifos/core/mariadb/config/MariaDBJavaConfiguration.java
index ded27a0..5603d97 100644
--- a/src/main/java/io/mifos/core/mariadb/config/MariaDBJavaConfiguration.java
+++ b/src/main/java/io/mifos/core/mariadb/config/MariaDBJavaConfiguration.java
@@ -15,17 +15,16 @@
*/
package io.mifos.core.mariadb.config;
+
import com.jolbox.bonecp.BoneCPDataSource;
import io.mifos.core.lang.ApplicationName;
import io.mifos.core.lang.config.EnableApplicationName;
-import io.mifos.core.mariadb.domain.ContextAwareRoutingDataSource;
import io.mifos.core.mariadb.domain.FlywayFactoryBean;
import io.mifos.core.mariadb.util.JdbcUrlBuilder;
import io.mifos.core.mariadb.util.MariaDBConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -40,7 +39,6 @@
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
-import java.util.HashMap;
import java.util.Properties;
@SuppressWarnings("WeakerAccess")
@@ -78,47 +76,6 @@
}
@Bean
- public DataSource dataSource(@Qualifier(MariaDBConstants.LOGGER_NAME) final Logger logger) {
- final BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
- boneCPDataSource.setDriverClass(
- this.env.getProperty(MariaDBConstants.MARIADB_DRIVER_CLASS_PROP, MariaDBConstants.MARIADB_DRIVER_CLASS_DEFAULT));
- boneCPDataSource.setJdbcUrl(JdbcUrlBuilder
- .create(JdbcUrlBuilder.DatabaseType.MARIADB)
- .host(this.env.getProperty(MariaDBConstants.MARIADB_HOST_PROP, MariaDBConstants.MARIADB_HOST_DEFAULT))
- .port(this.env.getProperty(MariaDBConstants.MARIADB_PORT_PROP, MariaDBConstants.MARIADB_PORT_DEFAULT))
- .instanceName(this.env.getProperty(MariaDBConstants.MARIADB_DATABASE_NAME_PROP, MariaDBConstants.MARIADB_DATABASE_NAME_DEFAULT))
- .build());
- boneCPDataSource.setUsername(
- this.env.getProperty(MariaDBConstants.MARIADB_USER_PROP, MariaDBConstants.MARIADB_USER_DEFAULT));
- boneCPDataSource.setPassword(
- this.env.getProperty(MariaDBConstants.MARIADB_PASSWORD_PROP, MariaDBConstants.MARIADB_PASSWORD_DEFAULT));
- boneCPDataSource.setIdleConnectionTestPeriodInMinutes(
- Long.valueOf(this.env.getProperty(MariaDBConstants.BONECP_IDLE_CONNECTION_TEST_PROP, MariaDBConstants.BONECP_IDLE_CONNECTION_TEST_DEFAULT)));
- boneCPDataSource.setIdleMaxAgeInMinutes(
- Long.valueOf(this.env.getProperty(MariaDBConstants.BONECP_IDLE_MAX_AGE_PROP, MariaDBConstants.BONECP_IDLE_MAX_AGE_DEFAULT)));
- boneCPDataSource.setMaxConnectionsPerPartition(
- Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_MAX_CONNECTION_PARTITION_PROP, MariaDBConstants.BONECP_MAX_CONNECTION_PARTITION_DEFAULT)));
- boneCPDataSource.setMinConnectionsPerPartition(
- Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_MIN_CONNECTION_PARTITION_PROP, MariaDBConstants.BONECP_MIN_CONNECTION_PARTITION_DEFAULT)));
- boneCPDataSource.setPartitionCount(
- Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_PARTITION_COUNT_PROP, MariaDBConstants.BONECP_PARTITION_COUNT_DEFAULT)));
- boneCPDataSource.setAcquireIncrement(
- Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_ACQUIRE_INCREMENT_PROP, MariaDBConstants.BONECP_ACQUIRE_INCREMENT_DEFAULT)));
- boneCPDataSource.setStatementsCacheSize(
- Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_STATEMENT_CACHE_PROP, MariaDBConstants.BONECP_STATEMENT_CACHE_DEFAULT)));
-
- final Properties driverProperties = new Properties();
- driverProperties.setProperty("useServerPrepStmts", "false");
- boneCPDataSource.setDriverProperties(driverProperties);
-
- final ContextAwareRoutingDataSource dataSource = new ContextAwareRoutingDataSource(logger, JdbcUrlBuilder.DatabaseType.MARIADB);
- dataSource.setMetaDataSource(boneCPDataSource);
- final HashMap<Object, Object> targetDataSources = new HashMap<>();
- dataSource.setTargetDataSources(targetDataSources);
- return dataSource;
- }
-
- @Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
@@ -135,6 +92,43 @@
return new FlywayFactoryBean(applicationName);
}
+ @Bean
+ public MetaDataSourceWrapper metaDataSourceWrapper() {
+
+ final BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
+ boneCPDataSource.setDriverClass(
+ this.env.getProperty(MariaDBConstants.MARIADB_DRIVER_CLASS_PROP, MariaDBConstants.MARIADB_DRIVER_CLASS_DEFAULT));
+ boneCPDataSource.setJdbcUrl(JdbcUrlBuilder
+ .create(JdbcUrlBuilder.DatabaseType.MARIADB)
+ .host(this.env.getProperty(MariaDBConstants.MARIADB_HOST_PROP, MariaDBConstants.MARIADB_HOST_DEFAULT))
+ .port(this.env.getProperty(MariaDBConstants.MARIADB_PORT_PROP, MariaDBConstants.MARIADB_PORT_DEFAULT))
+ .instanceName(this.env.getProperty(MariaDBConstants.MARIADB_DATABASE_NAME_PROP, MariaDBConstants.MARIADB_DATABASE_NAME_DEFAULT))
+ .build());
+ boneCPDataSource.setUsername(
+ this.env.getProperty(MariaDBConstants.MARIADB_USER_PROP, MariaDBConstants.MARIADB_USER_DEFAULT));
+ boneCPDataSource.setPassword(
+ this.env.getProperty(MariaDBConstants.MARIADB_PASSWORD_PROP, MariaDBConstants.MARIADB_PASSWORD_DEFAULT));
+ boneCPDataSource.setIdleConnectionTestPeriodInMinutes(
+ Long.valueOf(this.env.getProperty(MariaDBConstants.BONECP_IDLE_CONNECTION_TEST_PROP, MariaDBConstants.BONECP_IDLE_CONNECTION_TEST_DEFAULT)));
+ boneCPDataSource.setIdleMaxAgeInMinutes(
+ Long.valueOf(this.env.getProperty(MariaDBConstants.BONECP_IDLE_MAX_AGE_PROP, MariaDBConstants.BONECP_IDLE_MAX_AGE_DEFAULT)));
+ boneCPDataSource.setMaxConnectionsPerPartition(
+ Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_MAX_CONNECTION_PARTITION_PROP, MariaDBConstants.BONECP_MAX_CONNECTION_PARTITION_DEFAULT)));
+ boneCPDataSource.setMinConnectionsPerPartition(
+ Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_MIN_CONNECTION_PARTITION_PROP, MariaDBConstants.BONECP_MIN_CONNECTION_PARTITION_DEFAULT)));
+ boneCPDataSource.setPartitionCount(
+ Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_PARTITION_COUNT_PROP, MariaDBConstants.BONECP_PARTITION_COUNT_DEFAULT)));
+ boneCPDataSource.setAcquireIncrement(
+ Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_ACQUIRE_INCREMENT_PROP, MariaDBConstants.BONECP_ACQUIRE_INCREMENT_DEFAULT)));
+ boneCPDataSource.setStatementsCacheSize(
+ Integer.valueOf(this.env.getProperty(MariaDBConstants.BONECP_STATEMENT_CACHE_PROP, MariaDBConstants.BONECP_STATEMENT_CACHE_DEFAULT)));
+
+ final Properties driverProperties = new Properties();
+ driverProperties.setProperty("useServerPrepStmts", "false");
+ boneCPDataSource.setDriverProperties(driverProperties);
+ return new MetaDataSourceWrapper(boneCPDataSource);
+ }
+
private Properties additionalProperties() {
final Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
diff --git a/src/main/java/io/mifos/core/mariadb/config/MariaDBJavaConfigurationImportSelector.java b/src/main/java/io/mifos/core/mariadb/config/MariaDBJavaConfigurationImportSelector.java
new file mode 100644
index 0000000..b1dc720
--- /dev/null
+++ b/src/main/java/io/mifos/core/mariadb/config/MariaDBJavaConfigurationImportSelector.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2017 The Mifos Initiative.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.mifos.core.mariadb.config;
+
+import org.springframework.context.annotation.ImportSelector;
+import org.springframework.core.type.AnnotationMetadata;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Myrle Krantz
+ */
+class MariaDBJavaConfigurationImportSelector implements ImportSelector {
+ @Override
+ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
+ final boolean forTenantContext = (boolean)importingClassMetadata
+ .getAnnotationAttributes(EnableMariaDB.class.getTypeName())
+ .get("forTenantContext");
+
+ final Set<Class> classesToImport = new HashSet<>();
+ final String prop = System.getProperty("mariadb.enabled");
+ if (prop == null || "true".equals(prop)) {
+ classesToImport.add(MariaDBJavaConfiguration.class);
+ if (forTenantContext) {
+ classesToImport.add(MariaDBTenantBasedJavaConfiguration.class);
+ }
+ else {
+ classesToImport.add(MariaDBTenantFreeJavaConfiguration.class);
+ }
+ }
+ return classesToImport.stream().map(Class::getCanonicalName).toArray(String[]::new);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/io/mifos/core/mariadb/config/MariaDBTenantBasedJavaConfiguration.java b/src/main/java/io/mifos/core/mariadb/config/MariaDBTenantBasedJavaConfiguration.java
new file mode 100644
index 0000000..7506cfb
--- /dev/null
+++ b/src/main/java/io/mifos/core/mariadb/config/MariaDBTenantBasedJavaConfiguration.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2017 The Mifos Initiative.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.mifos.core.mariadb.config;
+
+import io.mifos.core.mariadb.domain.ContextAwareRoutingDataSource;
+import io.mifos.core.mariadb.util.JdbcUrlBuilder;
+import io.mifos.core.mariadb.util.MariaDBConstants;
+import org.slf4j.Logger;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.sql.DataSource;
+import java.util.HashMap;
+
+@SuppressWarnings("WeakerAccess")
+@Configuration
+@ConditionalOnProperty(prefix = "mariadb", name = "enabled", matchIfMissing = true)
+public class MariaDBTenantBasedJavaConfiguration {
+ @Bean
+ public DataSource dataSource(@Qualifier(MariaDBConstants.LOGGER_NAME) final Logger logger,
+ final MetaDataSourceWrapper metaDataSource) {
+
+ final ContextAwareRoutingDataSource dataSource = new ContextAwareRoutingDataSource(logger, JdbcUrlBuilder.DatabaseType.MARIADB);
+ dataSource.setMetaDataSource(metaDataSource.getMetaDataSource());
+ final HashMap<Object, Object> targetDataSources = new HashMap<>();
+ dataSource.setTargetDataSources(targetDataSources);
+ return dataSource;
+ }
+}
diff --git a/src/main/java/io/mifos/core/mariadb/config/MariaDBTenantFreeJavaConfiguration.java b/src/main/java/io/mifos/core/mariadb/config/MariaDBTenantFreeJavaConfiguration.java
new file mode 100644
index 0000000..cf6e7fe
--- /dev/null
+++ b/src/main/java/io/mifos/core/mariadb/config/MariaDBTenantFreeJavaConfiguration.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2017 The Mifos Initiative.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.mifos.core.mariadb.config;
+
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.sql.DataSource;
+
+/**
+ * @author Myrle Krantz
+ */
+@SuppressWarnings("WeakerAccess")
+@Configuration
+@ConditionalOnProperty(prefix = "mariadb", name = "enabled", matchIfMissing = true)
+public class MariaDBTenantFreeJavaConfiguration {
+ @Bean
+ public DataSource dataSource(final MetaDataSourceWrapper metaDataSource) {
+ return metaDataSource.getMetaDataSource();
+ }
+}
diff --git a/src/main/java/io/mifos/core/mariadb/config/MetaDataSourceWrapper.java b/src/main/java/io/mifos/core/mariadb/config/MetaDataSourceWrapper.java
new file mode 100644
index 0000000..43b497c
--- /dev/null
+++ b/src/main/java/io/mifos/core/mariadb/config/MetaDataSourceWrapper.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2017 The Mifos Initiative.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.mifos.core.mariadb.config;
+
+import com.jolbox.bonecp.BoneCPDataSource;
+
+/**
+ * @author Myrle Krantz
+ */
+public class MetaDataSourceWrapper {
+ private final BoneCPDataSource metaDataSource;
+
+ public MetaDataSourceWrapper(final BoneCPDataSource metaDataSource) {
+ this.metaDataSource = metaDataSource;
+ }
+
+ BoneCPDataSource getMetaDataSource() {
+ return metaDataSource;
+ }
+}
\ No newline at end of file