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.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.FileTypes;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
26 import org.apache.maven.archiva.consumers.ConsumerException;
27 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.codehaus.plexus.digest.ChecksumFile;
29 import org.codehaus.plexus.digest.Digester;
30 import org.codehaus.plexus.digest.DigesterException;
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.List;
42 * ArtifactMissingChecksumsConsumer - Create missing checksums for the artifact.
44 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
47 * role-hint="create-missing-checksums"
48 * instantiation-strategy="per-lookup"
50 public class ArtifactMissingChecksumsConsumer
51 extends AbstractMonitoredConsumer
52 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
55 * @plexus.configuration default-value="create-missing-checksums"
60 * @plexus.configuration default-value="Create Missing Checksums (.sha1 & .md5)"
62 private String description;
67 private ArchivaConfiguration configuration;
72 private FileTypes filetypes;
75 * @plexus.requirement role-hint="sha1"
77 private Digester digestSha1;
80 * @plexus.requirement role-hint="md5";
82 private Digester digestMd5;
87 private ChecksumFile checksum;
89 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
91 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
93 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
95 private File repositoryDir;
97 private List<String> propertyNameTriggers = new ArrayList<String>();
99 private List<String> includes = new ArrayList<String>();
101 public String getId()
106 public String getDescription()
108 return this.description;
111 public boolean isPermanent()
116 public void beginScan( ManagedRepositoryConfiguration repo )
117 throws ConsumerException
119 this.repositoryDir = new File( repo.getLocation() );
122 public void completeScan()
127 public List<String> getExcludes()
132 public List<String> getIncludes()
137 public void processFile( String path )
138 throws ConsumerException
140 createIfMissing( path, digestSha1 );
141 createIfMissing( path, digestMd5 );
144 private void createIfMissing( String path, Digester digester )
146 File checksumFile = new File( this.repositoryDir, path + digester.getFilenameExtension() );
147 if ( !checksumFile.exists() )
151 checksum.createChecksum( new File( this.repositoryDir, path ), digester );
152 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
154 catch ( DigesterException e )
156 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC,
157 "Cannot calculate checksum for file " + checksumFile + ": " + e.getMessage() );
159 catch ( IOException e )
161 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE,
162 "Cannot create checksum for file " + checksumFile + ": " + e.getMessage() );
165 else if ( !checksumFile.isFile() )
167 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
168 "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
173 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
175 if ( propertyNameTriggers.contains( propertyName ) )
181 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
186 private void initIncludes()
190 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
193 public void initialize()
194 throws InitializationException
196 propertyNameTriggers = new ArrayList<String>();
197 propertyNameTriggers.add( "repositoryScanning" );
198 propertyNameTriggers.add( "fileTypes" );
199 propertyNameTriggers.add( "fileType" );
200 propertyNameTriggers.add( "patterns" );
201 propertyNameTriggers.add( "pattern" );
203 configuration.addChangeListener( this );