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.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.metadata.model.facets.RepositoryProblemFacet;
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;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Collections;
54 import java.util.Date;
55 import java.util.List;
58 * Search the artifact repository of known SHA1 Checksums for potential duplicate artifacts.
60 * TODO: no need for this to be a scanner - we can just query the database / content repository to get a full list
62 @Service ( "knownRepositoryContentConsumer#duplicate-artifacts" )
63 @Scope ( "prototype" )
64 public class DuplicateArtifactsConsumer
65 extends AbstractMonitoredConsumer
66 implements KnownRepositoryContentConsumer, RegistryListener
68 private Logger log = LoggerFactory.getLogger( DuplicateArtifactsConsumer.class );
70 private String id = "duplicate-artifacts";
72 private String description = "Check for Duplicate Artifacts via SHA1 Checksums";
75 private ArchivaConfiguration configuration;
78 private FileTypes filetypes;
81 * FIXME: this could be multiple implementations and needs to be configured.
84 private RepositorySessionFactory repositorySessionFactory;
86 private List<String> includes = new ArrayList<>();
88 private File repositoryDir;
90 private String repoId;
93 * FIXME: needs to be selected based on the repository in question
96 @Named ( value = "repositoryPathTranslator#maven2" )
97 private RepositoryPathTranslator pathTranslator;
99 private RepositorySession repositorySession;
102 public String getId()
108 public String getDescription()
114 public List<String> getIncludes()
120 public List<String> getExcludes()
122 return Collections.emptyList();
126 public void beginScan( ManagedRepository repo, Date whenGathered )
127 throws ConsumerException
129 repoId = repo.getId();
130 this.repositoryDir = new File( repo.getLocation() );
131 repositorySession = repositorySessionFactory.createSession();
135 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
136 throws ConsumerException
138 beginScan( repo, whenGathered );
142 public void processFile( String path )
143 throws ConsumerException
145 File artifactFile = new File( this.repositoryDir, path );
147 // TODO: would be quicker to somehow make sure it ran after the update database consumer, or as a part of that
148 // perhaps could use an artifact context that is retained for all consumers? First in can set the SHA-1
149 // alternatively this could come straight from the storage resolver, which could populate the artifact metadata
150 // in the later parse call with the desired checksum and use that
152 ChecksummedFile checksummedFile = new ChecksummedFile( artifactFile.toPath() );
155 checksumSha1 = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
157 catch ( IOException e )
159 throw new ConsumerException( e.getMessage(), e );
162 MetadataRepository metadataRepository = repositorySession.getRepository();
164 Collection<ArtifactMetadata> results;
167 results = metadataRepository.getArtifactsByChecksum( repoId, checksumSha1 );
169 catch ( MetadataRepositoryException e )
171 repositorySession.close();
172 throw new ConsumerException( e.getMessage(), e );
175 if ( CollectionUtils.isNotEmpty( results ) )
177 ArtifactMetadata originalArtifact;
180 originalArtifact = pathTranslator.getArtifactForPath( repoId, path );
182 catch ( Exception e )
184 log.warn( "Not reporting problem for invalid artifact in checksum check: {}", e.getMessage() );
188 for ( ArtifactMetadata dupArtifact : results )
190 String id = path.substring( path.lastIndexOf( '/' ) + 1 );
191 if ( dupArtifact.getId().equals( id ) && dupArtifact.getNamespace().equals(
192 originalArtifact.getNamespace() ) && dupArtifact.getProject().equals(
193 originalArtifact.getProject() ) && dupArtifact.getVersion().equals(
194 originalArtifact.getVersion() ) )
196 // Skip reference to itself.
198 log.debug( "Not counting duplicate for artifact {} for path {}", dupArtifact, path );
203 RepositoryProblemFacet problem = new RepositoryProblemFacet();
204 problem.setRepositoryId( repoId );
205 problem.setNamespace( originalArtifact.getNamespace() );
206 problem.setProject( originalArtifact.getProject() );
207 problem.setVersion( originalArtifact.getVersion() );
209 // FIXME: need to get the right storage resolver for the repository the dupe artifact is in, it might be
211 // FIXME: we need the project version here, not the artifact version
212 problem.setMessage( "Duplicate Artifact Detected: " + path + " <--> " + pathTranslator.toPath(
213 dupArtifact.getNamespace(), dupArtifact.getProject(), dupArtifact.getVersion(),
214 dupArtifact.getId() ) );
215 problem.setProblem( "duplicate-artifact" );
219 metadataRepository.addMetadataFacet( repoId, problem );
221 catch ( MetadataRepositoryException e )
223 throw new ConsumerException( e.getMessage(), e );
230 public void processFile( String path, boolean executeOnEntireRepo )
231 throws ConsumerException
237 public void completeScan()
239 repositorySession.close();
243 public void completeScan( boolean executeOnEntireRepo )
249 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
251 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
258 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
263 private void initIncludes()
267 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
271 public void initialize()
274 configuration.addChangeListener( this );