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