From 5980487d899aceec47c760df8af99a8f9f52cb0a Mon Sep 17 00:00:00 2001 From: taskain7 Date: Fri, 15 Apr 2022 01:32:26 +0200 Subject: [PATCH] FINERACT-1571: introducing environment variables for read/write/batch separation --- .../apache/fineract/ServerApplication.java | 3 +- .../FineractModeValidationCondition.java | 38 +++++++ .../config/FineractModeValidationConfig.java | 50 ++++++++++ .../core/config/FineractProperties.java | 99 +++---------------- .../src/main/resources/application.properties | 4 + 5 files changed, 109 insertions(+), 85 deletions(-) create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractModeValidationCondition.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractModeValidationConfig.java diff --git a/fineract-provider/src/main/java/org/apache/fineract/ServerApplication.java b/fineract-provider/src/main/java/org/apache/fineract/ServerApplication.java index f567a6c2a..87ce9053d 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/ServerApplication.java +++ b/fineract-provider/src/main/java/org/apache/fineract/ServerApplication.java @@ -23,7 +23,6 @@ import org.apache.fineract.infrastructure.core.boot.AbstractApplicationConfigura import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; -import org.springframework.context.ConfigurableApplicationContext; /** * Fineract main() application which launches Fineract in an embedded Tomcat HTTP (using Spring Boot). @@ -52,7 +51,7 @@ public class ServerApplication extends SpringBootServletInitializer { } public static void main(String[] args) throws IOException { - ConfigurableApplicationContext ctx = configureApplication(new SpringApplicationBuilder(ServerApplication.class)).run(args); + configureApplication(new SpringApplicationBuilder(ServerApplication.class)).run(args); // ApplicationExitUtil.waitForKeyPressToCleanlyExit(ctx); } } diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractModeValidationCondition.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractModeValidationCondition.java new file mode 100644 index 000000000..aafca6218 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractModeValidationCondition.java @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.fineract.infrastructure.core.config; + +import java.util.Optional; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +public class FineractModeValidationCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + boolean isReadModeEnabled = Optional.ofNullable(context.getEnvironment().getProperty("fineract.mode.read-enabled", Boolean.class)) + .orElse(true); + boolean isWriteModeEnabled = Optional.ofNullable(context.getEnvironment().getProperty("fineract.mode.write-enabled", Boolean.class)) + .orElse(true); + boolean isBatchModeEnabled = Optional.ofNullable(context.getEnvironment().getProperty("fineract.mode.batch-enabled", Boolean.class)) + .orElse(true); + return !isReadModeEnabled && !isWriteModeEnabled && !isBatchModeEnabled; + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractModeValidationConfig.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractModeValidationConfig.java new file mode 100644 index 000000000..7fa3bb917 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractModeValidationConfig.java @@ -0,0 +1,50 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.fineract.infrastructure.core.config; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +@RequiredArgsConstructor +@Conditional(FineractModeValidationCondition.class) +public class FineractModeValidationConfig implements InitializingBean { + + private final ApplicationContext applicationContext; + + @Override + public void afterPropertiesSet() throws Exception { + terminateApplication(); + } + + private void terminateApplication() { + log.error( + "The Fineract instance type is not configured properly. At least one of these environment variables should be true: FINERACT_MODE_READ_ENABLED, FINERACT_MODE_WRITE_ENABLED, FINERACT_MODE_BATCH_ENABLED"); + ((ConfigurableApplicationContext) this.applicationContext).close(); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java index 2b7f535a1..e36e7a107 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java @@ -19,8 +19,12 @@ package org.apache.fineract.infrastructure.core.config; +import lombok.Getter; +import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; +@Getter +@Setter @ConfigurationProperties(prefix = "fineract") public class FineractProperties { @@ -28,22 +32,10 @@ public class FineractProperties { private FineractTenantProperties tenant; - public String getNodeId() { - return nodeId; - } - - public void setNodeId(String nodeId) { - this.nodeId = nodeId; - } - - public FineractTenantProperties getTenant() { - return tenant; - } - - public void setTenant(FineractTenantProperties tenant) { - this.tenant = tenant; - } + private FineractModeProperties mode; + @Getter + @Setter public static class FineractTenantProperties { private String host; @@ -55,77 +47,18 @@ public class FineractProperties { private String identifier; private String name; private String description; + } - public String getHost() { - return host; - } + @Getter + @Setter + public static class FineractModeProperties { - public void setHost(String host) { - this.host = host; - } + private boolean readEnabled; + private boolean writeEnabled; + private boolean batchEnabled; - public Integer getPort() { - return port; - } - - public void setPort(Integer port) { - this.port = port; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getParameters() { - return parameters; - } - - public void setParameters(String parameters) { - this.parameters = parameters; - } - - public String getTimezone() { - return timezone; - } - - public void setTimezone(String timezone) { - this.timezone = timezone; - } - - public String getIdentifier() { - return identifier; - } - - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; + public boolean isReadOnlyMode() { + return readEnabled && !writeEnabled && !batchEnabled; } } } diff --git a/fineract-provider/src/main/resources/application.properties b/fineract-provider/src/main/resources/application.properties index 8d25774aa..9eb4aae35 100644 --- a/fineract-provider/src/main/resources/application.properties +++ b/fineract-provider/src/main/resources/application.properties @@ -33,6 +33,10 @@ fineract.tenant.identifier=${FINERACT_DEFAULT_TENANTDB_IDENTIFIER:default} fineract.tenant.name=${FINERACT_DEFAULT_TENANTDB_NAME:fineract_default} fineract.tenant.description=${FINERACT_DEFAULT_TENANTDB_DESCRIPTION:Default Demo Tenant} +fineract.mode.read-enabled=${FINERACT_MODE_READ_ENABLED:true} +fineract.mode.write-enabled=${FINERACT_MODE_WRITE_ENABLED:true} +fineract.mode.batch-enabled=${FINERACT_MODE_BATCH_ENABLED:true} + management.health.jms.enabled=false # FINERACT 1296