Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ buildscript {
'fineract-command-audit',
'fineract-command-async',
'fineract-command-disruptor',
'fineract-command-idempotency',
'fineract-accounting',
'fineract-provider',
'fineract-branch',
Expand Down Expand Up @@ -70,6 +71,7 @@ buildscript {
'fineract-command',
'fineract-command-jdbc',
'fineract-command-audit',
'fineract-command-idempotency',
'fineract-accounting',
'fineract-provider',
'fineract-investor',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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.command.async;

import static org.apache.fineract.command.core.CommandConstants.COMMAND_PROPERTIES_PREFIX;

public final class AsyncCommandConstants {

private AsyncCommandConstants() {}

public static final String COMMAND_ASYNC_PROPERTIES_PREFIX = COMMAND_PROPERTIES_PREFIX + ".async";
public static final String COMMAND_ASYNC_PROPERTY_ENABLED = COMMAND_ASYNC_PROPERTIES_PREFIX + ".enabled";
public static final String COMMAND_ASYNC_BASE_PACKAGE = "org.apache.fineract.command.async.implementation";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.fineract.command.async;

import static org.apache.fineract.command.async.AsyncCommandConstants.COMMAND_ASYNC_PROPERTIES_PREFIX;

import java.io.Serial;
import java.io.Serializable;
import java.time.Duration;
Expand All @@ -33,7 +35,7 @@
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
@ConfigurationProperties(prefix = "fineract.command.async")
@ConfigurationProperties(prefix = COMMAND_ASYNC_PROPERTIES_PREFIX)
public final class AsyncCommandProperties implements Serializable {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
package org.apache.fineract.command.async.implementation;

import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.apache.fineract.command.async.AsyncCommandConstants.COMMAND_ASYNC_PROPERTY_ENABLED;
import static org.apache.fineract.command.core.CommandState.PROCESSED;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.async.AsyncCommandProperties;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.command.core.CommandContext;
import org.apache.fineract.command.core.CommandDispatcher;
import org.apache.fineract.command.core.CommandHandlerManager;
import org.apache.fineract.command.core.CommandHookManager;
Expand All @@ -38,34 +42,45 @@
@Slf4j
@RequiredArgsConstructor
@Component
@ConditionalOnProperty(value = "fineract.command.async.enabled", havingValue = "true")
@ConditionalOnProperty(value = COMMAND_ASYNC_PROPERTY_ENABLED, havingValue = "true")
public class AsyncCommandDispatcher implements CommandDispatcher {

private final CommandHandlerManager handlerManager;
private final CommandHookManager hookManager;
private final AsyncCommandProperties properties;

@Override
public <REQ, RES> Supplier<RES> dispatch(final Command<REQ> command) {
requireNonNull(command, "Command must not be null");

final var ctx = CommandContext.<REQ, RES>builder().command(command).build();

CompletableFuture<RES> future = CompletableFuture.supplyAsync(() -> {
hookManager.before(command);
hookManager.before(ctx);

if (ctx.isSkipExecution()) {
return ctx.getResponse();
}

RES response = handlerManager.handle(command);

hookManager.after(command, response);
ctx.setResponse(response);
ctx.setState(PROCESSED);

return response;
hookManager.after(ctx);

return ctx.getResponse();
}).whenComplete((response, t) -> {
if (t != null) {
hookManager.error(command, t);
ctx.setError(t);

hookManager.error(ctx);
}
});

return () -> {
try {
// TODO: make this configurable
return future.get(3, SECONDS);
return future.get(properties.getTimeout().toMillis(), MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.fineract.command.async.starter;

import static org.apache.fineract.command.async.AsyncCommandConstants.COMMAND_ASYNC_BASE_PACKAGE;
import static org.apache.fineract.command.async.AsyncCommandConstants.COMMAND_ASYNC_PROPERTY_ENABLED;

import org.apache.fineract.command.async.AsyncCommandProperties;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -26,6 +29,6 @@

@AutoConfiguration
@EnableConfigurationProperties(AsyncCommandProperties.class)
@ComponentScan("org.apache.fineract.command.async.implementation")
@ConditionalOnProperty(value = "fineract.command.async.enabled", havingValue = "true")
@ComponentScan(COMMAND_ASYNC_BASE_PACKAGE)
@ConditionalOnProperty(value = COMMAND_ASYNC_PROPERTY_ENABLED, havingValue = "true")
public class AsyncCommandAutoConfiguration {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@
*/
package org.apache.fineract.command.audit;

import static org.apache.fineract.command.core.CommandConstants.COMMAND_PROPERTIES_PREFIX;

public final class AuditCommandConstants {

private AuditCommandConstants() {}

public static final int COMMAND_HOOK_AUDIT_BEFORE = 20;
public static final int COMMAND_HOOK_AUDIT_AFTER = 20;
public static final int COMMAND_HOOK_AUDIT_ERROR = 20;
public static final String COMMAND_AUDIT_PROPERTIES_PREFIX = COMMAND_PROPERTIES_PREFIX + ".audit";
public static final String COMMAND_AUDIT_PROPERTY_ENABLED = COMMAND_AUDIT_PROPERTIES_PREFIX + ".enabled";
public static final String COMMAND_AUDIT_PROPERTY_HOOK_PRE_ENABLED = COMMAND_AUDIT_PROPERTIES_PREFIX + ".hook-pre-enabled";
public static final String COMMAND_AUDIT_PROPERTY_HOOK_POST_ENABLED = COMMAND_AUDIT_PROPERTIES_PREFIX + ".hook-post-enabled";
public static final String COMMAND_AUDIT_PROPERTY_HOOK_ERROR_ENABLED = COMMAND_AUDIT_PROPERTIES_PREFIX + ".hook-error-enabled";
public static final String COMMAND_AUDIT_HOOK_BASE_PACKAGE = "org.apache.fineract.command.audit.hook";
public static final String COMMAND_AUDIT_HOOK_CHECK_PACKAGE = COMMAND_AUDIT_HOOK_BASE_PACKAGE + ".check";
public static final int COMMAND_AUDIT_HOOK_BEFORE = 30;
public static final int COMMAND_AUDIT_HOOK_AFTER = 30;
public static final int COMMAND_AUDIT_HOOK_ERROR = 30;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.fineract.command.audit;

import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_AUDIT_PROPERTIES_PREFIX;

import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
Expand All @@ -32,12 +34,18 @@
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
@ConfigurationProperties(prefix = "fineract.command.audit")
@ConfigurationProperties(prefix = COMMAND_AUDIT_PROPERTIES_PREFIX)
public final class AuditCommandProperties implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

@Builder.Default
private Boolean enabled = false;
private Boolean enabled = true;
@Builder.Default
private Boolean hookPreEnabled = true;
@Builder.Default
private Boolean hookPostEnabled = true;
@Builder.Default
private Boolean hookErrorEnabled = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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.command.audit.check;

import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_AUDIT_PROPERTY_HOOK_ERROR_ENABLED;
import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_AUDIT_PROPERTY_HOOK_POST_ENABLED;
import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_AUDIT_PROPERTY_HOOK_PRE_ENABLED;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
final class AuditCommandPropertyCheck {

private final Environment environment;

@PostConstruct
public void check() {
if (environment.containsProperty(COMMAND_AUDIT_PROPERTY_HOOK_PRE_ENABLED)) {
log.info("Audit pre command hook enabled.");
} else {
log.warn("Audit pre command hook disabled! IT IS RECOMMENDED ENABLE THIS FEATURE IN PRODUCTION ENVIRONMENTS!");
}
if (environment.containsProperty(COMMAND_AUDIT_PROPERTY_HOOK_POST_ENABLED)) {
log.info("Audit post command hook enabled.");
} else {
log.warn("Audit post command hook disabled! IT IS RECOMMENDED ENABLE THIS FEATURE IN PRODUCTION ENVIRONMENTS!");
}
if (environment.containsProperty(COMMAND_AUDIT_PROPERTY_HOOK_ERROR_ENABLED)) {
log.info("Audit error command hook enabled.");
} else {
log.warn("Audit error command hook disabled! IT IS RECOMMENDED ENABLE THIS FEATURE IN PRODUCTION ENVIRONMENTS!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/
package org.apache.fineract.command.audit.hook;

import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_HOOK_AUDIT_AFTER;
import static org.apache.fineract.command.core.CommandState.PROCESSED;
import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_AUDIT_HOOK_AFTER;
import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_AUDIT_PROPERTY_HOOK_POST_ENABLED;

import java.time.Instant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.command.core.CommandContext;
import org.apache.fineract.command.core.CommandHookAfter;
import org.apache.fineract.command.core.CommandStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -34,20 +34,22 @@
@Slf4j
@RequiredArgsConstructor
@Component
@Order(COMMAND_HOOK_AUDIT_AFTER)
@ConditionalOnProperty(value = "fineract.command.hooks.audit-post", havingValue = "true")
@Order(COMMAND_AUDIT_HOOK_AFTER)
@ConditionalOnProperty(value = COMMAND_AUDIT_PROPERTY_HOOK_POST_ENABLED, havingValue = "true", matchIfMissing = false)
final class AuditCommandHookAfter implements CommandHookAfter<Object, Object> {

private final CommandStore store;

@Override
public void onAfter(Command<Object> command, Object response) {
public void onAfter(CommandContext<Object, Object> ctx) {
final var now = Instant.now();

var command = ctx.getCommand();

command.setExecutedByUsername(command.getInitiatedByUsername());
command.setUpdatedAt(now);
command.setExecutedAt(now);

store.store(command, response, PROCESSED);
store.store(ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
*/
package org.apache.fineract.command.audit.hook;

import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_HOOK_AUDIT_BEFORE;
import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_AUDIT_HOOK_BEFORE;
import static org.apache.fineract.command.audit.AuditCommandConstants.COMMAND_AUDIT_PROPERTY_HOOK_PRE_ENABLED;
import static org.apache.fineract.command.core.CommandState.UNDER_PROCESSING;

import java.time.Instant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.command.core.CommandContext;
import org.apache.fineract.command.core.CommandHookBefore;
import org.apache.fineract.command.core.CommandStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -34,20 +35,24 @@
@Slf4j
@RequiredArgsConstructor
@Component
@Order(COMMAND_HOOK_AUDIT_BEFORE)
@ConditionalOnProperty(value = "fineract.command.hooks.audit-pre", havingValue = "true")
final class AuditCommandHookBefore implements CommandHookBefore<Object> {
@Order(COMMAND_AUDIT_HOOK_BEFORE)
@ConditionalOnProperty(value = COMMAND_AUDIT_PROPERTY_HOOK_PRE_ENABLED, havingValue = "true")
final class AuditCommandHookBefore implements CommandHookBefore<Object, Object> {

private final CommandStore store;

@Override
public void onBefore(Command<Object> command) {
public void onBefore(CommandContext<Object, Object> ctx) {
final var now = Instant.now();

var command = ctx.getCommand();

command.setExecutedByUsername(command.getInitiatedByUsername());
command.setUpdatedAt(now);
command.setExecutedAt(now);

store.store(command, null, UNDER_PROCESSING);
ctx.setState(UNDER_PROCESSING);

store.store(ctx);
}
}
Loading
Loading