This is an automated email from the ASF dual-hosted git repository. ikamga pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/fineract-cn-mariadb.git
commit 154667e2c225cbba95983fb11a94fd55631f1855 Author: myrle-krantz <[email protected]> AuthorDate: Wed May 17 16:44:38 2017 +0200 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. --- build.gradle | 4 ++ .../mifos/core/mariadb/config/EnableMariaDB.java | 5 +- .../mariadb/config/MariaDBJavaConfiguration.java | 78 ++++++++++------------ .../MariaDBJavaConfigurationImportSelector.java | 47 +++++++++++++ .../MariaDBTenantBasedJavaConfiguration.java | 44 ++++++++++++ ...ava => MariaDBTenantFreeJavaConfiguration.java} | 28 ++++---- ...ableMariaDB.java => MetaDataSourceWrapper.java} | 27 ++++---- 7 files changed, 162 insertions(+), 71 deletions(-) diff --git a/build.gradle b/build.gradle index 152c6e5..666b501 100644 --- a/build.gradle +++ b/build.gradle @@ -59,6 +59,10 @@ dependencies { ) } +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.Retention; 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 org.springframework.transaction.annotation.EnableTransactionManagement; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; -import java.util.HashMap; import java.util.Properties; @SuppressWarnings("WeakerAccess") @@ -78,61 +76,57 @@ public class MariaDBJavaConfiguration { } @Bean - public DataSource dataSource(@Qualifier(MariaDBConstants.LOGGER_NAME) final Logger logger) { + public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { + final JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(emf); + return transactionManager; + } + + @Bean + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { + return new PersistenceExceptionTranslationPostProcessor(); + } + + @Bean + public FlywayFactoryBean flywayFactoryBean(final ApplicationName applicationName) { + 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)); + 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()); + .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)); + this.env.getProperty(MariaDBConstants.MARIADB_USER_PROP, MariaDBConstants.MARIADB_USER_DEFAULT)); boneCPDataSource.setPassword( - this.env.getProperty(MariaDBConstants.MARIADB_PASSWORD_PROP, MariaDBConstants.MARIADB_PASSWORD_DEFAULT)); + 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))); + 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))); + 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))); + 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))); + 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))); + 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))); + 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))); + 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); - return transactionManager; - } - - @Bean - public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { - return new PersistenceExceptionTranslationPostProcessor(); - } - - @Bean - public FlywayFactoryBean flywayFactoryBean(final ApplicationName applicationName) { - return new FlywayFactoryBean(applicationName); + return new MetaDataSourceWrapper(boneCPDataSource); } private Properties additionalProperties() { 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/EnableMariaDB.java b/src/main/java/io/mifos/core/mariadb/config/MariaDBTenantFreeJavaConfiguration.java similarity index 53% copy from src/main/java/io/mifos/core/mariadb/config/EnableMariaDB.java copy to src/main/java/io/mifos/core/mariadb/config/MariaDBTenantFreeJavaConfiguration.java index b756123..cf6e7fe 100644 --- a/src/main/java/io/mifos/core/mariadb/config/EnableMariaDB.java +++ b/src/main/java/io/mifos/core/mariadb/config/MariaDBTenantFreeJavaConfiguration.java @@ -15,20 +15,22 @@ */ package io.mifos.core.mariadb.config; -import org.springframework.context.annotation.Import; -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@Inherited -@Import({MariaDBJavaConfiguration.class}) -public @interface EnableMariaDB { +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/EnableMariaDB.java b/src/main/java/io/mifos/core/mariadb/config/MetaDataSourceWrapper.java similarity index 58% copy from src/main/java/io/mifos/core/mariadb/config/EnableMariaDB.java copy to src/main/java/io/mifos/core/mariadb/config/MetaDataSourceWrapper.java index b756123..43b497c 100644 --- a/src/main/java/io/mifos/core/mariadb/config/EnableMariaDB.java +++ b/src/main/java/io/mifos/core/mariadb/config/MetaDataSourceWrapper.java @@ -15,20 +15,19 @@ */ package io.mifos.core.mariadb.config; -import org.springframework.context.annotation.Import; +import com.jolbox.bonecp.BoneCPDataSource; -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; +/** + * @author Myrle Krantz + */ +public class MetaDataSourceWrapper { + private final BoneCPDataSource metaDataSource; -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@Inherited -@Import({MariaDBJavaConfiguration.class}) -public @interface EnableMariaDB { + public MetaDataSourceWrapper(final BoneCPDataSource metaDataSource) { + this.metaDataSource = metaDataSource; + } -} + BoneCPDataSource getMetaDataSource() { + return metaDataSource; + } +} \ No newline at end of file
