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