]> source.dussan.org Git - archiva.git/blob
a5f884633518d5d5db5e6de3df2021cc851a706e
[archiva.git] /
1 package org.apache.maven.archiva.reporting.processor;
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.indexer.RepositoryArtifactIndex;
22 import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory;
23 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
24 import org.apache.maven.archiva.indexer.lucene.LuceneQuery;
25 import org.apache.maven.archiva.indexer.record.StandardArtifactIndexRecord;
26 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
27 import org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor;
28 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.model.Model;
32 import org.codehaus.plexus.digest.Digester;
33 import org.codehaus.plexus.digest.DigesterException;
34
35 import java.io.File;
36 import java.util.Iterator;
37 import java.util.List;
38
39 /**
40  * Validates an artifact file for duplicates within the same groupId based from what's available in a repository index.
41  *
42  * @author Edwin Punzalan
43  * @plexus.component role="org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor" role-hint="duplicate"
44  */
45 public class DuplicateArtifactFileReportProcessor
46     implements ArtifactReportProcessor
47 {
48     /**
49      * @plexus.requirement role-hint="md5"
50      */
51     private Digester digester;
52
53     /**
54      * @plexus.requirement
55      */
56     private RepositoryArtifactIndexFactory indexFactory;
57
58     /**
59      * @plexus.configuration
60      */
61     private String indexDirectory;
62
63     private static final String ROLE_HINT = "duplicate";
64
65     public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
66     {
67         ArtifactRepository repository = artifact.getRepository();
68         if ( artifact.getFile() != null )
69         {
70             System.out.println( "indexDirectory = " + indexDirectory );
71             
72             File indexDirectoryFile = new File( indexDirectory );
73
74             RepositoryArtifactIndex index = indexFactory.createStandardIndex( indexDirectoryFile );
75
76             String checksum = null;
77             try
78             {
79                 checksum = digester.calc( artifact.getFile() );
80             }
81             catch ( DigesterException e )
82             {
83                 addWarning( reporter, artifact, null,
84                             "Unable to generate checksum for " + artifact.getFile() + ": " + e );
85             }
86
87             if ( checksum != null )
88             {
89                 try
90                 {
91                     List results = index.search( new LuceneQuery(
92                         new TermQuery( new Term( StandardIndexRecordFields.MD5, checksum.toLowerCase() ) ) ) );
93
94                     if ( !results.isEmpty() )
95                     {
96                         for ( Iterator i = results.iterator(); i.hasNext(); )
97                         {
98                             StandardArtifactIndexRecord result = (StandardArtifactIndexRecord) i.next();
99
100                             //make sure it is not the same artifact
101                             if ( !result.getFilename().equals( repository.pathOf( artifact ) ) )
102                             {
103                                 //report only duplicates from the same groupId
104                                 String groupId = artifact.getGroupId();
105                                 if ( groupId.equals( result.getGroupId() ) )
106                                 {
107                                     addFailure( reporter, artifact, "duplicate",
108                                                  "Found duplicate for " + artifact.getId() );
109                                 }
110                             }
111                         }
112                     }
113                 }
114                 catch ( RepositoryIndexSearchException e )
115                 {
116                     addWarning( reporter, artifact, null, "Failed to search in index" + e );
117                 }
118             }
119         }
120         else
121         {
122             addWarning( reporter, artifact, null, "Artifact file is null" );
123         }
124     }
125
126     private static void addFailure( 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.addFailure( artifact, ROLE_HINT, problem, reason );
130     }
131
132     private static void addWarning( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
133     {
134         // TODO: reason could be an i18n key derived from the processor and the problem ID and the
135         reporter.addWarning( artifact, ROLE_HINT, problem, reason );
136     }
137 }