]> source.dussan.org Git - archiva.git/blob
2d5bf80b4d305b7656f6705ae84d54bcb135494a
[archiva.git] /
1 package org.apache.maven.archiva.reporting.processor;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.maven.archiva.layer.RepositoryQueryLayer;
23 import org.apache.maven.archiva.layer.RepositoryQueryLayerFactory;
24 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.factory.ArtifactFactory;
27 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
28 import org.apache.maven.artifact.versioning.VersionRange;
29 import org.apache.maven.model.Dependency;
30 import org.apache.maven.model.Model;
31
32 import java.text.MessageFormat;
33 import java.util.Iterator;
34 import java.util.List;
35
36 /**
37  * @plexus.component role="org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor" role-hint="dependency"
38  */
39 public class DependencyArtifactReportProcessor
40     implements ArtifactReportProcessor
41 {
42     /**
43      * @plexus.requirement
44      */
45     private ArtifactFactory artifactFactory;
46
47     /**
48      * @plexus.requirement
49      */
50     private RepositoryQueryLayerFactory layerFactory;
51
52     private static final String POM = "pom";
53
54     private static final String ROLE_HINT = "dependency";
55
56     public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
57     {
58         RepositoryQueryLayer queryLayer = layerFactory.createRepositoryQueryLayer( artifact.getRepository() );
59         if ( !queryLayer.containsArtifact( artifact ) )
60         {
61             // TODO: is this even possible?
62             addFailure( reporter, artifact, "missing-artifact", "Artifact does not exist in the repository" );
63         }
64
65         if ( model != null && POM.equals( artifact.getType() ) )
66         {
67             List dependencies = model.getDependencies();
68             processDependencies( dependencies, reporter, queryLayer, artifact );
69         }
70     }
71
72     private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
73     {
74         // TODO: reason could be an i18n key derived from the processor and the problem ID and the
75         reporter.addFailure( artifact, ROLE_HINT, problem, reason );
76     }
77
78     private void processDependencies( List dependencies, ReportingDatabase reporter,
79                                       RepositoryQueryLayer repositoryQueryLayer, Artifact sourceArtifact )
80     {
81         if ( dependencies.size() > 0 )
82         {
83             Iterator iterator = dependencies.iterator();
84             while ( iterator.hasNext() )
85             {
86                 Dependency dependency = (Dependency) iterator.next();
87
88                 try
89                 {
90                     Artifact artifact = createArtifact( dependency );
91
92                     // TODO: handle ranges properly. We should instead be mapping out all the artifacts in the
93                     // repository and mapping out the graph
94
95                     if ( artifact.getVersion() == null )
96                     {
97                         // it was a range, for now presume it exists
98                         continue;
99                     }
100
101                     if ( !repositoryQueryLayer.containsArtifact( artifact ) )
102                     {
103                         String reason = MessageFormat.format(
104                             "Artifact''s dependency {0} does not exist in the repository",
105                             new String[]{getDependencyString( dependency )} );
106                         addFailure( reporter, sourceArtifact, "missing-dependency:" + getDependencyKey( dependency ),
107                                     reason );
108                     }
109                 }
110                 catch ( InvalidVersionSpecificationException e )
111                 {
112                     String reason = MessageFormat.format( "Artifact''s dependency {0} contains an invalid version {1}",
113                                                           new String[]{getDependencyString( dependency ),
114                                                               dependency.getVersion()} );
115                     addFailure( reporter, sourceArtifact, "bad-version:" + getDependencyKey( dependency ), reason );
116                 }
117             }
118         }
119     }
120
121     private String getDependencyKey( Dependency dependency )
122     {
123         String str = dependency.getGroupId();
124         str += ":" + dependency.getArtifactId();
125         str += ":" + dependency.getVersion();
126         str += ":" + dependency.getType();
127         if ( dependency.getClassifier() != null )
128         {
129             str += ":" + dependency.getClassifier();
130         }
131         return str;
132     }
133
134     static String getDependencyString( Dependency dependency )
135     {
136         String str = "(group=" + dependency.getGroupId();
137         str += ", artifact=" + dependency.getArtifactId();
138         str += ", version=" + dependency.getVersion();
139         str += ", type=" + dependency.getType();
140         if ( dependency.getClassifier() != null )
141         {
142             str += ", classifier=" + dependency.getClassifier();
143         }
144         str += ")";
145         return str;
146     }
147
148     private Artifact createArtifact( Dependency dependency )
149         throws InvalidVersionSpecificationException
150     {
151         VersionRange spec = VersionRange.createFromVersionSpec( dependency.getVersion() );
152
153         if ( spec == null )
154         {
155             throw new InvalidVersionSpecificationException( "Dependency version was null" );
156         }
157
158         return artifactFactory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), spec,
159                                                          dependency.getType(), dependency.getClassifier(),
160                                                          dependency.getScope() );
161     }
162 }