1 package org.apache.archiva.reports.consumers;
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.archiva.metadata.model.ArtifactMetadata;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
30 import org.apache.archiva.reports.RepositoryProblemFacet;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.configuration.ConfigurationNames;
34 import org.apache.maven.archiva.configuration.FileTypes;
35 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
36 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
37 import org.apache.maven.archiva.consumers.ConsumerException;
38 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
39 import org.codehaus.plexus.registry.Registry;
40 import org.codehaus.plexus.registry.RegistryListener;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.context.annotation.Scope;
44 import org.springframework.stereotype.Service;
46 import javax.annotation.PostConstruct;
47 import javax.inject.Inject;
48 import javax.inject.Named;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.Date;
54 import java.util.List;
57 * Search the database of known SHA1 Checksums for potential duplicate artifacts.
59 * TODO: no need for this to be a scanner - we can just query the database / content repository to get a full list
63 @Service( "knownRepositoryContentConsumer#duplicate-artifacts" )
65 public class DuplicateArtifactsConsumer
66 extends AbstractMonitoredConsumer
67 implements KnownRepositoryContentConsumer, RegistryListener
69 private Logger log = LoggerFactory.getLogger( DuplicateArtifactsConsumer.class );
72 * default-value="duplicate-artifacts"
74 private String id = "duplicate-artifacts";
77 * default-value="Check for Duplicate Artifacts via SHA1 Checksums"
79 private String description = "Check for Duplicate Artifacts via SHA1 Checksums";
85 private ArchivaConfiguration configuration;
91 private FileTypes filetypes;
94 * FIXME: can be of other types
98 private RepositorySessionFactory repositorySessionFactory;
100 private List<String> includes = new ArrayList<String>();
102 private File repositoryDir;
104 private String repoId;
107 * FIXME: needs to be selected based on the repository in question
111 @Named( value = "repositoryPathTranslator#maven2" )
112 private RepositoryPathTranslator pathTranslator;
114 private RepositorySession repositorySession;
116 public String getId()
121 public String getDescription()
126 public boolean isPermanent()
131 public List<String> getIncludes()
136 public List<String> getExcludes()
138 return Collections.emptyList();
141 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
142 throws ConsumerException
144 repoId = repo.getId();
145 this.repositoryDir = new File( repo.getLocation() );
146 repositorySession = repositorySessionFactory.createSession();
149 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered, boolean executeOnEntireRepo )
150 throws ConsumerException
152 beginScan( repo, whenGathered );
155 public void processFile( String path )
156 throws ConsumerException
158 File artifactFile = new File( this.repositoryDir, path );
160 // TODO: would be quicker to somehow make sure it ran after the update database consumer, or as a part of that
161 // perhaps could use an artifact context that is retained for all consumers? First in can set the SHA-1
162 // alternatively this could come straight from the storage resolver, which could populate the artifact metadata
163 // in the later parse call with the desired checksum and use that
165 ChecksummedFile checksummedFile = new ChecksummedFile( artifactFile );
168 checksumSha1 = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
170 catch ( IOException e )
172 throw new ConsumerException( e.getMessage(), e );
175 MetadataRepository metadataRepository = repositorySession.getRepository();
177 List<ArtifactMetadata> results;
180 results = metadataRepository.getArtifactsByChecksum( repoId, checksumSha1 );
182 catch ( MetadataRepositoryException e )
184 throw new ConsumerException( e.getMessage(), e );
187 if ( CollectionUtils.isNotEmpty( results ) )
189 ArtifactMetadata originalArtifact;
192 originalArtifact = pathTranslator.getArtifactForPath( repoId, path );
194 catch ( Exception e )
196 log.warn( "Not reporting problem for invalid artifact in checksum check: " + e.getMessage() );
200 for ( ArtifactMetadata dupArtifact : results )
202 String id = path.substring( path.lastIndexOf( "/" ) + 1 );
203 if ( dupArtifact.getId().equals( id )
204 && dupArtifact.getNamespace().equals( originalArtifact.getNamespace() )
205 && dupArtifact.getProject().equals( originalArtifact.getProject() )
206 && dupArtifact.getVersion().equals( originalArtifact.getVersion() ) )
208 // Skip reference to itself.
209 if ( log.isDebugEnabled() )
211 log.debug( "Not counting duplicate for artifact " + dupArtifact + " for path " + path );
216 RepositoryProblemFacet problem = new RepositoryProblemFacet();
217 problem.setRepositoryId( repoId );
218 problem.setNamespace( originalArtifact.getNamespace() );
219 problem.setProject( originalArtifact.getProject() );
220 problem.setVersion( originalArtifact.getVersion() );
222 // FIXME: need to get the right storage resolver for the repository the dupe artifact is in, it might be
224 // FIXME: we need the project version here, not the artifact version
225 problem.setMessage( "Duplicate Artifact Detected: " + path + " <--> " + pathTranslator.toPath(
226 dupArtifact.getNamespace(), dupArtifact.getProject(), dupArtifact.getVersion(),
227 dupArtifact.getId() ) );
228 problem.setProblem( "duplicate-artifact" );
232 metadataRepository.addMetadataFacet( repoId, problem );
234 catch ( MetadataRepositoryException e )
236 throw new ConsumerException( e.getMessage(), e );
242 public void processFile( String path, boolean executeOnEntireRepo )
243 throws ConsumerException
248 public void completeScan()
250 repositorySession.close();
253 public void completeScan( boolean executeOnEntireRepo )
258 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
260 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
266 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
271 private void initIncludes()
275 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
279 public void initialize()
282 configuration.addChangeListener( this );