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