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.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
29 import org.codehaus.plexus.digest.ChecksumFile;
30 import org.codehaus.plexus.digest.Digester;
31 import org.codehaus.plexus.digest.DigesterException;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
33 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
34 import org.codehaus.plexus.registry.Registry;
35 import org.codehaus.plexus.registry.RegistryListener;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.List;
44 * ArtifactMissingChecksumsConsumer - Create missing checksums for the artifact.
46 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
48 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
49 * role-hint="create-missing-checksums"
50 * instantiation-strategy="per-lookup"
52 public class ArtifactMissingChecksumsConsumer
53 extends AbstractMonitoredConsumer
54 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
57 * @plexus.configuration default-value="create-missing-checksums"
62 * @plexus.configuration default-value="Create Missing Checksums (.sha1 & .md5)"
64 private String description;
69 private ArchivaConfiguration configuration;
74 private FileTypes filetypes;
77 * @plexus.requirement role="org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout"
79 private Map bidirectionalLayoutMap; // TODO: replace with new bidir-repo-layout-factory
82 * @plexus.requirement role-hint="sha1"
84 private Digester digestSha1;
87 * @plexus.requirement role-hint="md5";
89 private Digester digestMd5;
94 private ChecksumFile checksum;
96 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
98 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
100 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
102 private ManagedRepositoryConfiguration repository;
104 private File repositoryDir;
106 private BidirectionalRepositoryLayout layout;
108 private List<String> propertyNameTriggers = new ArrayList<String>();
110 private List<String> includes = new ArrayList<String>();
112 public String getId()
117 public String getDescription()
119 return this.description;
122 public boolean isPermanent()
127 public void beginScan( ManagedRepositoryConfiguration repository )
128 throws ConsumerException
130 this.repository = repository;
131 this.repositoryDir = new File( repository.getLocation() );
133 String layoutName = repository.getLayout();
134 if ( !bidirectionalLayoutMap.containsKey( layoutName ) )
136 throw new ConsumerException( "Unable to process repository with layout [" + layoutName +
137 "] as there is no corresponding " + BidirectionalRepositoryLayout.class.getName() +
138 " implementation available." );
141 this.layout = (BidirectionalRepositoryLayout) bidirectionalLayoutMap.get( layoutName );
144 public void completeScan()
149 public List<String> getExcludes()
154 public List<String> getIncludes()
159 public void processFile( String path )
160 throws ConsumerException
162 createIfMissing( path, digestSha1 );
163 createIfMissing( path, digestMd5 );
166 private void createIfMissing( String path, Digester digester )
168 File checksumFile = new File( this.repositoryDir, path + digester.getFilenameExtension() );
169 if ( !checksumFile.exists() )
173 checksum.createChecksum( new File( this.repositoryDir, path ), digester );
174 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
176 catch ( DigesterException e )
178 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC,
179 "Cannot calculate checksum for file " + checksumFile + ": " + e.getMessage() );
181 catch ( IOException e )
183 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE,
184 "Cannot create checksum for file " + checksumFile + ": " + e.getMessage() );
187 else if ( !checksumFile.isFile() )
189 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
190 "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
195 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
197 if ( propertyNameTriggers.contains( propertyName ) )
203 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
208 private void initIncludes()
212 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
215 public void initialize()
216 throws InitializationException
218 propertyNameTriggers = new ArrayList<String>();
219 propertyNameTriggers.add( "repositoryScanning" );
220 propertyNameTriggers.add( "fileTypes" );
221 propertyNameTriggers.add( "fileType" );
222 propertyNameTriggers.add( "patterns" );
223 propertyNameTriggers.add( "pattern" );
225 configuration.addChangeListener( this );