Polish "Improve AuditEventRepository"

Closes gh-5854
pull/5879/merge
Stephane Nicoll 9 years ago
parent 5f19323fbd
commit 3ba2b24301

@ -111,8 +111,8 @@ public class AuditEvent implements Serializable {
} }
/** /**
* Returns the user principal responsible for the event or {@code null}. * Returns the user principal responsible for the event.
* @return the principal or {@code null} * @return the principal
*/ */
public String getPrincipal() { public String getPrincipal() {
return this.principal; return this.principal;

@ -47,12 +47,12 @@ 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
* @param type the event type to search for
* @param after timestamp of earliest result required * @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 * @return audit events of specified type relating to the principal
* @since 1.4.0 * @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. * Log an event.

@ -36,7 +36,7 @@ public class InMemoryAuditEventRepository implements AuditEventRepository {
/** /**
* Circular buffer of the event with tail pointing to the last element. * Circular buffer of the event with tail pointing to the last element.
*/ */
private final AuditEvent[] events; private AuditEvent[] events;
private volatile int tail = -1; private volatile int tail = -1;
@ -48,67 +48,67 @@ public class InMemoryAuditEventRepository implements AuditEventRepository {
this.events = new AuditEvent[capacity]; 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 @Override
public List<AuditEvent> find(Date after) { public synchronized List<AuditEvent> find(Date after) {
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>(); LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
synchronized (this.events) { 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) { break;
break; }
} if (isMatch(event, after)) {
if (isMatch(event, after)) { events.addFirst(event);
events.addFirst(event);
}
} }
} }
return events; return events;
} }
@Override @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"); Assert.notNull(principal, "Principal must not be null");
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>(); LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
synchronized (this.events) { 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) { break;
break; }
} if (isMatch(event, principal, after)) {
if (isMatch(event, principal, after)) { events.addFirst(event);
events.addFirst(event);
}
} }
} }
return events; return events;
} }
@Override @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(principal, "Principal must not be null");
Assert.notNull(type, "Type must not be null"); Assert.notNull(type, "Type must not be null");
LinkedList<AuditEvent> events = new LinkedList<AuditEvent>(); LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
synchronized (this.events) { 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) { break;
break; }
} if (isMatch(event, principal, type, after)) {
if (isMatch(event, principal, type, after)) { events.addFirst(event);
events.addFirst(event);
}
} }
} }
return events; return events;
} }
@Override @Override
public void add(AuditEvent event) { public synchronized void add(AuditEvent event) {
Assert.notNull(event, "AuditEvent must not be null"); Assert.notNull(event, "AuditEvent must not be null");
synchronized (this.events) { this.tail = (this.tail + 1) % this.events.length;
this.tail = (this.tail + 1) % this.events.length; this.events[this.tail] = event;
this.events[this.tail] = event;
}
} }
private AuditEvent resolveTailEvent(int offset) { private AuditEvent resolveTailEvent(int offset) {

@ -91,7 +91,7 @@ public class InMemoryAuditEventRepositoryTests {
repository.add(new AuditEvent("phil", "b")); repository.add(new AuditEvent("phil", "b"));
repository.add(new AuditEvent("dave", "c")); repository.add(new AuditEvent("dave", "c"));
repository.add(new AuditEvent("phil", "d")); 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.size()).isEqualTo(1);
assertThat(events.get(0).getPrincipal()).isEqualTo("dave"); assertThat(events.get(0).getPrincipal()).isEqualTo("dave");
assertThat(events.get(0).getType()).isEqualTo("a"); assertThat(events.get(0).getType()).isEqualTo("a");

Loading…
Cancel
Save