From 649f02cc6d312f25959f0b1326bc72fb9e1e3338 Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Thu, 1 Dec 2005 08:28:12 +0000 Subject: [PATCH] PR: MRM-16 Still incomplete. Requires unit tests, waiting for abstract unit test. git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@350205 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/RepositoryFileFilter.java | 43 ++++ .../reporting/ArtifactReporter.java | 7 + .../reporting/BadMetadataReporter.java | 194 ++++++++++++++++++ .../reporting/DefaultArtifactReporter.java | 18 +- .../resources/META-INF/plexus/components.xml | 13 ++ 5 files changed, 270 insertions(+), 5 deletions(-) create mode 100644 maven-repository-reports-standard/src/main/java/org/apache/maven/repository/RepositoryFileFilter.java create mode 100644 maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/BadMetadataReporter.java diff --git a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/RepositoryFileFilter.java b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/RepositoryFileFilter.java new file mode 100644 index 000000000..5116b1cd2 --- /dev/null +++ b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/RepositoryFileFilter.java @@ -0,0 +1,43 @@ +package org.apache.maven.repository; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This class is used to ignore several files that may be present inside the repository so that the other classes + * may not worry about them and then can concentrate on doing their tasks. + * + */ +public class RepositoryFileFilter implements java.io.FileFilter +{ + public boolean accept(java.io.File pathname) + { + if ( pathname.isDirectory() ) + { + if ( ".svn".equals( pathname.getName() ) ) return false; + if ( "CVS".equals( pathname.getName() ) ) return false; + } + else + { + String name = pathname.getName(); + if ( name.endsWith( ".md5" ) ) return false; + if ( name.endsWith( ".sha1" ) ) return false; + } + + return true; + } +} diff --git a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ArtifactReporter.java b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ArtifactReporter.java index 7b64d6867..c6f59eb7f 100644 --- a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ArtifactReporter.java +++ b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ArtifactReporter.java @@ -17,6 +17,7 @@ */ import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; /** * This interface is used by the single artifact processor. @@ -34,4 +35,10 @@ public interface ArtifactReporter void addSuccess( Artifact artifact ); void addWarning( Artifact artifact, String message ); + + void addFailure( RepositoryMetadata metadata, String reason ); + + void addSuccess( RepositoryMetadata metadata ); + + void addWarning( RepositoryMetadata metadata, String message ); } diff --git a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/BadMetadataReporter.java b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/BadMetadataReporter.java new file mode 100644 index 000000000..115bdaa99 --- /dev/null +++ b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/BadMetadataReporter.java @@ -0,0 +1,194 @@ +package org.apache.maven.repository.reporting; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.util.Iterator; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.manager.WagonManager; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.Snapshot; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.apache.maven.repository.RepositoryFileFilter; +import org.apache.maven.wagon.ResourceDoesNotExistException; +import org.apache.maven.wagon.TransferFailedException; + +/** + * This class will report on bad metadata files. These include invalid version declarations and incomplete version + * information inside the metadata file. Plugin metadata will be checked for validity of the latest plugin artifacts. + * + */ +public class BadMetadataReporter implements MetadataReportProcessor +{ + private WagonManager wagon; + private ArtifactFactory artifactFactory; + + public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter ) + { + boolean hasFailures = false; + + String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated(); + if ( lastUpdated == null || lastUpdated.length() == 0 ) + { + reporter.addFailure( metadata, "Missing lastUpdated element inside the metadata." ); + hasFailures = true; + } + + if ( metadata.storedInGroupDirectory() ) + { + checkPluginMetadata( metadata, repository, reporter ); + } + else if ( metadata.storedInArtifactVersionDirectory() ) + { + //snapshot metadata + } + else + { + if ( !checkMetadataVersions( metadata, repository, reporter ) ) hasFailures = true; + + if ( checkRepositoryVersions( metadata, repository, reporter ) ) hasFailures = true; + } + + if ( !hasFailures ) reporter.addSuccess( metadata ); + } + + /** + * Checks the plugin metadata + */ + public boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository, + ArtifactReporter reporter ) + { + boolean hasFailures = false; + + + + return hasFailures; + } + + /** + * Checks the snapshot metadata + */ + public boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository, + ArtifactReporter reporter ) + { + boolean hasFailures = false; + + Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot(); + String timestamp = snapshot.getTimestamp(); + String buildNumber = String.valueOf( snapshot.getBuildNumber() ); + String artifactName = metadata.getArtifactId() + "-" + timestamp + "-" + buildNumber + ".pom"; + + //@todo use wagon instead + Artifact artifact = createArtifact( metadata ); + File artifactFile = new File ( repository.pathOf( artifact ) ); + File snapshotFile = new File( artifactFile.getParentFile(), artifactName ); + if ( !snapshotFile.exists() ) + { + reporter.addFailure( metadata, "Snapshot artifact " + artifactName + " does not exist." ); + hasFailures = true; + } + + return hasFailures; + } + + /** + * Checks the declared metadata versions if the artifacts are present in the repository + */ + public boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository, + ArtifactReporter reporter ) + { + boolean hasFailures = false; + Versioning versioning = metadata.getMetadata().getVersioning(); + for ( Iterator versions = versioning.getVersions().iterator(); versions.hasNext(); ) + { + String version = (String) versions.next(); + + Artifact artifact = createArtifact( metadata, version ); + + try + { + wagon.getArtifact( artifact, repository ); + } + catch ( TransferFailedException e ) + { + reporter.addWarning( artifact, "An error occurred during the transfer of the artifact in " + + "the repository." ); + } + catch ( ResourceDoesNotExistException e ) + { + //do nothing, will check later that this artifact has not been resolved + } + + if ( !artifact.isResolved() ) + { + reporter.addFailure( metadata, "Artifact version " + version + " is present in metadata but " + + "missing in the repository." ); + if ( !hasFailures ) hasFailures = true; + } + } + return hasFailures; + } + + /** + * Searches the artifact repository directory for all versions and verifies that all of them are listed in the + * metadata file. + */ + public boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository, + ArtifactReporter reporter ) + { + boolean hasFailures = false; + Versioning versioning = metadata.getMetadata().getVersioning(); + String repositoryPath = repository.getBasedir(); + File versionsDir = new File( repositoryPath, formatAsDirectory( metadata.getGroupId() ) + + File.pathSeparator + metadata.getArtifactId() ); + File[] versions = versionsDir.listFiles( new RepositoryFileFilter() ); + for( int idx=0; idxJohn Tolentino @@ -40,4 +36,16 @@ public void addSuccess( Artifact artifact ) public void addWarning( Artifact artifact, String message ) { } + + public void addWarning(org.apache.maven.artifact.repository.metadata.RepositoryMetadata metadata, String message) + { + } + + public void addFailure(org.apache.maven.artifact.repository.metadata.RepositoryMetadata metadata, String reason) + { + } + + public void addSuccess(org.apache.maven.artifact.repository.metadata.RepositoryMetadata metadata) + { + } } diff --git a/maven-repository-reports-standard/src/main/resources/META-INF/plexus/components.xml b/maven-repository-reports-standard/src/main/resources/META-INF/plexus/components.xml index af08238d9..a88a64d22 100644 --- a/maven-repository-reports-standard/src/main/resources/META-INF/plexus/components.xml +++ b/maven-repository-reports-standard/src/main/resources/META-INF/plexus/components.xml @@ -12,5 +12,18 @@ org.apache.maven.repository.reporting.DefaultRepositoryQueryLayer per-lookup + + org.apache.maven.repository.reporting.BadMetadataReporter + default + org.apache.maven.repository.reporting.BadMetadataReporter + + + org.apache.maven.artifact.manager.WagonManager + + + org.apache.maven.artifact.factory.ArtifactFactory + + +