mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Polish "Improve AuditEventRepository"
Closes gh-5854
This commit is contained in:
parent
5f19323fbd
commit
3ba2b24301
@ -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;
|
||||
|
@ -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.
|
||||
|
@ -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) {
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user