]> source.dussan.org Git - archiva.git/blob
c62b2f16ffe7a35037b5a704f0e67e236e7ad522
[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     private static final String ROLE_HINT = "dependency";
51
52     public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
53     {
54         RepositoryQueryLayer queryLayer = layerFactory.createRepositoryQueryLayer( artifact.getRepository() );
55         if ( !queryLayer.containsArtifact( artifact ) )
56         {
57             // TODO: is this even possible?
58             addFailure( reporter, artifact, "missing-artifact", "Artifact does not exist in the repository" );
59         }
60
61         if ( model != null && POM.equals( artifact.getType() ) )
62         {
63             List dependencies = model.getDependencies();
64             processDependencies( dependencies, reporter, queryLayer, artifact );
65         }
66     }
67
68     private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
69     {
70         // TODO: reason could be an i18n key derived from the processor and the problem ID and the
71         reporter.addFailure( artifact, ROLE_HINT, problem, reason );
72     }
73
74     private void processDependencies( List dependencies, ReportingDatabase reporter,
75                                       RepositoryQueryLayer repositoryQueryLayer, Artifact sourceArtifact )
76     {
77         if ( dependencies.size() > 0 )
78         {
79             Iterator iterator = dependencies.iterator();
80             while ( iterator.hasNext() )
81             {
82                 Dependency dependency = (Dependency) iterator.next();
83
84                 try
85                 {
86                     Artifact artifact = createArtifact( dependency );
87
88                     // TODO: handle ranges properly. We should instead be mapping out all the artifacts in the
89                     // repository and mapping out the graph
90
91                     if ( artifact.getVersion() == null )
92                     {
93                         // it was a range, for now presume it exists
94                         continue;
95                     }
96
97                     if ( !repositoryQueryLayer.containsArtifact( artifact ) )
98                     {
99                         String reason = MessageFormat.format(
100                             "Artifact''s dependency {0} does not exist in the repository",
101                             new String[]{getDependencyString( dependency )} );
102                         addFailure( reporter, sourceArtifact, "missing-dependency:" + getDependencyKey( dependency ),
103                                     reason );
104                     }
105                 }
106                 catch ( InvalidVersionSpecificationException e )
107                 {
108                     String reason = MessageFormat.format( "Artifact''s dependency {0} contains an invalid version {1}",
109                                                           new String[]{getDependencyString( dependency ),
110                                                               dependency.getVersion()} );
111                     addFailure( reporter, sourceArtifact, "bad-version:" + getDependencyKey( dependency ), reason );
112                 }
113             }
114         }
115     }
116
117     private String getDependencyKey( Dependency dependency )
118     {
119         String str = dependency.getGroupId();
120         str += ":" + dependency.getArtifactId();
121         str += ":" + dependency.getVersion();
122         str += ":" + dependency.getType();
123         if ( dependency.getClassifier() != null )
124         {
125             str += ":" + dependency.getClassifier();
126         }
127         return str;
128     }
129
130     static String getDependencyString( Dependency dependency )
131     {
132         String str = "(group=" + dependency.getGroupId();
133         str += ", artifact=" + dependency.getArtifactId();
134         str += ", version=" + dependency.getVersion();
135         str += ", type=" + dependency.getType();
136         if ( dependency.getClassifier() != null )
137         {
138             str += ", classifier=" + dependency.getClassifier();
139         }
140         str += ")";
141         return str;
142     }
143
144     private Artifact createArtifact( Dependency dependency )
145         throws InvalidVersionSpecificationException
146     {
147         VersionRange spec = VersionRange.createFromVersionSpec( dependency.getVersion() );
148
149         if ( spec == null )
150         {
151             throw new InvalidVersionSpecificationException( "Dependency version was null" );
152         }
153
154         return artifactFactory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), spec,
155                                                          dependency.getType(), dependency.getClassifier(),
156                                                          dependency.getScope() );
157     }
158 }