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 * plexus.configuration default-value="duplicate-artifacts"
74 private String id = "duplicate-artifacts";
77 * plexus.configuration 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
99 private RepositorySessionFactory repositorySessionFactory;
101 private List<String> includes = new ArrayList<String>();
103 private File repositoryDir;
105 private String repoId;
108 * FIXME: needs to be selected based on the repository in question
110 * plexus.requirement role-hint="maven2"
113 @Named( value = "repositoryPathTranslator#maven2" )
114 private RepositoryPathTranslator pathTranslator;
116 private RepositorySession repositorySession;
118 public String getId()
123 public String getDescription()
128 public boolean isPermanent()
133 public List<String> getIncludes()
138 public List<String> getExcludes()
140 return Collections.emptyList();
143 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
144 throws ConsumerException
146 repoId = repo.getId();
147 this.repositoryDir = new File( repo.getLocation() );
148 repositorySession = repositorySessionFactory.createSession();
151 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered, boolean executeOnEntireRepo )
152 throws ConsumerException
154 beginScan( repo, whenGathered );
157 public void processFile( String path )
158 throws ConsumerException
160 File artifactFile = new File( this.repositoryDir, path );
162 // TODO: would be quicker to somehow make sure it ran after the update database consumer, or as a part of that
163 // perhaps could use an artifact context that is retained for all consumers? First in can set the SHA-1
164 // alternatively this could come straight from the storage resolver, which could populate the artifact metadata
165 // in the later parse call with the desired checksum and use that
167 ChecksummedFile checksummedFile = new ChecksummedFile( artifactFile );
170 checksumSha1 = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
172 catch ( IOException e )
174 throw new ConsumerException( e.getMessage(), e );
177 MetadataRepository metadataRepository = repositorySession.getRepository();
179 List<ArtifactMetadata> results;
182 results = metadataRepository.getArtifactsByChecksum( repoId, checksumSha1 );
184 catch ( MetadataRepositoryException e )
186 throw new ConsumerException( e.getMessage(), e );
189 if ( CollectionUtils.isNotEmpty( results ) )
191 ArtifactMetadata originalArtifact;
194 originalArtifact = pathTranslator.getArtifactForPath( repoId, path );
196 catch ( Exception e )
198 log.warn( "Not reporting problem for invalid artifact in checksum check: " + e.getMessage() );
202 for ( ArtifactMetadata dupArtifact : results )
204 String id = path.substring( path.lastIndexOf( "/" ) + 1 );
205 if ( dupArtifact.getId().equals( id )
206 && dupArtifact.getNamespace().equals( originalArtifact.getNamespace() )
207 && dupArtifact.getProject().equals( originalArtifact.getProject() )
208 && dupArtifact.getVersion().equals( originalArtifact.getVersion() ) )
210 // Skip reference to itself.
211 if ( log.isDebugEnabled() )
213 log.debug( "Not counting duplicate for artifact " + dupArtifact + " for path " + path );
218 RepositoryProblemFacet problem = new RepositoryProblemFacet();
219 problem.setRepositoryId( repoId );
220 problem.setNamespace( originalArtifact.getNamespace() );
221 problem.setProject( originalArtifact.getProject() );
222 problem.setVersion( originalArtifact.getVersion() );
224 // FIXME: need to get the right storage resolver for the repository the dupe artifact is in, it might be
226 // FIXME: we need the project version here, not the artifact version
227 problem.setMessage( "Duplicate Artifact Detected: " + path + " <--> " + pathTranslator.toPath(
228 dupArtifact.getNamespace(), dupArtifact.getProject(), dupArtifact.getVersion(),
229 dupArtifact.getId() ) );
230 problem.setProblem( "duplicate-artifact" );
234 metadataRepository.addMetadataFacet( repoId, problem );
236 catch ( MetadataRepositoryException e )
238 throw new ConsumerException( e.getMessage(), e );
244 public void processFile( String path, boolean executeOnEntireRepo )
245 throws ConsumerException
250 public void completeScan()
252 repositorySession.close();
255 public void completeScan( boolean executeOnEntireRepo )
260 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
262 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
268 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
273 private void initIncludes()
277 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
281 public void initialize()
284 configuration.addChangeListener( this );