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