]> source.dussan.org Git - archiva.git/blob
22e220a66db18a129a0357c0483c467e0c243a23
[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.maven.archiva.layer.RepositoryQueryLayer;
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.artifact.factory.ArtifactFactory;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.model.Dependency;
24 import org.apache.maven.model.Model;
25
26 import java.util.Iterator;
27 import java.util.List;
28
29 /**
30  * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="default"
31  */
32 public class DefaultArtifactReportProcessor
33     implements ArtifactReportProcessor
34 {
35     private static final String EMPTY_STRING = "";
36
37     // plexus components
38     private ArtifactFactory artifactFactory;
39
40     private RepositoryQueryLayer repositoryQueryLayer;
41
42     public void processArtifact( Model model, Artifact artifact, ArtifactReporter reporter,
43                                  ArtifactRepository repository )
44     {
45         if ( artifact == null )
46         {
47             reporter.addFailure( artifact, ArtifactReporter.NULL_ARTIFACT );
48         }
49         else
50         {
51             processArtifact( artifact, reporter );
52         }
53
54         if ( model == null )
55         {
56             reporter.addFailure( artifact, ArtifactReporter.NULL_MODEL );
57         }
58         else
59         {
60             List dependencies = model.getDependencies();
61             processDependencies( dependencies, reporter );
62         }
63     }
64
65     private void processArtifact( Artifact artifact, ArtifactReporter reporter )
66     {
67         boolean hasFailed = false;
68         if ( EMPTY_STRING.equals( artifact.getGroupId() ) || artifact.getGroupId() == null )
69         {
70             reporter.addFailure( artifact, ArtifactReporter.EMPTY_GROUP_ID );
71             hasFailed = true;
72         }
73         if ( EMPTY_STRING.equals( artifact.getArtifactId() ) || artifact.getArtifactId() == null )
74         {
75             reporter.addFailure( artifact, ArtifactReporter.EMPTY_ARTIFACT_ID );
76             hasFailed = true;
77         }
78         if ( EMPTY_STRING.equals( artifact.getVersion() ) || artifact.getVersion() == null )
79         {
80             reporter.addFailure( artifact, ArtifactReporter.EMPTY_VERSION );
81             hasFailed = true;
82         }
83         if ( !hasFailed )
84         {
85             if ( repositoryQueryLayer.containsArtifact( artifact ) )
86             {
87                 reporter.addSuccess( artifact );
88             }
89             else
90             {
91                 reporter.addFailure( artifact, ArtifactReporter.ARTIFACT_NOT_FOUND );
92             }
93         }
94     }
95
96     private void processDependencies( List dependencies, ArtifactReporter reporter )
97     {
98         if ( dependencies.size() > 0 )
99         {
100             Iterator iterator = dependencies.iterator();
101             while ( iterator.hasNext() )
102             {
103                 boolean hasFailed = false;
104                 Dependency dependency = (Dependency) iterator.next();
105                 Artifact artifact = createArtifact( dependency );
106                 if ( EMPTY_STRING.equals( dependency.getGroupId() ) || dependency.getGroupId() == null )
107                 {
108                     reporter.addFailure( artifact, ArtifactReporter.EMPTY_DEPENDENCY_GROUP_ID );
109                     hasFailed = true;
110                 }
111                 if ( EMPTY_STRING.equals( dependency.getArtifactId() ) || dependency.getArtifactId() == null )
112                 {
113                     reporter.addFailure( artifact, ArtifactReporter.EMPTY_DEPENDENCY_ARTIFACT_ID );
114                     hasFailed = true;
115                 }
116                 if ( EMPTY_STRING.equals( dependency.getVersion() ) || dependency.getVersion() == null )
117                 {
118                     reporter.addFailure( artifact, ArtifactReporter.EMPTY_DEPENDENCY_VERSION );
119                     hasFailed = true;
120                 }
121                 if ( !hasFailed )
122                 {
123                     if ( repositoryQueryLayer.containsArtifact( artifact ) )
124                     {
125                         reporter.addSuccess( artifact );
126                     }
127                     else
128                     {
129                         reporter.addFailure( artifact, ArtifactReporter.DEPENDENCY_NOT_FOUND );
130                     }
131                 }
132             }
133         }
134
135     }
136
137     /**
138      * Only used for passing a mock object when unit testing
139      *
140      * @param repositoryQueryLayer
141      */
142     protected void setRepositoryQueryLayer( RepositoryQueryLayer repositoryQueryLayer )
143     {
144         this.repositoryQueryLayer = repositoryQueryLayer;
145     }
146
147     /**
148      * Only used for passing a mock object when unit testing
149      *
150      * @param artifactFactory
151      */
152     protected void setArtifactFactory( ArtifactFactory artifactFactory )
153     {
154         this.artifactFactory = artifactFactory;
155     }
156
157     private Artifact createArtifact( Dependency dependency )
158     {
159         return artifactFactory.createBuildArtifact( dependency.getGroupId(), dependency.getArtifactId(),
160                                                     dependency.getVersion(), "pom" );
161     }
162 }