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.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
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 CollectLibrariesNodeVisitor
38 implements DependencyNodeVisitor
39 {
40
41
42
43
44 private final List<Artifact> nodes;
45
46 private Map<String, Artifact> artifacts;
47
48 private Map<Artifact, ExamineManifest> examinerCache;
49
50 private List<String> explicitLibs;
51
52 private final Log log;
53
54 private MojoExecutionException throwable;
55
56 private DependencyNode root;
57
58 private Set<String> duplicates;
59
60 private Set<String> conflicts;
61
62 private Set<String> includes;
63
64 private final boolean useOsgiDependencies;
65
66
67
68
69 public CollectLibrariesNodeVisitor( List<String> explicitLibraries,
70 List<Artifact> runtimeArtifacts, Map<Artifact, ExamineManifest> examinerCache,
71 Log log, DependencyNode root, boolean useOsgiDependencies )
72 {
73 nodes = new ArrayList<Artifact>();
74 artifacts = new HashMap<String, Artifact>();
75 for ( Artifact a : runtimeArtifacts )
76 {
77 artifacts.put( a.getDependencyConflictId(), a );
78 }
79 this.examinerCache = examinerCache;
80 this.explicitLibs = explicitLibraries;
81 this.log = log;
82 this.root = root;
83 this.useOsgiDependencies = useOsgiDependencies;
84 duplicates = new HashSet<String>();
85 conflicts = new HashSet<String>();
86 includes = new HashSet<String>();
87 }
88
89
90
91
92 public boolean visit( DependencyNode node )
93 {
94 if ( throwable != null )
95 {
96 return false;
97 }
98 if ( root == node )
99 {
100 return true;
101 }
102 try
103 {
104 Artifact artifact = node.getArtifact();
105 if ( !artifacts.containsKey( artifact.getDependencyConflictId() ) )
106 {
107
108 return false;
109 }
110
111 artifact = artifacts.get( artifact.getDependencyConflictId() );
112
113 ExamineManifest depExaminator = examinerCache.get( artifact );
114 if ( depExaminator == null )
115 {
116 depExaminator = new ExamineManifest( log );
117 depExaminator.setArtifactFile( artifact.getFile() );
118 depExaminator.checkFile();
119 examinerCache.put( artifact, depExaminator );
120 }
121 if ( AbstractNbmMojo.matchesLibrary( artifact, explicitLibs, depExaminator, log, useOsgiDependencies ) )
122 {
123 if ( depExaminator.isNetBeansModule() )
124 {
125 log.warn(
126 "You are using a NetBeans Module as a Library (classpath extension): " + artifact.getId() );
127 }
128
129 nodes.add( artifact );
130 includes.add( artifact.getDependencyConflictId() );
131
132 return true;
133 }
134 }
135 catch ( MojoExecutionException mojoExecutionException )
136 {
137 throwable = mojoExecutionException;
138 }
139
140 return false;
141 }
142
143
144
145
146 public boolean endVisit( DependencyNode node )
147 {
148 if ( throwable != null )
149 {
150 return false;
151 }
152 if ( node == root )
153 {
154 if ( nodes.size() > 0 )
155 {
156 log.info( "Adding on module's Class-Path:" );
157 for ( Artifact inc : nodes )
158 {
159 log.info( " " + inc.getId() );
160 }
161 }
162 }
163 return true;
164 }
165
166
167
168
169
170
171 public List<Artifact> getArtifacts()
172 throws MojoExecutionException
173 {
174 if ( throwable != null )
175 {
176 throw throwable;
177 }
178 return nodes;
179 }
180 }