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<Artifact>();
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<Artifact>();
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        if ( exceptions == null )
236        {
237            initList( exceptions );
238        }
239
240        exceptions.add( e );
241
242        return this;
243    }
244
245    public List<ArtifactResolutionException> getErrorArtifactExceptions()
246    {
247        if ( errorArtifactExceptions == null )
248        {
249            return Collections.emptyList();
250        }
251
252        return errorArtifactExceptions;
253    }
254
255    // ------------------------------------------------------------------------
256    // Circular Dependency Exceptions
257    // ------------------------------------------------------------------------
258
259    public boolean hasCircularDependencyExceptions()
260    {
261        return circularDependencyExceptions != null;
262    }
263
264    public MetadataResolutionResult addCircularDependencyException( CyclicDependencyException e )
265    {
266        circularDependencyExceptions = initList( circularDependencyExceptions );
267
268        circularDependencyExceptions.add( e );
269
270        exceptions = initList( exceptions );
271
272        exceptions.add( e );
273
274        return this;
275    }
276
277    public CyclicDependencyException getCircularDependencyException( int i )
278    {
279        return circularDependencyExceptions.get( i );
280    }
281
282    public List<CyclicDependencyException> getCircularDependencyExceptions()
283    {
284        if ( circularDependencyExceptions == null )
285        {
286            return Collections.emptyList();
287        }
288
289        return circularDependencyExceptions;
290    }
291
292    // ------------------------------------------------------------------------
293    // Repositories
294    // ------------------------------------------------------------------------
295
296    public List<ArtifactRepository> getRepositories()
297    {
298        if ( repositories == null )
299        {
300            return Collections.emptyList();
301        }
302
303        return repositories;
304    }
305
306    public MetadataResolutionResult setRepositories( final List<ArtifactRepository> repositories )
307    {
308        this.repositories = repositories;
309
310        return this;
311    }
312
313    //
314    // Internal
315    //
316
317    private <T> List<T> initList( final List<T> l )
318    {
319        if ( l == null )
320        {
321            return new ArrayList<T>();
322        }
323        return l;
324    }
325
326    public String toString()
327    {
328        StringBuilder sb = new StringBuilder();
329
330        if ( artifacts != null )
331        {
332            int i = 1;
333            sb.append( "---------" ).append( "\n" );
334            sb.append( artifacts.size() ).append( "\n" );
335            for ( Artifact a : artifacts )
336            {
337                sb.append( i ).append( " " ).append( a ).append( "\n" );
338                i++;
339            }
340            sb.append( "---------" ).append( "\n" );
341        }
342
343        return sb.toString();
344    }
345
346    public MetadataGraph getResolvedTree()
347    {
348        return resolvedTree;
349    }
350
351    public void setResolvedTree( MetadataGraph resolvedTree )
352    {
353        this.resolvedTree = resolvedTree;
354    }
355
356}