View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
30  import org.apache.maven.artifact.resolver.CyclicDependencyException;
31  import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
32  
33  /**
34   *
35   *
36   * @author Oleg Gusakov
37   *
38   */
39  public class MetadataResolutionResult {
40      private Artifact originatingArtifact;
41  
42      private List<Artifact> missingArtifacts;
43  
44      // Exceptions
45  
46      private List<Exception> exceptions;
47  
48      private List<Exception> versionRangeViolations;
49  
50      private List<ArtifactResolutionException> metadataResolutionExceptions;
51  
52      private List<CyclicDependencyException> circularDependencyExceptions;
53  
54      private List<ArtifactResolutionException> errorArtifactExceptions;
55  
56      // file system errors
57  
58      private List<ArtifactRepository> repositories;
59  
60      private Set<Artifact> requestedArtifacts;
61  
62      private Set<Artifact> artifacts;
63  
64      private MetadataGraph dirtyTree;
65  
66      private MetadataGraph resolvedTree;
67  
68      private MetadataGraph resolvedGraph;
69  
70      public Artifact getOriginatingArtifact() {
71          return originatingArtifact;
72      }
73  
74      public MetadataResolutionResult listOriginatingArtifact(final Artifact originatingArtifact) {
75          this.originatingArtifact = originatingArtifact;
76  
77          return this;
78      }
79  
80      public void addArtifact(Artifact artifact) {
81          if (artifacts == null) {
82              artifacts = new LinkedHashSet<>();
83          }
84  
85          artifacts.add(artifact);
86      }
87  
88      public Set<Artifact> getArtifacts() {
89          return artifacts;
90      }
91  
92      public void addRequestedArtifact(Artifact artifact) {
93          if (requestedArtifacts == null) {
94              requestedArtifacts = new LinkedHashSet<>();
95          }
96  
97          requestedArtifacts.add(artifact);
98      }
99  
100     public Set<Artifact> getRequestedArtifacts() {
101         return requestedArtifacts;
102     }
103 
104     public boolean hasMissingArtifacts() {
105         return missingArtifacts != null && !missingArtifacts.isEmpty();
106     }
107 
108     public List<Artifact> getMissingArtifacts() {
109         return missingArtifacts == null
110                 ? Collections.<Artifact>emptyList()
111                 : Collections.unmodifiableList(missingArtifacts);
112     }
113 
114     public MetadataResolutionResult addMissingArtifact(Artifact artifact) {
115         missingArtifacts = initList(missingArtifacts);
116 
117         missingArtifacts.add(artifact);
118 
119         return this;
120     }
121 
122     public MetadataResolutionResult setUnresolvedArtifacts(final List<Artifact> unresolvedArtifacts) {
123         this.missingArtifacts = unresolvedArtifacts;
124 
125         return this;
126     }
127 
128     // ------------------------------------------------------------------------
129     // Exceptions
130     // ------------------------------------------------------------------------
131 
132     public boolean hasExceptions() {
133         return exceptions != null && !exceptions.isEmpty();
134     }
135 
136     public List<Exception> getExceptions() {
137         return exceptions == null ? Collections.<Exception>emptyList() : Collections.unmodifiableList(exceptions);
138     }
139 
140     // ------------------------------------------------------------------------
141     // Version Range Violations
142     // ------------------------------------------------------------------------
143 
144     public boolean hasVersionRangeViolations() {
145         return versionRangeViolations != null;
146     }
147 
148     /**
149      * TODO this needs to accept a {@link OverConstrainedVersionException} as returned by
150      *       {@link #getVersionRangeViolation(int)} but it's not used like that in
151      *       {@link org.apache.maven.repository.legacy.resolver.DefaultLegacyArtifactCollector}
152      */
153     public MetadataResolutionResult addVersionRangeViolation(Exception e) {
154         versionRangeViolations = initList(versionRangeViolations);
155 
156         versionRangeViolations.add(e);
157 
158         exceptions = initList(exceptions);
159 
160         exceptions.add(e);
161 
162         return this;
163     }
164 
165     public OverConstrainedVersionException getVersionRangeViolation(int i) {
166         return (OverConstrainedVersionException) versionRangeViolations.get(i);
167     }
168 
169     public List<Exception> getVersionRangeViolations() {
170         return versionRangeViolations == null
171                 ? Collections.<Exception>emptyList()
172                 : Collections.unmodifiableList(versionRangeViolations);
173     }
174 
175     // ------------------------------------------------------------------------
176     // Metadata Resolution Exceptions: ArtifactResolutionExceptions
177     // ------------------------------------------------------------------------
178 
179     public boolean hasMetadataResolutionExceptions() {
180         return metadataResolutionExceptions != null;
181     }
182 
183     public MetadataResolutionResult addMetadataResolutionException(ArtifactResolutionException e) {
184         metadataResolutionExceptions = initList(metadataResolutionExceptions);
185 
186         metadataResolutionExceptions.add(e);
187 
188         exceptions = initList(exceptions);
189 
190         exceptions.add(e);
191 
192         return this;
193     }
194 
195     public ArtifactResolutionException getMetadataResolutionException(int i) {
196         return metadataResolutionExceptions.get(i);
197     }
198 
199     public List<ArtifactResolutionException> getMetadataResolutionExceptions() {
200         return metadataResolutionExceptions == null
201                 ? Collections.<ArtifactResolutionException>emptyList()
202                 : Collections.unmodifiableList(metadataResolutionExceptions);
203     }
204 
205     // ------------------------------------------------------------------------
206     // ErrorArtifactExceptions: ArtifactResolutionExceptions
207     // ------------------------------------------------------------------------
208 
209     public boolean hasErrorArtifactExceptions() {
210         return errorArtifactExceptions != null;
211     }
212 
213     public MetadataResolutionResult addError(Exception e) {
214         exceptions = initList(exceptions);
215 
216         exceptions.add(e);
217 
218         return this;
219     }
220 
221     public List<ArtifactResolutionException> getErrorArtifactExceptions() {
222         if (errorArtifactExceptions == null) {
223             return Collections.emptyList();
224         }
225 
226         return Collections.unmodifiableList(errorArtifactExceptions);
227     }
228 
229     // ------------------------------------------------------------------------
230     // Circular Dependency Exceptions
231     // ------------------------------------------------------------------------
232 
233     public boolean hasCircularDependencyExceptions() {
234         return circularDependencyExceptions != null;
235     }
236 
237     public MetadataResolutionResult addCircularDependencyException(CyclicDependencyException e) {
238         circularDependencyExceptions = initList(circularDependencyExceptions);
239 
240         circularDependencyExceptions.add(e);
241 
242         exceptions = initList(exceptions);
243 
244         exceptions.add(e);
245 
246         return this;
247     }
248 
249     public CyclicDependencyException getCircularDependencyException(int i) {
250         return circularDependencyExceptions.get(i);
251     }
252 
253     public List<CyclicDependencyException> getCircularDependencyExceptions() {
254         if (circularDependencyExceptions == null) {
255             return Collections.emptyList();
256         }
257 
258         return Collections.unmodifiableList(circularDependencyExceptions);
259     }
260 
261     // ------------------------------------------------------------------------
262     // Repositories
263     // ------------------------------------------------------------------------
264 
265     public List<ArtifactRepository> getRepositories() {
266         if (repositories == null) {
267             return Collections.emptyList();
268         }
269 
270         return Collections.unmodifiableList(repositories);
271     }
272 
273     public MetadataResolutionResult setRepositories(final List<ArtifactRepository> repositories) {
274         this.repositories = repositories;
275 
276         return this;
277     }
278 
279     //
280     // Internal
281     //
282 
283     private <T> List<T> initList(final List<T> l) {
284         if (l == null) {
285             return new ArrayList<>();
286         }
287         return l;
288     }
289 
290     public String toString() {
291         if (artifacts == null) {
292             return "";
293         }
294         StringBuilder sb = new StringBuilder(256);
295         int i = 1;
296         sb.append("---------\n");
297         sb.append(artifacts.size()).append('\n');
298         for (Artifact a : artifacts) {
299             sb.append(i).append(' ').append(a).append('\n');
300             i++;
301         }
302         sb.append("---------\n");
303         return sb.toString();
304     }
305 
306     public MetadataGraph getResolvedTree() {
307         return resolvedTree;
308     }
309 
310     public void setResolvedTree(MetadataGraph resolvedTree) {
311         this.resolvedTree = resolvedTree;
312     }
313 }