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