/
ServiceLoader.java
317 lines (289 loc) · 12 KB
/
ServiceLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.metadata.FileMetadata;
import org.jboss.weld.resources.ClassLoaderResourceLoader;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.resources.spi.ResourceLoadingException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* This class handles looking up service providers on the class path. It
* implements the <a href="http://java.sun.com/javase/6/docs/technotes/guides/jar/jar.html#Service%20Provider"
* >Service Provider section of the JAR File Specification</a>.
* <p/>
* The Service Provider programmatic lookup was not specified prior to Java 6 so
* this interface allows use of the specification prior to Java 6.
* <p/>
* The API is copied from <a
* href="http://java.sun.com/javase/6/docs/api/java/util/ServiceLoader.html"
* >java.util.ServiceLoader</a> and enhanced to support the {@link Metadata}
* contract.
*
* @author Pete Muir
* @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
* @author Nicklas Karlsson
*/
public class ServiceLoader<S> implements Iterable<Metadata<S>> {
private static final String ERROR_INSTANTIATING = "Error instantiating ";
private static final String SERVICES = "META-INF/services";
private static final Logger log = Logger.getLogger("ServiceLoader");
/**
* Creates a new service loader for the given service type, using the current
* thread's context class loader.
* <p/>
* An invocation of this convenience method of the form
* <p/>
* {@code ServiceLoader.load(service)</code>}
* <p/>
* is equivalent to
* <p/>
* <code>ServiceLoader.load(service,
* Thread.currentThread().getContextClassLoader())</code>
*
* @param service The interface or abstract class representing the service
* @return A new service loader
*/
public static <S> ServiceLoader<S> load(Class<S> service) {
return load(service, Thread.currentThread().getContextClassLoader());
}
/**
* Creates a new service loader for the given service type and class loader.
*
* @param service The interface or abstract class representing the service
* @param loader The class loader to be used to load provider-configuration
* files and provider classes, or null if the system class loader
* (or, failing that, the bootstrap class loader) is to be used
* @return A new service loader
*/
public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader) {
if (loader == null) {
loader = service.getClassLoader();
}
return new ServiceLoader<S>(service, new ClassLoaderResourceLoader(loader));
}
public static <S> ServiceLoader<S> load(Class<S> service, ResourceLoader loader) {
if (loader == null) {
return load(service, service.getClassLoader());
}
return new ServiceLoader<S>(service, loader);
}
/**
* Creates a new service loader for the given service type, using the
* extension class loader.
* <p/>
* This convenience method simply locates the extension class loader, call it
* extClassLoader, and then returns
* <p/>
* <code>ServiceLoader.load(service, extClassLoader)</code>
* <p/>
* If the extension class loader cannot be found then the system class loader
* is used; if there is no system class loader then the bootstrap class
* loader is used.
* <p/>
* This method is intended for use when only installed providers are desired.
* The resulting service will only find and load providers that have been
* installed into the current Java virtual machine; providers on the
* application's class path will be ignored.
*
* @param service The interface or abstract class representing the service
* @return A new service loader
*/
public static <S> ServiceLoader<S> loadInstalled(Class<S> service) {
throw new UnsupportedOperationException("Not implemented");
}
private final String serviceFile;
private Class<S> expectedType;
private final ResourceLoader loader;
private Set<Metadata<S>> providers;
private ServiceLoader(Class<S> service, ResourceLoader loader) {
this.loader = loader;
this.serviceFile = SERVICES + "/" + service.getName();
this.expectedType = service;
}
/**
* Clear this loader's provider cache so that all providers will be reloaded.
* <p/>
* After invoking this method, subsequent invocations of the iterator method
* will lazily look up and instantiate providers from scratch, just as is
* done by a newly-created loader.
* <p/>
* This method is intended for use in situations in which new providers can
* be installed into a running Java virtual machine.
*/
public void reload() {
providers = new HashSet<Metadata<S>>();
for (URL serviceFile : loadServiceFiles()) {
loadServiceFile(serviceFile);
}
}
private List<URL> loadServiceFiles() {
return new ArrayList<URL>(loader.getResources(serviceFile));
}
@SuppressFBWarnings(value = "OS_OPEN_STREAM", justification = "False positive")
private void loadServiceFile(URL serviceFile) {
InputStream is = null;
try {
is = serviceFile.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String serviceClassName = null;
int i = 0;
while ((serviceClassName = reader.readLine()) != null) {
i++;
serviceClassName = trim(serviceClassName);
if (serviceClassName.length() > 0) {
loadService(serviceClassName, serviceFile, i);
}
}
} catch (IOException e) {
throw new RuntimeException("Could not read services file " + serviceFile, e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException("Could not close services file " + serviceFile, e);
}
}
}
}
private String trim(String line) {
final int comment = line.indexOf('#');
if (comment > -1) {
line = line.substring(0, comment);
}
return line.trim();
}
private void loadService(String serviceClassName, URL file, int lineNumber) {
Class<? extends S> serviceClass = loadClass(serviceClassName);
if (serviceClass == null) {
return;
}
S serviceInstance = prepareInstance(serviceClass);
if (serviceInstance == null) {
return;
}
providers.add(new FileMetadata<S>(serviceInstance, file, lineNumber));
}
private Class<? extends S> loadClass(String serviceClassName) {
Class<?> clazz = null;
Class<? extends S> serviceClass = null;
try {
clazz = loader.classForName(serviceClassName);
serviceClass = clazz.asSubclass(expectedType);
} catch (ResourceLoadingException e) {
log.warning("Could not load service class " + serviceClassName);
} catch (ClassCastException e) {
throw new RuntimeException("Service class " + serviceClassName + " didn't implement the required interface");
}
return serviceClass;
}
private S prepareInstance(final Class<? extends S> serviceClass) {
SecurityManager securityManager = System.getSecurityManager();
if (securityManager != null) {
return AccessController.doPrivileged(new PrivilegedAction<S>() {
@Override
public S run() {
return createInstance(serviceClass);
}
});
} else {
return createInstance(serviceClass);
}
}
/**
* Lazily loads the available providers of this loader's service.
* <p/>
* The iterator returned by this method first yields all of the elements of
* the provider cache, in instantiation order. It then lazily loads and
* instantiates any remaining providers, adding each one to the cache in
* turn.
* <p/>
* To achieve laziness the actual work of parsing the available
* provider-configuration files and instantiating providers must be done by
* the iterator itself. Its hasNext and next methods can therefore throw a
* ServiceConfigurationError if a provider-configuration file violates the
* specified format, or if it names a provider class that cannot be found and
* instantiated, or if the result of instantiating the class is not
* assignable to the service type, or if any other kind of exception or error
* is thrown as the next provider is located and instantiated. To write
* robust code it is only necessary to catch ServiceConfigurationError when
* using a service iterator.
* <p/>
* If such an error is thrown then subsequent invocations of the iterator
* will make a best effort to locate and instantiate the next available
* provider, but in general such recovery cannot be guaranteed.
* <p/>
* Design Note Throwing an error in these cases may seem extreme. The
* rationale for this behavior is that a malformed provider-configuration
* file, like a malformed class file, indicates a serious problem with the
* way the Java virtual machine is configured or is being used. As such it is
* preferable to throw an error rather than try to recover or, even worse,
* fail silently.
* <p/>
* The iterator returned by this method does not support removal. Invoking
* its remove method will cause an UnsupportedOperationException to be
* thrown.
*
* @return An iterator that lazily loads providers for this loader's service
*/
public Iterator<Metadata<S>> iterator() {
if (providers == null) {
reload();
}
return providers.iterator();
}
/**
* Returns a string describing this service.
*
* @return A descriptive string
*/
@Override
public String toString() {
return "Services for " + serviceFile;
}
public Stream<Metadata<S>> stream() {
return StreamSupport.stream(spliterator(), false);
}
private <S> S createInstance(Class<? extends S> serviceClass) {
Constructor<? extends S> constructor = null;
try {
constructor = serviceClass.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (Throwable t) {
throw new ServiceConfigurationError(ERROR_INSTANTIATING + ":" + serviceClass.getName(), t);
}
}
}