Skip to content
Draft
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 @@ -73,6 +74,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,21 @@
*/
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_BASE_PACKAGE = "org.apache.fineract.command.audit";
public static final String COMMAND_AUDIT_HOOK_BASE_PACKAGE = COMMAND_AUDIT_BASE_PACKAGE + ".hook";
public static final String COMMAND_AUDIT_CHECK_BASE_PACKAGE = COMMAND_AUDIT_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,24 @@
@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;

/**
* Auditing is on as soon as the module is on the classpath; it has to be switched off explicitly. Losing the audit
* trail should be a decision, not the consequence of an unset property. These defaults are kept in step with
* {@code matchIfMissing} on the corresponding {@code @ConditionalOnProperty}; changing one without the other makes
* this class describe behaviour the application does not have.
*/
@Builder.Default
private Boolean enabled = true;
@Builder.Default
private Boolean hookPreEnabled = true;
@Builder.Default
private Boolean hookPostEnabled = true;
@Builder.Default
private Boolean enabled = false;
private Boolean hookErrorEnabled = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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 jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.audit.AuditCommandProperties;
import org.springframework.stereotype.Component;

/**
* Reports at startup whether command auditing is actually switched on. Reads the bound property values rather than
* testing for their presence, so that an explicit {@code false} is reported as disabled.
*/
@Slf4j
@RequiredArgsConstructor
@Component
final class AuditCommandPropertyCheck {

private static final String RECOMMENDATION = "IT IS RECOMMENDED TO ENABLE THIS FEATURE IN PRODUCTION ENVIRONMENTS!";

private final AuditCommandProperties properties;

@PostConstruct
void check() {
if (!Boolean.TRUE.equals(properties.getEnabled())) {
log.warn("Command audit is disabled! {}", RECOMMENDATION);

// the individual hooks are not created at all while the module is disabled
return;
}

report("pre", properties.getHookPreEnabled());
report("post", properties.getHookPostEnabled());
report("error", properties.getHookErrorEnabled());
}

private void report(String hook, Boolean enabled) {
if (Boolean.TRUE.equals(enabled)) {
log.info("Audit {} command hook enabled.", hook);
} else {
log.warn("Audit {} command hook disabled! {}", hook, RECOMMENDATION);
}
}
}
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 = true)
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", matchIfMissing = 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