1 package org.apache.maven.archiva.reporting;
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.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.metadata.Plugin;
23 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
24 import org.apache.maven.artifact.repository.metadata.Snapshot;
25 import org.apache.maven.artifact.repository.metadata.Versioning;
26 import org.codehaus.plexus.util.FileUtils;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
38 * This class will report on bad metadata files. These include invalid version declarations and incomplete version
39 * information inside the metadata file. Plugin metadata will be checked for validity of the latest plugin artifacts.
41 * @plexus.component role="org.apache.maven.archiva.reporting.MetadataReportProcessor" role-hint="bad-metadata"
43 public class BadMetadataReportProcessor
44 implements MetadataReportProcessor
49 private ArtifactFactory artifactFactory;
54 private RepositoryQueryLayerFactory repositoryQueryLayerFactory;
57 * Process the metadata encountered in the repository and report all errors found, if any.
59 * @param metadata the metadata to be processed.
60 * @param repository the repository where the metadata was encountered
61 * @param reporter the ArtifactReporter to receive processing results
62 * @throws ReportProcessorException if an error was occurred while processing the metadata
64 public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter )
65 throws ReportProcessorException
67 boolean hasFailures = false;
69 if ( metadata.storedInGroupDirectory() )
73 hasFailures = checkPluginMetadata( metadata, repository, reporter );
75 catch ( IOException e )
77 throw new ReportProcessorException( "Error getting plugin artifact directories versions", e );
82 String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated();
83 if ( lastUpdated == null || lastUpdated.length() == 0 )
85 reporter.addFailure( metadata, "Missing lastUpdated element inside the metadata." );
89 if ( metadata.storedInArtifactVersionDirectory() )
91 hasFailures |= checkSnapshotMetadata( metadata, repository, reporter );
95 if ( !checkMetadataVersions( metadata, repository, reporter ) )
102 if ( checkRepositoryVersions( metadata, repository, reporter ) )
107 catch ( IOException e )
109 throw new ReportProcessorException( "Error getting versions", e );
116 reporter.addSuccess( metadata );
121 * Method for processing a GroupRepositoryMetadata
123 * @param metadata the metadata to be processed.
124 * @param repository the repository where the metadata was encountered
125 * @param reporter the ArtifactReporter to receive processing results
127 private boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
128 ArtifactReporter reporter )
131 boolean hasFailures = false;
134 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( metadata ) ).getParentFile();
135 List pluginDirs = getArtifactIdFiles( metadataDir );
137 Map prefixes = new HashMap();
138 for ( Iterator plugins = metadata.getMetadata().getPlugins().iterator(); plugins.hasNext(); )
140 Plugin plugin = (Plugin) plugins.next();
142 String artifactId = plugin.getArtifactId();
143 if ( artifactId == null || artifactId.length() == 0 )
145 reporter.addFailure( metadata, "Missing or empty artifactId in group metadata." );
149 String prefix = plugin.getPrefix();
150 if ( prefix == null || prefix.length() == 0 )
152 reporter.addFailure( metadata, "Missing or empty plugin prefix for artifactId " + artifactId + "." );
157 if ( prefixes.containsKey( prefix ) )
159 reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix + "." );
164 prefixes.put( prefix, plugin );
168 if ( artifactId != null && artifactId.length() > 0 )
170 File pluginDir = new File( metadataDir, artifactId );
171 if ( !pluginDirs.contains( pluginDir ) )
173 reporter.addFailure( metadata, "Metadata plugin " + artifactId + " not found in the repository" );
178 pluginDirs.remove( pluginDir );
183 if ( pluginDirs.size() > 0 )
185 for ( Iterator plugins = pluginDirs.iterator(); plugins.hasNext(); )
187 File plugin = (File) plugins.next();
188 reporter.addFailure( metadata, "Plugin " + plugin.getName() + " is present in the repository but " +
189 "missing in the metadata." );
198 * Method for processing a SnapshotArtifactRepository
200 * @param metadata the metadata to be processed.
201 * @param repository the repository where the metadata was encountered
202 * @param reporter the ArtifactReporter to receive processing results
204 private boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
205 ArtifactReporter reporter )
207 RepositoryQueryLayer repositoryQueryLayer =
208 repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
210 boolean hasFailures = false;
212 Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot();
213 String timestamp = snapshot.getTimestamp();
214 String buildNumber = String.valueOf( snapshot.getBuildNumber() );
216 Artifact artifact = createArtifact( metadata );
217 if ( !repositoryQueryLayer.containsArtifact( artifact, snapshot ) )
219 reporter.addFailure( metadata, "Snapshot artifact " + timestamp + "-" + buildNumber + " does not exist." );
227 * Method for validating the versions declared inside an ArtifactRepositoryMetadata
229 * @param metadata the metadata to be processed.
230 * @param repository the repository where the metadata was encountered
231 * @param reporter the ArtifactReporter to receive processing results
233 private boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository,
234 ArtifactReporter reporter )
236 RepositoryQueryLayer repositoryQueryLayer =
237 repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
239 boolean hasFailures = false;
240 Versioning versioning = metadata.getMetadata().getVersioning();
241 for ( Iterator versions = versioning.getVersions().iterator(); versions.hasNext(); )
243 String version = (String) versions.next();
245 Artifact artifact = createArtifact( metadata, version );
247 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
249 reporter.addFailure( metadata, "Artifact version " + version + " is present in metadata but " +
250 "missing in the repository." );
258 * Searches the artifact repository directory for all versions and verifies that all of them are listed in the
259 * ArtifactRepositoryMetadata
261 * @param metadata the metadata to be processed.
262 * @param repository the repository where the metadata was encountered
263 * @param reporter the ArtifactReporter to receive processing results
265 private boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
266 ArtifactReporter reporter )
269 boolean hasFailures = false;
270 Versioning versioning = metadata.getMetadata().getVersioning();
272 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( metadata ) ).getParentFile();
273 List versions = FileUtils.getFileNames( versionsDir, "*/*.pom", null, false );
274 for ( Iterator i = versions.iterator(); i.hasNext(); )
276 File path = new File( (String) i.next() );
277 String version = path.getParentFile().getName();
278 if ( !versioning.getVersions().contains( version ) )
280 reporter.addFailure( metadata, "Artifact version " + version + " found in the repository but " +
281 "missing in the metadata." );
289 * Used to create an artifact object from a metadata base version
291 private Artifact createArtifact( RepositoryMetadata metadata )
293 return artifactFactory.createProjectArtifact( metadata.getGroupId(), metadata.getArtifactId(),
294 metadata.getBaseVersion() );
298 * Used to create an artifact object with a specified version
300 private Artifact createArtifact( RepositoryMetadata metadata, String version )
302 return artifactFactory.createProjectArtifact( metadata.getGroupId(), metadata.getArtifactId(), version );
306 * Used to gather artifactIds from a groupId directory
308 private List getArtifactIdFiles( File groupIdDir )
311 List artifactIdFiles = new ArrayList();
313 List fileArray = new ArrayList( Arrays.asList( groupIdDir.listFiles() ) );
314 for ( Iterator files = fileArray.iterator(); files.hasNext(); )
316 File artifactDir = (File) files.next();
318 if ( artifactDir.isDirectory() )
320 List versions = FileUtils.getFileNames( artifactDir, "*/*.pom", null, false );
321 if ( versions.size() > 0 )
323 artifactIdFiles.add( artifactDir );
328 return artifactIdFiles;