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