1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.maven.shared.artifact.filter; |
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.HashSet; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
import org.apache.maven.artifact.Artifact; |
28 | |
import org.apache.maven.artifact.ArtifactUtils; |
29 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
30 | |
import org.apache.maven.artifact.versioning.DefaultArtifactVersion; |
31 | |
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; |
32 | |
import org.apache.maven.artifact.versioning.VersionRange; |
33 | |
import org.codehaus.plexus.logging.Logger; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class PatternIncludesArtifactFilter |
42 | |
implements ArtifactFilter, StatisticsReportingArtifactFilter |
43 | |
{ |
44 | |
private final List positivePatterns; |
45 | |
|
46 | |
private final List negativePatterns; |
47 | |
|
48 | |
private final boolean actTransitively; |
49 | |
|
50 | 76 | private final Set patternsTriggered = new HashSet(); |
51 | |
|
52 | 76 | private final List filteredArtifactIds = new ArrayList(); |
53 | |
|
54 | |
public PatternIncludesArtifactFilter( final List patterns ) |
55 | |
{ |
56 | 62 | this( patterns, false ); |
57 | 62 | } |
58 | |
|
59 | |
public PatternIncludesArtifactFilter( final List patterns, final boolean actTransitively ) |
60 | 76 | { |
61 | 76 | this.actTransitively = actTransitively; |
62 | 76 | final List pos = new ArrayList(); |
63 | 76 | final List neg = new ArrayList(); |
64 | 76 | if ( ( patterns != null ) && !patterns.isEmpty() ) |
65 | |
{ |
66 | 76 | for ( final Iterator it = patterns.iterator(); it.hasNext(); ) |
67 | |
{ |
68 | 102 | final String pattern = (String) it.next(); |
69 | |
|
70 | 102 | if ( pattern.startsWith( "!" ) ) |
71 | |
{ |
72 | 6 | neg.add( pattern.substring( 1 ) ); |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 96 | pos.add( pattern ); |
77 | |
} |
78 | 102 | } |
79 | |
} |
80 | |
|
81 | 76 | positivePatterns = pos; |
82 | 76 | negativePatterns = neg; |
83 | 76 | } |
84 | |
|
85 | |
public boolean include( final Artifact artifact ) |
86 | |
{ |
87 | 36 | final boolean shouldInclude = patternMatches( artifact ); |
88 | |
|
89 | 36 | if ( !shouldInclude ) |
90 | |
{ |
91 | 10 | addFilteredArtifactId( artifact.getId() ); |
92 | |
} |
93 | |
|
94 | 36 | return shouldInclude; |
95 | |
} |
96 | |
|
97 | |
protected boolean patternMatches( final Artifact artifact ) |
98 | |
{ |
99 | 88 | return ( positiveMatch( artifact ) == Boolean.TRUE ) || ( negativeMatch( artifact ) == Boolean.FALSE ); |
100 | |
} |
101 | |
|
102 | |
protected void addFilteredArtifactId( final String artifactId ) |
103 | |
{ |
104 | 44 | filteredArtifactIds.add( artifactId ); |
105 | 44 | } |
106 | |
|
107 | |
private Boolean negativeMatch( final Artifact artifact ) |
108 | |
{ |
109 | 28 | if ( ( negativePatterns == null ) || negativePatterns.isEmpty() ) |
110 | |
{ |
111 | 22 | return null; |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | 6 | return Boolean.valueOf( match( artifact, negativePatterns ) ); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
protected Boolean positiveMatch( final Artifact artifact ) |
120 | |
{ |
121 | 88 | if ( ( positivePatterns == null ) || positivePatterns.isEmpty() ) |
122 | |
{ |
123 | 6 | return null; |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 82 | return Boolean.valueOf( match( artifact, positivePatterns ) ); |
128 | |
} |
129 | |
} |
130 | |
|
131 | |
private boolean match( final Artifact artifact, final List patterns ) |
132 | |
{ |
133 | 88 | final String shortId = ArtifactUtils.versionlessKey( artifact ); |
134 | 88 | final String id = artifact.getDependencyConflictId(); |
135 | 88 | final String wholeId = artifact.getId(); |
136 | |
|
137 | 88 | if ( matchAgainst( wholeId, patterns, false ) ) |
138 | |
{ |
139 | 56 | return true; |
140 | |
} |
141 | |
|
142 | 32 | if ( matchAgainst( id, patterns, false ) ) |
143 | |
{ |
144 | 0 | return true; |
145 | |
} |
146 | |
|
147 | 32 | if ( matchAgainst( shortId, patterns, false ) ) |
148 | |
{ |
149 | 0 | return true; |
150 | |
} |
151 | |
|
152 | 32 | if ( actTransitively ) |
153 | |
{ |
154 | 14 | final List depTrail = artifact.getDependencyTrail(); |
155 | |
|
156 | 14 | if ( ( depTrail != null ) && depTrail.size() > 1 ) |
157 | |
{ |
158 | 10 | for ( final Iterator iterator = depTrail.iterator(); iterator.hasNext(); ) |
159 | |
{ |
160 | 20 | final String trailItem = (String) iterator.next(); |
161 | 20 | if ( matchAgainst( trailItem, patterns, true ) ) |
162 | |
{ |
163 | 10 | return true; |
164 | |
} |
165 | 10 | } |
166 | |
} |
167 | |
} |
168 | |
|
169 | 22 | return false; |
170 | |
} |
171 | |
|
172 | |
private boolean matchAgainst( final String value, final List patterns, final boolean regionMatch ) |
173 | |
{ |
174 | 172 | for ( final Iterator iterator = patterns.iterator(); iterator.hasNext(); ) |
175 | |
{ |
176 | 230 | final String pattern = (String) iterator.next(); |
177 | |
|
178 | 230 | final String[] patternTokens = pattern.split( ":" ); |
179 | 230 | final String[] tokens = value.split( ":" ); |
180 | |
|
181 | |
|
182 | 230 | boolean matched = ( patternTokens.length <= tokens.length ); |
183 | |
|
184 | 532 | for ( int i = 0; matched && i < patternTokens.length; i++ ) |
185 | |
{ |
186 | 302 | matched = matches( tokens[i], patternTokens[i] ); |
187 | |
} |
188 | |
|
189 | |
|
190 | 230 | if ( !matched && patternTokens.length < tokens.length && patternTokens.length > 0 |
191 | |
&& "*".equals( patternTokens[0] ) ) |
192 | |
{ |
193 | 8 | matched = true; |
194 | 28 | for ( int i = 0; matched && i < patternTokens.length; i++ ) |
195 | |
{ |
196 | 20 | matched = matches( tokens[i + ( tokens.length - patternTokens.length )], patternTokens[i] ); |
197 | |
} |
198 | |
} |
199 | |
|
200 | 230 | if ( matched ) |
201 | |
{ |
202 | 66 | patternsTriggered.add( pattern ); |
203 | 66 | return true; |
204 | |
} |
205 | |
|
206 | 164 | if ( regionMatch && value.indexOf( pattern ) > -1 ) |
207 | |
{ |
208 | 0 | patternsTriggered.add( pattern ); |
209 | 0 | return true; |
210 | |
} |
211 | |
|
212 | 164 | } |
213 | 106 | return false; |
214 | |
|
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
private boolean matches( final String token, final String pattern ) |
225 | |
{ |
226 | |
boolean matches; |
227 | |
|
228 | |
|
229 | 322 | if ( "*".equals( pattern ) || pattern.length() == 0 ) |
230 | |
{ |
231 | 46 | matches = true; |
232 | |
} |
233 | |
|
234 | 276 | else if ( pattern.startsWith( "*" ) && pattern.endsWith( "*" ) ) |
235 | |
{ |
236 | 0 | final String contains = pattern.substring( 1, pattern.length() - 1 ); |
237 | |
|
238 | 0 | matches = ( token.indexOf( contains ) != -1 ); |
239 | 0 | } |
240 | |
|
241 | 276 | else if ( pattern.startsWith( "*" ) ) |
242 | |
{ |
243 | 0 | final String suffix = pattern.substring( 1, pattern.length() ); |
244 | |
|
245 | 0 | matches = token.endsWith( suffix ); |
246 | 0 | } |
247 | |
|
248 | 276 | else if ( pattern.endsWith( "*" ) ) |
249 | |
{ |
250 | 32 | final String prefix = pattern.substring( 0, pattern.length() - 1 ); |
251 | |
|
252 | 32 | matches = token.startsWith( prefix ); |
253 | 32 | } |
254 | |
|
255 | 244 | else if ( pattern.indexOf( '*' ) > -1 ) |
256 | |
{ |
257 | 4 | String[] parts = pattern.split( "\\*" ); |
258 | 4 | int lastPartEnd = -1; |
259 | 4 | boolean match = true; |
260 | |
|
261 | 12 | for ( String part : parts ) |
262 | |
{ |
263 | 8 | int idx = token.indexOf( part ); |
264 | 8 | if ( idx <= lastPartEnd ) |
265 | |
{ |
266 | 0 | match = false; |
267 | 0 | break; |
268 | |
} |
269 | |
|
270 | 8 | lastPartEnd = idx + part.length(); |
271 | |
} |
272 | |
|
273 | 4 | matches = match; |
274 | 4 | } |
275 | |
|
276 | 240 | else if ( pattern.startsWith( "[" ) || pattern.startsWith( "(" ) ) |
277 | |
{ |
278 | 0 | matches = isVersionIncludedInRange( token, pattern ); |
279 | |
} |
280 | |
|
281 | |
else |
282 | |
{ |
283 | 240 | matches = token.equals( pattern ); |
284 | |
} |
285 | |
|
286 | 322 | return matches; |
287 | |
} |
288 | |
|
289 | |
private boolean isVersionIncludedInRange( final String version, final String range ) |
290 | |
{ |
291 | |
try |
292 | |
{ |
293 | 0 | return VersionRange.createFromVersionSpec( range ).containsVersion( new DefaultArtifactVersion( version ) ); |
294 | |
} |
295 | 0 | catch ( final InvalidVersionSpecificationException e ) |
296 | |
{ |
297 | 0 | return false; |
298 | |
} |
299 | |
} |
300 | |
|
301 | |
public void reportMissedCriteria( final Logger logger ) |
302 | |
{ |
303 | |
|
304 | 0 | if ( !positivePatterns.isEmpty() || !negativePatterns.isEmpty() ) |
305 | |
{ |
306 | 0 | final List missed = new ArrayList(); |
307 | 0 | missed.addAll( positivePatterns ); |
308 | 0 | missed.addAll( negativePatterns ); |
309 | |
|
310 | 0 | missed.removeAll( patternsTriggered ); |
311 | |
|
312 | 0 | if ( !missed.isEmpty() && logger.isWarnEnabled() ) |
313 | |
{ |
314 | 0 | final StringBuffer buffer = new StringBuffer(); |
315 | |
|
316 | 0 | buffer.append( "The following patterns were never triggered in this " ); |
317 | 0 | buffer.append( getFilterDescription() ); |
318 | 0 | buffer.append( ':' ); |
319 | |
|
320 | 0 | for ( final Iterator it = missed.iterator(); it.hasNext(); ) |
321 | |
{ |
322 | 0 | final String pattern = (String) it.next(); |
323 | |
|
324 | 0 | buffer.append( "\no \'" ).append( pattern ).append( "\'" ); |
325 | 0 | } |
326 | |
|
327 | 0 | buffer.append( "\n" ); |
328 | |
|
329 | 0 | logger.warn( buffer.toString() ); |
330 | |
} |
331 | |
} |
332 | 0 | } |
333 | |
|
334 | |
@Override |
335 | |
public String toString() |
336 | |
{ |
337 | 0 | return "Includes filter:" + getPatternsAsString(); |
338 | |
} |
339 | |
|
340 | |
protected String getPatternsAsString() |
341 | |
{ |
342 | 0 | final StringBuffer buffer = new StringBuffer(); |
343 | 0 | for ( final Iterator it = positivePatterns.iterator(); it.hasNext(); ) |
344 | |
{ |
345 | 0 | final String pattern = (String) it.next(); |
346 | |
|
347 | 0 | buffer.append( "\no \'" ).append( pattern ).append( "\'" ); |
348 | 0 | } |
349 | |
|
350 | 0 | return buffer.toString(); |
351 | |
} |
352 | |
|
353 | |
protected String getFilterDescription() |
354 | |
{ |
355 | 0 | return "artifact inclusion filter"; |
356 | |
} |
357 | |
|
358 | |
public void reportFilteredArtifacts( final Logger logger ) |
359 | |
{ |
360 | 0 | if ( !filteredArtifactIds.isEmpty() && logger.isDebugEnabled() ) |
361 | |
{ |
362 | 0 | final StringBuffer buffer = |
363 | |
new StringBuffer( "The following artifacts were removed by this " + getFilterDescription() + ": " ); |
364 | |
|
365 | 0 | for ( final Iterator it = filteredArtifactIds.iterator(); it.hasNext(); ) |
366 | |
{ |
367 | 0 | final String artifactId = (String) it.next(); |
368 | |
|
369 | 0 | buffer.append( '\n' ).append( artifactId ); |
370 | 0 | } |
371 | |
|
372 | 0 | logger.debug( buffer.toString() ); |
373 | |
} |
374 | 0 | } |
375 | |
|
376 | |
public boolean hasMissedCriteria() |
377 | |
{ |
378 | |
|
379 | 0 | if ( !positivePatterns.isEmpty() || !negativePatterns.isEmpty() ) |
380 | |
{ |
381 | 0 | final List missed = new ArrayList(); |
382 | 0 | missed.addAll( positivePatterns ); |
383 | 0 | missed.addAll( negativePatterns ); |
384 | |
|
385 | 0 | missed.removeAll( patternsTriggered ); |
386 | |
|
387 | 0 | return !missed.isEmpty(); |
388 | |
} |
389 | |
|
390 | 0 | return false; |
391 | |
} |
392 | |
|
393 | |
} |