/
RemoteExtensionLoader.java
286 lines (248 loc) · 11.7 KB
/
RemoteExtensionLoader.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2009 Red Hat Inc. and/or its affiliates and other 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.arquillian.container.test.impl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
import org.jboss.arquillian.core.spi.ExtensionLoader;
import org.jboss.arquillian.core.spi.LoadableExtension;
import org.jboss.arquillian.core.spi.Validate;
/**
* ARQ-456 Temp fix. Load a different type of LoadableExtension on the container side, RemoteLoadableExtension.
* <p>
* Should be replaced when proper Modularity, Classloading is in order.
* <p>
* ServiceLoader implementation that use META-INF/services/interface files to registered Services.
*
* @author <a href="mailto:aslak@conduct.no">Aslak Knutsen</a>
* @version $Revision: $
*/
public class RemoteExtensionLoader implements ExtensionLoader {
//-------------------------------------------------------------------------------------||
// Class Members ----------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
private static final String SERVICES = "META-INF/services";
private static final String EXCLUSIONS = "META-INF/exclusions";
//-------------------------------------------------------------------------------------||
// Required Implementations - ExtensionLoader -----------------------------------------||
//-------------------------------------------------------------------------------------||
@Override
public Collection<LoadableExtension> load() {
List<LoadableExtension> extensions = new ArrayList<LoadableExtension>();
Collection<RemoteLoadableExtension> loaded = Collections.emptyList();
if (SecurityActions.getThreadContextClassLoader() != null) {
loaded = all(SecurityActions.getThreadContextClassLoader(), RemoteLoadableExtension.class);
}
if (loaded.size() == 0) {
loaded = all(RemoteExtensionLoader.class.getClassLoader(), RemoteLoadableExtension.class);
}
for (RemoteLoadableExtension extension : loaded) {
extensions.add(extension);
}
return extensions;
}
@Override
public Map<Class<?>, Set<Class<?>>> loadVetoed() {
return loadVetoed(SecurityActions.getThreadContextClassLoader());
}
//-------------------------------------------------------------------------------------||
// General JDK SPI Loader -------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
private <T> Collection<T> all(ClassLoader classLoader, Class<T> serviceClass) {
Validate.notNull(classLoader, "ClassLoader must be provided");
Validate.notNull(serviceClass, "ServiceClass must be provided");
return createInstances(
serviceClass,
load(serviceClass, classLoader));
}
/**
* This method first finds all files that are in claspath placed at META-INF/exclusions
* Each of this file has a name that represents the service type that needs to veto.
* The content of this file is a list of real implementations that you want to veto.
*
* @return List of vetos
*/
public Map<Class<?>, Set<Class<?>>> loadVetoed(ClassLoader classLoader) {
Validate.notNull(classLoader, "ClassLoader must be provided");
final Map<Class<?>, Set<Class<?>>> vetoed = new LinkedHashMap<Class<?>, Set<Class<?>>>();
try {
final Enumeration<URL> exclusions = classLoader.getResources(EXCLUSIONS);
while (exclusions.hasMoreElements()) {
URL exclusion = exclusions.nextElement();
Properties vetoedElements = new Properties();
final InputStream inStream = exclusion.openStream();
try {
vetoedElements.load(inStream);
final Set<Map.Entry<Object, Object>> entries = vetoedElements.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
String service = (String) entry.getKey();
String serviceImpls = (String) entry.getValue();
addVetoedClasses(service, serviceImpls, classLoader, vetoed);
}
} finally {
if (inStream != null) {
inStream.close();
}
}
}
} catch (IOException e) {
throw new RuntimeException("Could not load exclusions from " + EXCLUSIONS, e);
}
return vetoed;
}
//-------------------------------------------------------------------------------------||
// Internal Helper Methods - Service Loading ------------------------------------------||
//-------------------------------------------------------------------------------------||
private void addVetoedClasses(String serviceName, String serviceImpls, ClassLoader classLoader,
Map<Class<?>, Set<Class<?>>> vetoed) {
try {
final Class<?> serviceClass = classLoader.loadClass(serviceName);
final Set<Class<?>> classes = loadVetoedServiceImpl(serviceImpls, classLoader);
final Set<Class<?>> registeredVetoedClasses = vetoed.get(serviceClass);
if (registeredVetoedClasses == null) {
vetoed.put(serviceClass, classes);
} else {
registeredVetoedClasses.addAll(classes);
}
} catch (ClassNotFoundException e) {
// ignores since this is a veto that it is not applicable
}
}
private Set<Class<?>> loadVetoedServiceImpl(String serviceImpls, ClassLoader classLoader) {
final StringTokenizer serviceImplsSeparator = new StringTokenizer(serviceImpls, ",");
final Set<Class<?>> serviceImplsClass = new LinkedHashSet<Class<?>>();
while (serviceImplsSeparator.hasMoreTokens()) {
try {
serviceImplsClass.add(classLoader.loadClass(serviceImplsSeparator.nextToken().trim()));
} catch (ClassNotFoundException e) {
// ignores since this is a veto that it is not applicable
}
}
return serviceImplsClass;
}
private <T> Set<Class<? extends T>> load(Class<T> serviceClass, ClassLoader loader) {
String serviceFile = SERVICES + "/" + serviceClass.getName();
LinkedHashSet<Class<? extends T>> providers = new LinkedHashSet<Class<? extends T>>();
LinkedHashSet<Class<? extends T>> vetoedProviders = new LinkedHashSet<Class<? extends T>>();
try {
Enumeration<URL> enumeration = loader.getResources(serviceFile);
while (enumeration.hasMoreElements()) {
final URL url = enumeration.nextElement();
URLConnection jarConnection = url.openConnection();
//Don't cache the file (avoids file leaks on GlassFish).
jarConnection.setUseCaches(false);
final InputStream is = jarConnection.getInputStream();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = reader.readLine();
while (null != line) {
line = skipCommentAndTrim(line);
if (line.length() > 0) {
try {
boolean mustBeVetoed = line.startsWith("!");
if (mustBeVetoed) {
line = line.substring(1);
}
Class<? extends T> provider = loader.loadClass(line).asSubclass(serviceClass);
if (mustBeVetoed) {
vetoedProviders.add(provider);
}
if (vetoedProviders.contains(provider)) {
providers.remove(provider);
} else {
providers.add(provider);
}
} catch (ClassCastException e) {
throw new IllegalStateException("Service " + line + " does not implement expected type "
+ serviceClass.getName());
}
}
line = reader.readLine();
}
} catch (IOException exc) {
throw new RuntimeException("Could not read file: " + url);
} finally {
if (is != null) {
is.close();
}
if (reader != null) {
reader.close();
}
}
}
} catch (Exception e) {
throw new RuntimeException("Could not load services for " + serviceClass.getName(), e);
}
return providers;
}
private String skipCommentAndTrim(String line) {
final int comment = line.indexOf('#');
if (comment > -1) {
line = line.substring(0, comment);
}
line = line.trim();
return line;
}
private <T> Set<T> createInstances(Class<T> serviceType, Set<Class<? extends T>> providers) {
Set<T> providerImpls = new LinkedHashSet<T>();
for (Class<? extends T> serviceClass : providers) {
providerImpls.add(createInstance(serviceClass));
}
return providerImpls;
}
/**
* Create a new instance of the found Service. <br/>
* <p>
* Verifies that the found ServiceImpl implements Service.
*
* @param serviceType
* The Service interface
* @param className
* The name of the implementation class
* @param loader
* The ClassLoader to load the ServiceImpl from
*
* @return A new instance of the ServiceImpl
*
* @throws Exception
* If problems creating a new instance
*/
private <T> T createInstance(Class<? extends T> serviceImplClass) {
try {
return SecurityActions.newInstance(serviceImplClass, new Class<?>[0], new Object[0]);
} catch (Exception e) {
throw new RuntimeException(
"Could not create a new instance of Service implementation " + serviceImplClass.getName(), e);
}
}
}