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.ConfigurationNames;
27 import org.apache.archiva.configuration.FileTypes;
28 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
29 import org.apache.archiva.consumers.ConsumerException;
30 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.archiva.redback.components.registry.Registry;
32 import org.apache.archiva.redback.components.registry.RegistryListener;
33 import org.springframework.context.annotation.Scope;
34 import org.springframework.stereotype.Service;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import javax.annotation.PostConstruct;
39 import javax.inject.Inject;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.List;
47 * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
51 @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
53 public class ArtifactMissingChecksumsConsumer
54 extends AbstractMonitoredConsumer
55 implements KnownRepositoryContentConsumer
56 // it's prototype bean so we assume configuration won't change during a run
60 private Logger log = LoggerFactory.getLogger( ArtifactMissingChecksumsConsumer.class );
62 private String id = "create-missing-checksums";
64 private String description = "Create Missing and/or Fix Invalid Checksums (.sha1, .md5)";
66 private ArchivaConfiguration configuration;
68 private FileTypes filetypes;
70 private ChecksummedFile checksum;
72 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
74 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
76 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
78 private File repositoryDir;
80 private List<String> includes = new ArrayList<>( 0 );
83 public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration, FileTypes filetypes )
85 this.configuration = configuration;
86 this.filetypes = filetypes;
88 //configuration.addChangeListener( this );
100 public String getDescription()
102 return this.description;
106 public boolean isPermanent()
112 public void beginScan( ManagedRepository repo, Date whenGathered )
113 throws ConsumerException
115 this.repositoryDir = new File( repo.getLocation() );
119 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
120 throws ConsumerException
122 beginScan( repo, whenGathered );
126 public void completeScan()
132 public void completeScan( boolean executeOnEntireRepo )
138 public List<String> getExcludes()
140 return getDefaultArtifactExclusions();
144 public List<String> getIncludes()
150 public void processFile( String path )
151 throws ConsumerException
153 createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
154 createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5 } );
158 public void processFile( String path, boolean executeOnEntireRepo )
159 throws ConsumerException
164 private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
166 File artifactFile = new File( this.repositoryDir, path );
167 File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
169 if ( checksumFile.exists() )
171 checksum = new ChecksummedFile( artifactFile );
174 if ( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
176 checksum.fixChecksums( checksumAlgorithm );
177 log.info( "Fixed checksum file {}", checksumFile.getAbsolutePath() );
178 triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
181 catch ( IOException e )
183 log.error( "Cannot calculate checksum for file {} :", checksumFile, e );
184 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
185 ": " + e.getMessage() );
188 else if ( !checksumFile.exists() )
190 checksum = new ChecksummedFile( artifactFile );
193 checksum.createChecksum( checksumAlgorithm[0] );
194 log.info( "Created missing checksum file {}", checksumFile.getAbsolutePath() );
195 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
197 catch ( IOException e )
199 log.error( "Cannot create checksum for file {} :", checksumFile, e );
200 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
201 ": " + e.getMessage() );
206 log.warn( "Checksum file {} is not a file. ", checksumFile.getAbsolutePath() );
207 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
208 "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
214 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
216 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
224 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
231 private void initIncludes()
233 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
238 public void initialize()
240 //configuration.addChangeListener( this );