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.Collections;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
30  
31  /**
32   * Specific problems during resolution that we want to account for:
33   * <ul>
34   *   <li>missing metadata</li>
35   *   <li>version range violations</li>
36   *   <li>version circular dependencies</li>
37   *   <li>missing artifacts</li>
38   *   <li>network/transfer errors</li>
39   *   <li>file system errors: permissions</li>
40   * </ul>
41   *
42   * @author Jason van Zyl
43   * TODO carlos: all these possible has*Exceptions and get*Exceptions methods make the clients too
44   *       complex requiring a long list of checks, need to create a parent/interface/encapsulation
45   *       for the types of exceptions
46   */
47  public class ArtifactResolutionResult {
48      private Artifact originatingArtifact;
49  
50      private List<Artifact> missingArtifacts;
51  
52      // Exceptions
53  
54      private List<Exception> exceptions;
55  
56      private List<Exception> versionRangeViolations;
57  
58      private List<ArtifactResolutionException> metadataResolutionExceptions;
59  
60      private List<CyclicDependencyException> circularDependencyExceptions;
61  
62      private List<ArtifactResolutionException> errorArtifactExceptions;
63  
64      // file system errors
65  
66      private List<ArtifactRepository> repositories;
67  
68      private Set<Artifact> artifacts;
69  
70      private Set<ResolutionNode> resolutionNodes;
71  
72      public Artifact getOriginatingArtifact() {
73          return originatingArtifact;
74      }
75  
76      public ArtifactResolutionResult setOriginatingArtifact(final Artifact originatingArtifact) {
77          this.originatingArtifact = originatingArtifact;
78  
79          return this;
80      }
81  
82      public void addArtifact(Artifact artifact) {
83          if (artifacts == null) {
84              artifacts = new LinkedHashSet<>();
85          }
86  
87          artifacts.add(artifact);
88      }
89  
90      public Set<Artifact> getArtifacts() {
91          if (artifacts == null) {
92              artifacts = new LinkedHashSet<>();
93          }
94  
95          return artifacts;
96      }
97  
98      public void setArtifacts(Set<Artifact> artifacts) {
99          this.artifacts = artifacts;
100     }
101 
102     public Set<ResolutionNode> getArtifactResolutionNodes() {
103         if (resolutionNodes == null) {
104             resolutionNodes = new LinkedHashSet<>();
105         }
106 
107         return resolutionNodes;
108     }
109 
110     public void setArtifactResolutionNodes(Set<ResolutionNode> resolutionNodes) {
111         this.resolutionNodes = resolutionNodes;
112     }
113 
114     public boolean hasMissingArtifacts() {
115         return missingArtifacts != null && !missingArtifacts.isEmpty();
116     }
117 
118     public List<Artifact> getMissingArtifacts() {
119         return missingArtifacts == null
120                 ? Collections.<Artifact>emptyList()
121                 : Collections.unmodifiableList(missingArtifacts);
122     }
123 
124     public ArtifactResolutionResult addMissingArtifact(Artifact artifact) {
125         missingArtifacts = initList(missingArtifacts);
126 
127         missingArtifacts.add(artifact);
128 
129         return this;
130     }
131 
132     public ArtifactResolutionResult setUnresolvedArtifacts(final List<Artifact> unresolvedArtifacts) {
133         this.missingArtifacts = unresolvedArtifacts;
134 
135         return this;
136     }
137 
138     public boolean isSuccess() {
139         return !(hasMissingArtifacts() || hasExceptions());
140     }
141 
142     // ------------------------------------------------------------------------
143     // Exceptions
144     // ------------------------------------------------------------------------
145 
146     public boolean hasExceptions() {
147         return exceptions != null && !exceptions.isEmpty();
148     }
149 
150     public List<Exception> getExceptions() {
151         return exceptions == null ? Collections.<Exception>emptyList() : Collections.unmodifiableList(exceptions);
152     }
153 
154     // ------------------------------------------------------------------------
155     // Version Range Violations
156     // ------------------------------------------------------------------------
157 
158     public boolean hasVersionRangeViolations() {
159         return versionRangeViolations != null;
160     }
161 
162     /**
163      * TODO this needs to accept a {@link OverConstrainedVersionException} as returned by
164      *       {@link #getVersionRangeViolation(int)} but it's not used like that in
165      *       DefaultLegacyArtifactCollector
166      */
167     public ArtifactResolutionResult addVersionRangeViolation(Exception e) {
168         versionRangeViolations = initList(versionRangeViolations);
169 
170         versionRangeViolations.add(e);
171 
172         exceptions = initList(exceptions);
173 
174         exceptions.add(e);
175 
176         return this;
177     }
178 
179     public OverConstrainedVersionException getVersionRangeViolation(int i) {
180         return (OverConstrainedVersionException) versionRangeViolations.get(i);
181     }
182 
183     public List<Exception> getVersionRangeViolations() {
184         return versionRangeViolations == null
185                 ? Collections.<Exception>emptyList()
186                 : Collections.unmodifiableList(versionRangeViolations);
187     }
188 
189     // ------------------------------------------------------------------------
190     // Metadata Resolution Exceptions: ArtifactResolutionExceptions
191     // ------------------------------------------------------------------------
192 
193     public boolean hasMetadataResolutionExceptions() {
194         return metadataResolutionExceptions != null;
195     }
196 
197     public ArtifactResolutionResult addMetadataResolutionException(ArtifactResolutionException e) {
198         metadataResolutionExceptions = initList(metadataResolutionExceptions);
199 
200         metadataResolutionExceptions.add(e);
201 
202         exceptions = initList(exceptions);
203 
204         exceptions.add(e);
205 
206         return this;
207     }
208 
209     public ArtifactResolutionException getMetadataResolutionException(int i) {
210         return metadataResolutionExceptions.get(i);
211     }
212 
213     public List<ArtifactResolutionException> getMetadataResolutionExceptions() {
214         return metadataResolutionExceptions == null
215                 ? Collections.<ArtifactResolutionException>emptyList()
216                 : Collections.unmodifiableList(metadataResolutionExceptions);
217     }
218 
219     // ------------------------------------------------------------------------
220     // ErrorArtifactExceptions: ArtifactResolutionExceptions
221     // ------------------------------------------------------------------------
222 
223     public boolean hasErrorArtifactExceptions() {
224         return errorArtifactExceptions != null;
225     }
226 
227     public ArtifactResolutionResult addErrorArtifactException(ArtifactResolutionException e) {
228         errorArtifactExceptions = initList(errorArtifactExceptions);
229 
230         errorArtifactExceptions.add(e);
231 
232         exceptions = initList(exceptions);
233 
234         exceptions.add(e);
235 
236         return this;
237     }
238 
239     public List<ArtifactResolutionException> getErrorArtifactExceptions() {
240         if (errorArtifactExceptions == null) {
241             return Collections.emptyList();
242         }
243 
244         return Collections.unmodifiableList(errorArtifactExceptions);
245     }
246 
247     // ------------------------------------------------------------------------
248     // Circular Dependency Exceptions
249     // ------------------------------------------------------------------------
250 
251     public boolean hasCircularDependencyExceptions() {
252         return circularDependencyExceptions != null;
253     }
254 
255     public ArtifactResolutionResult addCircularDependencyException(CyclicDependencyException e) {
256         circularDependencyExceptions = initList(circularDependencyExceptions);
257 
258         circularDependencyExceptions.add(e);
259 
260         exceptions = initList(exceptions);
261 
262         exceptions.add(e);
263 
264         return this;
265     }
266 
267     public CyclicDependencyException getCircularDependencyException(int i) {
268         return circularDependencyExceptions.get(i);
269     }
270 
271     public List<CyclicDependencyException> getCircularDependencyExceptions() {
272         if (circularDependencyExceptions == null) {
273             return Collections.emptyList();
274         }
275 
276         return Collections.unmodifiableList(circularDependencyExceptions);
277     }
278 
279     // ------------------------------------------------------------------------
280     // Repositories
281     // ------------------------------------------------------------------------
282 
283     public List<ArtifactRepository> getRepositories() {
284         if (repositories == null) {
285             return Collections.emptyList();
286         }
287 
288         return Collections.unmodifiableList(repositories);
289     }
290 
291     public ArtifactResolutionResult setRepositories(final List<ArtifactRepository> repositories) {
292         this.repositories = repositories;
293 
294         return this;
295     }
296 
297     //
298     // Internal
299     //
300 
301     private <T> List<T> initList(final List<T> l) {
302         if (l == null) {
303             return new ArrayList<>();
304         }
305         return l;
306     }
307 
308     public String toString() {
309         StringBuilder sb = new StringBuilder();
310 
311         if (artifacts != null) {
312             int i = 1;
313             sb.append("---------\n");
314             sb.append(artifacts.size()).append('\n');
315             for (Artifact a : artifacts) {
316                 sb.append(i).append(' ').append(a).append('\n');
317                 i++;
318             }
319             sb.append("---------\n");
320         }
321 
322         return sb.toString();
323     }
324 }