]> source.dussan.org Git - archiva.git/blob
ba8e1e8f09ef52658e9ec6f07174c65388e2caad
[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.archiva.layer.RepositoryQueryLayerFactory;
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.artifact.factory.ArtifactFactory;
23 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
24 import org.apache.maven.artifact.versioning.VersionRange;
25 import org.apache.maven.model.Dependency;
26 import org.apache.maven.model.Model;
27
28 import java.text.MessageFormat;
29 import java.util.Iterator;
30 import java.util.List;
31
32 /**
33  * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="dependency"
34  */
35 public class DependencyArtifactReportProcessor
36     implements ArtifactReportProcessor
37 {
38     /**
39      * @plexus.requirement
40      */
41     private ArtifactFactory artifactFactory;
42
43     /**
44      * @plexus.requirement
45      */
46     private RepositoryQueryLayerFactory layerFactory;
47
48     private static final String POM = "pom";
49
50     public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
51     {
52         RepositoryQueryLayer queryLayer = layerFactory.createRepositoryQueryLayer( artifact.getRepository() );
53         processArtifact( artifact, reporter, queryLayer );
54
55         if ( model != null && POM.equals( artifact.getType() ) )
56         {
57             List dependencies = model.getDependencies();
58             processDependencies( dependencies, reporter, queryLayer, artifact );
59         }
60     }
61
62     private void processArtifact( Artifact artifact, ReportingDatabase reporter,
63                                   RepositoryQueryLayer repositoryQueryLayer )
64     {
65         if ( !repositoryQueryLayer.containsArtifact( artifact ) )
66         {
67             reporter.addFailure( artifact, "Artifact does not exist in the repository" );
68         }
69     }
70
71     private void processDependencies( List dependencies, ReportingDatabase reporter,
72                                       RepositoryQueryLayer repositoryQueryLayer, Artifact sourceArtifact )
73     {
74         if ( dependencies.size() > 0 )
75         {
76             Iterator iterator = dependencies.iterator();
77             while ( iterator.hasNext() )
78             {
79                 Dependency dependency = (Dependency) iterator.next();
80
81                 try
82                 {
83                     Artifact artifact = createArtifact( dependency );
84
85                     if ( !repositoryQueryLayer.containsArtifact( artifact ) )
86                     {
87                         String reason = MessageFormat.format(
88                             "Artifact''s dependency {0} does not exist in the repository",
89                             new String[]{dependency.toString()} );
90                         reporter.addFailure( sourceArtifact, reason );
91                     }
92                 }
93                 catch ( InvalidVersionSpecificationException e )
94                 {
95                     String reason = MessageFormat.format( "Artifact''s dependency {0} contains an invalid version {1}",
96                                                           new String[]{dependency.toString(),
97                                                               dependency.getVersion()} );
98                     reporter.addFailure( sourceArtifact, reason );
99                 }
100             }
101         }
102     }
103
104     private Artifact createArtifact( Dependency dependency )
105         throws InvalidVersionSpecificationException
106     {
107         return artifactFactory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(),
108                                                          VersionRange.createFromVersionSpec( dependency.getVersion() ),
109                                                          dependency.getType(), dependency.getClassifier(),
110                                                          dependency.getScope() );
111     }
112 }