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