From 17185123e48aa02f86dea6ebd7cbe82b8abc639e Mon Sep 17 00:00:00 2001 From: Pingex Date: Sun, 25 Dec 2016 22:14:21 +0100 Subject: [PATCH] Audit main work #1. --- .../dcf/commands/audit/AuditManager.java | 88 +++++++++++++++++++ .../audit/IAuditComponentProvider.java | 31 +++++++ 2 files changed, 119 insertions(+) create mode 100644 src/main/java/net/pingex/dcf/commands/audit/AuditManager.java create mode 100644 src/main/java/net/pingex/dcf/commands/audit/IAuditComponentProvider.java diff --git a/src/main/java/net/pingex/dcf/commands/audit/AuditManager.java b/src/main/java/net/pingex/dcf/commands/audit/AuditManager.java new file mode 100644 index 0000000..198b36c --- /dev/null +++ b/src/main/java/net/pingex/dcf/commands/audit/AuditManager.java @@ -0,0 +1,88 @@ +package net.pingex.dcf.commands.audit; + +import net.pingex.dcf.commands.Context; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import java.util.Collection; +import java.util.Comparator; +import java.util.Set; +import java.util.TreeSet; + +/** + * The Audit Manager stores all components and executes an audit when told to, usually from CommandHandler. + */ +public class AuditManager +{ + /** + * This pool contains all main components to be tested when executing an audit. + * This pool does not have to contain every sub-component. + * This pool is automatically sorted, thus + */ + private static Set mainComponentsPool = new TreeSet<>(Comparator.comparingInt(IAuditComponentProvider::priority)); + + private static final Logger LOGGER = LogManager.getLogger(AuditManager.class); + + /** + * Execute an audit against known components. + * @param context The invocation with its context. + * @return The result of this audit. + */ + public static AuditResult doAudit(Context context) + { + LOGGER.trace("Auditing {} invocation for user #{}.", context.getCommand().getName(), context.getOriginatingMessage().getAuthor().getID()); + if(mainComponentsPool.isEmpty()) return new AuditResult(AuditResult.ResultCode.NOOP, null); + + AuditResult.Builder resultBuilder = AuditResult.builder(); + for(IAuditComponentProvider component : mainComponentsPool) + { + AuditResult result = component.doAudit(context); + resultBuilder.appendAuditResult(component, result); + LOGGER.trace("Auditing component {} [{}].", component.name(), result.getOpcode()); + } + resultBuilder.setOpcode(resultBuilder.getSubAuditsResults().stream().min(Comparator.comparing(entry -> entry.getValue().getOpcode())).get().getValue().getOpcode()); + resultBuilder.setMessage(resultBuilder.getSubAuditsResults().stream().min(Comparator.comparing(entry -> entry.getValue().getOpcode())).get().getValue().getMessage()); + + LOGGER.debug("Command {} audit for user #{} returned {}.", context.getCommand().getName(), context.getOriginatingMessage().getAuthor().getID(), resultBuilder.getOpcode()); + return resultBuilder.build(); + } + + // DELEGATED METHODS + // ================= + + public static int size() + { + return mainComponentsPool.size(); + } + + public static boolean contains(IAuditComponentProvider o) + { + return mainComponentsPool.contains(o); + } + + public static boolean add(IAuditComponentProvider iAuditComponentProvider) + { + LOGGER.debug("Adding audit component {}", iAuditComponentProvider.name()); + return mainComponentsPool.add(iAuditComponentProvider); + } + + public static boolean remove(IAuditComponentProvider o) + { + LOGGER.debug("Removing audit component {}", o.name()); + return mainComponentsPool.remove(o); + } + + public static boolean containsAll(Collection c) + { + return mainComponentsPool.containsAll(c); + } + + public static void addAll(Collection c) + { + c.forEach(AuditManager::add); + } + + public static void removeAll(Collection c) + { + c.forEach(AuditManager::remove); + } +} diff --git a/src/main/java/net/pingex/dcf/commands/audit/IAuditComponentProvider.java b/src/main/java/net/pingex/dcf/commands/audit/IAuditComponentProvider.java new file mode 100644 index 0000000..26b0792 --- /dev/null +++ b/src/main/java/net/pingex/dcf/commands/audit/IAuditComponentProvider.java @@ -0,0 +1,31 @@ +package net.pingex.dcf.commands.audit; + +import net.pingex.dcf.commands.Context; + +/** + * Each module which wants to audit an incoming command have to implement this interface. + */ +public interface IAuditComponentProvider +{ + /** + * This is the proper test method. + * @param context Command context + * @return The result of this component's audit. + */ + AuditResult doAudit(Context context); + + /** + * Gives the name of the component. + */ + String name(); + + /** + * Gives a short descrpition about what this component does. + */ + String description(); + + /** + * Gives the priority of this audit. Lower priority takes precedence. + */ + int priority(); +} \ No newline at end of file