You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by jo...@apache.org on 2007/10/11 18:43:16 UTC

svn commit: r583875 - in /maven/archiva/trunk/archiva-base: archiva-common/ archiva-common/src/main/java/org/apache/maven/archiva/common/utils/ archiva-common/src/test/java/org/apache/maven/archiva/common/ archiva-common/src/test/java/org/apache/maven/...

Author: joakime
Date: Thu Oct 11 09:43:13 2007
New Revision: 583875

URL: http://svn.apache.org/viewvc?rev=583875&view=rev
Log:
[MRM-533] Checksum files (sha1/md5) are not kept up to date on maven-metadata.xml files.
Grabbed checksum check/update logic out of ChecksumPolicy into standalone Checksums component.
Using new component in ChecksumPolicy and MetadatTools.


Added:
    maven/archiva/trunk/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/Checksums.java   (with props)
    maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java   (with props)
    maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/
    maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar   (with props)
    maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-bad
    maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-good
    maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-bad
    maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-good
Removed:
    maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/AllTests.java
    maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/AllTests.java
Modified:
    maven/archiva/trunk/archiva-base/archiva-common/pom.xml
    maven/archiva/trunk/archiva-base/archiva-policies/pom.xml
    maven/archiva/trunk/archiva-base/archiva-policies/src/main/java/org/apache/maven/archiva/policies/ChecksumPolicy.java
    maven/archiva/trunk/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java
    maven/archiva/trunk/archiva-base/archiva-repository-layer/src/test/resources/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.xml

Modified: maven/archiva/trunk/archiva-base/archiva-common/pom.xml
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-common/pom.xml?rev=583875&r1=583874&r2=583875&view=diff
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-common/pom.xml (original)
+++ maven/archiva/trunk/archiva-base/archiva-common/pom.xml Thu Oct 11 09:43:13 2007
@@ -40,6 +40,10 @@
     </dependency>
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-digest</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-component-api</artifactId>
     </dependency>
     <dependency>

