1 package org.apache.maven.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.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.ConfigurationNames;
26 import org.apache.maven.archiva.configuration.FileTypes;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
29 import org.apache.maven.archiva.consumers.ConsumerException;
30 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.codehaus.plexus.registry.Registry;
32 import org.codehaus.plexus.registry.RegistryListener;
34 import javax.annotation.PostConstruct;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.Date;
39 import java.util.List;
42 * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
46 public class ArtifactMissingChecksumsConsumer
47 extends AbstractMonitoredConsumer
48 implements KnownRepositoryContentConsumer, RegistryListener
52 private String description;
54 private ArchivaConfiguration configuration;
56 private FileTypes filetypes;
58 private ChecksummedFile checksum;
60 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
62 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
64 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
66 private File repositoryDir;
68 private List<String> includes = new ArrayList<String>();
70 public ArtifactMissingChecksumsConsumer(String id,
72 ArchivaConfiguration configuration,
73 FileTypes filetypes) {
75 this.description = description;
76 this.configuration = configuration;
77 this.filetypes = filetypes;
79 configuration.addChangeListener( this );
89 public String getDescription()
91 return this.description;
94 public boolean isPermanent()
99 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
100 throws ConsumerException
102 this.repositoryDir = new File( repo.getLocation() );
105 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered, boolean executeOnEntireRepo )
106 throws ConsumerException
108 beginScan( repo, whenGathered );
111 public void completeScan()
116 public void completeScan( boolean executeOnEntireRepo )
121 public List<String> getExcludes()
123 return getDefaultArtifactExclusions();
126 public List<String> getIncludes()
131 public void processFile( String path )
132 throws ConsumerException
134 createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } );
135 createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5 } );
138 public void processFile( String path, boolean executeOnEntireRepo )
139 throws ConsumerException
144 private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
146 File artifactFile = new File( this.repositoryDir, path );
147 File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
149 if( checksumFile.exists() )
151 checksum = new ChecksummedFile( artifactFile );
154 if( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
156 checksum.fixChecksums( checksumAlgorithm );
157 triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
160 catch ( IOException e )
162 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
163 ": " + e.getMessage() );
166 else if( !checksumFile.exists() )
168 checksum = new ChecksummedFile( artifactFile );
171 checksum.createChecksum( checksumAlgorithm[0] );
172 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
174 catch ( IOException e )
176 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
177 ": " + e.getMessage() );
182 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
183 "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
187 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
189 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
195 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
200 private void initIncludes()
204 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
208 public void initialize()
210 configuration.addChangeListener( this );