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