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