]> source.dussan.org Git - archiva.git/commitdiff
avoid timing issues with audit logs
authorBrett Porter <brett@apache.org>
Tue, 16 Feb 2010 09:48:16 +0000 (09:48 +0000)
committerBrett Porter <brett@apache.org>
Tue, 16 Feb 2010 09:48:16 +0000 (09:48 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@910447 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/audit/AuditEvent.java
archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java

index 12189f31bc92315565af4b2c38b4972ef59c7d92..d6b50887505e97fe1130957f1d02b9e635eb141d 100644 (file)
@@ -117,6 +117,8 @@ public class AuditEvent
 
     private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
 
+    private static final int TS_LENGTH = TIMESTAMP_FORMAT.length();
+
     public AuditEvent()
     {
         /* do nothing */
@@ -124,14 +126,26 @@ public class AuditEvent
 
     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;
     }
 
@@ -211,7 +225,11 @@ public class AuditEvent
 
     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()
index 96065838eb70d49da013a333b47056763e40b691..1a5b1ef189476b150935fb365af31cfb62fd7672 100644 (file)
@@ -57,10 +57,10 @@ public class DefaultAuditManager
         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 );
index 3825ed062a64f9881c6ae9f697ed84bda52fefa8..f70f7b04ffc998c8475bd04f5c386e8c06651f8e 100644 (file)
@@ -178,10 +178,11 @@ public class AuditManagerTest
         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(
@@ -551,10 +552,10 @@ public class AuditManagerTest
     }
 
     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 )
@@ -566,7 +567,8 @@ public class AuditManagerTest
     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 )