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.checksum.ChecksumValidationException;
23 import org.apache.archiva.checksum.ChecksumValidator;
24 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
25 import org.apache.archiva.consumers.ConsumerException;
26 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.context.annotation.Scope;
31 import org.springframework.stereotype.Service;
33 import javax.annotation.PostConstruct;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.Date;
38 import java.util.List;
41 import static org.apache.archiva.checksum.ChecksumValidationException.ValidationError.*;
44 * ValidateChecksumConsumer - validate the provided checksum against the file it represents.
46 @Service( "knownRepositoryContentConsumer#validate-checksums" )
48 public class ValidateChecksumConsumer
49 extends AbstractMonitoredConsumer
50 implements KnownRepositoryContentConsumer
52 private Logger log = LoggerFactory.getLogger( ValidateChecksumConsumer.class );
54 private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
56 private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
58 private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
60 private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
62 private String id = "validate-checksums";
64 private String description = "Validate checksums against file.";
66 ThreadLocal<ChecksumValidator> validatorThreadLocal = new ThreadLocal<>();
68 private Path repositoryDir;
70 private List<String> includes;
73 public String getId( )
79 public String getDescription( )
81 return this.description;
85 public void beginScan( ManagedRepository repository, Date whenGathered )
86 throws ConsumerException
88 this.repositoryDir = Paths.get( repository.getLocation( ) );
92 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
93 throws ConsumerException
95 beginScan( repository, whenGathered );
99 public void completeScan( )
105 public void completeScan( boolean executeOnEntireRepo )
111 public List<String> getExcludes( )
117 public List<String> getIncludes( )
119 return this.includes;
123 public void processFile( String path )
124 throws ConsumerException
126 ChecksumValidator validator;
127 if ((validator=validatorThreadLocal.get())==null) {
128 validator = new ChecksumValidator();
129 validatorThreadLocal.set(validator);
131 Path checksumFile = this.repositoryDir.resolve( path );
135 if ( !validator.isValidChecksum( checksumFile ) )
137 log.warn( "The checksum for {} is invalid.", checksumFile );
138 triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
141 catch ( ChecksumValidationException e )
143 if (e.getErrorType()==READ_ERROR) {
144 log.error( "Checksum read error during validation on {}", checksumFile );
145 triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
146 } else if (e.getErrorType()==INVALID_FORMAT || e.getErrorType()==DIGEST_ERROR) {
147 log.error( "Digester failure during checksum validation on {}", checksumFile );
148 triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
149 "Digester failure during checksum validation on " + checksumFile );
150 } else if (e.getErrorType()==FILE_NOT_FOUND) {
151 log.error( "File not found during checksum validation: ", e );
152 triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage( ) );
158 public void processFile( String path, boolean executeOnEntireReDpo )
165 public void initialize( )
167 ChecksumValidator validator;
168 if ((validator=validatorThreadLocal.get())==null) {
169 validator = new ChecksumValidator();
170 validatorThreadLocal.set(validator);
172 Set<String> extensions = validator.getSupportedExtensions();
173 includes = new ArrayList<>( extensions.size() );
174 for ( String ext : extensions )
176 includes.add( "**/*." + ext );