]> source.dussan.org Git - archiva.git/blob
5364851c172e2857a389ca11deeff59395e4a39a
[archiva.git] /
1 package org.apache.maven.archiva.reporting;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.lucene.index.Term;
20 import org.apache.lucene.search.TermQuery;
21 import org.apache.maven.archiva.digest.Digester;
22 import org.apache.maven.archiva.digest.DigesterException;
23 import org.apache.maven.archiva.indexer.RepositoryArtifactIndex;
24 import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory;
25 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
26 import org.apache.maven.archiva.indexer.lucene.LuceneQuery;
27 import org.apache.maven.archiva.indexer.record.StandardArtifactIndexRecord;
28 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.model.Model;
32
33 import java.io.File;
34 import java.util.Iterator;
35 import java.util.List;
36
37 /**
38  * Validates an artifact file for duplicates within the same groupId based from what's available in a repository index.
39  *
40  * @author Edwin Punzalan
41  * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="duplicate"
42  */
43 public class DuplicateArtifactFileReportProcessor
44     implements ArtifactReportProcessor
45 {
46     /**
47      * @plexus.requirement role-hint="md5"
48      */
49     private Digester digester;
50
51     /**
52      * @plexus.requirement
53      */
54     private RepositoryArtifactIndexFactory indexFactory;
55
56     /**
57      * @plexus.configuration
58      */
59     private String indexDirectory;
60
61     private static final String ROLE_HINT = "duplicate";
62
63     public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
64     {
65         ArtifactRepository repository = artifact.getRepository();
66         if ( artifact.getFile() != null )
67         {
68             RepositoryArtifactIndex index = indexFactory.createStandardIndex( new File( indexDirectory ) );
69
70             String checksum = null;
71             try
72             {
73                 checksum = digester.calc( artifact.getFile() );
74             }
75             catch ( DigesterException e )
76             {
77                 addWarning( reporter, artifact, null,
78                             "Unable to generate checksum for " + artifact.getFile() + ": " + e );
79             }
80
81             if ( checksum != null )
82             {
83                 try
84                 {
85                     List results = index.search( new LuceneQuery(
86                         new TermQuery( new Term( StandardIndexRecordFields.MD5, checksum.toLowerCase() ) ) ) );
87
88                     if ( !results.isEmpty() )
89                     {
90                         for ( Iterator i = results.iterator(); i.hasNext(); )
91                         {
92                             StandardArtifactIndexRecord result = (StandardArtifactIndexRecord) i.next();
93
94                             //make sure it is not the same artifact
95                             if ( !result.getFilename().equals( repository.pathOf( artifact ) ) )
96                             {
97                                 //report only duplicates from the same groupId
98                                 String groupId = artifact.getGroupId();
99                                 if ( groupId.equals( result.getGroupId() ) )
100                                 {
101                                     addFailures( reporter, artifact, "duplicate",
102                                                  "Found duplicate for " + artifact.getId() );
103                                 }
104                             }
105                         }
106                     }
107                 }
108                 catch ( RepositoryIndexSearchException e )
109                 {
110                     addWarning( reporter, artifact, null, "Failed to search in index" + e );
111                 }
112             }
113         }
114         else
115         {
116             addWarning( reporter, artifact, null, "Artifact file is null" );
117         }
118     }
119
120     private static void addFailures( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
121     {
122         // TODO: reason could be an i18n key derived from the processor and the problem ID and the
123         reporter.addFailure( artifact, ROLE_HINT, problem, reason );
124     }
125
126     private static void addWarning( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
127     {
128         // TODO: reason could be an i18n key derived from the processor and the problem ID and the
129         reporter.addWarning( artifact, ROLE_HINT, problem, reason );
130     }
131 }