1 package org.apache.maven.artifact.resolver;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25
26 import org.codehaus.plexus.component.annotations.Component;
27
28 /**
29 * @author Benjamin Bentmann
30 */
31 @Component( role = ResolutionErrorHandler.class )
32 public class DefaultResolutionErrorHandler
33 implements ResolutionErrorHandler
34 {
35
36 public void throwErrors( ArtifactResolutionRequest request, ArtifactResolutionResult result )
37 throws ArtifactResolutionException
38 {
39 // Metadata cannot be found
40
41 if ( result.hasMetadataResolutionExceptions() )
42 {
43 throw result.getMetadataResolutionException( 0 );
44 }
45
46 // Metadata cannot be retrieved
47
48 // Cyclic Dependency Error
49
50 if ( result.hasCircularDependencyExceptions() )
51 {
52 throw result.getCircularDependencyException( 0 );
53 }
54
55 // Version Range Violation
56
57 if ( result.hasVersionRangeViolations() )
58 {
59 throw result.getVersionRangeViolation( 0 );
60 }
61
62 // Transfer Error
63
64 if ( result.hasErrorArtifactExceptions() )
65 {
66 throw result.getErrorArtifactExceptions().get( 0 );
67 }
68
69 if ( result.hasMissingArtifacts() )
70 {
71 throw new MultipleArtifactsNotFoundException( request.getArtifact(), toList( result.getArtifacts() ),
72 result.getMissingArtifacts(),
73 request.getRemoteRepositories() );
74 }
75
76 // this should never happen since we checked all possible error sources before but better be sure
77 if ( result.hasExceptions() )
78 {
79 throw new ArtifactResolutionException( "Unknown error during artifact resolution, " + request + ", "
80 + result.getExceptions(), request.getArtifact(), request.getRemoteRepositories() );
81 }
82 }
83
84 private static <T> List<T> toList( Collection<T> items )
85 {
86 return ( items != null ) ? new ArrayList<>( items ) : null;
87 }
88
89 }