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 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      {
76          return originatingArtifact;
77      }
78  
79      public ArtifactResolutionResult setOriginatingArtifact( final Artifact originatingArtifact )
80      {
81          this.originatingArtifact = originatingArtifact;
82  
83          return this;
84      }
85  
86      public void addArtifact( Artifact artifact )
87      {
88          if ( artifacts == null )
89          {
90              artifacts = new LinkedHashSet<>();
91          }
92  
93          artifacts.add( artifact );
94      }
95  
96      public Set<Artifact> getArtifacts()
97      {
98          if ( artifacts == null )
99          {
100             artifacts = new LinkedHashSet<>();
101         }
102 
103         return artifacts;
104     }
105 
106     public void setArtifacts( Set<Artifact> artifacts )
107     {
108         this.artifacts = artifacts;
109     }
110 
111     public Set<ResolutionNode> getArtifactResolutionNodes()
112     {
113         if ( resolutionNodes == null )
114         {
115             resolutionNodes = new LinkedHashSet<>();
116         }
117 
118         return resolutionNodes;
119     }
120 
121     public void setArtifactResolutionNodes( Set<ResolutionNode> resolutionNodes )
122     {
123         this.resolutionNodes = resolutionNodes;
124     }
125 
126     public boolean hasMissingArtifacts()
127     {
128         return missingArtifacts != null && !missingArtifacts.isEmpty();
129     }
130 
131     public List<Artifact> getMissingArtifacts()
132     {
133         return missingArtifacts == null
134                    ? Collections.<Artifact>emptyList()
135                    : Collections.unmodifiableList( missingArtifacts );
136 
137     }
138 
139     public ArtifactResolutionResult addMissingArtifact( Artifact artifact )
140     {
141         missingArtifacts = initList( missingArtifacts );
142 
143         missingArtifacts.add( artifact );
144 
145         return this;
146     }
147 
148     public ArtifactResolutionResult setUnresolvedArtifacts( final List<Artifact> unresolvedArtifacts )
149     {
150         this.missingArtifacts = unresolvedArtifacts;
151 
152         return this;
153     }
154 
155     public boolean isSuccess()
156     {
157         return !( hasMissingArtifacts() || hasExceptions() );
158     }
159 
160     // ------------------------------------------------------------------------
161     // Exceptions
162     // ------------------------------------------------------------------------
163 
164     public boolean hasExceptions()
165     {
166         return exceptions != null && !exceptions.isEmpty();
167     }
168 
169     public List<Exception> getExceptions()
170     {
171         return exceptions == null
172                    ? Collections.<Exception>emptyList()
173                    : Collections.unmodifiableList( exceptions );
174 
175     }
176 
177     // ------------------------------------------------------------------------
178     // Version Range Violations
179     // ------------------------------------------------------------------------
180 
181     public boolean hasVersionRangeViolations()
182     {
183         return versionRangeViolations != null;
184     }
185 
186     /**
187      * TODO this needs to accept a {@link OverConstrainedVersionException} as returned by
188      *       {@link #getVersionRangeViolation(int)} but it's not used like that in
189      *       DefaultLegacyArtifactCollector
190      */
191     public ArtifactResolutionResult addVersionRangeViolation( Exception e )
192     {
193         versionRangeViolations = initList( versionRangeViolations );
194 
195         versionRangeViolations.add( e );
196 
197         exceptions = initList( exceptions );
198 
199         exceptions.add( e );
200 
201         return this;
202     }
203 
204     public OverConstrainedVersionException getVersionRangeViolation( int i )
205     {
206         return (OverConstrainedVersionException) versionRangeViolations.get( i );
207     }
208 
209     public List<Exception> getVersionRangeViolations()
210     {
211         return versionRangeViolations == null
212                    ? Collections.<Exception>emptyList()
213                    : Collections.unmodifiableList( versionRangeViolations );
214 
215     }
216 
217     // ------------------------------------------------------------------------
218     // Metadata Resolution Exceptions: ArtifactResolutionExceptions
219     // ------------------------------------------------------------------------
220 
221     public boolean hasMetadataResolutionExceptions()
222     {
223         return metadataResolutionExceptions != null;
224     }
225 
226     public ArtifactResolutionResult addMetadataResolutionException( ArtifactResolutionException e )
227     {
228         metadataResolutionExceptions = initList( metadataResolutionExceptions );
229 
230         metadataResolutionExceptions.add( e );
231 
232         exceptions = initList( exceptions );
233 
234         exceptions.add( e );
235 
236         return this;
237     }
238 
239     public ArtifactResolutionException getMetadataResolutionException( int i )
240     {
241         return metadataResolutionExceptions.get( i );
242     }
243 
244     public List<ArtifactResolutionException> getMetadataResolutionExceptions()
245     {
246         return metadataResolutionExceptions == null
247                    ? Collections.<ArtifactResolutionException>emptyList()
248                    : Collections.unmodifiableList( metadataResolutionExceptions );
249 
250     }
251 
252     // ------------------------------------------------------------------------
253     // ErrorArtifactExceptions: ArtifactResolutionExceptions
254     // ------------------------------------------------------------------------
255 
256     public boolean hasErrorArtifactExceptions()
257     {
258         return errorArtifactExceptions != null;
259     }
260 
261     public ArtifactResolutionResult addErrorArtifactException( ArtifactResolutionException e )
262     {
263         errorArtifactExceptions = initList( errorArtifactExceptions );
264 
265         errorArtifactExceptions.add( e );
266 
267         exceptions = initList( exceptions );
268 
269         exceptions.add( e );
270 
271         return this;
272     }
273 
274     public List<ArtifactResolutionException> getErrorArtifactExceptions()
275     {
276         if ( errorArtifactExceptions == null )
277         {
278             return Collections.emptyList();
279         }
280 
281         return Collections.unmodifiableList( errorArtifactExceptions );
282     }
283 
284     // ------------------------------------------------------------------------
285     // Circular Dependency Exceptions
286     // ------------------------------------------------------------------------
287 
288     public boolean hasCircularDependencyExceptions()
289     {
290         return circularDependencyExceptions != null;
291     }
292 
293     public ArtifactResolutionResult addCircularDependencyException( CyclicDependencyException e )
294     {
295         circularDependencyExceptions = initList( circularDependencyExceptions );
296 
297         circularDependencyExceptions.add( e );
298 
299         exceptions = initList( exceptions );
300 
301         exceptions.add( e );
302 
303         return this;
304     }
305 
306     public CyclicDependencyException getCircularDependencyException( int i )
307     {
308         return circularDependencyExceptions.get( i );
309     }
310 
311     public List<CyclicDependencyException> getCircularDependencyExceptions()
312     {
313         if ( circularDependencyExceptions == null )
314         {
315             return Collections.emptyList();
316         }
317 
318         return Collections.unmodifiableList( circularDependencyExceptions );
319     }
320 
321     // ------------------------------------------------------------------------
322     // Repositories
323     // ------------------------------------------------------------------------
324 
325     public List<ArtifactRepository> getRepositories()
326     {
327         if ( repositories == null )
328         {
329             return Collections.emptyList();
330         }
331 
332         return Collections.unmodifiableList( repositories );
333     }
334 
335     public ArtifactResolutionResult setRepositories( final List<ArtifactRepository> repositories )
336     {
337         this.repositories = repositories;
338 
339         return this;
340     }
341 
342     //
343     // Internal
344     //
345 
346     private <T> List<T> initList( final List<T> l )
347     {
348         if ( l == null )
349         {
350             return new ArrayList<>();
351         }
352         return l;
353     }
354 
355     public String toString()
356     {
357         StringBuilder sb = new StringBuilder();
358 
359         if ( artifacts != null )
360         {
361             int i = 1;
362             sb.append( "---------\n" );
363             sb.append( artifacts.size() ).append( '\n' );
364             for ( Artifact a : artifacts )
365             {
366                 sb.append( i ).append( ' ' ).append( a ).append( '\n' );
367                 i++;
368             }
369             sb.append( "---------\n" );
370         }
371 
372         return sb.toString();
373     }
374 }