001package org.apache.maven.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.LinkedHashSet;
025import java.util.List;
026import java.util.Set;
027
028import org.apache.maven.artifact.Artifact;
029import org.apache.maven.artifact.repository.ArtifactRepository;
030import org.apache.maven.artifact.resolver.ArtifactResolutionException;
031import org.apache.maven.artifact.resolver.CyclicDependencyException;
032import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
033
034/**
035 *
036 *
037 * @author Oleg Gusakov
038 *
039 */
040public class MetadataResolutionResult
041{
042    private Artifact originatingArtifact;
043
044    private List<Artifact> missingArtifacts;
045
046    // Exceptions
047
048    private List<Exception> exceptions;
049
050    private List<Exception> versionRangeViolations;
051
052    private List<ArtifactResolutionException> metadataResolutionExceptions;
053
054    private List<CyclicDependencyException> circularDependencyExceptions;
055
056    private List<ArtifactResolutionException> errorArtifactExceptions;
057
058    // file system errors
059
060    private List<ArtifactRepository> repositories;
061
062    private Set<Artifact> requestedArtifacts;
063
064    private Set<Artifact> artifacts;
065
066    private MetadataGraph dirtyTree;
067
068    private MetadataGraph resolvedTree;
069
070    private MetadataGraph resolvedGraph;
071
072    public Artifact getOriginatingArtifact()
073    {
074        return originatingArtifact;
075    }
076
077    public MetadataResolutionResult ListOriginatingArtifact( final Artifact originatingArtifact )
078    {
079        this.originatingArtifact = originatingArtifact;
080
081        return this;
082    }
083
084    public void addArtifact( Artifact artifact )
085    {
086        if ( artifacts == null )
087        {
088            artifacts = new LinkedHashSet<>();
089        }
090
091        artifacts.add( artifact );
092    }
093
094    public Set<Artifact> getArtifacts()
095    {
096        return artifacts;
097    }
098
099    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 ? Collections.<Artifact> emptyList() : missingArtifacts;
122    }
123
124    public MetadataResolutionResult addMissingArtifact( Artifact artifact )
125    {
126        missingArtifacts = initList( missingArtifacts );
127
128        missingArtifacts.add( artifact );
129
130        return this;
131    }
132
133    public MetadataResolutionResult setUnresolvedArtifacts( final List<Artifact> unresolvedArtifacts )
134    {
135        this.missingArtifacts = unresolvedArtifacts;
136
137        return this;
138    }
139
140    // ------------------------------------------------------------------------
141    // Exceptions
142    // ------------------------------------------------------------------------
143
144    public boolean hasExceptions()
145    {
146        return exceptions != null && !exceptions.isEmpty();
147    }
148
149    public List<Exception> getExceptions()
150    {
151        return exceptions == null ? Collections.<Exception> emptyList() : exceptions;
152    }
153
154    // ------------------------------------------------------------------------
155    // Version Range Violations
156    // ------------------------------------------------------------------------
157
158    public boolean hasVersionRangeViolations()
159    {
160        return versionRangeViolations != null;
161    }
162
163    /**
164     * @TODO this needs to accept a {@link OverConstrainedVersionException} as returned by
165     *       {@link #getVersionRangeViolation(int)} but it's not used like that in
166     *       {@link DefaultLegacyArtifactCollector}
167     */
168    public MetadataResolutionResult addVersionRangeViolation( Exception e )
169    {
170        versionRangeViolations = initList( versionRangeViolations );
171
172        versionRangeViolations.add( e );
173
174        exceptions = initList( exceptions );
175
176        exceptions.add( e );
177
178        return this;
179    }
180
181    public OverConstrainedVersionException getVersionRangeViolation( int i )
182    {
183        return (OverConstrainedVersionException) versionRangeViolations.get( i );
184    }
185
186    public List<Exception> getVersionRangeViolations()
187    {
188        return versionRangeViolations == null ? Collections.<Exception> emptyList() : versionRangeViolations;
189    }
190
191    // ------------------------------------------------------------------------
192    // Metadata Resolution Exceptions: ArtifactResolutionExceptions
193    // ------------------------------------------------------------------------
194
195    public boolean hasMetadataResolutionExceptions()
196    {
197        return metadataResolutionExceptions != null;
198    }
199
200    public MetadataResolutionResult addMetadataResolutionException( ArtifactResolutionException e )
201    {
202        metadataResolutionExceptions = initList( metadataResolutionExceptions );
203
204        metadataResolutionExceptions.add( e );
205
206        exceptions = initList( exceptions );
207
208        exceptions.add( e );
209
210        return this;
211    }
212
213    public ArtifactResolutionException getMetadataResolutionException( int i )
214    {
215        return metadataResolutionExceptions.get( i );
216    }
217
218    public List<ArtifactResolutionException> getMetadataResolutionExceptions()
219    {
220        return metadataResolutionExceptions == null ? Collections.<ArtifactResolutionException> emptyList()
221                        : metadataResolutionExceptions;
222    }
223
224    // ------------------------------------------------------------------------
225    // ErrorArtifactExceptions: ArtifactResolutionExceptions
226    // ------------------------------------------------------------------------
227
228    public boolean hasErrorArtifactExceptions()
229    {
230        return errorArtifactExceptions != null;
231    }
232
233    public MetadataResolutionResult addError( Exception e )
234    {
235        exceptions = initList( exceptions );
236
237        exceptions.add( e );
238
239        return this;
240    }
241
242    public List<ArtifactResolutionException> getErrorArtifactExceptions()
243    {
244        if ( errorArtifactExceptions == null )
245        {
246            return Collections.emptyList();
247        }
248
249        return errorArtifactExceptions;
250    }
251
252    // ------------------------------------------------------------------------
253    // Circular Dependency Exceptions
254    // ------------------------------------------------------------------------
255
256    public boolean hasCircularDependencyExceptions()
257    {
258        return circularDependencyExceptions != null;
259    }
260
261    public MetadataResolutionResult addCircularDependencyException( CyclicDependencyException e )
262    {
263        circularDependencyExceptions = initList( circularDependencyExceptions );
264
265        circularDependencyExceptions.add( e );
266
267        exceptions = initList( exceptions );
268
269        exceptions.add( e );
270
271        return this;
272    }
273
274    public CyclicDependencyException getCircularDependencyException( int i )
275    {
276        return circularDependencyExceptions.get( i );
277    }
278
279    public List<CyclicDependencyException> getCircularDependencyExceptions()
280    {
281        if ( circularDependencyExceptions == null )
282        {
283            return Collections.emptyList();
284        }
285
286        return circularDependencyExceptions;
287    }
288
289    // ------------------------------------------------------------------------
290    // Repositories
291    // ------------------------------------------------------------------------
292
293    public List<ArtifactRepository> getRepositories()
294    {
295        if ( repositories == null )
296        {
297            return Collections.emptyList();
298        }
299
300        return repositories;
301    }
302
303    public MetadataResolutionResult setRepositories( final List<ArtifactRepository> repositories )
304    {
305        this.repositories = repositories;
306
307        return this;
308    }
309
310    //
311    // Internal
312    //
313
314    private <T> List<T> initList( final List<T> l )
315    {
316        if ( l == null )
317        {
318            return new ArrayList<>();
319        }
320        return l;
321    }
322
323    public String toString()
324    {
325        StringBuilder sb = new StringBuilder();
326
327        if ( artifacts != null )
328        {
329            int i = 1;
330            sb.append( "---------" ).append( "\n" );
331            sb.append( artifacts.size() ).append( "\n" );
332            for ( Artifact a : artifacts )
333            {
334                sb.append( i ).append( " " ).append( a ).append( "\n" );
335                i++;
336            }
337            sb.append( "---------" ).append( "\n" );
338        }
339
340        return sb.toString();
341    }
342
343    public MetadataGraph getResolvedTree()
344    {
345        return resolvedTree;
346    }
347
348    public void setResolvedTree( MetadataGraph resolvedTree )
349    {
350        this.resolvedTree = resolvedTree;
351    }
352
353}