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.reports.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.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
61 @Service ( "knownRepositoryContentConsumer#duplicate-artifacts" )
62 @Scope ( "prototype" )
63 public class DuplicateArtifactsConsumer
64 extends AbstractMonitoredConsumer
65 implements KnownRepositoryContentConsumer, RegistryListener
67 private Logger log = LoggerFactory.getLogger( DuplicateArtifactsConsumer.class );
70 * default-value="duplicate-artifacts"
72 private String id = "duplicate-artifacts";
75 * default-value="Check for Duplicate Artifacts via SHA1 Checksums"
77 private String description = "Check for Duplicate Artifacts via SHA1 Checksums";
83 private ArchivaConfiguration configuration;
89 private FileTypes filetypes;
92 * FIXME: can be of other types
95 private RepositorySessionFactory repositorySessionFactory;
97 private List<String> includes = new ArrayList<String>();
99 private File repositoryDir;
101 private String repoId;
104 * FIXME: needs to be selected based on the repository in question
107 @Named ( value = "repositoryPathTranslator#maven2" )
108 private RepositoryPathTranslator pathTranslator;
110 private RepositorySession repositorySession;
112 public String getId()
117 public String getDescription()
122 public boolean isPermanent()
127 public List<String> getIncludes()
132 public List<String> getExcludes()
134 return Collections.emptyList();
137 public void beginScan( ManagedRepository repo, Date whenGathered )
138 throws ConsumerException
140 repoId = repo.getId();
141 this.repositoryDir = new File( repo.getLocation() );
142 repositorySession = repositorySessionFactory.createSession();
145 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
146 throws ConsumerException
148 beginScan( repo, whenGathered );
151 public void processFile( String path )
152 throws ConsumerException
154 File artifactFile = new File( this.repositoryDir, path );
156 // TODO: would be quicker to somehow make sure it ran after the update database consumer, or as a part of that
157 // perhaps could use an artifact context that is retained for all consumers? First in can set the SHA-1
158 // alternatively this could come straight from the storage resolver, which could populate the artifact metadata
159 // in the later parse call with the desired checksum and use that
161 ChecksummedFile checksummedFile = new ChecksummedFile( artifactFile );
164 checksumSha1 = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
166 catch ( IOException e )
168 throw new ConsumerException( e.getMessage(), e );
171 MetadataRepository metadataRepository = repositorySession.getRepository();
173 List<ArtifactMetadata> results;
176 results = metadataRepository.getArtifactsByChecksum( repoId, checksumSha1 );
178 catch ( MetadataRepositoryException e )
180 repositorySession.close();
181 throw new ConsumerException( e.getMessage(), e );
184 if ( CollectionUtils.isNotEmpty( results ) )
186 ArtifactMetadata originalArtifact;
189 originalArtifact = pathTranslator.getArtifactForPath( repoId, path );
191 catch ( Exception e )
193 log.warn( "Not reporting problem for invalid artifact in checksum check: " + e.getMessage() );
197 for ( ArtifactMetadata dupArtifact : results )
199 String id = path.substring( path.lastIndexOf( "/" ) + 1 );
200 if ( dupArtifact.getId().equals( id ) && dupArtifact.getNamespace().equals(
201 originalArtifact.getNamespace() ) && dupArtifact.getProject().equals(
202 originalArtifact.getProject() ) && dupArtifact.getVersion().equals(
203 originalArtifact.getVersion() ) )
205 // Skip reference to itself.
207 log.debug( "Not counting duplicate for artifact {} for path {}", dupArtifact, path );
212 RepositoryProblemFacet problem = new RepositoryProblemFacet();
213 problem.setRepositoryId( repoId );
214 problem.setNamespace( originalArtifact.getNamespace() );
215 problem.setProject( originalArtifact.getProject() );
216 problem.setVersion( originalArtifact.getVersion() );
218 // FIXME: need to get the right storage resolver for the repository the dupe artifact is in, it might be
220 // FIXME: we need the project version here, not the artifact version
221 problem.setMessage( "Duplicate Artifact Detected: " + path + " <--> " + pathTranslator.toPath(
222 dupArtifact.getNamespace(), dupArtifact.getProject(), dupArtifact.getVersion(),
223 dupArtifact.getId() ) );
224 problem.setProblem( "duplicate-artifact" );
228 metadataRepository.addMetadataFacet( repoId, problem );
230 catch ( MetadataRepositoryException e )
232 throw new ConsumerException( e.getMessage(), e );
238 public void processFile( String path, boolean executeOnEntireRepo )
239 throws ConsumerException
244 public void completeScan()
246 repositorySession.close();
249 public void completeScan( boolean executeOnEntireRepo )
254 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
256 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
262 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
267 private void initIncludes()
271 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
275 public void initialize()
278 configuration.addChangeListener( this );