1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.metadata;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import org.apache.maven.artifact.ArtifactScopeEnum;
25 import org.codehaus.plexus.component.annotations.Component;
26 import org.codehaus.plexus.component.annotations.Requirement;
27
28
29
30
31
32
33
34 @Component(role = ClasspathTransformation.class)
35 public class DefaultClasspathTransformation implements ClasspathTransformation {
36 @Requirement
37 GraphConflictResolver conflictResolver;
38
39
40 public ClasspathContainer transform(MetadataGraph dirtyGraph, ArtifactScopeEnum scope, boolean resolve)
41 throws MetadataGraphTransformationException {
42 try {
43 if (dirtyGraph == null || dirtyGraph.isEmpty()) {
44 return null;
45 }
46
47 MetadataGraph cleanGraph = conflictResolver.resolveConflicts(dirtyGraph, scope);
48
49 if (cleanGraph == null || cleanGraph.isEmpty()) {
50 return null;
51 }
52
53 ClasspathContainer cpc = new ClasspathContainer(scope);
54 if (cleanGraph.isEmptyEdges()) {
55
56 ArtifactMetadata amd = cleanGraph.getEntry().getMd();
57 cpc.add(amd);
58 } else {
59 ClasspathGraphVisitor v = new ClasspathGraphVisitor(cleanGraph, cpc);
60 MetadataGraphVertex entry = cleanGraph.getEntry();
61
62 v.visit(entry);
63 }
64
65 return cpc;
66 } catch (GraphConflictResolutionException e) {
67 throw new MetadataGraphTransformationException(e);
68 }
69 }
70
71
72
73
74
75
76 private class ClasspathGraphVisitor {
77 MetadataGraph graph;
78
79 ClasspathContainer cpc;
80
81 List<MetadataGraphVertex> visited;
82
83
84 protected ClasspathGraphVisitor(MetadataGraph cleanGraph, ClasspathContainer cpc) {
85 this.cpc = cpc;
86 this.graph = cleanGraph;
87
88 visited = new ArrayList<>(cleanGraph.getVertices().size());
89 }
90
91
92 protected void visit(MetadataGraphVertex node)
93 {
94 ArtifactMetadata md = node.getMd();
95 if (visited.contains(node)) {
96 return;
97 }
98
99 cpc.add(md);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 List<MetadataGraphEdge> exits = graph.getExcidentEdges(node);
123
124 if (exits != null && exits.size() > 0) {
125 MetadataGraphEdge[] sortedExits = exits.toArray(new MetadataGraphEdge[0]);
126 Arrays.sort(sortedExits, (e1, e2) -> {
127 if (e1.getDepth() == e2.getDepth()) {
128 if (e2.getPomOrder() == e1.getPomOrder()) {
129 return e1.getTarget()
130 .toString()
131 .compareTo(e2.getTarget().toString());
132 }
133 return e2.getPomOrder() - e1.getPomOrder();
134 }
135 return e2.getDepth() - e1.getDepth();
136 });
137
138 for (MetadataGraphEdge e : sortedExits) {
139 MetadataGraphVertex targetNode = e.getTarget();
140 targetNode.getMd().setArtifactScope(e.getScope());
141 targetNode.getMd().setWhy(e.getSource().getMd().toString());
142 visit(targetNode);
143 }
144 }
145 }
146
147
148 }
149
150
151 }