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