]> source.dussan.org Git - archiva.git/blob
f9960bfc2f05ff6d10e92717b021b4237326abe4
[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                     // TODO: handle ranges properly. We should instead be mapping out all the artifacts in the
86                     // repository and mapping out the graph
87
88                     if ( artifact.getVersion() == null )
89                     {
90                         // it was a range, for now presume it exists
91                         continue;
92                     }
93
94                     if ( !repositoryQueryLayer.containsArtifact( artifact ) )
95                     {
96                         String reason = MessageFormat.format(
97                             "Artifact''s dependency {0} does not exist in the repository",
98                             new String[]{getDependencyString( dependency )} );
99                         reporter.addFailure( sourceArtifact, reason );
100                     }
101                 }
102                 catch ( InvalidVersionSpecificationException e )
103                 {
104                     String reason = MessageFormat.format( "Artifact''s dependency {0} contains an invalid version {1}",
105                                                           new String[]{getDependencyString( dependency ),
106                                                               dependency.getVersion()} );
107                     reporter.addFailure( sourceArtifact, reason );
108                 }
109             }
110         }
111     }
112
113     static String getDependencyString( Dependency dependency )
114     {
115         String str = "(group=" + dependency.getGroupId();
116         str += ", artifact=" + dependency.getArtifactId();
117         str += ", version=" + dependency.getVersion();
118         str += ", type=" + dependency.getType();
119         if ( dependency.getClassifier() != null )
120         {
121             str += ", classifier=" + dependency.getClassifier();
122         }
123         str += ")";
124         return str;
125     }
126
127     private Artifact createArtifact( Dependency dependency )
128         throws InvalidVersionSpecificationException
129     {
130         VersionRange spec = VersionRange.createFromVersionSpec( dependency.getVersion() );
131
132         if ( spec == null )
133         {
134             throw new InvalidVersionSpecificationException( "Dependency version was null" );
135         }
136
137         return artifactFactory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), spec,
138                                                          dependency.getType(), dependency.getClassifier(),
139                                                          dependency.getScope() );
140     }
141 }