FileAuditor extends AbstractAuditor implements AuditorMBean { @Override public synchronized CompositeData[] getEvents(Integer eventCount) { if (eventType == null || eventCount == 0) return null; Vector<CompositeData> retVal = new Vector<CompositeData>(); long savePosition = -1; try { savePosition = auditFile.getFilePointer(); long currentPosition = savePosition; CompositeData event = getLastEvent(); if (event != null) retVal.add(event); int cnt = 1; while (cnt < eventCount && event != null) { int size = getRecordSize(currentPosition); currentPosition -= (RECORD_METADATA_SIZE + size + 4); auditFile.seek(currentPosition); event = getLastEvent(); if (event != null) retVal.add(event); ++cnt; } } catch (IOException e) { } finally { try { if (savePosition > -1) auditFile.seek(savePosition); } catch (IOException e) {} } return (retVal.size() > 0) ? retVal.toArray(new CompositeData[retVal.size()]) : null; } @Inject  FileAuditor(@AuditFile File auditFile); @Override void writeEvent(UUID eventId, Calendar eventTimeStamp, String principal, AuditEvent event, Collection<? extends AuditContext> contexts); @Override synchronized Integer getEventCount(); @Override synchronized CompositeData[] getEvents(Integer eventCount); @Override synchronized CompositeData getLastEvent(); @Override synchronized void clear();  }
@Test public void testGetEvents_AssertGotAllRecordsRequested() { FileAuditor auditor = new FileAuditor(auditFile); AuditEvent event1 = new AuditEvent("Category" + UUID.randomUUID(), "type"); AuditEvent event2 = new AuditEvent("Category" + UUID.randomUUID(), "type"); DefaultAuditContext context1 = new DefaultAuditContext("name1", "value1"); DefaultAuditContext context2 = new DefaultAuditContext("name2", "value2"); auditor.audit(PRINCIPAL, event1); auditor.audit(PRINCIPAL, event2, Arrays.asList(context1, context2)); CompositeData[] events = auditor.getEvents(2); assertNotNull(events); assertEquals(2, events.length); CompositeData lastMessage = events[0]; assertEquals(event2.getName(), lastMessage.get("Event Name")); assertEquals(event2.getType(), lastMessage.get("Event Type")); assertTrue(lastMessage.get("Event Id").toString().length() > 0); assertTrue(lastMessage.get("Event Time").toString().length() > 0); assertNotNull(lastMessage.get("Contexts")); String[] contexts = (String[])lastMessage.get("Contexts"); assertEquals(2, contexts.length); assertEquals("name1:value1", contexts[0]); assertEquals("name2:value2", contexts[1]); }