diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventRepository.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventRepository.java index 746dc8d492f..0462d929ca2 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventRepository.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventRepository.java @@ -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 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 find(String principal, Date after, String type); - /** - * Log an event. - * @param event the audit event to log - */ - void add(AuditEvent event); - } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepository.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepository.java index 5b730c59668..96dc0e5f9ad 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepository.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepository.java @@ -56,54 +56,6 @@ public class InMemoryAuditEventRepository implements AuditEventRepository { this.events = new AuditEvent[capacity]; } - @Override - public synchronized List find(Date after) { - LinkedList events = new LinkedList(); - 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 find(String principal, Date after) { - Assert.notNull(principal, "Principal must not be null"); - LinkedList events = new LinkedList(); - 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 find(String principal, Date after, String type) { - Assert.notNull(principal, "Principal must not be null"); - Assert.notNull(type, "Type must not be null"); - LinkedList events = new LinkedList(); - 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 find(Date after) { + return find(null, after, null); + } + + @Override + public synchronized List find(String principal, Date after) { + return find(principal, after, null); + } + + @Override + public synchronized List find(String principal, Date after, String type) { + LinkedList events = new LinkedList(); + 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)); - } - } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/listener/AuditApplicationEvent.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/listener/AuditApplicationEvent.java index a08138cea78..cb8893f2a12 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/listener/AuditApplicationEvent.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/listener/AuditApplicationEvent.java @@ -89,4 +89,5 @@ public class AuditApplicationEvent extends ApplicationEvent { public AuditEvent getAuditEvent() { return this.auditEvent; } + }