1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.codehaus.mojo.nbm; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collections; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.Stack; |
25 | |
import org.apache.maven.artifact.Artifact; |
26 | |
import org.apache.maven.plugin.MojoExecutionException; |
27 | |
import org.apache.maven.plugin.logging.Log; |
28 | |
import org.apache.maven.shared.dependency.graph.DependencyNode; |
29 | |
import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor; |
30 | |
import org.codehaus.mojo.nbm.utils.ExamineManifest; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class CollectModuleLibrariesNodeVisitor |
38 | |
implements DependencyNodeVisitor |
39 | |
{ |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private final Map<String, List<Artifact>> directNodes; |
45 | |
|
46 | |
private final Map<String, List<Artifact>> transitiveNodes; |
47 | |
|
48 | |
private Map<String, Artifact> artifacts; |
49 | |
|
50 | |
private Map<Artifact, ExamineManifest> examinerCache; |
51 | |
|
52 | |
private final Log log; |
53 | |
|
54 | |
private MojoExecutionException throwable; |
55 | |
|
56 | |
private DependencyNode root; |
57 | |
|
58 | 0 | private Stack<String> currentModule = new Stack<String>(); |
59 | |
private static final String LIB_ID = "!@#$%^&ROOT"; |
60 | |
|
61 | |
private final boolean useOSGiDependencies; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public CollectModuleLibrariesNodeVisitor( |
67 | |
List<Artifact> runtimeArtifacts, Map<Artifact, ExamineManifest> examinerCache, |
68 | |
Log log, DependencyNode root, boolean useOSGiDependencies ) |
69 | 0 | { |
70 | 0 | directNodes = new HashMap<String, List<Artifact>>(); |
71 | 0 | transitiveNodes = new HashMap<String, List<Artifact>>(); |
72 | 0 | artifacts = new HashMap<String, Artifact>(); |
73 | 0 | for ( Artifact a : runtimeArtifacts ) |
74 | |
{ |
75 | 0 | artifacts.put( a.getDependencyConflictId(), a ); |
76 | 0 | } |
77 | 0 | this.examinerCache = examinerCache; |
78 | 0 | this.log = log; |
79 | 0 | this.root = root; |
80 | 0 | this.useOSGiDependencies = useOSGiDependencies; |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public boolean visit( DependencyNode node ) |
87 | |
{ |
88 | 0 | if ( throwable != null ) |
89 | |
{ |
90 | 0 | return false; |
91 | |
} |
92 | 0 | if ( root == node ) |
93 | |
{ |
94 | 0 | return true; |
95 | |
} |
96 | |
try |
97 | |
{ |
98 | 0 | Artifact artifact = node.getArtifact(); |
99 | 0 | if ( !artifacts.containsKey( artifact.getDependencyConflictId() ) ) |
100 | |
{ |
101 | |
|
102 | 0 | return false; |
103 | |
} |
104 | |
|
105 | 0 | artifact = artifacts.get( artifact.getDependencyConflictId() ); |
106 | |
|
107 | 0 | ExamineManifest depExaminator = examinerCache.get( artifact ); |
108 | 0 | if ( depExaminator == null ) |
109 | |
{ |
110 | 0 | depExaminator = new ExamineManifest( log ); |
111 | 0 | depExaminator.setArtifactFile( artifact.getFile() ); |
112 | 0 | depExaminator.checkFile(); |
113 | 0 | examinerCache.put( artifact, depExaminator ); |
114 | |
} |
115 | 0 | if ( depExaminator.isNetBeansModule() || ( useOSGiDependencies && depExaminator.isOsgiBundle() ) ) |
116 | |
{ |
117 | 0 | currentModule.push( artifact.getDependencyConflictId() ); |
118 | 0 | ArrayList<Artifact> arts = new ArrayList<Artifact>(); |
119 | 0 | arts.add( artifact ); |
120 | 0 | if ( currentModule.size() == 1 ) |
121 | |
{ |
122 | 0 | directNodes.put( currentModule.peek(), arts ); |
123 | |
} |
124 | |
else |
125 | |
{ |
126 | 0 | transitiveNodes.put( currentModule.peek(), arts ); |
127 | |
} |
128 | 0 | return true; |
129 | |
} |
130 | 0 | if ( currentModule.size() > 0 ) |
131 | |
{ |
132 | |
|
133 | 0 | if ( !currentModule.peek().startsWith( LIB_ID ) && |
134 | |
AbstractNbmMojo.matchesLibrary( artifact, Collections.<String>emptyList(), depExaminator, log, useOSGiDependencies ) ) |
135 | |
{ |
136 | 0 | if ( currentModule.size() == 1 ) |
137 | |
{ |
138 | 0 | directNodes.get( currentModule.peek() ).add( artifact ); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 0 | transitiveNodes.get( currentModule.peek() ).add( artifact ); |
143 | |
} |
144 | |
|
145 | 0 | return true; |
146 | |
} |
147 | |
} |
148 | |
else |
149 | |
{ |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | 0 | currentModule.push( LIB_ID + artifact.getDependencyConflictId() ); |
155 | |
} |
156 | |
} |
157 | 0 | catch ( MojoExecutionException mojoExecutionException ) |
158 | |
{ |
159 | 0 | throwable = mojoExecutionException; |
160 | 0 | } |
161 | 0 | return true; |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public boolean endVisit( DependencyNode node ) |
168 | |
{ |
169 | 0 | if ( throwable != null ) |
170 | |
{ |
171 | 0 | return false; |
172 | |
} |
173 | 0 | if ( !currentModule.empty() |
174 | |
&& ( currentModule.peek().equals( node.getArtifact().getDependencyConflictId() ) |
175 | |
|| currentModule.peek().equals( LIB_ID + node.getArtifact().getDependencyConflictId() ) ) ) |
176 | |
{ |
177 | 0 | currentModule.pop(); |
178 | |
} |
179 | 0 | return true; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public Map<String, List<Artifact>> getDeclaredArtifacts() |
188 | |
throws MojoExecutionException |
189 | |
{ |
190 | 0 | if ( throwable != null ) |
191 | |
{ |
192 | 0 | throw throwable; |
193 | |
} |
194 | 0 | return directNodes; |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public Map<String, List<Artifact>> getTransitiveArtifacts() |
203 | |
throws MojoExecutionException |
204 | |
{ |
205 | 0 | if ( throwable != null ) |
206 | |
{ |
207 | 0 | throw throwable; |
208 | |
} |
209 | 0 | return transitiveNodes; |
210 | |
} |
211 | |
} |