1 package org.apache.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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.plexusbridge.DigesterUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
26 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.archiva.consumers.ConsumerException;
28 import org.apache.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.List;
45 * ValidateChecksumConsumer - validate the provided checksum against the file it represents.
49 @Service( "knownRepositoryContentConsumer#validate-checksums" )
51 public class ValidateChecksumConsumer
52 extends AbstractMonitoredConsumer
53 implements KnownRepositoryContentConsumer
55 private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
57 private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
59 private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
61 private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
64 * default-value="validate-checksums"
66 private String id = "validate-checksums";
69 * default-value="Validate checksums against file."
71 private String description = "Validate checksums against file.";
76 private ChecksumFile checksum;
81 private List<Digester> allDigesters;
84 private PlexusSisuBridge plexusSisuBridge;
87 private DigesterUtils digesterUtils;
89 private File repositoryDir;
91 private List<String> includes;
100 public String getDescription()
102 return this.description;
106 public boolean isPermanent()
112 public void beginScan( ManagedRepository repository, Date whenGathered )
113 throws ConsumerException
115 this.repositoryDir = new File( repository.getLocation() );
119 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
120 throws ConsumerException
122 beginScan( repository, whenGathered );
126 public void completeScan()
132 public void completeScan( boolean executeOnEntireRepo )
138 public List<String> getExcludes()
144 public List<String> getIncludes()
146 return this.includes;
150 public void processFile( String path )
151 throws ConsumerException
153 File checksumFile = new File( this.repositoryDir, path );
156 if ( !checksum.isValidChecksum( checksumFile ) )
158 triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
161 catch ( FileNotFoundException e )
163 triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage() );
165 catch ( DigesterException e )
167 triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
168 "Digester failure during checksum validation on " + checksumFile );
170 catch ( IOException e )
172 triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
177 public void processFile( String path, boolean executeOnEntireReDpo )
184 public void initialize()
185 throws PlexusSisuBridgeException
187 checksum = plexusSisuBridge.lookup( ChecksumFile.class );
188 List<Digester> allDigesters = new ArrayList<>( digesterUtils.getAllDigesters() );
189 includes = new ArrayList<>( allDigesters.size() );
190 for ( Digester digester : allDigesters )
192 includes.add( "**/*" + digester.getFilenameExtension() );