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