1 | |
package org.apache.maven.archiva.dependency.graph; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.collections.CollectionUtils; |
23 | |
import org.apache.commons.collections.map.ListOrderedMap; |
24 | |
import org.apache.maven.archiva.dependency.graph.functors.EdgeFromPredicate; |
25 | |
import org.apache.maven.archiva.dependency.graph.functors.EdgeToPredicate; |
26 | |
import org.apache.maven.archiva.model.ArtifactReference; |
27 | |
|
28 | |
import java.util.ArrayList; |
29 | |
import java.util.Collection; |
30 | |
import java.util.HashSet; |
31 | |
import java.util.List; |
32 | |
import java.util.Set; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class DependencyGraph |
40 | |
{ |
41 | |
public static final int DISABLED_CYCLIC = 0; |
42 | |
|
43 | |
public static final int DISABLED_EXCLUDED = 1; |
44 | |
|
45 | |
public static final int DISABLED_OPTIONAL = 2; |
46 | |
|
47 | |
public static final int DISABLED_NEARER_DEP = 3; |
48 | |
|
49 | |
public static final int DISABLED_NEARER_EDGE = 4; |
50 | |
|
51 | |
private DependencyGraphNode rootNode; |
52 | |
|
53 | 0 | private Set<DependencyGraphEdge> edges = new HashSet<DependencyGraphEdge>(); |
54 | |
|
55 | 0 | private ListOrderedMap nodes = new ListOrderedMap(); |
56 | |
|
57 | |
public DependencyGraph( String groupId, String artifactId, String version ) |
58 | 0 | { |
59 | 0 | ArtifactReference rootRef = new ArtifactReference(); |
60 | 0 | rootRef.setGroupId( groupId ); |
61 | 0 | rootRef.setArtifactId( artifactId ); |
62 | 0 | rootRef.setVersion( version ); |
63 | 0 | rootRef.setClassifier( "" ); |
64 | 0 | rootRef.setType( "pom" ); |
65 | |
|
66 | 0 | this.rootNode = new DependencyGraphNode( rootRef ); |
67 | 0 | } |
68 | |
|
69 | |
public DependencyGraph( DependencyGraphNode root ) |
70 | 0 | { |
71 | 0 | this.rootNode = root; |
72 | 0 | } |
73 | |
|
74 | |
public Collection<DependencyGraphEdge> getEdges() |
75 | |
{ |
76 | 0 | return edges; |
77 | |
} |
78 | |
|
79 | |
@SuppressWarnings("unchecked") |
80 | |
public Collection<DependencyGraphNode> getNodes() |
81 | |
{ |
82 | 0 | return nodes.values(); |
83 | |
} |
84 | |
|
85 | |
public DependencyGraphNode getRootNode() |
86 | |
{ |
87 | 0 | return rootNode; |
88 | |
} |
89 | |
|
90 | |
public void setRootNode( DependencyGraphNode rootNode ) |
91 | |
{ |
92 | 0 | this.rootNode = rootNode; |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public void addEdge( final DependencyGraphEdge edge ) |
101 | |
{ |
102 | 0 | if ( edge.getNodeFrom() == null ) |
103 | |
{ |
104 | 0 | throw new IllegalArgumentException( "edge.nodeFrom cannot be null." ); |
105 | |
} |
106 | |
|
107 | 0 | if ( edge.getNodeTo() == null ) |
108 | |
{ |
109 | 0 | throw new IllegalArgumentException( "edge.nodeTo cannot be null." ); |
110 | |
} |
111 | |
|
112 | 0 | this.edges.add( edge ); |
113 | 0 | } |
114 | |
|
115 | |
public DependencyGraphNode addNode( DependencyGraphNode node ) |
116 | |
{ |
117 | 0 | if ( node == null ) |
118 | |
{ |
119 | 0 | throw new IllegalArgumentException( "Unable to add a null node." ); |
120 | |
} |
121 | |
|
122 | 0 | if ( node.getArtifact() == null ) |
123 | |
{ |
124 | 0 | throw new IllegalArgumentException( "Unable to add a node with a null artifact reference." ); |
125 | |
} |
126 | |
|
127 | 0 | int prevNodeIdx = this.nodes.indexOf( node ); |
128 | |
|
129 | |
|
130 | 0 | if ( prevNodeIdx >= 0 ) |
131 | |
{ |
132 | |
|
133 | 0 | DependencyGraphNode previousNode = (DependencyGraphNode) this.nodes.get( prevNodeIdx ); |
134 | |
|
135 | 0 | if ( CollectionUtils.isNotEmpty( node.getExcludes() ) ) |
136 | |
{ |
137 | 0 | previousNode.getExcludes().addAll( node.getExcludes() ); |
138 | |
} |
139 | |
|
140 | 0 | if ( CollectionUtils.isNotEmpty( node.getDependencyManagement() ) ) |
141 | |
{ |
142 | 0 | previousNode.getDependencyManagement().addAll( node.getDependencyManagement() ); |
143 | |
} |
144 | |
|
145 | 0 | if ( node.isFromParent() ) |
146 | |
{ |
147 | 0 | previousNode.setFromParent( true ); |
148 | |
} |
149 | |
|
150 | |
|
151 | 0 | return previousNode; |
152 | |
} |
153 | |
|
154 | |
|
155 | 0 | this.nodes.put( node.getArtifact(), node ); |
156 | |
|
157 | 0 | return node; |
158 | |
} |
159 | |
|
160 | |
public boolean hasNode( DependencyGraphNode node ) |
161 | |
{ |
162 | 0 | return this.nodes.containsKey( node.getArtifact() ); |
163 | |
} |
164 | |
|
165 | |
public boolean hasEdge( DependencyGraphEdge edge ) |
166 | |
{ |
167 | 0 | return this.edges.contains( edge ); |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public List<DependencyGraphEdge> getEdgesFrom( DependencyGraphNode node ) |
177 | |
{ |
178 | 0 | List<DependencyGraphEdge> ret = new ArrayList<DependencyGraphEdge>(); |
179 | 0 | CollectionUtils.select( this.edges, new EdgeFromPredicate( node.getArtifact() ), ret ); |
180 | 0 | return ret; |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public List<DependencyGraphEdge> getEdgesTo( DependencyGraphNode node ) |
190 | |
{ |
191 | 0 | List<DependencyGraphEdge> ret = new ArrayList<DependencyGraphEdge>(); |
192 | 0 | CollectionUtils.select( this.edges, new EdgeToPredicate( node.getArtifact() ), ret ); |
193 | 0 | return ret; |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public DependencyGraphNode getNode( ArtifactReference ref ) |
203 | |
{ |
204 | 0 | return (DependencyGraphNode) this.nodes.get( ref ); |
205 | |
} |
206 | |
|
207 | |
public void removeEdge( DependencyGraphEdge edge ) |
208 | |
{ |
209 | 0 | this.edges.remove( edge ); |
210 | 0 | } |
211 | |
|
212 | |
public void removeNode( DependencyGraphNode node ) |
213 | |
{ |
214 | 0 | List<DependencyGraphEdge> edges = getEdgesFrom( node ); |
215 | 0 | if ( !edges.isEmpty() ) |
216 | |
{ |
217 | 0 | System.out.println( "Removing node left <" + edges + "> hanging <from> edges." ); |
218 | |
} |
219 | |
|
220 | 0 | edges = getEdgesTo( node ); |
221 | 0 | if ( !edges.isEmpty() ) |
222 | |
{ |
223 | 0 | System.out.println( "Removing node left <" + edges + "> hanging <to> edges." ); |
224 | |
} |
225 | |
|
226 | 0 | this.nodes.remove( node.getArtifact() ); |
227 | 0 | } |
228 | |
} |