1 | |
package org.apache.maven.plugin.surefire.booterclient; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import javax.annotation.Nonnull; |
23 | |
import java.io.BufferedReader; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.net.URL; |
28 | |
import java.util.Collections; |
29 | |
import java.util.Enumeration; |
30 | |
import java.util.HashSet; |
31 | |
import java.util.Set; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class ProviderDetector |
39 | |
{ |
40 | |
|
41 | |
@Nonnull public static Set<String> getServiceNames( Class<?> clazz, ClassLoader classLoader ) |
42 | |
throws IOException |
43 | |
{ |
44 | 0 | final String resourceName = "META-INF/services/" + clazz.getName(); |
45 | |
|
46 | 0 | if ( classLoader == null ) |
47 | |
{ |
48 | 0 | return Collections.emptySet(); |
49 | |
} |
50 | 0 | final Enumeration<URL> urlEnumeration = classLoader.getResources( resourceName ); |
51 | 0 | return getNames( urlEnumeration ); |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Nonnull private static Set<String> getNames( final Enumeration<URL> urlEnumeration ) |
64 | |
throws IOException |
65 | |
{ |
66 | 0 | final Set<String> names = new HashSet<String>(); |
67 | |
nextUrl: |
68 | 0 | while ( urlEnumeration.hasMoreElements() ) |
69 | |
{ |
70 | 0 | final URL url = urlEnumeration.nextElement(); |
71 | 0 | final BufferedReader reader = getReader( url ); |
72 | |
try |
73 | |
{ |
74 | |
String line; |
75 | 0 | while ( ( line = reader.readLine() ) != null ) |
76 | |
{ |
77 | 0 | int ci = line.indexOf( '#' ); |
78 | 0 | if ( ci >= 0 ) |
79 | |
{ |
80 | 0 | line = line.substring( 0, ci ); |
81 | |
} |
82 | 0 | line = line.trim(); |
83 | 0 | int n = line.length(); |
84 | 0 | if ( n == 0 ) |
85 | |
{ |
86 | 0 | continue; |
87 | |
} |
88 | |
|
89 | 0 | if ( ( line.indexOf( ' ' ) >= 0 ) || ( line.indexOf( '\t' ) >= 0 ) ) |
90 | |
{ |
91 | |
continue nextUrl; |
92 | |
} |
93 | 0 | char cp = line.charAt( 0 ); |
94 | 0 | if ( !Character.isJavaIdentifierStart( cp ) ) |
95 | |
{ |
96 | |
continue nextUrl; |
97 | |
} |
98 | 0 | for ( int i = 1; i < n; i++ ) |
99 | |
{ |
100 | 0 | cp = line.charAt( i ); |
101 | 0 | if ( !Character.isJavaIdentifierPart( cp ) && ( cp != '.' ) ) |
102 | |
{ |
103 | |
continue nextUrl; |
104 | |
} |
105 | |
} |
106 | 0 | if ( !names.contains( line ) ) |
107 | |
{ |
108 | 0 | names.add( line ); |
109 | |
} |
110 | 0 | } |
111 | |
} |
112 | |
finally |
113 | |
{ |
114 | 0 | reader.close(); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | 0 | return names; |
119 | |
} |
120 | |
|
121 | |
@Nonnull private static BufferedReader getReader( @Nonnull URL url ) |
122 | |
throws IOException |
123 | |
{ |
124 | 0 | final InputStream inputStream = url.openStream(); |
125 | 0 | final InputStreamReader inputStreamReader = new InputStreamReader( inputStream ); |
126 | 0 | return new BufferedReader( inputStreamReader ); |
127 | |
} |
128 | |
} |