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.ChecksumAlgorithm;
23 import org.apache.archiva.checksum.ChecksummedFile;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.archiva.consumers.ConsumerException;
28 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.archiva.repository.ManagedRepository;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.context.annotation.Scope;
33 import org.springframework.stereotype.Service;
35 import javax.annotation.PostConstruct;
36 import javax.inject.Inject;
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.ArrayList;
42 import java.util.Date;
43 import java.util.List;
46 * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
48 @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
50 public class ArtifactMissingChecksumsConsumer
51 extends AbstractMonitoredConsumer
52 implements KnownRepositoryContentConsumer
53 // it's prototype bean so we assume configuration won't change during a run
57 private Logger log = LoggerFactory.getLogger( ArtifactMissingChecksumsConsumer.class );
59 private String id = "create-missing-checksums";
61 private String description = "Create Missing and/or Fix Invalid Checksums (.sha1, .md5)";
63 private ArchivaConfiguration configuration;
65 private FileTypes filetypes;
67 private ChecksummedFile checksum;
69 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
71 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
73 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
75 private Path repositoryDir;
77 private List<String> includes = new ArrayList<>( 0 );
80 public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration, FileTypes filetypes )
82 this.configuration = configuration;
83 this.filetypes = filetypes;
85 //configuration.addChangeListener( this );
91 public String getId( )
97 public String getDescription( )
99 return this.description;
103 public void beginScan( ManagedRepository repo, Date whenGathered )
104 throws ConsumerException
106 this.repositoryDir = Paths.get( repo.getLocation( ) );
110 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
111 throws ConsumerException
113 beginScan( repo, whenGathered );
117 public void completeScan( )
123 public void completeScan( boolean executeOnEntireRepo )
129 public List<String> getExcludes( )
131 return getDefaultArtifactExclusions( );
135 public List<String> getIncludes( )
141 public void processFile( String path )
142 throws ConsumerException
144 createFixChecksum( path, ChecksumAlgorithm.SHA1 );
145 createFixChecksum( path, ChecksumAlgorithm.MD5 );
149 public void processFile( String path, boolean executeOnEntireRepo )
150 throws ConsumerException
155 private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm )
157 Path artifactFile = repositoryDir.resolve(path);
158 Path checksumFile = repositoryDir.resolve(path + "." + checksumAlgorithm.getExt( ).get(0) );
160 if ( Files.exists(checksumFile) )
162 checksum = new ChecksummedFile( artifactFile);
163 if ( !checksum.isValidChecksum( checksumAlgorithm ) )
165 checksum.fixChecksums( new ChecksumAlgorithm[]{checksumAlgorithm} );
166 log.info( "Fixed checksum file {}", checksumFile.toAbsolutePath( ) );
167 triggerConsumerInfo( "Fixed checksum file " + checksumFile.toAbsolutePath( ) );
170 else if ( !Files.exists(checksumFile) )
172 checksum = new ChecksummedFile( artifactFile);
175 checksum.createChecksum( checksumAlgorithm );
176 log.info( "Created missing checksum file {}", checksumFile.toAbsolutePath( ) );
177 triggerConsumerInfo( "Created missing checksum file " + checksumFile.toAbsolutePath( ) );
179 catch ( IOException e )
181 log.error( "Cannot create checksum for file {} :", checksumFile, e );
182 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
183 ": " + e.getMessage( ) );
188 log.warn( "Checksum file {} is not a file. ", checksumFile.toAbsolutePath( ) );
189 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
190 "Checksum file " + checksumFile.toAbsolutePath( ) + " is not a file." );
196 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
198 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
206 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
213 private void initIncludes( )
215 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
220 public void initialize( )
222 //configuration.addChangeListener( this );