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   * TODO carlos: all these possible has*Exceptions and get*Exceptions methods make the clients too
43   *       complex requiring a long list of checks, need to create a parent/interface/encapsulation
44   *       for the types of exceptions
45   */
46  @Deprecated
47  public class ArtifactResolutionResult {
48      private static final String LS = System.lineSeparator();
49  
50      private Artifact originatingArtifact;
51  
52      private List<Artifact> missingArtifacts;
53  
54      // Exceptions
55  
56      private List<Exception> exceptions;
57  
58      private List<Exception> versionRangeViolations;
59  
60      private List<ArtifactResolutionException> metadataResolutionExceptions;
61  
62      private List<CyclicDependencyException> circularDependencyExceptions;
63  
64      private List<ArtifactResolutionException> errorArtifactExceptions;
65  
66      // file system errors
67  
68      private List<ArtifactRepository> repositories;
69  
70      private Set<Artifact> artifacts;
71  
72      private Set<ResolutionNode> resolutionNodes;
73  
74      public Artifact getOriginatingArtifact() {
75          return originatingArtifact;
76      }
77  
78      public ArtifactResolutionResult setOriginatingArtifact(final Artifact originatingArtifact) {
79          this.originatingArtifact = originatingArtifact;
80  
81          return this;
82      }
83  
84      public void addArtifact(Artifact artifact) {
85          if (artifacts == null) {
86              artifacts = new LinkedHashSet<>();
87          }
88  
89          artifacts.add(artifact);
90      }
91  
92      public Set<Artifact> getArtifacts() {
93          if (artifacts == null) {
94              artifacts = new LinkedHashSet<>();
95          }
96  
97          return artifacts;
98      }
99  
100     public void setArtifacts(Set<Artifact> artifacts) {
101         this.artifacts = artifacts;
102     }
103 
104     public Set<ResolutionNode> getArtifactResolutionNodes() {
105         if (resolutionNodes == null) {
106             resolutionNodes = new LinkedHashSet<>();
107         }
108 
109         return resolutionNodes;
110     }
111 
112     public void setArtifactResolutionNodes(Set<ResolutionNode> resolutionNodes) {
113         this.resolutionNodes = resolutionNodes;
114     }
115 
116     public boolean hasMissingArtifacts() {
117         return missingArtifacts != null && !missingArtifacts.isEmpty();
118     }
119 
120     public List<Artifact> getMissingArtifacts() {
121         return missingArtifacts == null ? Collections.emptyList() : 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.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      * @param e an exception
168      * @return {@code this}
169      */
170     public ArtifactResolutionResult addVersionRangeViolation(Exception e) {
171         versionRangeViolations = initList(versionRangeViolations);
172 
173         versionRangeViolations.add(e);
174 
175         exceptions = initList(exceptions);
176 
177         exceptions.add(e);
178 
179         return this;
180     }
181 
182     public OverConstrainedVersionException getVersionRangeViolation(int i) {
183         return (OverConstrainedVersionException) versionRangeViolations.get(i);
184     }
185 
186     public List<Exception> getVersionRangeViolations() {
187         return versionRangeViolations == null
188                 ? Collections.emptyList()
189                 : Collections.unmodifiableList(versionRangeViolations);
190     }
191 
192     // ------------------------------------------------------------------------
193     // Metadata Resolution Exceptions: ArtifactResolutionExceptions
194     // ------------------------------------------------------------------------
195 
196     public boolean hasMetadataResolutionExceptions() {
197         return metadataResolutionExceptions != null;
198     }
199 
200     public ArtifactResolutionResult addMetadataResolutionException(ArtifactResolutionException e) {
201         metadataResolutionExceptions = initList(metadataResolutionExceptions);
202 
203         metadataResolutionExceptions.add(e);
204 
205         exceptions = initList(exceptions);
206 
207         exceptions.add(e);
208 
209         return this;
210     }
211 
212     public ArtifactResolutionException getMetadataResolutionException(int i) {
213         return metadataResolutionExceptions.get(i);
214     }
215 
216     public List<ArtifactResolutionException> getMetadataResolutionExceptions() {
217         return metadataResolutionExceptions == null
218                 ? Collections.emptyList()
219                 : Collections.unmodifiableList(metadataResolutionExceptions);
220     }
221 
222     // ------------------------------------------------------------------------
223     // ErrorArtifactExceptions: ArtifactResolutionExceptions
224     // ------------------------------------------------------------------------
225 
226     public boolean hasErrorArtifactExceptions() {
227         return errorArtifactExceptions != null;
228     }
229 
230     public ArtifactResolutionResult addErrorArtifactException(ArtifactResolutionException e) {
231         errorArtifactExceptions = initList(errorArtifactExceptions);
232 
233         errorArtifactExceptions.add(e);
234 
235         exceptions = initList(exceptions);
236 
237         exceptions.add(e);
238 
239         return this;
240     }
241 
242     public List<ArtifactResolutionException> getErrorArtifactExceptions() {
243         if (errorArtifactExceptions == null) {
244             return Collections.emptyList();
245         }
246 
247         return Collections.unmodifiableList(errorArtifactExceptions);
248     }
249 
250     // ------------------------------------------------------------------------
251     // Circular Dependency Exceptions
252     // ------------------------------------------------------------------------
253 
254     public boolean hasCircularDependencyExceptions() {
255         return circularDependencyExceptions != null;
256     }
257 
258     public ArtifactResolutionResult addCircularDependencyException(CyclicDependencyException e) {
259         circularDependencyExceptions = initList(circularDependencyExceptions);
260 
261         circularDependencyExceptions.add(e);
262 
263         exceptions = initList(exceptions);
264 
265         exceptions.add(e);
266 
267         return this;
268     }
269 
270     public CyclicDependencyException getCircularDependencyException(int i) {
271         return circularDependencyExceptions.get(i);
272     }
273 
274     public List<CyclicDependencyException> getCircularDependencyExceptions() {
275         if (circularDependencyExceptions == null) {
276             return Collections.emptyList();
277         }
278 
279         return Collections.unmodifiableList(circularDependencyExceptions);
280     }
281 
282     // ------------------------------------------------------------------------
283     // Repositories
284     // ------------------------------------------------------------------------
285 
286     public List<ArtifactRepository> getRepositories() {
287         if (repositories == null) {
288             return Collections.emptyList();
289         }
290 
291         return Collections.unmodifiableList(repositories);
292     }
293 
294     public ArtifactResolutionResult setRepositories(final List<ArtifactRepository> repositories) {
295         this.repositories = repositories;
296 
297         return this;
298     }
299 
300     //
301     // Internal
302     //
303 
304     private <T> List<T> initList(final List<T> l) {
305         if (l == null) {
306             return new ArrayList<>();
307         }
308         return l;
309     }
310 
311     public String toString() {
312         StringBuilder sb = new StringBuilder();
313 
314         if (artifacts != null) {
315             int i = 1;
316             sb.append("---------").append(LS);
317             sb.append(artifacts.size()).append(LS);
318             for (Artifact a : artifacts) {
319                 sb.append(i).append(' ').append(a).append(LS);
320                 i++;
321             }
322             sb.append("---------");
323         }
324 
325         return sb.toString();
326     }
327 }