Added: maven/archiva/trunk/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/Checksums.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/Checksums.java?rev=583875&view=auto
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/Checksums.java (added)
+++ maven/archiva/trunk/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/Checksums.java Thu Oct 11 09:43:13 2007
@@ -0,0 +1,238 @@
+package org.apache.maven.archiva.common.utils;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.codehaus.plexus.digest.ChecksumFile;
+import org.codehaus.plexus.digest.Digester;
+import org.codehaus.plexus.digest.DigesterException;
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+/**
+ * Checksums utility component to validate or update checksums on Files. 
+ *
+ * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
+ * @version $Id$
+ * 
+ * @plexus.component role="org.apache.maven.archiva.common.utils.Checksums"
+ */
+public class Checksums
+    extends AbstractLogEnabled
+{
+    /**
+     * @plexus.requirement role-hint="sha1"
+     */
+    private Digester digestSha1;
+
+    /**
+     * @plexus.requirement role-hint="md5"
+     */
+    private Digester digestMd5;
+
+    /**
+     * @plexus.requirement
+     */
+    private ChecksumFile checksumFile;
+
+    public boolean check( File file )
+    {
+        boolean checksPass = true;
+
+        File sha1File = getSha1File( file );
+        File md5File = getMd5File( file );
+
+        // Both files missing is a failure.
+        if ( !sha1File.exists() && !md5File.exists() )
+        {
+            getLogger().error( "File " + file.getPath() + " has no checksum files (sha1 or md5)." );
+            checksPass = false;
+        }
+
+        if ( sha1File.exists() )
+        {
+            // Bad sha1 checksum is a failure.
+            if ( !validateChecksum( sha1File, "sha1" ) )
+            {
+                getLogger().warn( "SHA1 is incorrect for " + file.getPath() );
+                checksPass = false;
+            }
+        }
+
+        if ( md5File.exists() )
+        {
+            // Bad md5 checksum is a failure.
+            if ( !validateChecksum( md5File, "md5" ) )
+            {
+                getLogger().warn( "MD5 is incorrect for " + file.getPath() );
+                checksPass = false;
+            }
+        }
+
+        // TODO: eek!
+        if ( !checksPass )
+        {
+            // On failure. delete files.
+            if ( sha1File.exists() )
+            {
+                sha1File.delete();
+            }
+
+            if ( md5File.exists() )
+            {
+                md5File.delete();
+            }
+
+            file.delete();
+        }
+
+        return checksPass;
+    }
+
+    public boolean update( File file )
+    {
+        boolean checksPass = true;
+
+        File sha1File = getSha1File( file );
+        File md5File = getMd5File( file );
+
+        if ( !fixChecksum( file, sha1File, digestSha1 ) )
+        {
+            checksPass = false;
+        }
+
+        if ( !fixChecksum( file, md5File, digestMd5 ) )
+        {
+            checksPass = false;
+        }
+
+        return checksPass;
+    }
+
+    private boolean createChecksum( File localFile, Digester digester )
+    {
+        try
+        {
+            checksumFile.createChecksum( localFile, digester );
+            return true;
+        }
+        catch ( DigesterException e )
+        {
+            getLogger().warn( "Unable to create " + digester.getFilenameExtension() + " file: " + e.getMessage(), e );
+            return false;
+        }
+        catch ( IOException e )
+        {
+            getLogger().warn( "Unable to create " + digester.getFilenameExtension() + " file: " + e.getMessage(), e );
+            return false;
+        }
+    }
+
+    private boolean fixChecksum( File localFile, File hashFile, Digester digester )
+    {
+        String ext = digester.getFilenameExtension();
+
+        if ( !hashFile.getPath().endsWith( ext ) )
+        {
+            throw new IllegalArgumentException( "Cannot fix " + hashFile.getPath() + " using " + ext + " digester." );
+        }
+
+        // If hashfile doesn't exist, create it.
+        if ( !hashFile.exists() )
+        {
+            return createChecksum( localFile, digester );
+        }
+
+        // Validate checksum, if bad, recreate it.
+        try
+        {
+            if ( checksumFile.isValidChecksum( hashFile ) )
+            {
+                getLogger().debug( "Valid checksum: " + hashFile.getPath() );
+                return true;
+            }
+            else
+            {
+                getLogger().debug( "Not valid checksum: " + hashFile.getPath() );
+                return createChecksum( localFile, digester );
+            }
+        }
+        catch ( FileNotFoundException e )
+        {
+            getLogger().warn( "Unable to find " + ext + " file: " + hashFile.getAbsolutePath(), e );
+            return false;
+        }
+        catch ( DigesterException e )
+        {
+            getLogger().warn( "Unable to process " + ext + " file: " + hashFile.getAbsolutePath(), e );
+            return false;
+        }
+        catch ( IOException e )
+        {
+            getLogger().warn( "Unable to process " + ext + " file: " + hashFile.getAbsolutePath(), e );
+            return false;
+        }
+    }
+
+    private File getMd5File( File file )
+    {
+        return new File( file.getAbsolutePath() + ".md5" );
+    }
+
+    private File getSha1File( File file )
+    {
+        return new File( file.getAbsolutePath() + ".sha1" );
+
+    }
+
+    private boolean validateChecksum( File hashFile, String type )
+    {
+        try
+        {
+            boolean validity = checksumFile.isValidChecksum( hashFile );
+            if ( validity )
+            {
+                getLogger().debug( "Valid checksum: " + hashFile.getPath() );
+            }
+            else
+            {
+                getLogger().debug( "Not valid checksum: " + hashFile.getPath() );
+            }
+            return validity;
+        }
+        catch ( FileNotFoundException e )
+        {
+            getLogger().warn( "Unable to find " + type + " file: " + hashFile.getAbsolutePath(), e );
+            return false;
+        }
+        catch ( DigesterException e )
+        {
+            getLogger().warn( "Unable to process " + type + " file: " + hashFile.getAbsolutePath(), e );
+            return false;
+        }
+        catch ( IOException e )
+        {
+            getLogger().warn( "Unable to process " + type + " file: " + hashFile.getAbsolutePath(), e );
+            return false;
+        }
+    }
+}

