1 package org.apache.maven.archiva.reporting.processor;
4 * Copyright 2005-2006 The Apache Software Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
30 import java.text.MessageFormat;
31 import java.util.Iterator;
32 import java.util.List;
35 * @plexus.component role="org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor" role-hint="dependency"
37 public class DependencyArtifactReportProcessor
38 implements ArtifactReportProcessor
43 private ArtifactFactory artifactFactory;
48 private RepositoryQueryLayerFactory layerFactory;
50 private static final String POM = "pom";
52 private static final String ROLE_HINT = "dependency";
54 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
56 RepositoryQueryLayer queryLayer = layerFactory.createRepositoryQueryLayer( artifact.getRepository() );
57 if ( !queryLayer.containsArtifact( artifact ) )
59 // TODO: is this even possible?
60 addFailure( reporter, artifact, "missing-artifact", "Artifact does not exist in the repository" );
63 if ( model != null && POM.equals( artifact.getType() ) )
65 List dependencies = model.getDependencies();
66 processDependencies( dependencies, reporter, queryLayer, artifact );
70 private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
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 );
76 private void processDependencies( List dependencies, ReportingDatabase reporter,
77 RepositoryQueryLayer repositoryQueryLayer, Artifact sourceArtifact )
79 if ( dependencies.size() > 0 )
81 Iterator iterator = dependencies.iterator();
82 while ( iterator.hasNext() )
84 Dependency dependency = (Dependency) iterator.next();
88 Artifact artifact = createArtifact( dependency );
90 // TODO: handle ranges properly. We should instead be mapping out all the artifacts in the
91 // repository and mapping out the graph
93 if ( artifact.getVersion() == null )
95 // it was a range, for now presume it exists
99 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
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 ),
108 catch ( InvalidVersionSpecificationException e )
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 );
119 private String getDependencyKey( Dependency dependency )
121 String str = dependency.getGroupId();
122 str += ":" + dependency.getArtifactId();
123 str += ":" + dependency.getVersion();
124 str += ":" + dependency.getType();
125 if ( dependency.getClassifier() != null )
127 str += ":" + dependency.getClassifier();
132 static String getDependencyString( Dependency dependency )
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 )
140 str += ", classifier=" + dependency.getClassifier();
146 private Artifact createArtifact( Dependency dependency )
147 throws InvalidVersionSpecificationException
149 VersionRange spec = VersionRange.createFromVersionSpec( dependency.getVersion() );
153 throw new InvalidVersionSpecificationException( "Dependency version was null" );
156 return artifactFactory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), spec,
157 dependency.getType(), dependency.getClassifier(),
158 dependency.getScope() );