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