Audit main work #1.

keep-around/23b8b9e8830874d5f04b57600c3660bddce1287b
Pingex aka Raphaël 9 years ago
parent 913ff8d573
commit 17185123e4

@ -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<IAuditComponentProvider> 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<? extends IAuditComponentProvider> c)
{
return mainComponentsPool.containsAll(c);
}
public static void addAll(Collection<? extends IAuditComponentProvider> c)
{
c.forEach(AuditManager::add);
}
public static void removeAll(Collection<? extends IAuditComponentProvider> c)
{
c.forEach(AuditManager::remove);
}
}

@ -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();
}