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;
36 import javax.annotation.PostConstruct;
37 import javax.inject.Inject;
39 import java.io.IOException;
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.
49 @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
51 public class ArtifactMissingChecksumsConsumer
52 extends AbstractMonitoredConsumer
53 implements KnownRepositoryContentConsumer, RegistryListener
55 private String id = "create-missing-checksums";
57 private String description = "Create Missing and/or Fix Invalid Checksums (.sha1, .md5)";
59 private ArchivaConfiguration configuration;
61 private FileTypes filetypes;
63 private ChecksummedFile checksum;
65 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
67 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
69 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
71 private File repositoryDir;
73 private List<String> includes = new ArrayList<String>( 0 );
76 public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration, FileTypes filetypes )
78 this.configuration = configuration;
79 this.filetypes = filetypes;
81 configuration.addChangeListener( this );
91 public String getDescription()
93 return this.description;
96 public boolean isPermanent()
101 public void beginScan( ManagedRepository repo, Date whenGathered )
102 throws ConsumerException
104 this.repositoryDir = new File( repo.getLocation() );
107 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
108 throws ConsumerException
110 beginScan( repo, whenGathered );
113 public void completeScan()
118 public void completeScan( boolean executeOnEntireRepo )
123 public List<String> getExcludes()
125 return getDefaultArtifactExclusions();
128 public List<String> getIncludes()
133 public void processFile( String path )
134 throws ConsumerException
136 createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
137 createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5 } );
140 public void processFile( String path, boolean executeOnEntireRepo )
141 throws ConsumerException
146 private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
148 File artifactFile = new File( this.repositoryDir, path );
149 File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
151 if ( checksumFile.exists() )
153 checksum = new ChecksummedFile( artifactFile );
156 if ( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
158 checksum.fixChecksums( checksumAlgorithm );
159 triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
162 catch ( IOException e )
164 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
165 ": " + e.getMessage() );
168 else if ( !checksumFile.exists() )
170 checksum = new ChecksummedFile( artifactFile );
173 checksum.createChecksum( checksumAlgorithm[0] );
174 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
176 catch ( IOException e )
178 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
179 ": " + e.getMessage() );
184 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
185 "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
189 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
191 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
197 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
202 private void initIncludes()
204 includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
209 public void initialize()
211 configuration.addChangeListener( this );