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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.checksum.ChecksumAlgorithm;
24 import org.apache.archiva.checksum.ChecksummedFile;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.repository.MetadataRepository;
27 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
30 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
31 import org.apache.archiva.reports.RepositoryProblemFacet;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.apache.archiva.configuration.ArchivaConfiguration;
34 import org.apache.archiva.configuration.ConfigurationNames;
35 import org.apache.archiva.configuration.FileTypes;
36 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
37 import org.apache.archiva.consumers.ConsumerException;
38 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
39 import org.apache.archiva.redback.components.registry.Registry;
40 import org.apache.archiva.redback.components.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 artifact repository 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( ManagedRepository 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( ManagedRepository 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 repositorySession.close();
185 throw new ConsumerException( e.getMessage(), e );
188 if ( CollectionUtils.isNotEmpty( results ) )
190 ArtifactMetadata originalArtifact;
193 originalArtifact = pathTranslator.getArtifactForPath( repoId, path );
195 catch ( Exception e )
197 log.warn( "Not reporting problem for invalid artifact in checksum check: " + e.getMessage() );
201 for ( ArtifactMetadata dupArtifact : results )
203 String id = path.substring( path.lastIndexOf( "/" ) + 1 );
204 if ( dupArtifact.getId().equals( id )
205 && dupArtifact.getNamespace().equals( originalArtifact.getNamespace() )
206 && dupArtifact.getProject().equals( originalArtifact.getProject() )
207 && dupArtifact.getVersion().equals( originalArtifact.getVersion() ) )
209 // Skip reference to itself.
210 if ( log.isDebugEnabled() )
212 log.debug( "Not counting duplicate for artifact " + dupArtifact + " for path " + path );
217 RepositoryProblemFacet problem = new RepositoryProblemFacet();
218 problem.setRepositoryId( repoId );
219 problem.setNamespace( originalArtifact.getNamespace() );
220 problem.setProject( originalArtifact.getProject() );
221 problem.setVersion( originalArtifact.getVersion() );
223 // FIXME: need to get the right storage resolver for the repository the dupe artifact is in, it might be
225 // FIXME: we need the project version here, not the artifact version
226 problem.setMessage( "Duplicate Artifact Detected: " + path + " <--> " + pathTranslator.toPath(
227 dupArtifact.getNamespace(), dupArtifact.getProject(), dupArtifact.getVersion(),
228 dupArtifact.getId() ) );
229 problem.setProblem( "duplicate-artifact" );
233 metadataRepository.addMetadataFacet( repoId, problem );
235 catch ( MetadataRepositoryException e )
237 throw new ConsumerException( e.getMessage(), e );
243 public void processFile( String path, boolean executeOnEntireRepo )
244 throws ConsumerException
249 public void completeScan()
251 repositorySession.close();
254 public void completeScan( boolean executeOnEntireRepo )
259 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
261 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
267 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
272 private void initIncludes()
276 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
280 public void initialize()
283 configuration.addChangeListener( this );