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.eclipse.aether.resolution;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.eclipse.aether.RepositorySystem;
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.repository.ArtifactRepository;
28  import org.eclipse.aether.repository.LocalArtifactResult;
29  import org.eclipse.aether.transfer.ArtifactNotFoundException;
30  
31  import static java.util.Objects.requireNonNull;
32  
33  /**
34   * The result of an artifact resolution request.
35   *
36   * @see RepositorySystem#resolveArtifacts(org.eclipse.aether.RepositorySystemSession, java.util.Collection)
37   * @see Artifact#getFile()
38   */
39  public final class ArtifactResult {
40  
41      private final ArtifactRequest request;
42  
43      private List<Exception> exceptions;
44  
45      private Artifact artifact;
46  
47      private ArtifactRepository repository;
48  
49      private LocalArtifactResult localArtifactResult;
50  
51      /**
52       * Creates a new result for the specified request.
53       *
54       * @param request The resolution request, must not be {@code null}.
55       */
56      public ArtifactResult(ArtifactRequest request) {
57          this.request = requireNonNull(request, "artifact request cannot be null");
58          exceptions = Collections.emptyList();
59      }
60  
61      /**
62       * Gets the resolution request that was made.
63       *
64       * @return The resolution request, never {@code null}.
65       */
66      public ArtifactRequest getRequest() {
67          return request;
68      }
69  
70      /**
71       * Gets the resolved artifact (if any). Use {@link #getExceptions()} to query the errors that occurred while trying
72       * to resolve the artifact.
73       *
74       * @return The resolved artifact or {@code null} if the resolution failed.
75       */
76      public Artifact getArtifact() {
77          return artifact;
78      }
79  
80      /**
81       * Sets the resolved artifact.
82       *
83       * @param artifact The resolved artifact, may be {@code null} if the resolution failed.
84       * @return This result for chaining, never {@code null}.
85       */
86      public ArtifactResult setArtifact(Artifact artifact) {
87          this.artifact = artifact;
88          return this;
89      }
90  
91      /**
92       * Gets the exceptions that occurred while resolving the artifact. Note that this list can be non-empty even if the
93       * artifact was successfully resolved, e.g. when one of the contacted remote repositories didn't contain the
94       * artifact but a later repository eventually contained it.
95       *
96       * @return The exceptions that occurred, never {@code null}.
97       * @see #isResolved()
98       */
99      public List<Exception> getExceptions() {
100         return exceptions;
101     }
102 
103     /**
104      * Records the specified exception while resolving the artifact.
105      *
106      * @param exception The exception to record, may be {@code null}.
107      * @return This result for chaining, never {@code null}.
108      */
109     public ArtifactResult addException(Exception exception) {
110         if (exception != null) {
111             if (exceptions.isEmpty()) {
112                 exceptions = new ArrayList<>();
113             }
114             exceptions.add(exception);
115         }
116         return this;
117     }
118 
119     /**
120      * Gets the repository from which the artifact was eventually resolved. Note that successive resolutions of the same
121      * artifact might yield different results if the employed local repository does not track the origin of an artifact.
122      *
123      * @return The repository from which the artifact was resolved or {@code null} if unknown.
124      */
125     public ArtifactRepository getRepository() {
126         return repository;
127     }
128 
129     /**
130      * Sets the repository from which the artifact was resolved.
131      *
132      * @param repository The repository from which the artifact was resolved, may be {@code null}.
133      * @return This result for chaining, never {@code null}.
134      */
135     public ArtifactResult setRepository(ArtifactRepository repository) {
136         this.repository = repository;
137         return this;
138     }
139 
140     /**
141      * Gets the {@link LocalArtifactResult} received during artifact resolution.
142      *
143      * @return The {@link LocalArtifactResult} or {@code null}.
144      * @since 1.9.6
145      */
146     public LocalArtifactResult getLocalArtifactResult() {
147         return localArtifactResult;
148     }
149 
150     /**
151      * Sets the {@link LocalArtifactResult} that is received during artifact resolution.
152      *
153      * @param localArtifactResult The local artifact result.
154      * @since 1.9.6
155      */
156     public void setLocalArtifactResult(LocalArtifactResult localArtifactResult) {
157         this.localArtifactResult = localArtifactResult;
158     }
159 
160     /**
161      * Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully
162      * resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some
163      * of the specified remote repositories.
164      *
165      * @return {@code true} if the artifact was resolved, {@code false} otherwise.
166      * @see Artifact#getFile()
167      */
168     public boolean isResolved() {
169         return getArtifact() != null && getArtifact().getFile() != null;
170     }
171 
172     /**
173      * Indicates whether the requested artifact is not present in any of the specified repositories.
174      *
175      * @return {@code true} if the artifact is not present in any repository, {@code false} otherwise.
176      */
177     public boolean isMissing() {
178         for (Exception e : getExceptions()) {
179             if (!(e instanceof ArtifactNotFoundException)) {
180                 return false;
181             }
182         }
183         return !isResolved();
184     }
185 
186     @Override
187     public String toString() {
188         return getArtifact() + " < " + getRepository();
189     }
190 }