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