1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.artifact.resolver;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
31 import org.apache.maven.artifact.versioning.ArtifactVersion;
32 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
33
34
35
36
37 @Deprecated
38 public class ResolutionNode {
39 private Artifact artifact;
40
41 private List<ResolutionNode> children;
42
43 private final List<Object> parents;
44
45 private final int depth;
46
47 private final ResolutionNode parent;
48
49 private final List<ArtifactRepository> remoteRepositories;
50
51 private boolean active = true;
52
53 private List<Artifact> trail;
54
55 public ResolutionNode(Artifact artifact, List<ArtifactRepository> remoteRepositories) {
56 this.artifact = artifact;
57 this.remoteRepositories = remoteRepositories;
58 depth = 0;
59 parents = Collections.emptyList();
60 parent = null;
61 }
62
63 public ResolutionNode(Artifact artifact, List<ArtifactRepository> remoteRepositories, ResolutionNode parent) {
64 this.artifact = artifact;
65 this.remoteRepositories = remoteRepositories;
66 depth = parent.depth + 1;
67 parents = new ArrayList<>();
68 parents.addAll(parent.parents);
69 parents.add(parent.getKey());
70 this.parent = parent;
71 }
72
73 public Artifact getArtifact() {
74 return artifact;
75 }
76
77 public Object getKey() {
78 return artifact.getDependencyConflictId();
79 }
80
81 public void addDependencies(
82 Set<Artifact> artifacts, List<ArtifactRepository> remoteRepositories, ArtifactFilter filter)
83 throws CyclicDependencyException, OverConstrainedVersionException {
84 if (artifacts != null && !artifacts.isEmpty()) {
85 children = new ArrayList<>(artifacts.size());
86
87 for (Artifact a : artifacts) {
88 if (parents.contains(a.getDependencyConflictId())) {
89 a.setDependencyTrail(getDependencyTrail());
90
91 throw new CyclicDependencyException("A dependency has introduced a cycle", a);
92 }
93
94 children.add(new ResolutionNode(a, remoteRepositories, this));
95 }
96 children = Collections.unmodifiableList(children);
97 } else {
98 children = Collections.emptyList();
99 }
100 trail = null;
101 }
102
103
104
105
106
107 public List<String> getDependencyTrail() throws OverConstrainedVersionException {
108 List<Artifact> trial = getTrail();
109
110 List<String> ret = new ArrayList<>(trial.size());
111
112 for (Artifact artifact : trial) {
113 ret.add(artifact.getId());
114 }
115
116 return ret;
117 }
118
119 private List<Artifact> getTrail() throws OverConstrainedVersionException {
120 if (trail == null) {
121 List<Artifact> ids = new LinkedList<>();
122 ResolutionNode node = this;
123 while (node != null) {
124 Artifact artifact = node.getArtifact();
125 if (artifact.getVersion() == null) {
126
127 ArtifactVersion selected = artifact.getSelectedVersion();
128
129
130 if (selected != null) {
131 artifact.selectVersion(selected.toString());
132 } else {
133 throw new OverConstrainedVersionException(
134 "Unable to get a selected Version for " + artifact.getArtifactId(), artifact);
135 }
136 }
137
138 ids.add(0, artifact);
139 node = node.parent;
140 }
141 trail = ids;
142 }
143 return trail;
144 }
145
146 public boolean isResolved() {
147 return children != null;
148 }
149
150
151
152
153
154
155 public boolean isChildOfRootNode() {
156 return parent != null && parent.parent == null;
157 }
158
159 public Iterator<ResolutionNode> getChildrenIterator() {
160 return children.iterator();
161 }
162
163 public int getDepth() {
164 return depth;
165 }
166
167 public List<ArtifactRepository> getRemoteRepositories() {
168 return remoteRepositories;
169 }
170
171 public boolean isActive() {
172 return active;
173 }
174
175 public void enable() {
176 active = true;
177
178
179 if (children != null) {
180 for (ResolutionNode node : children) {
181 node.enable();
182 }
183 }
184 }
185
186 public void disable() {
187 active = false;
188 if (children != null) {
189 for (ResolutionNode node : children) {
190 node.disable();
191 }
192 }
193 }
194
195 public boolean filterTrail(ArtifactFilter filter) throws OverConstrainedVersionException {
196 boolean success = true;
197 if (filter != null) {
198 for (Artifact artifact : getTrail()) {
199 if (!filter.include(artifact)) {
200 success = false;
201 }
202 }
203 }
204 return success;
205 }
206
207 @Override
208 public String toString() {
209 return artifact.toString() + " (" + depth + "; " + (active ? "enabled" : "disabled") + ")";
210 }
211
212 public void setArtifact(Artifact artifact) {
213 this.artifact = artifact;
214 }
215 }