Propchange: maven/archiva/trunk/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/Checksums.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/archiva/trunk/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/Checksums.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: maven/archiva/trunk/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/Checksums.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java?rev=583875&view=auto
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java (added)
+++ maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java Thu Oct 11 09:43:13 2007
@@ -0,0 +1,291 @@
+package org.apache.maven.archiva.common.utils;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+
+/**
+ * ChecksumsTest 
+ *
+ * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class ChecksumsTest
+    extends PlexusTestCase
+{
+    private static final String GOOD = "good";
+
+    private static final String BAD = "bad";
+
+    public void testCheckOnFileOnly()
+        throws Exception
+    {
+        assertCheck( false, null, null );
+    }
+
+    public void testCheckOnFileWithBadMd5AndBadSha1()
+        throws Exception
+    {
+        assertCheck( false, BAD, BAD );
+    }
+
+    public void testCheckOnFileWithBadMd5AndGoodSha1()
+        throws Exception
+    {
+        assertCheck( false, BAD, GOOD );
+    }
+
+    public void testCheckOnFileWithBadMd5Only()
+        throws Exception
+    {
+        assertCheck( false, BAD, null );
+    }
+
+    public void testCheckOnFileWithBadSha1Only()
+        throws Exception
+    {
+        assertCheck( false, null, BAD );
+    }
+
+    public void testCheckOnFileWithGoodMd5AndBadSha1()
+        throws Exception
+    {
+        assertCheck( false, GOOD, BAD );
+    }
+
+    public void testCheckOnFileWithGoodMd5AndGoodSha1()
+        throws Exception
+    {
+        assertCheck( true, GOOD, GOOD );
+    }
+
+    public void testCheckOnFileWithGoodMd5Only()
+        throws Exception
+    {
+        assertCheck( true, GOOD, null );
+    }
+
+    public void testCheckOnFileWithGoodSha1Only()
+        throws Exception
+    {
+        assertCheck( true, null, GOOD );
+    }
+
+    public void testUpdateOnFileOnly()
+        throws Exception
+    {
+        assertUpdate( true, null, null );
+    }
+
+    public void testUpdateOnFileWithBadMd5AndBadSha1()
+        throws Exception
+    {
+        assertUpdate( true, BAD, BAD );
+    }
+
+    public void testUpdateOnFileWithBadMd5AndGoodSha1()
+        throws Exception
+    {
+        assertUpdate( true, BAD, GOOD );
+    }
+
+    public void testUpdateOnFileWithBadMd5Only()
+        throws Exception
+    {
+        assertUpdate( true, BAD, null );
+    }
+
+    public void testUpdateOnFileWithBadSha1Only()
+        throws Exception
+    {
+        assertUpdate( true, null, BAD );
+    }
+
+    public void testUpdateOnFileWithGoodMd5AndBadSha1()
+        throws Exception
+    {
+        assertUpdate( true, GOOD, BAD );
+    }
+
+    public void testUpdateOnFileWithGoodMd5AndGoodSha1()
+        throws Exception
+    {
+        assertUpdate( true, GOOD, GOOD );
+    }
+
+    public void testUpdateOnFileWithGoodMd5Only()
+        throws Exception
+    {
+        assertUpdate( true, GOOD, null );
+    }
+
+    public void testUpdateOnFileWithGoodSha1Only()
+        throws Exception
+    {
+        assertUpdate( true, null, GOOD );
+    }
+
+    private void assertCheck( boolean expectedResult, String md5State, String sha1State )
+        throws Exception
+    {
+        Checksums checksums = lookupChecksums();
+        File localFile = createTestableFiles( md5State, sha1State );
+
+        boolean actualResult = checksums.check( localFile );
+        String msg = createMessage( "check", md5State, sha1State );
+
+        if ( actualResult == false )
+        {
+            assertFalse( msg + " local file should not exist:", localFile.exists() );
+            File md5File = new File( localFile.getAbsolutePath() + ".sha1" );
+            File sha1File = new File( localFile.getAbsolutePath() + ".md5" );
+            assertFalse( msg + " local md5 file should not exist:", md5File.exists() );
+            assertFalse( msg + " local sha1 file should not exist:", sha1File.exists() );
+        }
+
+        assertEquals( msg, expectedResult, actualResult );
+    }
+
+    private void assertUpdate( boolean expectedResult, String md5State, String sha1State )
+        throws Exception
+    {
+        Checksums checksums = lookupChecksums();
+        File localFile = createTestableFiles( md5State, sha1State );
+
+        boolean actualResult = checksums.update( localFile );
+        String msg = createMessage( "update", md5State, sha1State );
+        assertEquals( msg, expectedResult, actualResult );
+
+        // End result should be legitimate SHA1 and MD5 files.
+        File md5File = new File( localFile.getAbsolutePath() + ".md5" );
+        File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
+
+        assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", md5File.exists() && md5File.isFile() );
+        assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", sha1File.exists() && sha1File.isFile() );
+
+        String actualMd5Contents = readChecksumFile( md5File );
+        String actualSha1Contents = readChecksumFile( sha1File );
+
+        String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548  artifact.jar";
+        String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f  artifact.jar";
+
+        assertEquals( msg + ": md5 contents:", expectedMd5Contents, actualMd5Contents );
+        assertEquals( msg + ": sha1 contents:", expectedSha1Contents, actualSha1Contents );
+    }
+
+    /**
+     * Read the first line from the checksum file, and return it (trimmed).
+     */
+    private String readChecksumFile( File checksumFile )
+        throws Exception
+    {
+        FileReader freader = null;
+        BufferedReader buf = null;
+
+        try
+        {
+            freader = new FileReader( checksumFile );
+            buf = new BufferedReader( freader );
+            return buf.readLine();
+        }
+        finally
+        {
+            if ( buf != null )
+            {
+                buf.close();
+            }
+
+            if ( freader != null )
+            {
+                freader.close();
+            }
+        }
+    }
+
+    private String createMessage( String method, String md5State, String sha1State )
+    {
+        StringBuffer msg = new StringBuffer();
+        msg.append( "Expected result of Checksums." ).append( method );
+        msg.append( "() when working with " );
+        if ( md5State == null )
+        {
+            msg.append( "NO" );
+        }
+        else
+        {
+            msg.append( "a " ).append( md5State.toUpperCase() );
+        }
+
+        msg.append( " MD5 and " );
+
+        if ( sha1State == null )
+        {
+            msg.append( "NO" );
+        }
+        else
+        {
+            msg.append( "a " ).append( sha1State.toUpperCase() );
+        }
+        msg.append( " SHA1:" );
+
+        return msg.toString();
+    }
+
+    private File createTestableFiles( String md5State, String sha1State )
+        throws Exception
+    {
+        File sourceDir = new File( "src/test/resources/checksums/" );
+        File destDir = new File( "target/checksum-tests/" + getName() + "/" );
+
+        FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
+
+        if ( md5State != null )
+        {
+            File md5File = new File( sourceDir, "artifact.jar.md5-" + md5State );
+            assertTrue( "Testable file exists: " + md5File.getName() + ":", md5File.exists() && md5File.isFile() );
+            File destFile = new File( destDir, "artifact.jar.md5" );
+            FileUtils.copyFile( md5File, destFile );
+        }
+
+        if ( sha1State != null )
+        {
+            File sha1File = new File( sourceDir, "artifact.jar.sha1-" + sha1State );
+            assertTrue( "Testable file exists: " + sha1File.getName() + ":", sha1File.exists() && sha1File.isFile() );
+            File destFile = new File( destDir, "artifact.jar.sha1" );
+            FileUtils.copyFile( sha1File, destFile );
+        }
+
+        File localFile = new File( destDir, "artifact.jar" );
+        return localFile;
+    }
+
+    private Checksums lookupChecksums()
+        throws Exception
+    {
+        Checksums policy = (Checksums) lookup( Checksums.class );
+        assertNotNull( policy );
+        return policy;
+    }
+}

