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.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.ConfigurationNames;
27 import org.apache.archiva.configuration.FileTypes;
28 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
29 import org.apache.archiva.consumers.ConsumerException;
30 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.archiva.metadata.model.ArtifactMetadata;
32 import org.apache.archiva.metadata.model.facets.RepositoryProblemFacet;
33 import org.apache.archiva.metadata.repository.MetadataRepository;
34 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
35 import org.apache.archiva.metadata.repository.RepositorySession;
36 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
37 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
38 import org.apache.archiva.redback.components.registry.Registry;
39 import org.apache.archiva.redback.components.registry.RegistryListener;
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;
55 * Search the artifact repository of known SHA1 Checksums for potential duplicate artifacts.
57 * TODO: no need for this to be a scanner - we can just query the database / content repository to get a full list
59 @Service ( "knownRepositoryContentConsumer#duplicate-artifacts" )
60 @Scope ( "prototype" )
61 public class DuplicateArtifactsConsumer
62 extends AbstractMonitoredConsumer
63 implements KnownRepositoryContentConsumer, RegistryListener
65 private Logger log = LoggerFactory.getLogger( DuplicateArtifactsConsumer.class );
67 private String id = "duplicate-artifacts";
69 private String description = "Check for Duplicate Artifacts via SHA1 Checksums";
72 private ArchivaConfiguration configuration;
75 private FileTypes filetypes;
78 * FIXME: this could be multiple implementations and needs to be configured.
81 private RepositorySessionFactory repositorySessionFactory;
83 private List<String> includes = new ArrayList<>();
85 private Path repositoryDir;
87 private String repoId;
90 * FIXME: needs to be selected based on the repository in question
93 @Named ( value = "repositoryPathTranslator#maven2" )
94 private RepositoryPathTranslator pathTranslator;
96 private RepositorySession repositorySession;
105 public String getDescription()
111 public List<String> getIncludes()
117 public List<String> getExcludes()
119 return Collections.emptyList();
123 public void beginScan( ManagedRepository repo, Date whenGathered )
124 throws ConsumerException
126 repoId = repo.getId();
127 this.repositoryDir = Paths.get( repo.getLocation() );
128 repositorySession = repositorySessionFactory.createSession();
132 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
133 throws ConsumerException
135 beginScan( repo, whenGathered );
139 public void processFile( String path )
140 throws ConsumerException
142 Path artifactFile = this.repositoryDir.resolve( path );
144 // TODO: would be quicker to somehow make sure it ran after the update database consumer, or as a part of that
145 // perhaps could use an artifact context that is retained for all consumers? First in can set the SHA-1
146 // alternatively this could come straight from the storage resolver, which could populate the artifact metadata
147 // in the later parse call with the desired checksum and use that
149 ChecksummedFile checksummedFile = new ChecksummedFile( artifactFile);
152 checksumSha1 = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
154 catch ( IOException e )
156 throw new ConsumerException( e.getMessage(), e );
159 MetadataRepository metadataRepository = repositorySession.getRepository();
161 Collection<ArtifactMetadata> results;
164 results = metadataRepository.getArtifactsByChecksum( repoId, checksumSha1 );
166 catch ( MetadataRepositoryException e )
168 repositorySession.close();
169 throw new ConsumerException( e.getMessage(), e );
172 if ( CollectionUtils.isNotEmpty( results ) )
174 ArtifactMetadata originalArtifact;
177 originalArtifact = pathTranslator.getArtifactForPath( repoId, path );
179 catch ( Exception e )
181 log.warn( "Not reporting problem for invalid artifact in checksum check: {}", e.getMessage() );
185 for ( ArtifactMetadata dupArtifact : results )
187 String id = path.substring( path.lastIndexOf( '/' ) + 1 );
188 if ( dupArtifact.getId().equals( id ) && dupArtifact.getNamespace().equals(
189 originalArtifact.getNamespace() ) && dupArtifact.getProject().equals(
190 originalArtifact.getProject() ) && dupArtifact.getVersion().equals(
191 originalArtifact.getVersion() ) )
193 // Skip reference to itself.
195 log.debug( "Not counting duplicate for artifact {} for path {}", dupArtifact, path );
200 RepositoryProblemFacet problem = new RepositoryProblemFacet();
201 problem.setRepositoryId( repoId );
202 problem.setNamespace( originalArtifact.getNamespace() );
203 problem.setProject( originalArtifact.getProject() );
204 problem.setVersion( originalArtifact.getVersion() );
206 // FIXME: need to get the right storage resolver for the repository the dupe artifact is in, it might be
208 // FIXME: we need the project version here, not the artifact version
209 problem.setMessage( "Duplicate Artifact Detected: " + path + " <--> " + pathTranslator.toPath(
210 dupArtifact.getNamespace(), dupArtifact.getProject(), dupArtifact.getVersion(),
211 dupArtifact.getId() ) );
212 problem.setProblem( "duplicate-artifact" );
216 metadataRepository.addMetadataFacet( repoId, problem );
218 catch ( MetadataRepositoryException e )
220 throw new ConsumerException( e.getMessage(), e );
227 public void processFile( String path, boolean executeOnEntireRepo )
228 throws ConsumerException
234 public void completeScan()
236 repositorySession.close();
240 public void completeScan( boolean executeOnEntireRepo )
246 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
248 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
255 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
260 private void initIncludes()
264 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
268 public void initialize()
271 configuration.addChangeListener( this );