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  import javax.inject.Named;
25  import javax.inject.Singleton;
26  
27  /**
28   * @author Benjamin Bentmann
29   */
30  @Named
31  @Singleton
32  public class DefaultResolutionErrorHandler implements ResolutionErrorHandler {
33  
34      public void throwErrors(ArtifactResolutionRequest request, ArtifactResolutionResult result)
35              throws ArtifactResolutionException {
36          // Metadata cannot be found
37  
38          if (result.hasMetadataResolutionExceptions()) {
39              throw result.getMetadataResolutionException(0);
40          }
41  
42          // Metadata cannot be retrieved
43  
44          // Cyclic Dependency Error
45  
46          if (result.hasCircularDependencyExceptions()) {
47              throw result.getCircularDependencyException(0);
48          }
49  
50          // Version Range Violation
51  
52          if (result.hasVersionRangeViolations()) {
53              throw result.getVersionRangeViolation(0);
54          }
55  
56          // Transfer Error
57  
58          if (result.hasErrorArtifactExceptions()) {
59              throw result.getErrorArtifactExceptions().get(0);
60          }
61  
62          if (result.hasMissingArtifacts()) {
63              throw new MultipleArtifactsNotFoundException(
64                      request.getArtifact(),
65                      toList(result.getArtifacts()),
66                      result.getMissingArtifacts(),
67                      request.getRemoteRepositories());
68          }
69  
70          // this should never happen since we checked all possible error sources before but better be sure
71          if (result.hasExceptions()) {
72              throw new ArtifactResolutionException(
73                      "Unknown error during artifact resolution, " + request + ", " + result.getExceptions(),
74                      request.getArtifact(),
75                      request.getRemoteRepositories());
76          }
77      }
78  
79      private static <T> List<T> toList(Collection<T> items) {
80          return (items != null) ? new ArrayList<>(items) : null;
81      }
82  }