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.*;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.FileTypes;
25 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
26 import org.apache.archiva.consumers.ConsumerException;
27 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.archiva.repository.ManagedRepository;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.context.annotation.Scope;
32 import org.springframework.stereotype.Service;
34 import javax.annotation.PostConstruct;
35 import javax.inject.Inject;
36 import java.io.IOException;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.ArrayList;
41 import java.util.Date;
42 import java.util.List;
45 * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
47 @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
49 public class ArtifactMissingChecksumsConsumer
50 extends AbstractMonitoredConsumer
51 implements KnownRepositoryContentConsumer
52 // it's prototype bean so we assume configuration won't change during a run
56 private Logger log = LoggerFactory.getLogger( ArtifactMissingChecksumsConsumer.class );
58 private String id = "create-missing-checksums";
60 private String description = "Create Missing and/or Fix Invalid Checksum files.";
62 private ArchivaConfiguration configuration;
64 private FileTypes filetypes;
66 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
68 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
70 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
72 private Path repositoryDir;
74 private List<String> includes = new ArrayList<>( 0 );
75 private List<ChecksumAlgorithm> algorithms;
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 = Paths.get( 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 Path artifactPath = repositoryDir.resolve(path);
143 ChecksummedFile csFile = new ChecksummedFile(artifactPath);
144 UpdateStatusList result = csFile.fixChecksums(algorithms);
145 if (result.getTotalStatus()== UpdateStatus.ERROR) {
146 log.warn( "Error accessing file {}. ", path );
147 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
148 "Error accessing file " + path + "." );
150 result.getStatusList().stream().forEach(st ->
151 triggerInfo(path, st));
155 private void triggerInfo(String path, UpdateStatus status) {
156 switch (status.getValue()) {
157 case UpdateStatus.ERROR:
158 log.error( "Cannot create checksum for file {} :", path, status.getError() );
159 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + path +
160 ": " + status.getError().getMessage( ) );
162 case UpdateStatus.CREATED:
163 log.info( "Created missing checksum file {}", path );
164 triggerConsumerInfo( "Created missing checksum file " + path );
166 case UpdateStatus.UPDATED:
167 log.info( "Fixed checksum file {}", path );
168 triggerConsumerInfo( "Fixed checksum file " + path );
175 public void processFile( String path, boolean executeOnEntireRepo )
176 throws ConsumerException
184 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
186 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
194 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
201 private void initIncludes( )
203 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
208 public void initialize( )
210 //configuration.addChangeListener( this );
213 algorithms = ChecksumUtil.getAlgorithms(configuration.getConfiguration().getArchivaRuntimeConfiguration().getChecksumTypes());