Author: brett
Date: Tue Feb 16 09:48:16 2010
New Revision: 910447

URL: http://svn.apache.org/viewvc?rev=910447&view=rev
Log:
avoid timing issues with audit logs

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

Modified: 
archiva/branches/MRM-1025/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/audit/AuditEvent.java
URL: 
http://svn.apache.org/viewvc/archiva/branches/MRM-1025/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/audit/AuditEvent.java?rev=910447&r1=910446&r2=910447&view=diff
==============================================================================
--- 
archiva/branches/MRM-1025/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/audit/AuditEvent.java
 (original)
+++ 
archiva/branches/MRM-1025/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/audit/AuditEvent.java
 Tue Feb 16 09:48:16 2010
@@ -117,6 +117,8 @@
 
     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 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 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()

Modified: 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
URL: 
http://svn.apache.org/viewvc/archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java?rev=910447&r1=910446&r2=910447&view=diff
==============================================================================
--- 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
 (original)
+++ 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
 Tue Feb 16 09:48:16 2010
@@ -57,10 +57,10 @@
         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 );

Modified: 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java
URL: 
http://svn.apache.org/viewvc/archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java?rev=910447&r1=910446&r2=910447&view=diff
==============================================================================
--- 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java
 (original)
+++ 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java
 Tue Feb 16 09:48:16 2010
@@ -178,10 +178,11 @@
         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 @@
     }
 
     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 @@
     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 )


Reply via email to