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