private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
+ private static final int TS_LENGTH = TIMESTAMP_FORMAT.length();
+
public AuditEvent()
{
/* do nothing */
public AuditEvent( String name, String repositoryId )
{
+ String ts = name.substring( 0, TS_LENGTH );
try
{
- timestamp = createNameFormat().parse( name );
+ timestamp = createNameFormat().parse( ts );
}
catch ( ParseException e )
{
- throw new IllegalArgumentException( "Improperly formatted timestamp for audit log event: " + name );
+ throw new IllegalArgumentException( "Improperly formatted timestamp for audit log event: " + ts );
}
+
+ if ( name.length() > TS_LENGTH )
+ {
+ if ( name.charAt( TS_LENGTH ) != '/' )
+ {
+ throw new IllegalArgumentException(
+ "Improperly formatted name for audit log event, no / separator between timestamp and resource: " +
+ name );
+ }
+ }
+
this.repositoryId = repositoryId;
}
public String getName()
{
- return createNameFormat().format( timestamp );
+ // use the hashCode here to make it unique if multiple events occur at a certain timestamp. None of the other
+ // fields is unique on its own
+ return createNameFormat().format( timestamp ) + "/" + Integer.toHexString( hashCode() );
+ // TODO: a simple incremental counter might be better since it will retain ordering, but then we need to do a
+ // bit of locking...
}
private static SimpleDateFormat createNameFormat()
List<AuditRecord> records = new ArrayList<AuditRecord>();
for ( String repositoryId : repositoryIds )
{
- List<String> timestamps = metadataRepository.getMetadataFacets( repositoryId, AuditEvent.FACET_ID );
- for ( String timestamp : timestamps )
+ List<String> names = metadataRepository.getMetadataFacets( repositoryId, AuditEvent.FACET_ID );
+ for ( String name : names )
{
- records.add( new AuditRecord( repositoryId, timestamp ) );
+ records.add( new AuditRecord( repositoryId, name ) );
}
}
Collections.sort( records );
eventNames.put( TEST_REPO_ID_2, new ArrayList<String>() );
for ( int i = 0; i < numEvents; i++ )
{
- String name = createEventName( TIMESTAMP_FORMAT.parse( AUDIT_EVENT_BASE + MILLIS_FORMAT.format( i ) ) );
String repositoryId = i % 2 == 0 ? TEST_REPO_ID : TEST_REPO_ID_2;
- eventNames.get( repositoryId ).add( name );
- events.add( createTestEvent( repositoryId, name ) );
+ String num = MILLIS_FORMAT.format( i );
+ AuditEvent event = createEvent( repositoryId, AUDIT_EVENT_BASE + num, getDefaultTestResourceName( num ) );
+ events.add( event );
+ eventNames.get( repositoryId ).add( event.getName() );
}
metadataRepositoryControl.expectAndReturn(
}
private static String createEventName( Date timestamp )
+ throws ParseException
{
- AuditEvent event = new AuditEvent();
- event.setTimestamp( timestamp );
- return event.getName();
+ // TODO: I think we can reverse the calls.
+ return createEvent( TEST_REPO_ID, TIMESTAMP_FORMAT.format( timestamp ), null ).getName();
}
private static AuditEvent createTestEvent( String name )
private static AuditEvent createTestEvent( String repoId, String name )
throws ParseException
{
- return createEvent( repoId, name, getDefaultTestResourceName( name.substring( name.length() - 3 ) ) );
+ return createEvent( repoId, name, getDefaultTestResourceName(
+ name.substring( AUDIT_EVENT_BASE.length(), AUDIT_EVENT_BASE.length() + 3 ) ) );
}
private static AuditEvent createEvent( String repositoryId, String timestamp, String resource )