-
-
Notifications
You must be signed in to change notification settings - Fork 280
/
WebsphereLibertyClassLoaderHandler.java
246 lines (228 loc) · 10.4 KB
/
WebsphereLibertyClassLoaderHandler.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
/*
* This file is part of ClassGraph.
*
* Author: R. Kempees
*
* With contributions from @cpierceworld (#414)
*
* Hosted at: https://github.com/classgraph/classgraph
*
* --
*
* The MIT License (MIT)
*
* Copyright (c) 2017 R. Kempees (contributed to the ClassGraph project)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package nonapi.io.github.classgraph.classloaderhandler;
import java.io.File;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import nonapi.io.github.classgraph.classpath.ClassLoaderOrder;
import nonapi.io.github.classgraph.classpath.ClasspathOrder;
import nonapi.io.github.classgraph.scanspec.ScanSpec;
import nonapi.io.github.classgraph.utils.LogNode;
import nonapi.io.github.classgraph.utils.ReflectionUtils;
/**
* WebsphereLibertyClassLoaderHandler.
*
* <p>
* Used to support WAS Liberty Profile classloading in io.github.classgraph
*
* @author R. Kempees
*/
class WebsphereLibertyClassLoaderHandler implements ClassLoaderHandler {
/** {@code "com.ibm.ws.classloading.internal."} */
private static final String PKG_PREFIX = "com.ibm.ws.classloading.internal.";
/** {@code "com.ibm.ws.classloading.internal.AppClassLoader"} */
private static final String IBM_APP_CLASS_LOADER = PKG_PREFIX + "AppClassLoader";
/** {@code "com.ibm.ws.classloading.internal.ThreadContextClassLoader"} */
private static final String IBM_THREAD_CONTEXT_CLASS_LOADER = PKG_PREFIX + "ThreadContextClassLoader";
/** Class cannot be constructed. */
private WebsphereLibertyClassLoaderHandler() {
}
/**
* Check whether this {@link ClassLoaderHandler} can handle a given {@link ClassLoader}.
*
* @param classLoaderClass
* the {@link ClassLoader} class or one of its superclasses.
* @param log
* the log
* @return true if this {@link ClassLoaderHandler} can handle the {@link ClassLoader}.
*/
public static boolean canHandle(final Class<?> classLoaderClass, final LogNode log) {
return IBM_APP_CLASS_LOADER.equals(classLoaderClass.getName())
|| IBM_THREAD_CONTEXT_CLASS_LOADER.equals(classLoaderClass.getName());
}
/**
* Find the {@link ClassLoader} delegation order for a {@link ClassLoader}.
*
* @param classLoader
* the {@link ClassLoader} to find the order for.
* @param classLoaderOrder
* a {@link ClassLoaderOrder} object to update.
* @param log
* the log
*/
public static void findClassLoaderOrder(final ClassLoader classLoader, final ClassLoaderOrder classLoaderOrder,
final LogNode log) {
classLoaderOrder.delegateTo(classLoader.getParent(), /* isParent = */ true, log);
classLoaderOrder.add(classLoader, log);
}
/**
* Get the paths from a containerClassLoader object.
*
* <p>
* The passed in object should be an instance of "com.ibm.ws.classloading.internal.ContainerClassLoader".
* <p>
* Will attempt to use "getContainerURLs" methods to recap the classpath.
*
* @param containerClassLoader
* the containerClassLoader object
* @return Collection of path objects as a {@link URL} or {@link String}.
*/
private static Collection<Object> getPaths(final Object containerClassLoader) {
if (containerClassLoader == null) {
return Collections.emptyList();
}
// Expecting this to be an instance of
// "com.ibm.ws.classloading.internal.ContainerClassLoader$UniversalContainer".
// Call "getContainerURLs" to get its container's classpath.
Collection<Object> urls = callGetUrls(containerClassLoader, "getContainerURLs");
if (urls != null && !urls.isEmpty()) {
return urls;
}
// "getContainerURLs" didn't work, try getting the container object...
final Object container = ReflectionUtils.getFieldVal(containerClassLoader, "container", false);
if (container == null) {
return Collections.emptyList();
}
// Should be an instance of "com.ibm.wsspi.adaptable.module.Container".
// Call "getURLs" to get its classpath.
urls = callGetUrls(container, "getURLs");
if (urls != null && !urls.isEmpty()) {
return urls;
}
// "getURLs" did not work, reverting to previous logic of introspection of the "delegate".
final Object delegate = ReflectionUtils.getFieldVal(container, "delegate", false);
if (delegate == null) {
return Collections.emptyList();
}
final String path = (String) ReflectionUtils.getFieldVal(delegate, "path", false);
if (path != null && path.length() > 0) {
return Collections.singletonList((Object) path);
}
final Object base = ReflectionUtils.getFieldVal(delegate, "base", false);
if (base == null) {
// giving up.
return Collections.emptyList();
}
final Object archiveFile = ReflectionUtils.getFieldVal(base, "archiveFile", false);
if (archiveFile != null) {
final File file = (File) archiveFile;
return Collections.singletonList((Object) file.getAbsolutePath());
}
return Collections.emptyList();
}
/**
* Utility to call a "getURLs" method, flattening "collections of collections" and ignoring
* "UnsupportedOperationException".
*
* All of the "getURLs" methods eventually call "com.ibm.wsspi.adaptable.module.Container#getURLs()".
*
* https://www.ibm.com/support/knowledgecenter/SSEQTP_liberty/com.ibm.websphere.javadoc.liberty.doc
* /com.ibm.websphere.appserver.spi.artifact_1.2-javadoc
* /com/ibm/wsspi/adaptable/module/Container.html?view=embed#getURLs() "A collection of URLs that represent all
* of the locations on disk that contribute to this container"
*/
@SuppressWarnings("unchecked")
private static Collection<Object> callGetUrls(final Object container, final String methodName) {
if (container != null) {
try {
final Collection<Object> results = (Collection<Object>) ReflectionUtils.invokeMethod(container,
methodName, false);
if (results != null && !results.isEmpty()) {
final Collection<Object> allUrls = new HashSet<>();
for (final Object result : results) {
if (result instanceof Collection) {
// SmartClassPath returns collection of collection of URLs.
for (final Object url : ((Collection<Object>) result)) {
if (url != null) {
allUrls.add(url);
}
}
} else if (result != null) {
allUrls.add(result);
}
}
return allUrls;
}
} catch (final UnsupportedOperationException e) {
/* ignore */
}
}
return Collections.emptyList();
}
/**
* Find the classpath entries for the associated {@link ClassLoader}.
*
* @param classLoader
* the {@link ClassLoader} to find the classpath entries order for.
* @param classpathOrder
* a {@link ClasspathOrder} object to update.
* @param scanSpec
* the {@link ScanSpec}.
* @param log
* the log.
*/
public static void findClasspathOrder(final ClassLoader classLoader, final ClasspathOrder classpathOrder,
final ScanSpec scanSpec, final LogNode log) {
Object smartClassPath;
final Object appLoader = ReflectionUtils.getFieldVal(classLoader, "appLoader", false);
if (appLoader != null) {
smartClassPath = ReflectionUtils.getFieldVal(appLoader, "smartClassPath", false);
} else {
smartClassPath = ReflectionUtils.getFieldVal(classLoader, "smartClassPath", false);
}
if (smartClassPath != null) {
// "com.ibm.ws.classloading.internal.ContainerClassLoader$SmartClassPath"
// interface specifies a "getClassPath" to return all urls that makeup its path.
final Collection<Object> paths = callGetUrls(smartClassPath, "getClassPath");
if (!paths.isEmpty()) {
for (final Object path : paths) {
classpathOrder.addClasspathEntry(path, classLoader, scanSpec, log);
}
} else {
// "getClassPath" didn't work... reverting to looping over "classPath" elements.
@SuppressWarnings("unchecked")
final List<Object> classPathElements = (List<Object>) ReflectionUtils.getFieldVal(smartClassPath,
"classPath", false);
if (classPathElements != null && !classPathElements.isEmpty()) {
for (final Object classPathElement : classPathElements) {
final Collection<Object> subPaths = getPaths(classPathElement);
for (final Object path : subPaths) {
classpathOrder.addClasspathEntry(path, classLoader, scanSpec, log);
}
}
}
}
}
}
}