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