Polish "Improve AuditEventRepository"

Closes gh-5854
This commit is contained in:
Stephane Nicoll 2016-06-30 11:44:01 +02:00
parent 5f19323fbd
commit 3ba2b24301
4 changed files with 41 additions and 41 deletions

View File

@ -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;

View File

@ -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<AuditEvent> find(String principal, String type, Date after);
List<AuditEvent> find(String principal, Date after, String type);
/**
* Log an event.

View File

@ -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<AuditEvent> find(Date after) {
public synchronized List<AuditEvent> find(Date after) {
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
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<AuditEvent> find(String principal, Date after) {
public synchronized List<AuditEvent> find(String principal, Date after) {
Assert.notNull(principal, "Principal must not be null");
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
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<AuditEvent> find(String principal, String type, Date after) {
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>();
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) {

View File

@ -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<AuditEvent> events = repository.find("dave", "a", null);
List<AuditEvent> 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");