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.checksum.ChecksumAlgorithm;
24 import org.apache.archiva.checksum.ChecksummedFile;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.archiva.consumers.ConsumerException;
29 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
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;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.List;
44 * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
46 @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
48 public class ArtifactMissingChecksumsConsumer
49 extends AbstractMonitoredConsumer
50 implements KnownRepositoryContentConsumer
51 // it's prototype bean so we assume configuration won't change during a run
55 private Logger log = LoggerFactory.getLogger( ArtifactMissingChecksumsConsumer.class );
57 private String id = "create-missing-checksums";
59 private String description = "Create Missing and/or Fix Invalid Checksums (.sha1, .md5)";
61 private ArchivaConfiguration configuration;
63 private FileTypes filetypes;
65 private ChecksummedFile checksum;
67 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
69 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
71 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
73 private File repositoryDir;
75 private List<String> includes = new ArrayList<>( 0 );
78 public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration, FileTypes filetypes )
80 this.configuration = configuration;
81 this.filetypes = filetypes;
83 //configuration.addChangeListener( this );
89 public String getId( )
95 public String getDescription( )
97 return this.description;
101 public void beginScan( ManagedRepository repo, Date whenGathered )
102 throws ConsumerException
104 this.repositoryDir = new File( repo.getLocation( ) );
108 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
109 throws ConsumerException
111 beginScan( repo, whenGathered );
115 public void completeScan( )
121 public void completeScan( boolean executeOnEntireRepo )
127 public List<String> getExcludes( )
129 return getDefaultArtifactExclusions( );
133 public List<String> getIncludes( )
139 public void processFile( String path )
140 throws ConsumerException
142 createFixChecksum( path, ChecksumAlgorithm.SHA1 );
143 createFixChecksum( path, ChecksumAlgorithm.MD5 );
147 public void processFile( String path, boolean executeOnEntireRepo )
148 throws ConsumerException
153 private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm )
155 File artifactFile = new File( this.repositoryDir, path );
156 File checksumFile = new File( this.repositoryDir, path + "." + checksumAlgorithm.getExt( ) );
158 if ( checksumFile.exists( ) )
160 checksum = new ChecksummedFile( artifactFile );
163 if ( !checksum.isValidChecksum( checksumAlgorithm ) )
165 checksum.fixChecksums( new ChecksumAlgorithm[]{checksumAlgorithm} );
166 log.info( "Fixed checksum file {}", checksumFile.getAbsolutePath( ) );
167 triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath( ) );
170 catch ( IOException e )
172 log.error( "Cannot calculate checksum for file {} :", checksumFile, e );
173 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
174 ": " + e.getMessage( ) );
177 else if ( !checksumFile.exists( ) )
179 checksum = new ChecksummedFile( artifactFile );
182 checksum.createChecksum( checksumAlgorithm );
183 log.info( "Created missing checksum file {}", checksumFile.getAbsolutePath( ) );
184 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath( ) );
186 catch ( IOException e )
188 log.error( "Cannot create checksum for file {} :", checksumFile, e );
189 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
190 ": " + e.getMessage( ) );
195 log.warn( "Checksum file {} is not a file. ", checksumFile.getAbsolutePath( ) );
196 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
197 "Checksum file " + checksumFile.getAbsolutePath( ) + " is not a file." );
203 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
205 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
213 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
220 private void initIncludes( )
222 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
227 public void initialize( )
229 //configuration.addChangeListener( this );