1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.geronimo.genesis.dependency;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Stack;
26
27 import org.apache.maven.artifact.Artifact;
28
29 import org.apache.geronimo.genesis.dependency.DependencyTree.Node;
30
31
32
33
34
35 /**
36 * ???
37 *
38 * @version $Rev: 454266 $ $Date: 2006-10-08 20:20:03 -0700 (Sun, 08 Oct 2006) $
39 */
40 public class DependencyResolutionListener
41 extends ResolutionListenerAdapter
42 {
43 private DependencyTree tree = new DependencyTree();
44
45 private int currentDepth = 0;
46
47 private Stack parents = new Stack();
48
49 private Map artifacts = new HashMap();
50
51 public DependencyTree getDependencyTree() {
52 return tree;
53 }
54
55 public Collection getArtifacts() {
56 return artifacts.values();
57 }
58
59
60
61
62
63 public void startProcessChildren(final Artifact artifact) {
64 Node node = (Node) artifacts.get(artifact.getDependencyConflictId());
65
66 node.depth = currentDepth++;
67 if (parents.isEmpty()) {
68 tree.rootNode = node;
69 }
70
71 parents.push(node);
72 }
73
74 public void endProcessChildren(final Artifact artifact) {
75 Node node = (Node) parents.pop();
76 assert artifact.equals(node.artifact);
77 currentDepth--;
78 }
79
80 public void omitForNearer(final Artifact omitted, final Artifact kept) {
81 assert omitted.getDependencyConflictId().equals(kept.getDependencyConflictId());
82
83 Node prev = (Node) artifacts.get(omitted.getDependencyConflictId());
84 if (prev != null) {
85 if (prev.parent != null) {
86 prev.parent.children.remove(prev);
87 }
88 artifacts.remove(omitted.getDependencyConflictId());
89 }
90
91 includeArtifact(kept);
92 }
93
94 public void omitForCycle(final Artifact artifact) {
95
96 }
97
98 public void includeArtifact(final Artifact artifact) {
99 if (artifacts.containsKey(artifact.getDependencyConflictId())) {
100 Node prev = (Node) artifacts.get(artifact.getDependencyConflictId());
101 if (prev.parent != null) {
102 prev.parent.children.remove(prev);
103 }
104 artifacts.remove(artifact.getDependencyConflictId());
105 }
106
107 Node node = new Node();
108 node.artifact = artifact;
109 if (!parents.isEmpty()) {
110 node.parent = (Node) parents.peek();
111 node.parent.children.add(node);
112 node.depth = currentDepth;
113 }
114 artifacts.put(artifact.getDependencyConflictId(), node);
115 }
116
117 public void updateScope(final Artifact artifact, final String scope) {
118 Node node = (Node) artifacts.get(artifact.getDependencyConflictId());
119 node.artifact.setScope(scope);
120 }
121
122 public void manageArtifact(final Artifact artifact, final Artifact replacement) {
123 Node node = (Node) artifacts.get(artifact.getDependencyConflictId());
124
125 if (node != null) {
126 if (replacement.getVersion() != null) {
127 node.artifact.setVersion(replacement.getVersion());
128 }
129 if (replacement.getScope() != null) {
130 node.artifact.setScope(replacement.getScope());
131 }
132 }
133 }
134 }