Revisit AuditEventRepository interface

Update AuditEventRepository to restore support for `null` arguments and
explicitly Javadoc their meaning.

See gh-5854
This commit is contained in:
Phillip Webb 2016-06-30 17:27:55 -07:00
parent ebdacfabc3
commit 516df88ea0
3 changed files with 46 additions and 72 deletions

View File

@ -27,9 +27,16 @@ import java.util.List;
*/
public interface AuditEventRepository {
/**
* Log an event.
* @param event the audit event to log
*/
void add(AuditEvent event);
/**
* Find audit events since the time provided.
* @param after timestamp of earliest result required
* @param after timestamp of earliest result required (or {@code null} if
* unrestricted)
* @return audit events
* @since 1.4.0
*/
@ -37,8 +44,9 @@ public interface AuditEventRepository {
/**
* Find audit events relating to the specified principal since the time provided.
* @param principal the principal name to search for
* @param after timestamp of earliest result required
* @param principal the principal name to search for (or {@code null} if unrestricted)
* @param after timestamp of earliest result required (or {@code null} if
* unrestricted)
* @return audit events relating to the principal
*/
List<AuditEvent> find(String principal, Date after);
@ -46,18 +54,13 @@ public interface AuditEventRepository {
/**
* Find audit events of specified type relating to the specified principal since the
* time provided.
* @param principal the principal name to search for
* @param after timestamp of earliest result required
* @param type the event type to search for
* @param principal the principal name to search for (or {@code null} if unrestricted)
* @param after timestamp of earliest result required (or {@code null} if
* unrestricted)
* @param type the event type to search for (or {@code null} if unrestricted)
* @return audit events of specified type relating to the principal
* @since 1.4.0
*/
List<AuditEvent> find(String principal, Date after, String type);
/**
* Log an event.
* @param event the audit event to log
*/
void add(AuditEvent event);
}

View File

@ -56,54 +56,6 @@ public class InMemoryAuditEventRepository implements AuditEventRepository {
this.events = new AuditEvent[capacity];
}
@Override
public synchronized List<AuditEvent> find(Date after) {
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
for (int i = 0; i < this.events.length; i++) {
AuditEvent event = resolveTailEvent(i);
if (event == null) {
break;
}
if (isMatch(event, after)) {
events.addFirst(event);
}
}
return events;
}
@Override
public synchronized List<AuditEvent> find(String principal, Date after) {
Assert.notNull(principal, "Principal must not be null");
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
for (int i = 0; i < this.events.length; i++) {
AuditEvent event = resolveTailEvent(i);
if (event == null) {
break;
}
if (isMatch(event, principal, after)) {
events.addFirst(event);
}
}
return events;
}
@Override
public synchronized List<AuditEvent> find(String principal, Date after, String type) {
Assert.notNull(principal, "Principal must not be null");
Assert.notNull(type, "Type must not be null");
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
for (int i = 0; i < this.events.length; i++) {
AuditEvent event = resolveTailEvent(i);
if (event == null) {
break;
}
if (isMatch(event, principal, type, after)) {
events.addFirst(event);
}
}
return events;
}
@Override
public synchronized void add(AuditEvent event) {
Assert.notNull(event, "AuditEvent must not be null");
@ -111,21 +63,39 @@ public class InMemoryAuditEventRepository implements AuditEventRepository {
this.events[this.tail] = event;
}
@Override
public synchronized List<AuditEvent> find(Date after) {
return find(null, after, null);
}
@Override
public synchronized List<AuditEvent> find(String principal, Date after) {
return find(principal, after, null);
}
@Override
public synchronized List<AuditEvent> find(String principal, Date after, String type) {
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
for (int i = 0; i < this.events.length; i++) {
AuditEvent event = resolveTailEvent(i);
if (event != null && isMatch(principal, after, type, event)) {
events.addFirst(event);
}
}
return events;
}
private boolean isMatch(String principal, Date after, String type, AuditEvent event) {
boolean match = true;
match &= (principal == null || event.getPrincipal().equals(principal));
match &= (after == null || event.getTimestamp().compareTo(after) >= 0);
match &= (type == null || event.getType().equals(type));
return match;
}
private AuditEvent resolveTailEvent(int offset) {
int index = ((this.tail + this.events.length - offset) % this.events.length);
return this.events[index];
}
private boolean isMatch(AuditEvent event, Date after) {
return (after == null || event.getTimestamp().compareTo(after) >= 0);
}
private boolean isMatch(AuditEvent event, String principal, Date after) {
return (event.getPrincipal().equals(principal) && isMatch(event, after));
}
private boolean isMatch(AuditEvent event, String principal, String type, Date after) {
return (event.getType().equals(type) && isMatch(event, principal, after));
}
}

View File

@ -89,4 +89,5 @@ public class AuditApplicationEvent extends ApplicationEvent {
public AuditEvent getAuditEvent() {
return this.auditEvent;
}
}