From 7c7f2cfd6df17d9226cd09433c5d0adfea3fa42c Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Wed, 25 Nov 2009 12:17:38 +0000 Subject: [PATCH] [MRM-1282] improve exception handling git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@884066 13f79535-47bb-0310-9956-ffa450edef68 --- .../web/action/ShowArtifactAction.java | 11 ++++++- .../repository/DefaultMetadataResolver.java | 1 + .../metadata/repository/MetadataResolver.java | 5 ++-- .../repository/MetadataResolverException.java | 29 +++++++++++++++++++ .../Maven2RepositoryMetadataResolver.java | 18 ++++++++---- .../Maven2RepositoryMetadataResolverTest.java | 3 ++ 6 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataResolverException.java diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/ShowArtifactAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/ShowArtifactAction.java index f1b63ae2a..4d87e7203 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/ShowArtifactAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/ShowArtifactAction.java @@ -26,6 +26,7 @@ import java.util.List; import com.opensymphony.xwork2.Validateable; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.repository.MetadataResolver; +import org.apache.archiva.metadata.repository.MetadataResolverException; import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet; import org.apache.commons.lang.StringUtils; import org.apache.maven.archiva.database.ArchivaDatabaseException; @@ -120,7 +121,15 @@ public class ShowArtifactAction // we don't want the implementation being that intelligent - so another resolver to do the // "just-in-time" nature of picking up the metadata (if appropriate for the repository type) is used - versionMetadata = metadataResolver.getProjectVersion( repoId, groupId, artifactId, version ); + try + { + versionMetadata = metadataResolver.getProjectVersion( repoId, groupId, artifactId, version ); + } + catch ( MetadataResolverException e ) + { + addActionError( "Error occurred resolving metadata for project: " + e.getMessage() ); + return ERROR; + } if ( versionMetadata != null ) { repositoryId = repoId; diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/DefaultMetadataResolver.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/DefaultMetadataResolver.java index 0fb3cbabb..e058ca22d 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/DefaultMetadataResolver.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/DefaultMetadataResolver.java @@ -49,6 +49,7 @@ public class DefaultMetadataResolver } public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId, String projectVersion ) + throws MetadataResolverException { ProjectVersionMetadata metadata = metadataRepository.getProjectVersion( repoId, namespace, projectId, projectVersion ); // TODO: do we want to detect changes as well by comparing timestamps? isProjectVersionNewerThan(updated) diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataResolver.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataResolver.java index 7050b0552..7e6aaef7d 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataResolver.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataResolver.java @@ -21,14 +21,15 @@ package org.apache.archiva.metadata.repository; import java.util.Collection; -import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.model.ProjectMetadata; +import org.apache.archiva.metadata.model.ProjectVersionMetadata; public interface MetadataResolver { ProjectMetadata getProject( String repoId, String namespace, String projectId ); - ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId, String projectVersion ); + ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId, String projectVersion ) + throws MetadataResolverException; Collection getArtifactVersions( String repoId, String namespace, String projectId, String projectVersion ); } diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataResolverException.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataResolverException.java new file mode 100644 index 000000000..c62a2577f --- /dev/null +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataResolverException.java @@ -0,0 +1,29 @@ +package org.apache.archiva.metadata.repository; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +public class MetadataResolverException + extends Exception +{ + public MetadataResolverException( String message, Exception cause ) + { + super( message, cause ); + } +} diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java index b3fa377cd..a18d4f57d 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java @@ -27,6 +27,7 @@ import java.util.List; import org.apache.archiva.metadata.model.ProjectMetadata; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.repository.MetadataResolver; +import org.apache.archiva.metadata.repository.MetadataResolverException; import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator; import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.configuration.ArchivaConfiguration; @@ -42,6 +43,8 @@ import org.apache.maven.model.building.DefaultModelBuildingRequest; import org.apache.maven.model.building.ModelBuilder; import org.apache.maven.model.building.ModelBuildingException; import org.apache.maven.model.building.ModelBuildingRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @plexus.component role="org.apache.archiva.metadata.repository.MetadataResolver" role-hint="maven2" @@ -64,6 +67,8 @@ public class Maven2RepositoryMetadataResolver */ private RepositoryPathTranslator pathTranslator; + private final static Logger log = LoggerFactory.getLogger( Maven2RepositoryMetadataResolver.class ); + public ProjectMetadata getProject( String repoId, String namespace, String projectId ) { throw new UnsupportedOperationException(); @@ -71,6 +76,7 @@ public class Maven2RepositoryMetadataResolver public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId, String projectVersion ) + throws MetadataResolverException { ManagedRepositoryConfiguration repositoryConfiguration = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId ); @@ -81,10 +87,11 @@ public class Maven2RepositoryMetadataResolver if ( VersionUtil.isSnapshot( projectVersion ) ) { // TODO: need much error handling here for incorrect metadata + File metadataFile = + pathTranslator.toFile( basedir, namespace, projectId, projectVersion, "maven-metadata.xml" ); try { - MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( - pathTranslator.toFile( basedir, namespace, projectId, projectVersion, "maven-metadata.xml" ) ); + MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile ); artifactVersion = artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end @@ -94,8 +101,8 @@ public class Maven2RepositoryMetadataResolver } catch ( XMLException e ) { - // TODO: handle it - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + // unable to parse metadata - log it, and continue with the version as the original SNAPSHOT version + log.warn( "Invalid metadata: " + metadataFile + " - " + e.getMessage() ); } } @@ -114,8 +121,7 @@ public class Maven2RepositoryMetadataResolver } catch ( ModelBuildingException e ) { - // TODO: handle it - throw new RuntimeException( e ); + throw new MetadataResolverException( "Unable to build Maven POM to derive metadata from: " + e.getMessage(), e ); } ProjectVersionMetadata metadata = new ProjectVersionMetadata(); diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java index a1de458d9..853738abf 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java @@ -24,6 +24,7 @@ import java.util.Arrays; import org.apache.archiva.metadata.model.License; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.repository.MetadataResolver; +import org.apache.archiva.metadata.repository.MetadataResolverException; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; @@ -59,6 +60,7 @@ public class Maven2RepositoryMetadataResolverTest } public void testGetProjectVersionMetadata() + throws MetadataResolverException { ProjectVersionMetadata metadata = resolver.getProjectVersion( TEST_REPO_ID, "org.apache.archiva", "archiva-common", "1.2.1" ); @@ -90,6 +92,7 @@ public class Maven2RepositoryMetadataResolverTest } public void testGetProjectVersionMetadataForTimestampedSnapshot() + throws MetadataResolverException { ProjectVersionMetadata metadata = resolver.getProjectVersion( TEST_REPO_ID, "org.apache", "apache", "5-SNAPSHOT" ); -- 2.39.5