1 package org.apache.maven.repository.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.repository.ArtifactRepository;
21 import org.apache.maven.model.Model;
22 import org.apache.maven.repository.digest.Digester;
23 import org.codehaus.plexus.util.FileUtils;
26 import java.io.IOException;
27 import java.security.NoSuchAlgorithmException;
30 * This class reports invalid and mismatched checksums of artifacts and metadata files.
31 * It validates MD5 and SHA-1 checksums.
33 * @plexus.component role="org.apache.maven.repository.reporting.ArtifactReportProcessor" role-hint="checksum"
35 public class ChecksumArtifactReporter
36 implements ArtifactReportProcessor
38 /** @plexus.requirement */
39 private Digester digester;
42 * Validate the checksum of the specified artifact.
49 public void processArtifact( Model model, Artifact artifact, ArtifactReporter reporter,
50 ArtifactRepository repository )
52 if ( !"file".equals( repository.getProtocol() ) )
54 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
55 throw new UnsupportedOperationException(
56 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
59 //check if checksum files exist
60 String path = repository.pathOf( artifact );
61 File file = new File( repository.getBasedir(), path );
63 File md5File = new File( repository.getBasedir(), path + ".md5" );
64 if ( md5File.exists() )
68 if ( digester.verifyChecksum( file, FileUtils.fileRead( md5File ), Digester.MD5 ) )
70 reporter.addSuccess( artifact );
74 reporter.addFailure( artifact, "MD5 checksum does not match." );
77 catch ( NoSuchAlgorithmException e )
79 reporter.addFailure( artifact, "Unable to read MD5: " + e.getMessage() );
81 catch ( IOException e )
83 reporter.addFailure( artifact, "Unable to read MD5: " + e.getMessage() );
88 reporter.addFailure( artifact, "MD5 checksum file does not exist." );
91 File sha1File = new File( repository.getBasedir(), path + ".sha1" );
92 if ( sha1File.exists() )
96 if ( digester.verifyChecksum( file, FileUtils.fileRead( sha1File ), Digester.SHA1 ) )
98 reporter.addSuccess( artifact );
102 reporter.addFailure( artifact, "SHA-1 checksum does not match." );
105 catch ( NoSuchAlgorithmException e )
107 reporter.addFailure( artifact, "Unable to read SHA-1: " + e.getMessage() );
109 catch ( IOException e )
111 reporter.addFailure( artifact, "Unable to read SHA-1: " + e.getMessage() );
116 reporter.addFailure( artifact, "SHA-1 checksum file does not exist." );