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.repository.ArtifactRepository;
20 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
21 import org.apache.maven.repository.digest.Digester;
22 import org.apache.maven.repository.digest.DigesterException;
23 import org.codehaus.plexus.util.FileUtils;
26 import java.io.IOException;
29 * This class reports invalid and mismatched checksums of artifacts and metadata files.
30 * It validates MD5 and SHA-1 checksums.
32 * @plexus.component role="org.apache.maven.repository.reporting.MetadataReportProcessor" role-hint="checksum-metadata"
34 public class ChecksumMetadataReporter
35 implements MetadataReportProcessor
40 private Digester digester;
43 * Validate the checksums of the metadata. Get the metadata file from the
44 * repository then validate the checksum.
46 public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter )
48 if ( !"file".equals( repository.getProtocol() ) )
50 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
51 throw new UnsupportedOperationException(
52 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
55 //check if checksum files exist
56 String path = repository.pathOfRemoteRepositoryMetadata( metadata );
57 File file = new File( repository.getBasedir(), path );
59 verifyChecksum( repository, path + ".md5", file, Digester.MD5, reporter, metadata );
60 verifyChecksum( repository, path + ".sha1", file, Digester.SHA1, reporter, metadata );
64 private void verifyChecksum( ArtifactRepository repository, String path, File file, String checksumAlgorithm,
65 ArtifactReporter reporter, RepositoryMetadata metadata )
67 File checksumFile = new File( repository.getBasedir(), path );
68 if ( checksumFile.exists() )
72 digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm );
74 reporter.addSuccess( metadata );
76 catch ( DigesterException e )
78 reporter.addFailure( metadata, e.getMessage() );
80 catch ( IOException e )
82 reporter.addFailure( metadata, "Read file error: " + e.getMessage() );
87 reporter.addFailure( metadata, checksumAlgorithm + " checksum file does not exist." );