From 3ba2b24301d149ef935d27416170cb2165792412 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 30 Jun 2016 11:44:01 +0200 Subject: [PATCH] Polish "Improve AuditEventRepository" Closes gh-5854 --- .../boot/actuate/audit/AuditEvent.java | 4 +- .../actuate/audit/AuditEventRepository.java | 4 +- .../audit/InMemoryAuditEventRepository.java | 72 +++++++++---------- .../InMemoryAuditEventRepositoryTests.java | 2 +- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java index d1b9089c1d..dbfeffaf14 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java @@ -111,8 +111,8 @@ public class AuditEvent implements Serializable { } /** - * Returns the user principal responsible for the event or {@code null}. - * @return the principal or {@code null} + * Returns the user principal responsible for the event. + * @return the principal */ public String getPrincipal() { return this.principal; 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 c6c9c3951f..746dc8d492 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 @@ -47,12 +47,12 @@ 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 type the event type to search for * @param after timestamp of earliest result required + * @param type the event type to search for * @return audit events of specified type relating to the principal * @since 1.4.0 */ - List find(String principal, String type, Date after); + List find(String principal, Date after, String type); /** * Log an 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 2fcefcf233..5b730c5966 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 @@ -36,7 +36,7 @@ public class InMemoryAuditEventRepository implements AuditEventRepository { /** * Circular buffer of the event with tail pointing to the last element. */ - private final AuditEvent[] events; + private AuditEvent[] events; private volatile int tail = -1; @@ -48,67 +48,67 @@ public class InMemoryAuditEventRepository implements AuditEventRepository { this.events = new AuditEvent[capacity]; } + /** + * Set the capacity of this event repository. + * @param capacity the capacity + */ + public synchronized void setCapacity(int capacity) { + this.events = new AuditEvent[capacity]; + } + @Override - public List find(Date after) { + public synchronized List find(Date after) { LinkedList events = new LinkedList(); - synchronized (this.events) { - for (int i = 0; i < this.events.length; i++) { - AuditEvent event = resolveTailEvent(i); - if (event == null) { - break; - } - if (isMatch(event, after)) { - events.addFirst(event); - } + 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 List find(String principal, Date after) { + public synchronized List find(String principal, Date after) { Assert.notNull(principal, "Principal must not be null"); LinkedList events = new LinkedList(); - synchronized (this.events) { - 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); - } + 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 List find(String principal, String type, Date after) { + 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(); - synchronized (this.events) { - 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); - } + 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 void add(AuditEvent event) { + public synchronized void add(AuditEvent event) { Assert.notNull(event, "AuditEvent must not be null"); - synchronized (this.events) { - this.tail = (this.tail + 1) % this.events.length; - this.events[this.tail] = event; - } + this.tail = (this.tail + 1) % this.events.length; + this.events[this.tail] = event; } private AuditEvent resolveTailEvent(int offset) { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepositoryTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepositoryTests.java index d010b31f94..a4aa8d298f 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepositoryTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/InMemoryAuditEventRepositoryTests.java @@ -91,7 +91,7 @@ public class InMemoryAuditEventRepositoryTests { repository.add(new AuditEvent("phil", "b")); repository.add(new AuditEvent("dave", "c")); repository.add(new AuditEvent("phil", "d")); - List events = repository.find("dave", "a", null); + List events = repository.find("dave", null, "a"); assertThat(events.size()).isEqualTo(1); assertThat(events.get(0).getPrincipal()).isEqualTo("dave"); assertThat(events.get(0).getType()).isEqualTo("a");