Propchange: maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: maven/archiva/trunk/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/ChecksumsTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar?rev=583875&view=auto
==============================================================================
Binary file - no diff available.

Propchange: maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar
------------------------------------------------------------------------------
    svn:mime-type = application/java-archive

Added: maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-bad
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-bad?rev=583875&view=auto
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-bad (added)
+++ maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-bad Thu Oct 11 09:43:13 2007
@@ -0,0 +1 @@
+444ccc111aaa222999888eee222fff00  artifact.jar

Added: maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-good
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-good?rev=583875&view=auto
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-good (added)
+++ maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.md5-good Thu Oct 11 09:43:13 2007
@@ -0,0 +1 @@
+360ccd01d8a0a2d94b86f9802c2fc548  artifact.jar

Added: maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-bad
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-bad?rev=583875&view=auto
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-bad (added)
+++ maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-bad Thu Oct 11 09:43:13 2007
@@ -0,0 +1 @@
+ddd888999000444888bbbaaa555333999777eee0  artifact.jar

Added: maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-good
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-good?rev=583875&view=auto
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-good (added)
+++ maven/archiva/trunk/archiva-base/archiva-common/src/test/resources/checksums/artifact.jar.sha1-good Thu Oct 11 09:43:13 2007
@@ -0,0 +1 @@
+7dd8929150664f182db60ad15f20359d875f059f  artifact.jar

