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