1 package org.apache.maven.archiva.consumers.core;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.plexusbridge.DigesterUtils;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.maven.archiva.consumers.ConsumerException;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.codehaus.plexus.digest.ChecksumFile;
30 import org.codehaus.plexus.digest.Digester;
31 import org.codehaus.plexus.digest.DigesterException;
32 import org.springframework.context.annotation.Scope;
33 import org.springframework.stereotype.Service;
35 import javax.annotation.PostConstruct;
36 import javax.inject.Inject;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Date;
42 import java.util.Iterator;
43 import java.util.List;
46 * ValidateChecksumConsumer - validate the provided checksum against the file it represents.
49 * plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
50 * role-hint="validate-checksum"
51 * instantiation-strategy="per-lookup"
53 @Service( "knownRepositoryContentConsumer#validate-checksum" )
55 public class ValidateChecksumConsumer
56 extends AbstractMonitoredConsumer
57 implements KnownRepositoryContentConsumer
59 private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
61 private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
63 private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
65 private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
68 * plexus.configuration default-value="validate-checksums"
70 private String id = "validate-checksums";
73 * plexus.configuration default-value="Validate checksums against file."
75 private String description = "Validate checksums against file.";
80 private ChecksumFile checksum;
83 * plexus.requirement role="org.codehaus.plexus.digest.Digester"
85 private List<Digester> allDigesters;
88 private PlexusSisuBridge plexusSisuBridge;
91 private DigesterUtils digesterUtils;
93 private File repositoryDir;
95 private List<String> includes = new ArrayList<String>();
102 public String getDescription()
104 return this.description;
107 public boolean isPermanent()
112 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
113 throws ConsumerException
115 this.repositoryDir = new File( repository.getLocation() );
118 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
119 throws ConsumerException
121 beginScan( repository, whenGathered );
124 public void completeScan()
129 public void completeScan( boolean executeOnEntireRepo )
134 public List<String> getExcludes()
139 public List<String> getIncludes()
141 return this.includes;
144 public void processFile( String path )
145 throws ConsumerException
147 File checksumFile = new File( this.repositoryDir, path );
150 if ( !checksum.isValidChecksum( checksumFile ) )
152 triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
155 catch ( FileNotFoundException e )
157 triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage() );
159 catch ( DigesterException e )
161 triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
162 "Digester failure during checksum validation on " + checksumFile );
164 catch ( IOException e )
166 triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
170 public void processFile( String path, boolean executeOnEntireReDpo )
177 public void initialize()
178 throws PlexusSisuBridgeException
180 checksum = plexusSisuBridge.lookup( ChecksumFile.class );
181 List<Digester> allDigesters = new ArrayList<Digester>( digesterUtils.getAllDigesters() );
182 for ( Digester digester : allDigesters )
184 includes.add( "**/*" + digester.getFilenameExtension() );