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.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.ConfigurationNames;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.archiva.consumers.ConsumerException;
29 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.archiva.metadata.model.ArtifactMetadata;
31 import org.apache.archiva.metadata.model.facets.RepositoryProblemFacet;
32 import org.apache.archiva.metadata.repository.MetadataRepository;
33 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
34 import org.apache.archiva.metadata.repository.RepositorySession;
35 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
36 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
37 import org.apache.archiva.redback.components.registry.Registry;
38 import org.apache.archiva.redback.components.registry.RegistryListener;
39 import org.apache.archiva.repository.ManagedRepository;
40 import org.apache.commons.collections.CollectionUtils;
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;
49 import java.io.IOException;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.ArrayList;
53 import java.util.Collection;
54 import java.util.Collections;
55 import java.util.Date;
56 import java.util.List;
59 * Search the artifact repository of known SHA1 Checksums for potential duplicate artifacts.
61 * 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" )
64 @Scope ( "prototype" )
65 public class DuplicateArtifactsConsumer
66 extends AbstractMonitoredConsumer
67 implements KnownRepositoryContentConsumer, RegistryListener
69 private Logger log = LoggerFactory.getLogger( DuplicateArtifactsConsumer.class );
71 private String id = "duplicate-artifacts";
73 private String description = "Check for Duplicate Artifacts via SHA1 Checksums";
76 private ArchivaConfiguration configuration;
79 private FileTypes filetypes;
82 * FIXME: this could be multiple implementations and needs to be configured.
85 private RepositorySessionFactory repositorySessionFactory;
87 private List<String> includes = new ArrayList<>();
89 private Path repositoryDir;
91 private String repoId;
94 * FIXME: needs to be selected based on the repository in question
97 @Named ( value = "repositoryPathTranslator#maven2" )
98 private RepositoryPathTranslator pathTranslator;
100 private RepositorySession repositorySession;
103 public String getId()
109 public String getDescription()
115 public List<String> getIncludes()
121 public List<String> getExcludes()
123 return Collections.emptyList();
127 public void beginScan( ManagedRepository repo, Date whenGathered )
128 throws ConsumerException
130 repoId = repo.getId();
131 this.repositoryDir = Paths.get( repo.getLocation() );
132 repositorySession = repositorySessionFactory.createSession();
136 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
137 throws ConsumerException
139 beginScan( repo, whenGathered );
143 public void processFile( String path )
144 throws ConsumerException
146 Path artifactFile = this.repositoryDir.resolve( path );
148 // TODO: would be quicker to somehow make sure it ran after the update database consumer, or as a part of that
149 // perhaps could use an artifact context that is retained for all consumers? First in can set the SHA-1
150 // alternatively this could come straight from the storage resolver, which could populate the artifact metadata
151 // in the later parse call with the desired checksum and use that
153 ChecksummedFile checksummedFile = new ChecksummedFile( artifactFile);
156 checksumSha1 = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
158 catch ( IOException e )
160 throw new ConsumerException( e.getMessage(), e );
163 MetadataRepository metadataRepository = repositorySession.getRepository();
165 Collection<ArtifactMetadata> results;
168 results = metadataRepository.getArtifactsByChecksum( repoId, checksumSha1 );
170 catch ( MetadataRepositoryException e )
172 repositorySession.close();
173 throw new ConsumerException( e.getMessage(), e );
176 if ( CollectionUtils.isNotEmpty( results ) )
178 ArtifactMetadata originalArtifact;
181 originalArtifact = pathTranslator.getArtifactForPath( repoId, path );
183 catch ( Exception e )
185 log.warn( "Not reporting problem for invalid artifact in checksum check: {}", e.getMessage() );
189 for ( ArtifactMetadata dupArtifact : results )
191 String id = path.substring( path.lastIndexOf( '/' ) + 1 );
192 if ( dupArtifact.getId().equals( id ) && dupArtifact.getNamespace().equals(
193 originalArtifact.getNamespace() ) && dupArtifact.getProject().equals(
194 originalArtifact.getProject() ) && dupArtifact.getVersion().equals(
195 originalArtifact.getVersion() ) )
197 // Skip reference to itself.
199 log.debug( "Not counting duplicate for artifact {} for path {}", dupArtifact, path );
204 RepositoryProblemFacet problem = new RepositoryProblemFacet();
205 problem.setRepositoryId( repoId );
206 problem.setNamespace( originalArtifact.getNamespace() );
207 problem.setProject( originalArtifact.getProject() );
208 problem.setVersion( originalArtifact.getVersion() );
210 // FIXME: need to get the right storage resolver for the repository the dupe artifact is in, it might be
212 // FIXME: we need the project version here, not the artifact version
213 problem.setMessage( "Duplicate Artifact Detected: " + path + " <--> " + pathTranslator.toPath(
214 dupArtifact.getNamespace(), dupArtifact.getProject(), dupArtifact.getVersion(),
215 dupArtifact.getId() ) );
216 problem.setProblem( "duplicate-artifact" );
220 metadataRepository.addMetadataFacet( repoId, problem );
222 catch ( MetadataRepositoryException e )
224 throw new ConsumerException( e.getMessage(), e );
231 public void processFile( String path, boolean executeOnEntireRepo )
232 throws ConsumerException
238 public void completeScan()
240 repositorySession.close();
244 public void completeScan( boolean executeOnEntireRepo )
250 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
252 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
259 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
264 private void initIncludes()
268 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
272 public void initialize()
275 configuration.addChangeListener( this );