001    package org.apache.maven.artifact.resolver;
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    
022    import java.util.ArrayList;
023    import java.util.Collections;
024    import java.util.LinkedHashSet;
025    import java.util.List;
026    import java.util.Set;
027    
028    import org.apache.maven.artifact.Artifact;
029    import org.apache.maven.artifact.repository.ArtifactRepository;
030    import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
031    
032    /**
033     * Specific problems during resolution that we want to account for:
034     * <p/>
035     * - missing metadata - version range violations - version circular dependencies - missing artifacts
036     * - network/transfer errors - file system errors: permissions
037     *
038     * @author Jason van Zyl
039     * @TODO carlos: all these possible has*Exceptions and get*Exceptions methods make the clients too
040     *       complex requiring a long list of checks, need to create a parent/interfact/encapsulation
041     *       for the types of exceptions
042     */
043    public class ArtifactResolutionResult
044    {
045        private Artifact originatingArtifact;
046    
047        private List<Artifact> missingArtifacts;
048    
049        // Exceptions
050    
051        private List<Exception> exceptions;
052    
053        private List<Exception> versionRangeViolations;
054    
055        private List<ArtifactResolutionException> metadataResolutionExceptions;
056    
057        private List<CyclicDependencyException> circularDependencyExceptions;
058    
059        private List<ArtifactResolutionException> errorArtifactExceptions;
060    
061        // file system errors
062    
063        private List<ArtifactRepository> repositories;
064    
065        private Set<Artifact> artifacts;
066    
067        private Set<ResolutionNode> resolutionNodes;
068    
069        public Artifact getOriginatingArtifact()
070        {
071            return originatingArtifact;
072        }
073    
074        public ArtifactResolutionResult setOriginatingArtifact( final Artifact originatingArtifact )
075        {
076            this.originatingArtifact = originatingArtifact;
077    
078            return this;
079        }
080    
081        public void addArtifact( Artifact artifact )
082        {
083            if ( artifacts == null )
084            {
085                artifacts = new LinkedHashSet<Artifact>();
086            }
087    
088            artifacts.add( artifact );
089        }
090    
091        public Set<Artifact> getArtifacts()
092        {
093            if ( artifacts == null )
094            {
095                artifacts = new LinkedHashSet<Artifact>();
096            }
097    
098            return artifacts;
099        }
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<ResolutionNode>();
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<T>();
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( "---------" ).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( "---------" ).append( "\n" );
354            }
355    
356            return sb.toString();
357        }
358    }