added annotation EnableMongoLiquibase so that services using the dependency can enable the liquibase support by choice

This commit is contained in:
maverickstuder 2024-03-26 17:18:15 +01:00
parent 1eb2a4fd97
commit 02486b9d4c
5 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,103 @@
package com.knecon.fforesight.mongo.database.commons.config;
import java.util.Optional;
import org.bson.codecs.Codec;
import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import com.knecon.fforesight.mongo.database.commons.service.MongoDataSources;
import com.mongodb.ClientSessionOptions;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
//@Configuration
public class _MultiTenantMongoDBFactory implements MongoDatabaseFactory {
private final MongoDataSources mongoDataSources;
public _MultiTenantMongoDBFactory(MongoDataSources mongoDataSources) {
this.mongoDataSources = mongoDataSources;
}
@Override
public MongoDatabase getMongoDatabase() throws DataAccessException {
return mongoDataSources.mongoDatabaseCurrentTenantResolver();
}
@Override
public MongoDatabase getMongoDatabase(String dbName) throws DataAccessException {
return mongoDataSources.mongoDatabaseCurrentTenantResolver();
}
@Override
public PersistenceExceptionTranslator getExceptionTranslator() {
return null;
}
@Override
public CodecRegistry getCodecRegistry() {
return MongoDatabaseFactory.super.getCodecRegistry();
}
@Override
public boolean hasCodecFor(Class<?> type) {
return MongoDatabaseFactory.super.hasCodecFor(type);
}
@Override
public <T> Optional<Codec<T>> getCodecFor(Class<T> type) {
return MongoDatabaseFactory.super.getCodecFor(type);
}
@Override
public ClientSession getSession(ClientSessionOptions options) {
return null;
}
@Override
public MongoDatabaseFactory withSession(ClientSessionOptions options) {
return MongoDatabaseFactory.super.withSession(options);
}
@Override
public MongoDatabaseFactory withSession(ClientSession session) {
return null;
}
@Override
public boolean isTransactionActive() {
return MongoDatabaseFactory.super.isTransactionActive();
}
}

View File

@ -0,0 +1,13 @@
package com.knecon.fforesight.mongo.database.commons.liquibase;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableMongoLiquibase {
}

View File

@ -0,0 +1,18 @@
package com.knecon.fforesight.mongo.database.commons.liquibase;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class EnableMongoLiquibaseCondition implements Condition {
@Override
public boolean matches(@NotNull ConditionContext context, @NotNull AnnotatedTypeMetadata metadata) {
return !Objects.requireNonNull(context.getBeanFactory()).getBeansWithAnnotation(EnableMongoLiquibase.class).isEmpty();
}
}

View File

@ -4,12 +4,14 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import com.knecon.fforesight.tenantcommons.EncryptionDecryptionService;
import com.knecon.fforesight.tenantcommons.TenantProvider;
@Configuration
@Conditional(EnableMongoLiquibaseCondition.class)
public class MongoLiquibaseConfig {
@Bean(name = "tenantMongoLiquibaseProperties")

View File

@ -2,6 +2,7 @@ package com.knecon.fforesight.mongo.database.commons.liquibase;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;
import jakarta.annotation.PostConstruct;
@ -12,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
@RequiredArgsConstructor
@Conditional(EnableMongoLiquibaseCondition.class)
public class MongoTenantCreatedListener {
private final TenantMongoLiquibaseExecutor tenantMongoLiquibaseExecutor;