Commit 516df88e authored by Phillip Webb's avatar Phillip Webb

Revisit AuditEventRepository interface

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

See gh-5854
parent ebdacfab
...@@ -27,9 +27,16 @@ import java.util.List; ...@@ -27,9 +27,16 @@ import java.util.List;
*/ */
public interface AuditEventRepository { public interface AuditEventRepository {
/**
* Log an event.
* @param event the audit event to log
*/
void add(AuditEvent event);
/** /**
* Find audit events since the time provided. * 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 * @return audit events
* @since 1.4.0 * @since 1.4.0
*/ */
...@@ -37,8 +44,9 @@ public interface AuditEventRepository { ...@@ -37,8 +44,9 @@ public interface AuditEventRepository {
/** /**
* Find audit events relating to the specified principal since the time provided. * Find audit events relating to the specified principal since the time provided.
* @param principal the principal name to search for * @param principal the principal name to search for (or {@code null} if unrestricted)
* @param after timestamp of earliest result required * @param after timestamp of earliest result required (or {@code null} if
* unrestricted)
* @return audit events relating to the principal * @return audit events relating to the principal
*/ */
List<AuditEvent> find(String principal, Date after); List<AuditEvent> find(String principal, Date after);
...@@ -46,18 +54,13 @@ public interface AuditEventRepository { ...@@ -46,18 +54,13 @@ public interface AuditEventRepository {
/** /**
* Find audit events of specified type relating to the specified principal since the * Find audit events of specified type relating to the specified principal since the
* time provided. * time provided.
* @param principal the principal name to search for * @param principal the principal name to search for (or {@code null} if unrestricted)
* @param after timestamp of earliest result required * @param after timestamp of earliest result required (or {@code null} if
* @param type the event type to search for * unrestricted)
* @param type the event type to search for (or {@code null} if unrestricted)
* @return audit events of specified type relating to the principal * @return audit events of specified type relating to the principal
* @since 1.4.0 * @since 1.4.0
*/ */
List<AuditEvent> find(String principal, Date after, String type); List<AuditEvent> find(String principal, Date after, String type);
/**
* Log an event.
* @param event the audit event to log
*/
void add(AuditEvent event);
} }
...@@ -56,59 +56,41 @@ public class InMemoryAuditEventRepository implements AuditEventRepository { ...@@ -56,59 +56,41 @@ public class InMemoryAuditEventRepository implements AuditEventRepository {
this.events = new AuditEvent[capacity]; this.events = new AuditEvent[capacity];
} }
@Override
public synchronized void add(AuditEvent event) {
Assert.notNull(event, "AuditEvent must not be null");
this.tail = (this.tail + 1) % this.events.length;
this.events[this.tail] = event;
}
@Override @Override
public synchronized List<AuditEvent> find(Date after) { public synchronized List<AuditEvent> find(Date after) {
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>(); return find(null, after, null);
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 @Override
public synchronized List<AuditEvent> find(String principal, Date after) { public synchronized List<AuditEvent> find(String principal, Date after) {
Assert.notNull(principal, "Principal must not be null"); return find(principal, after, 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 @Override
public synchronized List<AuditEvent> find(String principal, Date after, String type) { 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>(); LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
for (int i = 0; i < this.events.length; i++) { for (int i = 0; i < this.events.length; i++) {
AuditEvent event = resolveTailEvent(i); AuditEvent event = resolveTailEvent(i);
if (event == null) { if (event != null && isMatch(principal, after, type, event)) {
break;
}
if (isMatch(event, principal, type, after)) {
events.addFirst(event); events.addFirst(event);
} }
} }
return events; return events;
} }
@Override private boolean isMatch(String principal, Date after, String type, AuditEvent event) {
public synchronized void add(AuditEvent event) { boolean match = true;
Assert.notNull(event, "AuditEvent must not be null"); match &= (principal == null || event.getPrincipal().equals(principal));
this.tail = (this.tail + 1) % this.events.length; match &= (after == null || event.getTimestamp().compareTo(after) >= 0);
this.events[this.tail] = event; match &= (type == null || event.getType().equals(type));
return match;
} }
private AuditEvent resolveTailEvent(int offset) { private AuditEvent resolveTailEvent(int offset) {
...@@ -116,16 +98,4 @@ public class InMemoryAuditEventRepository implements AuditEventRepository { ...@@ -116,16 +98,4 @@ public class InMemoryAuditEventRepository implements AuditEventRepository {
return this.events[index]; 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));
}
} }
...@@ -89,4 +89,5 @@ public class AuditApplicationEvent extends ApplicationEvent { ...@@ -89,4 +89,5 @@ public class AuditApplicationEvent extends ApplicationEvent {
public AuditEvent getAuditEvent() { public AuditEvent getAuditEvent() {
return this.auditEvent; return this.auditEvent;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment