1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.resolution;
20
21 import java.util.Collections;
22 import java.util.List;
23
24 import org.eclipse.aether.RepositoryException;
25 import org.eclipse.aether.repository.LocalArtifactResult;
26 import org.eclipse.aether.transfer.ArtifactFilteredOutException;
27 import org.eclipse.aether.transfer.ArtifactNotFoundException;
28 import org.eclipse.aether.transfer.RepositoryOfflineException;
29
30
31
32
33 public class ArtifactResolutionException extends RepositoryException {
34 private final transient List<ArtifactResult> results;
35
36
37
38
39
40
41 public ArtifactResolutionException(List<ArtifactResult> results) {
42 super(getMessage(results), getCause(results));
43 this.results = results != null ? results : Collections.emptyList();
44 }
45
46
47
48
49
50
51
52 public ArtifactResolutionException(List<ArtifactResult> results, String message) {
53 super(message, getCause(results));
54 this.results = results != null ? results : Collections.emptyList();
55 }
56
57
58
59
60
61
62
63
64 public ArtifactResolutionException(List<ArtifactResult> results, String message, Throwable cause) {
65 super(message, cause);
66 this.results = results != null ? results : Collections.emptyList();
67 }
68
69
70
71
72
73
74
75 public List<ArtifactResult> getResults() {
76 return results;
77 }
78
79
80
81
82
83
84
85 public ArtifactResult getResult() {
86 return (results != null && !results.isEmpty()) ? results.get(0) : null;
87 }
88
89 private static String getMessage(List<? extends ArtifactResult> results) {
90 if (results == null) {
91 return null;
92 }
93 StringBuilder buffer = new StringBuilder(256);
94
95 buffer.append("The following artifacts could not be resolved: ");
96
97 String sep = "";
98 for (ArtifactResult result : results) {
99 if (!result.isResolved()) {
100 buffer.append(sep);
101 buffer.append(result.getRequest().getArtifact());
102 LocalArtifactResult localResult = result.getLocalArtifactResult();
103 if (localResult != null) {
104 buffer.append(" (");
105 if (localResult.getFile() != null) {
106 buffer.append("present");
107 if (!localResult.isAvailable()) {
108 buffer.append(", but unavailable");
109 }
110 } else {
111 buffer.append("absent");
112 }
113 buffer.append(")");
114 }
115 sep = ", ";
116 }
117 }
118
119 Throwable cause = getCause(results);
120 if (cause != null) {
121 buffer.append(": ").append(cause.getMessage());
122 }
123
124 return buffer.toString();
125 }
126
127
128
129
130
131
132 private static Throwable getCause(List<? extends ArtifactResult> results) {
133 if (results == null) {
134 return null;
135 }
136 for (ArtifactResult result : results) {
137 if (!result.isResolved()) {
138 Throwable notFound = null, offline = null;
139 for (Throwable t : result.getExceptions()) {
140 if (t instanceof ArtifactNotFoundException) {
141 if (notFound == null || notFound instanceof ArtifactFilteredOutException) {
142 notFound = t;
143 }
144 if (offline == null && t.getCause() instanceof RepositoryOfflineException) {
145 offline = t;
146 }
147 } else {
148 return t;
149 }
150 }
151 if (offline != null) {
152 return offline;
153 }
154 if (notFound != null) {
155 return notFound;
156 }
157 }
158 }
159 return null;
160 }
161 }