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.personality.plexus.lifecycle.phase.Initializable;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
33 import org.codehaus.plexus.registry.Registry;
34 import org.codehaus.plexus.registry.RegistryListener;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Date;
40 import java.util.List;
43 * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
47 public class ArtifactMissingChecksumsConsumer
48 extends AbstractMonitoredConsumer
49 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
53 private String description;
55 private ArchivaConfiguration configuration;
57 private FileTypes filetypes;
59 private ChecksummedFile checksum;
61 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
63 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
65 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
67 private File repositoryDir;
69 private List<String> includes = new ArrayList<String>();
71 public ArtifactMissingChecksumsConsumer(String id,
73 ArchivaConfiguration configuration,
74 FileTypes filetypes) {
76 this.description = description;
77 this.configuration = configuration;
78 this.filetypes = filetypes;
86 public String getDescription()
88 return this.description;
91 public boolean isPermanent()
96 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
97 throws ConsumerException
99 this.repositoryDir = new File( repo.getLocation() );
102 public void completeScan()
107 public List<String> getExcludes()
109 return getDefaultArtifactExclusions();
112 public List<String> getIncludes()
117 public void processFile( String path )
118 throws ConsumerException
120 createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } );
121 createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5 } );
124 private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
126 File artifactFile = new File( this.repositoryDir, path );
127 File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
129 if( checksumFile.exists() )
131 checksum = new ChecksummedFile( artifactFile );
134 if( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
136 checksum.fixChecksums( checksumAlgorithm );
137 triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
140 catch ( IOException e )
142 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
143 ": " + e.getMessage() );
146 else if( !checksumFile.exists() )
148 checksum = new ChecksummedFile( artifactFile );
151 checksum.createChecksum( checksumAlgorithm[0] );
152 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
154 catch ( IOException e )
156 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
157 ": " + e.getMessage() );
162 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
163 "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
167 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
169 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
175 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
180 private void initIncludes()
184 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
187 public void initialize()
188 throws InitializationException
190 configuration.addChangeListener( this );