001package org.apache.maven.artifact.resolver;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.List;
025
026import org.codehaus.plexus.component.annotations.Component;
027
028/**
029 * @author Benjamin Bentmann
030 */
031@Component( role = ResolutionErrorHandler.class )
032public class DefaultResolutionErrorHandler
033    implements ResolutionErrorHandler
034{
035
036    public void throwErrors( ArtifactResolutionRequest request, ArtifactResolutionResult result )
037        throws ArtifactResolutionException
038    {
039        // Metadata cannot be found
040
041        if ( result.hasMetadataResolutionExceptions() )
042        {
043            throw result.getMetadataResolutionException( 0 );
044        }
045
046        // Metadata cannot be retrieved
047
048        // Cyclic Dependency Error
049
050        if ( result.hasCircularDependencyExceptions() )
051        {
052            throw result.getCircularDependencyException( 0 );
053        }
054
055        // Version Range Violation
056
057        if ( result.hasVersionRangeViolations() )
058        {
059            throw result.getVersionRangeViolation( 0 );
060        }
061
062        // Transfer Error
063
064        if ( result.hasErrorArtifactExceptions() )
065        {
066            throw result.getErrorArtifactExceptions().get( 0 );
067        }
068
069        if ( result.hasMissingArtifacts() )
070        {
071            throw new MultipleArtifactsNotFoundException( request.getArtifact(), toList( result.getArtifacts() ),
072                                                          result.getMissingArtifacts(),
073                                                          request.getRemoteRepositories() );
074        }
075
076        // this should never happen since we checked all possible error sources before but better be sure
077        if ( result.hasExceptions() )
078        {
079            throw new ArtifactResolutionException( "Unknown error during artifact resolution, " + request + ", "
080                + result.getExceptions(), request.getArtifact(), request.getRemoteRepositories() );
081        }
082    }
083
084    private static <T> List<T> toList( Collection<T> items )
085    {
086        return ( items != null ) ? new ArrayList<T>( items ) : null;
087    }
088
089}