Modified: maven/archiva/trunk/archiva-base/archiva-policies/pom.xml
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-policies/pom.xml?rev=583875&r1=583874&r2=583875&view=diff
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-policies/pom.xml (original)
+++ maven/archiva/trunk/archiva-base/archiva-policies/pom.xml Thu Oct 11 09:43:13 2007
@@ -37,10 +37,6 @@
       <artifactId>commons-lang</artifactId>
     </dependency>
     <dependency>
-      <groupId>org.codehaus.plexus</groupId>
-      <artifactId>plexus-digest</artifactId>
-    </dependency>
-    <dependency>
       <groupId>org.codehaus.plexus.cache</groupId>
       <artifactId>plexus-cache-ehcache</artifactId>
     </dependency>

Modified: maven/archiva/trunk/archiva-base/archiva-policies/src/main/java/org/apache/maven/archiva/policies/ChecksumPolicy.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-policies/src/main/java/org/apache/maven/archiva/policies/ChecksumPolicy.java?rev=583875&r1=583874&r2=583875&view=diff
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-policies/src/main/java/org/apache/maven/archiva/policies/ChecksumPolicy.java (original)
+++ maven/archiva/trunk/archiva-base/archiva-policies/src/main/java/org/apache/maven/archiva/policies/ChecksumPolicy.java Thu Oct 11 09:43:13 2007
@@ -19,14 +19,10 @@
  * under the License.
  */
 
-import org.codehaus.plexus.digest.ChecksumFile;
-import org.codehaus.plexus.digest.Digester;
-import org.codehaus.plexus.digest.DigesterException;
+import org.apache.maven.archiva.common.utils.Checksums;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
@@ -59,21 +55,11 @@
     public static final String FIX = "fix";
 
     /**
-     * @plexus.requirement role-hint="sha1"
-     */
-    private Digester digestSha1;
-
-    /**
-     * @plexus.requirement role-hint="md5"
-     */
-    private Digester digestMd5;
-
-    /**
      * @plexus.requirement
      */
