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 ? Collections.emptyList() : Collections.unmodifiableList(missingArtifacts);
110     }
111 
112     public MetadataResolutionResult addMissingArtifact(Artifact artifact) {
113         missingArtifacts = initList(missingArtifacts);
114 
115         missingArtifacts.add(artifact);
116 
117         return this;
118     }
119 
120     public MetadataResolutionResult setUnresolvedArtifacts(final List<Artifact> unresolvedArtifacts) {
121         this.missingArtifacts = unresolvedArtifacts;
122 
123         return this;
124     }
125 
126     // ------------------------------------------------------------------------
127     // Exceptions
128     // ------------------------------------------------------------------------
129 
130     public boolean hasExceptions() {
131         return exceptions != null && !exceptions.isEmpty();
132     }
133 
134     public List<Exception> getExceptions() {
135         return exceptions == null ? Collections.emptyList() : Collections.unmodifiableList(exceptions);
136     }
137 
138     // ------------------------------------------------------------------------
139     // Version Range Violations
140     // ------------------------------------------------------------------------
141 
142     public boolean hasVersionRangeViolations() {
143         return versionRangeViolations != null;
144     }
145 
146     /**
147      * TODO this needs to accept a {@link OverConstrainedVersionException} as returned by
148      *       {@link #getVersionRangeViolation(int)} but it's not used like that in
149      *       {@link org.apache.maven.repository.legacy.resolver.DefaultLegacyArtifactCollector}
150      */
151     public MetadataResolutionResult addVersionRangeViolation(Exception e) {
152         versionRangeViolations = initList(versionRangeViolations);
153 
154         versionRangeViolations.add(e);
155 
156         exceptions = initList(exceptions);
157 
158         exceptions.add(e);
159 
160         return this;
161     }
162 
163     public OverConstrainedVersionException getVersionRangeViolation(int i) {
164         return (OverConstrainedVersionException) versionRangeViolations.get(i);
165     }
166 
167     public List<Exception> getVersionRangeViolations() {
168         return versionRangeViolations == null
169                 ? Collections.emptyList()
170                 : Collections.unmodifiableList(versionRangeViolations);
171     }
172 
173     // ------------------------------------------------------------------------
174     // Metadata Resolution Exceptions: ArtifactResolutionExceptions
175     // ------------------------------------------------------------------------
176 
177     public boolean hasMetadataResolutionExceptions() {
178         return metadataResolutionExceptions != null;
179     }
180 
181     public MetadataResolutionResult addMetadataResolutionException(ArtifactResolutionException e) {
182         metadataResolutionExceptions = initList(metadataResolutionExceptions);
183 
184         metadataResolutionExceptions.add(e);
185 
186         exceptions = initList(exceptions);
187 
188         exceptions.add(e);
189 
190         return this;
191     }
192 
193     public ArtifactResolutionException getMetadataResolutionException(int i) {
194         return metadataResolutionExceptions.get(i);
195     }
196 
197     public List<ArtifactResolutionException> getMetadataResolutionExceptions() {
198         return metadataResolutionExceptions == null
199                 ? Collections.emptyList()
200                 : Collections.unmodifiableList(metadataResolutionExceptions);
201     }
202 
203     // ------------------------------------------------------------------------
204     // ErrorArtifactExceptions: ArtifactResolutionExceptions
205     // ------------------------------------------------------------------------
206 
207     public boolean hasErrorArtifactExceptions() {
208         return errorArtifactExceptions != null;
209     }
210 
211     public MetadataResolutionResult addError(Exception e) {
212         exceptions = initList(exceptions);
213 
214         exceptions.add(e);
215 
216         return this;
217     }
218 
219     public List<ArtifactResolutionException> getErrorArtifactExceptions() {
220         if (errorArtifactExceptions == null) {
221             return Collections.emptyList();
222         }
223 
224         return Collections.unmodifiableList(errorArtifactExceptions);
225     }
226 
227     // ------------------------------------------------------------------------
228     // Circular Dependency Exceptions
229     // ------------------------------------------------------------------------
230 
231     public boolean hasCircularDependencyExceptions() {
232         return circularDependencyExceptions != null;
233     }
234 
235     public MetadataResolutionResult addCircularDependencyException(CyclicDependencyException e) {
236         circularDependencyExceptions = initList(circularDependencyExceptions);
237 
238         circularDependencyExceptions.add(e);
239 
240         exceptions = initList(exceptions);
241 
242         exceptions.add(e);
243 
244         return this;
245     }
246 
247     public CyclicDependencyException getCircularDependencyException(int i) {
248         return circularDependencyExceptions.get(i);
249     }
250 
251     public List<CyclicDependencyException> getCircularDependencyExceptions() {
252         if (circularDependencyExceptions == null) {
253             return Collections.emptyList();
254         }
255 
256         return Collections.unmodifiableList(circularDependencyExceptions);
257     }
258 
259     // ------------------------------------------------------------------------
260     // Repositories
261     // ------------------------------------------------------------------------
262 
263     public List<ArtifactRepository> getRepositories() {
264         if (repositories == null) {
265             return Collections.emptyList();
266         }
267 
268         return Collections.unmodifiableList(repositories);
269     }
270 
271     public MetadataResolutionResult setRepositories(final List<ArtifactRepository> repositories) {
272         this.repositories = repositories;
273 
274         return this;
275     }
276 
277     //
278     // Internal
279     //
280 
281     private <T> List<T> initList(final List<T> l) {
282         if (l == null) {
283             return new ArrayList<>();
284         }
285         return l;
286     }
287 
288     public String toString() {
289         if (artifacts == null) {
290             return "";
291         }
292         StringBuilder sb = new StringBuilder(256);
293         int i = 1;
294         sb.append("---------\n");
295         sb.append(artifacts.size()).append('\n');
296         for (Artifact a : artifacts) {
297             sb.append(i).append(' ').append(a).append('\n');
298             i++;
299         }
300         sb.append("---------\n");
301         return sb.toString();
302     }
303 
304     public MetadataGraph getResolvedTree() {
305         return resolvedTree;
306     }
307 
308     public void setResolvedTree(MetadataGraph resolvedTree) {
309         this.resolvedTree = resolvedTree;
310     }
311 }