-    private ChecksumFile checksumFile;
+    private Checksums checksums;
 
-    private List options = new ArrayList();
+    private List<String> options = new ArrayList<String>();
 
     public ChecksumPolicy()
     {
@@ -104,42 +90,14 @@
             return false;
         }
 
-        File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
-        File md5File = new File( localFile.getAbsolutePath() + ".md5" );
-
         if ( FAIL.equals( policySetting ) )
         {
-            boolean checksPass = true;
-
-            // Both files missing is a failure.
-            if ( !sha1File.exists() && !md5File.exists() )
+            boolean checksPass = checksums.check( localFile ); 
+            if( ! checksPass )
             {
-                getLogger().error( "File " + localFile.getPath() + " has no checksum files (sha1 or md5)." );
-                checksPass = false;
-            }
+                File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
+                File md5File = new File( localFile.getAbsolutePath() + ".md5" );
 
-            if ( sha1File.exists() )
-            {
-                // Bad sha1 checksum is a failure.
-                if ( !validateChecksum( sha1File, "sha1" ) )
-                {
-                    getLogger().warn( "SHA1 is incorrect for " + localFile.getPath() );
-                    checksPass = false;
-                }
-            }
-
-            if ( md5File.exists() )
-            {
-                // Bad md5 checksum is a failure.
-                if ( !validateChecksum( md5File, "md5" ) )
-                {
-                    getLogger().warn( "MD5 is incorrect for " + localFile.getPath() );
-                    checksPass = false;
-                }
-            }
-
-            if ( !checksPass )
-            {
                 // On failure. delete files.
                 if ( sha1File.exists() )
                 {
@@ -159,122 +117,13 @@
 
         if ( FIX.equals( policySetting ) )
         {
-            boolean checksPass = true;
-
-            if ( !fixChecksum( localFile, sha1File, digestSha1 ) )
-            {
-                checksPass = false;
-            }
-
-            if ( !fixChecksum( localFile, md5File, digestMd5 ) )
-            {
-                checksPass = false;
-            }
-
-            return checksPass;
+            return checksums.update( localFile );
         }
 
         getLogger().error( "Unhandled policyCode [" + policySetting + "]" );
         return false;
     }
 
-    private boolean createChecksum( File localFile, Digester digester )
-    {
-        try
-        {
-            checksumFile.createChecksum( localFile, digester );
-            return true;
-        }
-        catch ( DigesterException e )
-        {
-            getLogger().warn( "Unable to create " + digester.getFilenameExtension() + " file: " + e.getMessage(), e );
-            return false;
-        }
-        catch ( IOException e )
-        {
-            getLogger().warn( "Unable to create " + digester.getFilenameExtension() + " file: " + e.getMessage(), e );
-            return false;
-        }
-    }
-
-    private boolean fixChecksum( File localFile, File hashFile, Digester digester )
-    {
-        String ext = digester.getFilenameExtension();
-
-        if ( !hashFile.getPath().endsWith( ext ) )
-        {
-            throw new IllegalArgumentException( "Cannot fix " + hashFile.getPath() + " using " + ext + " digester." );
-        }
-
-        // If hashfile doesn't exist, create it.
-        if ( !hashFile.exists() )
-        {
-            return createChecksum( localFile, digester );
-        }
-
-        // Validate checksum, if bad, recreate it.
-        try
-        {
-            if ( checksumFile.isValidChecksum( hashFile ) )
-            {
-                getLogger().debug( "Valid checksum: " + hashFile.getPath() );
-                return true;
-            }
-            else
-            {
-                getLogger().debug( "Not valid checksum: " + hashFile.getPath() );
-                return createChecksum( localFile, digester );
-            }
-        }
-        catch ( FileNotFoundException e )
-        {
-            getLogger().warn( "Unable to find " + ext + " file: " + hashFile.getAbsolutePath(), e );
-            return false;
-        }
-        catch ( DigesterException e )
-        {
-            getLogger().warn( "Unable to process " + ext + " file: " + hashFile.getAbsolutePath(), e );
-            return false;
-        }
-        catch ( IOException e )
-        {
-            getLogger().warn( "Unable to process " + ext + " file: " + hashFile.getAbsolutePath(), e );
-            return false;
-        }
-    }
-
-    private boolean validateChecksum( File hashFile, String type )
-    {
-        try
-        {
-            boolean validity = checksumFile.isValidChecksum( hashFile );
-            if ( validity )
-            {
-                getLogger().debug( "Valid checksum: " + hashFile.getPath() );
-            }
-            else
-            {
-                getLogger().debug( "Not valid checksum: " + hashFile.getPath() );
-            }
-            return validity;
-        }
-        catch ( FileNotFoundException e )
-        {
-            getLogger().warn( "Unable to find " + type + " file: " + hashFile.getAbsolutePath(), e );
-            return false;
-        }
-        catch ( DigesterException e )
-        {
-            getLogger().warn( "Unable to process " + type + " file: " + hashFile.getAbsolutePath(), e );
-            return false;
-        }
-        catch ( IOException e )
-        {
-            getLogger().warn( "Unable to process " + type + " file: " + hashFile.getAbsolutePath(), e );
-            return false;
-        }
-    }
-
     public String getDefaultOption()
     {
         return FIX;
@@ -285,9 +134,8 @@
         return "checksum";
     }
 
-    public List getOptions()
+    public List<String> getOptions()
     {
         return options;
     }
-
 }

Modified: maven/archiva/trunk/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java?rev=583875&r1=583874&r2=583875&view=diff
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java (original)
+++ maven/archiva/trunk/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java Thu Oct 11 09:43:13 2007
@@ -20,6 +20,7 @@
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.math.NumberUtils;
 import org.apache.commons.lang.time.DateUtils;
+import org.apache.maven.archiva.common.utils.Checksums;
 import org.apache.maven.archiva.common.utils.PathUtil;
 import org.apache.maven.archiva.common.utils.VersionComparator;
 import org.apache.maven.archiva.common.utils.VersionUtil;
@@ -88,6 +89,11 @@
      * @plexus.requirement
      */
     private FileTypes filetypes;
+    
+    /**
+     * @plexus.requirement
+     */
+    private Checksums checksums;
 
     private List<String> artifactPatterns;
 
@@ -485,6 +491,7 @@
 
         // Save the metadata model to disk.
         RepositoryMetadataWriter.write( metadata, metadataFile );
+        checksums.update( metadataFile );
     }
 
     private Date toLastUpdatedDate( long lastUpdated )
@@ -656,6 +663,7 @@
 
         // Save the metadata model to disk.
         RepositoryMetadataWriter.write( metadata, metadataFile );
+        checksums.update( metadataFile );
     }
 
     private void initConfigVariables()

Modified: maven/archiva/trunk/archiva-base/archiva-repository-layer/src/test/resources/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.xml
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-base/archiva-repository-layer/src/test/resources/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.xml?rev=583875&r1=583874&r2=583875&view=diff
==============================================================================
--- maven/archiva/trunk/archiva-base/archiva-repository-layer/src/test/resources/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.xml (original)
+++ maven/archiva/trunk/archiva-base/archiva-repository-layer/src/test/resources/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.xml Thu Oct 11 09:43:13 2007
@@ -35,6 +35,10 @@
           <field-name>filetypes</field-name>
         </requirement>
         <requirement>
+          <role>org.apache.maven.archiva.common.utils.Checksums</role>
+          <field-name>checksums</field-name>
+        </requirement>
+        <requirement>
           <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
           <role-hint>mock</role-hint>
           <field-name>configuration</field-name>