refactor: simplify code

This commit is contained in:
ReaJason
2026-01-12 02:12:34 +08:00
parent 117b48a9d0
commit 52437d67be
23 changed files with 585 additions and 733 deletions
@@ -224,30 +224,25 @@ public class ApusicFilterInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -197,30 +197,25 @@ public class ApusicListenerInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -198,30 +198,25 @@ public class ApusicServletInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -218,30 +218,25 @@ public class BesFilterInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -232,30 +232,25 @@ public static void setFieldValue(final Object obj, final String fieldName, final
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -198,30 +198,25 @@ public class ResinFilterInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -182,30 +182,25 @@ public class ResinListenerInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -183,30 +183,25 @@ public class ResinServletInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -226,30 +226,25 @@ public class TomcatListenerInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -232,30 +232,25 @@ public class TongWebFilterInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@@ -218,30 +218,25 @@ public class TongWebListenerInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -78,6 +78,7 @@ public class UndertowFilterInjector {
return c + "(" + r + ")"; return c + "(" + r + ")";
} }
@SuppressWarnings("Duplicates")
public Set<Object> getContext() throws Exception { public Set<Object> getContext() throws Exception {
Set<Object> contexts = new HashSet<Object>(); Set<Object> contexts = new HashSet<Object>();
Set<Thread> threads = Thread.getAllStackTraces().keySet(); Set<Thread> threads = Thread.getAllStackTraces().keySet();
@@ -218,30 +219,25 @@ public class UndertowFilterInjector {
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -191,30 +191,25 @@ public class UndertowListenerInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -213,30 +213,25 @@ public class UndertowServletInjector {
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -191,30 +191,25 @@ public class WebLogicFilterInjector {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -12,9 +12,9 @@ import java.util.*;
public class ApusicFilterProbe { public class ApusicFilterProbe {
@Override @Override
@SuppressWarnings("Duplicates")
public String toString() { public String toString() {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
Set<Object> contexts = null; Set<Object> contexts = null;
try { try {
contexts = getContext(); contexts = getContext();
@@ -24,6 +24,7 @@ public class ApusicFilterProbe {
if (contexts == null || contexts.isEmpty()) { if (contexts == null || contexts.isEmpty()) {
msg.append("context not found\n"); msg.append("context not found\n");
} else { } else {
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
for (Object context : contexts) { for (Object context : contexts) {
String contextRoot = getContextRoot(context); String contextRoot = getContextRoot(context);
try { try {
@@ -38,14 +39,15 @@ public class ApusicFilterProbe {
return msg.toString(); return msg.toString();
} }
@SuppressWarnings("all") @SuppressWarnings({"unchecked", "Duplicates"})
private List<Map<String, String>> collectFiltersData(Object context) throws Exception { private List<Map<String, String>> collectFiltersData(Object context) throws Exception {
// context -> com.apusic.web.container.WebContainer
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
// webModule -> com.apusic.deploy.runtime.WebModule
Object webModule = getFieldValue(context, "webapp"); Object webModule = getFieldValue(context, "webapp");
Object[] filterMappings = (Object[]) invokeMethod(webModule, "getAllFilterMappings"); Object[] filterMappings = (Object[]) invokeMethod(webModule, "getAllFilterMappings");
Map<String, String> filterClassMap = new HashMap<>();
for (Object fm : filterMappings) { for (Object fm : filterMappings) {
// fm -> com.apusic.deploy.runtime.FilterMapping
String name = (String) invokeMethod(fm, "getFilterName"); String name = (String) invokeMethod(fm, "getFilterName");
if (!aggregatedData.containsKey(name)) { if (!aggregatedData.containsKey(name)) {
Map<String, Object> info = new HashMap<>(); Map<String, Object> info = new HashMap<>();
@@ -58,15 +60,10 @@ public class ApusicFilterProbe {
} }
Map<String, Object> info = aggregatedData.get(name); Map<String, Object> info = aggregatedData.get(name);
String urlPattern = (String) invokeMethod(fm, "getUrlPattern"); String urlPattern = (String) invokeMethod(fm, "getUrlPattern");
if (urlPattern != null) { if (urlPattern != null && !urlPattern.isEmpty()) ((Set<String>) info.get("urlPatterns")).add(urlPattern);
((Set<String>) info.get("urlPatterns")).add(urlPattern);
}
String servletName = (String) invokeMethod(fm, "getServletName"); String servletName = (String) invokeMethod(fm, "getServletName");
if (servletName != null) { if (servletName != null && !servletName.isEmpty()) ((Set<String>) info.get("servletNames")).add(servletName);
((Set<String>) info.get("servletNames")).add(servletName);
}
} }
List<Map<String, String>> result = new ArrayList<>(); List<Map<String, String>> result = new ArrayList<>();
for (Map<String, Object> entry : aggregatedData.values()) { for (Map<String, Object> entry : aggregatedData.values()) {
Map<String, String> finalInfo = new HashMap<>(); Map<String, String> finalInfo = new HashMap<>();
@@ -92,10 +89,10 @@ public class ApusicFilterProbe {
output.append("No filters found\n"); output.append("No filters found\n");
} else { } else {
for (Map<String, String> info : filters) { for (Map<String, String> info : filters) {
appendIfPresent(output, "", info.get("filterName"), ""); appendIfPresent(output, "", info.get("filterName"));
appendIfPresent(output, " -> ", info.get("filterClass"), ""); appendIfPresent(output, " -> ", info.get("filterClass"));
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); appendIfPresent(output, " -> URL:", info.get("urlPatterns"));
appendIfPresent(output, " -> Servlet:", info.get("servletNames"), ""); appendIfPresent(output, " -> Servlet:", info.get("servletNames"));
output.append("\n"); output.append("\n");
} }
} }
@@ -103,13 +100,13 @@ public class ApusicFilterProbe {
return output.toString(); return output.toString();
} }
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { private void appendIfPresent(StringBuilder sb, String prefix, String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
sb.append(prefix).append(value).append(suffix); sb.append(prefix).append(value);
} }
} }
@SuppressWarnings("all") @SuppressWarnings("Duplicates")
private String getContextRoot(Object context) { private String getContextRoot(Object context) {
String r = null; String r = null;
try { try {
@@ -131,6 +128,7 @@ public class ApusicFilterProbe {
* context - webapp: com.apusic.deploy.runtime.WebModule * context - webapp: com.apusic.deploy.runtime.WebModule
* /usr/local/ass/lib/apusic.jar * /usr/local/ass/lib/apusic.jar
*/ */
@SuppressWarnings("Duplicates")
public Set<Object> getContext() throws Exception { public Set<Object> getContext() throws Exception {
Set<Object> contexts = new HashSet<Object>(); Set<Object> contexts = new HashSet<Object>();
Set<Thread> threads = Thread.getAllStackTraces().keySet(); Set<Thread> threads = Thread.getAllStackTraces().keySet();
@@ -38,32 +38,24 @@ public class GlassFishFilterProbe {
return msg.toString(); return msg.toString();
} }
@SuppressWarnings("unchecked") @SuppressWarnings({"unchecked", "Duplicates"})
private List<Map<String, String>> collectFiltersData(Object context) throws Exception { private List<Map<String, String>> collectFiltersData(Object context) throws Exception {
// context -> org.apache.catalina.core.StandardContext
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
List filterMaps = (List) invokeMethod(context, "findFilterMaps"); List<Object> filterMaps = (List<Object>) invokeMethod(context, "findFilterMaps");
if (filterMaps == null || filterMaps.isEmpty()) return Collections.emptyList();
Object[] filterDefs = (Object[]) invokeMethod(context, "findFilterDefs");
for (Object fm : filterMaps) { for (Object fm : filterMaps) {
// fm -> org.apache.catalina.deploy.FilterMap
String name = (String) invokeMethod(fm, "getFilterName"); String name = (String) invokeMethod(fm, "getFilterName");
if (name == null) continue;
if (!aggregatedData.containsKey(name)) { if (!aggregatedData.containsKey(name)) {
String filterClass = "N/A"; // filterDef -> org.apache.catalina.deploy.FilterDef
if (filterDefs != null) { Object filterDef = invokeMethod(context, "findFilterDef", new Class[]{String.class}, new Object[]{name});
Object filterDef = invokeMethod(context, "findFilterDef", new Class[]{String.class}, new Object[]{name}); String filterClass = (String) invokeMethod(filterDef, "getFilterClassName");
try { if (filterClass == null) {
filterClass = (String) invokeMethod(filterDef, "getFilterClass"); // filterConfig -> org.apache.catalina.core.ApplicationFilterConfig
} catch (Throwable throwable) { Object filterConfig = invokeMethod(context, "findFilterConfig", new Class[]{String.class}, new Object[]{name});
filterClass = (String) invokeMethod(filterDef, "getFilterClassName"); Object filter = invokeMethod(filterConfig, "getFilter");
} if (filter != null) filterClass = filter.getClass().getName();
if (filterClass == null) {
Object filterConfig = invokeMethod(context, "findFilterConfig", new Class[]{String.class}, new Object[]{name});
Object filter = invokeMethod(filterConfig, "getFilter");
if (filter != null) filterClass = filter.getClass().getName();
}
} }
Map<String, Object> info = new HashMap<>(); Map<String, Object> info = new HashMap<>();
info.put("filterName", name); info.put("filterName", name);
@@ -79,9 +71,7 @@ public class GlassFishFilterProbe {
} catch (Exception e) { } catch (Exception e) {
// Tomcat 5 // Tomcat 5
String urlPattern = (String) invokeMethod(fm, "getURLPattern"); String urlPattern = (String) invokeMethod(fm, "getURLPattern");
if (urlPattern != null) { if (urlPattern != null) urls = new String[]{urlPattern};
urls = new String[]{urlPattern};
}
} }
if (urls != null) ((Set<String>) info.get("urlPatterns")).addAll(Arrays.asList(urls)); if (urls != null) ((Set<String>) info.get("urlPatterns")).addAll(Arrays.asList(urls));
String[] servletNames = null; String[] servletNames = null;
@@ -90,9 +80,7 @@ public class GlassFishFilterProbe {
} catch (Exception e) { } catch (Exception e) {
// Tomcat 5 // Tomcat 5
String servletName = (String) invokeMethod(fm, "getServletName"); String servletName = (String) invokeMethod(fm, "getServletName");
if (servletName != null) { if (servletName != null) servletNames = new String[]{servletName};
servletNames = new String[]{servletName};
}
} }
if (servletNames != null) ((Set<String>) info.get("servletNames")).addAll(Arrays.asList(servletNames)); if (servletNames != null) ((Set<String>) info.get("servletNames")).addAll(Arrays.asList(servletNames));
} }
@@ -124,10 +112,10 @@ public class GlassFishFilterProbe {
output.append(filters.get(0).get("error")).append("\n"); output.append(filters.get(0).get("error")).append("\n");
} else { } else {
for (Map<String, String> info : filters) { for (Map<String, String> info : filters) {
appendIfPresent(output, "", info.get("filterName"), ""); appendIfPresent(output, "", info.get("filterName"));
appendIfPresent(output, " -> ", info.get("filterClass"), ""); appendIfPresent(output, " -> ", info.get("filterClass"));
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); appendIfPresent(output, " -> URL:", info.get("urlPatterns"));
appendIfPresent(output, " -> Servlet:", info.get("servletNames"), ""); appendIfPresent(output, " -> Servlet:", info.get("servletNames"));
output.append("\n"); output.append("\n");
} }
} }
@@ -135,22 +123,13 @@ public class GlassFishFilterProbe {
return output.toString(); return output.toString();
} }
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { private void appendIfPresent(StringBuilder sb, String prefix, String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
sb.append(prefix).append(value).append(suffix); sb.append(prefix).append(value);
} }
} }
@SuppressWarnings("all") @SuppressWarnings("Duplicates")
private static String repeatString(String str, int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(str);
}
return sb.toString();
}
@SuppressWarnings("all")
private String getContextRoot(Object context) { private String getContextRoot(Object context) {
String r = null; String r = null;
try { try {
@@ -171,8 +150,9 @@ public class GlassFishFilterProbe {
* com.sun.enterprise.web.WebModule * com.sun.enterprise.web.WebModule
* /xxx/modules/web-glue.jar * /xxx/modules/web-glue.jar
*/ */
@SuppressWarnings("Duplicates")
public Set<Object> getContext() throws Exception { public Set<Object> getContext() throws Exception {
Set<Object> contexts = new HashSet<Object>(); Set<Object> contexts = new HashSet<>();
Set<Thread> threads = Thread.getAllStackTraces().keySet(); Set<Thread> threads = Thread.getAllStackTraces().keySet();
for (Thread thread : threads) { for (Thread thread : threads) {
if (thread.getName().contains("ContainerBackgroundProcessor")) { if (thread.getName().contains("ContainerBackgroundProcessor")) {
@@ -186,35 +166,30 @@ public class GlassFishFilterProbe {
return contexts; return contexts;
} }
public static Object invokeMethod(Object obj, String methodName) { public static Object invokeMethod(Object obj, String methodName) throws Exception {
return invokeMethod(obj, methodName, null, null); return invokeMethod(obj, methodName, null, null);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -13,9 +13,9 @@ import java.util.*;
public class JettyFilterProbe { public class JettyFilterProbe {
@Override @Override
@SuppressWarnings("Duplicates")
public String toString() { public String toString() {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
Set<Object> contexts = null; Set<Object> contexts = null;
try { try {
contexts = getContext(); contexts = getContext();
@@ -25,6 +25,7 @@ public class JettyFilterProbe {
if (contexts == null || contexts.isEmpty()) { if (contexts == null || contexts.isEmpty()) {
msg.append("context not found\n"); msg.append("context not found\n");
} else { } else {
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
for (Object context : contexts) { for (Object context : contexts) {
String contextRoot = getContextRoot(context); String contextRoot = getContextRoot(context);
try { try {
@@ -39,24 +40,37 @@ public class JettyFilterProbe {
return msg.toString(); return msg.toString();
} }
/**
* jetty6 -> org.mortbay.jetty.webapp.WebAppContext
* jetty7+ -> org.eclipse.jetty.webapp.WebAppContext
* org.eclipse.jetty.ee8.webapp.WebAppContext
* org.eclipse.jetty.ee9.webapp.WebAppContext
* org.eclipse.jetty.ee10.webapp.WebAppContext
* org.eclipse.jetty.ee11.webapp.WebAppContext
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Map<String, String>> collectFiltersData(Object context) throws Exception { private List<Map<String, String>> collectFiltersData(Object context) throws Exception {
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
// servletHandler -> org.mortbay.jetty.servlet.ServletHandler(jetty 6)
// servletHandler -> org.eclipse.jetty.servlet.ServletHandler(jetty 7+)
Object servletHandler = getFieldValue(context, "_servletHandler"); Object servletHandler = getFieldValue(context, "_servletHandler");
Object[] filterMappings = (Object[]) invokeMethod(servletHandler, "getFilterMappings"); Object[] filterMappings = (Object[]) invokeMethod(servletHandler, "getFilterMappings");
if (filterMappings == null || filterMappings.length == 0) return Collections.emptyList(); if (filterMappings == null || filterMappings.length == 0) return Collections.emptyList();
Object[] filterHolders = (Object[]) invokeMethod(servletHandler, "getFilters"); Object[] filterHolders = (Object[]) invokeMethod(servletHandler, "getFilters");
for (Object mapping : filterMappings) { for (Object mapping : filterMappings) {
// mapping -> org.mortbay.jetty.servlet.FilterMapping(jetty 6)
// mapping -> org.eclipse.jetty.servlet.FilterMapping(jetty 7+)
String name = (String) invokeMethod(mapping, "getFilterName"); String name = (String) invokeMethod(mapping, "getFilterName");
if (name == null) continue;
if (!aggregatedData.containsKey(name)) { if (!aggregatedData.containsKey(name)) {
String filterClass = "N/A"; String filterClass = "N/A";
if (filterHolders != null) { if (filterHolders != null) {
for (Object holder : filterHolders) { for (Object holder : filterHolders) {
// hodler -> org.mortbay.jetty.servlet.FilterHolder(jetty 6)
// hodler -> org.eclipse.jetty.servlet.FilterHolder(jetty 7+)
String holderName = (String) invokeMethod(holder, "getName"); String holderName = (String) invokeMethod(holder, "getName");
if (!name.equals(holderName)) continue; if (!name.equals(holderName)) continue;
String cls = (String) invokeMethod(holder, "getClassName"); String cls = (String) invokeMethod(holder, "getClassName");
@@ -114,10 +128,10 @@ public class JettyFilterProbe {
output.append(filters.get(0).get("error")).append("\n"); output.append(filters.get(0).get("error")).append("\n");
} else { } else {
for (Map<String, String> info : filters) { for (Map<String, String> info : filters) {
appendIfPresent(output, "", info.get("filterName"), ""); appendIfPresent(output, "", info.get("filterName"));
appendIfPresent(output, " -> ", info.get("filterClass"), ""); appendIfPresent(output, " -> ", info.get("filterClass"));
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); appendIfPresent(output, " -> URL:", info.get("urlPatterns"));
appendIfPresent(output, " -> Servlet:", info.get("servletNames"), ""); appendIfPresent(output, " -> Servlet:", info.get("servletNames"));
output.append("\n"); output.append("\n");
} }
} }
@@ -125,9 +139,9 @@ public class JettyFilterProbe {
return output.toString(); return output.toString();
} }
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { private void appendIfPresent(StringBuilder sb, String prefix, String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
sb.append(prefix).append(value).append(suffix); sb.append(prefix).append(value);
} }
} }
@@ -148,13 +162,6 @@ public class JettyFilterProbe {
return c + "(" + r + ")"; return c + "(" + r + ")";
} }
/**
* org.mortbay.jetty.webapp.WebAppContext
* org.eclipse.jetty.webapp.WebAppContext
* org.eclipse.jetty.ee8.webapp.WebAppContext
* org.eclipse.jetty.ee9.webapp.WebAppContext
* org.eclipse.jetty.ee10.webapp.WebAppContext
*/
public Set<Object> getContext() throws Exception { public Set<Object> getContext() throws Exception {
Set<Object> contexts = new HashSet<Object>(); Set<Object> contexts = new HashSet<Object>();
Set<Thread> threads = Thread.getAllStackTraces().keySet(); Set<Thread> threads = Thread.getAllStackTraces().keySet();
@@ -12,9 +12,9 @@ import java.util.*;
public class ResinFilterProbe { public class ResinFilterProbe {
@Override @Override
@SuppressWarnings("Duplicates")
public String toString() { public String toString() {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
Set<Object> contexts = null; Set<Object> contexts = null;
try { try {
contexts = getContext(); contexts = getContext();
@@ -24,6 +24,7 @@ public class ResinFilterProbe {
if (contexts == null || contexts.isEmpty()) { if (contexts == null || contexts.isEmpty()) {
msg.append("context not found\n"); msg.append("context not found\n");
} else { } else {
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
for (Object context : contexts) { for (Object context : contexts) {
String contextRoot = getContextRoot(context); String contextRoot = getContextRoot(context);
try { try {
@@ -40,14 +41,18 @@ public class ResinFilterProbe {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Map<String, String>> collectFiltersData(Object context) throws Exception { private List<Map<String, String>> collectFiltersData(Object context) throws Exception {
// context -> com.caucho.server.webapp.Application
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
// filterMapper -> com.caucho.server.dispatch.FilterMapper
Object filterMapper = getFieldValue(context, "_filterMapper"); Object filterMapper = getFieldValue(context, "_filterMapper");
// filterManager -> com.caucho.server.dispatch.FilterManager
Object filterManager = getFieldValue(context, "_filterManager"); Object filterManager = getFieldValue(context, "_filterManager");
if (filterMapper == null) return Collections.emptyList(); if (filterMapper == null) return Collections.emptyList();
ArrayList<Object> filterMappings = (ArrayList<Object>) getFieldValue(filterMapper, "_filterMap"); ArrayList<Object> filterMappings = (ArrayList<Object>) getFieldValue(filterMapper, "_filterMap");
for (Object filterMapping : filterMappings) { for (Object filterMapping : filterMappings) {
// filterMapping -> com.caucho.server.dispatch.FilterMapping
String filterName = (String) invokeMethod(filterMapping, "getFilterName", null, null); String filterName = (String) invokeMethod(filterMapping, "getFilterName", null, null);
if (aggregatedData.get(filterName) == null) { if (!aggregatedData.containsKey(filterName)) {
Map<String, Object> info = new HashMap<>(); Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName); info.put("filterName", filterName);
String filterClassName = (String) invokeMethod(filterMapping, "getFilterClassName", null, null); String filterClassName = (String) invokeMethod(filterMapping, "getFilterClassName", null, null);
@@ -83,11 +88,11 @@ public class ResinFilterProbe {
urlPatterns.add(urlPattern); urlPatterns.add(urlPattern);
} }
if (!urlPatterns.isEmpty()) { if (!urlPatterns.isEmpty()) {
((Set) info.get("urlPatterns")).addAll(urlPatterns); ((Set<String>) info.get("urlPatterns")).addAll(urlPatterns);
} }
List<String> servletNames = (List<String>) getFieldValue(filterMapping, "_servletNames"); List<String> servletNames = (List<String>) getFieldValue(filterMapping, "_servletNames");
if (servletNames != null && !servletNames.isEmpty()) { if (servletNames != null && !servletNames.isEmpty()) {
((Set) info.get("servletNames")).addAll(servletNames); ((Set<String>) info.get("servletNames")).addAll(servletNames);
} }
} }
List<Map<String, String>> result = new ArrayList<>(); List<Map<String, String>> result = new ArrayList<>();
@@ -117,10 +122,10 @@ public class ResinFilterProbe {
output.append(filters.get(0).get("error")).append("\n"); output.append(filters.get(0).get("error")).append("\n");
} else { } else {
for (Map<String, String> info : filters) { for (Map<String, String> info : filters) {
appendIfPresent(output, "", info.get("filterName"), ""); appendIfPresent(output, "", info.get("filterName"));
appendIfPresent(output, " -> ", info.get("filterClass"), ""); appendIfPresent(output, " -> ", info.get("filterClass"));
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); appendIfPresent(output, " -> URL:", info.get("urlPatterns"));
appendIfPresent(output, " -> Servlet:", info.get("servletNames"), ""); appendIfPresent(output, " -> Servlet:", info.get("servletNames"));
output.append("\n"); output.append("\n");
} }
} }
@@ -128,9 +133,9 @@ public class ResinFilterProbe {
return output.toString(); return output.toString();
} }
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { private void appendIfPresent(StringBuilder sb, String prefix, String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
sb.append(prefix).append(value).append(suffix); sb.append(prefix).append(value);
} }
} }
@@ -176,30 +181,25 @@ public class ResinFilterProbe {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -12,9 +12,9 @@ import java.util.*;
public class TomcatFilterProbe { public class TomcatFilterProbe {
@Override @Override
@SuppressWarnings("Duplicates")
public String toString() { public String toString() {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
Set<Object> contexts = null; Set<Object> contexts = null;
try { try {
contexts = getContext(); contexts = getContext();
@@ -24,6 +24,7 @@ public class TomcatFilterProbe {
if (contexts == null || contexts.isEmpty()) { if (contexts == null || contexts.isEmpty()) {
msg.append("context not found\n"); msg.append("context not found\n");
} else { } else {
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
for (Object context : contexts) { for (Object context : contexts) {
String contextRoot = getContextRoot(context); String contextRoot = getContextRoot(context);
try { try {
@@ -40,26 +41,22 @@ public class TomcatFilterProbe {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Map<String, String>> collectFiltersData(Object context) throws Exception { private List<Map<String, String>> collectFiltersData(Object context) throws Exception {
// context -> org.apache.catalina.core.StandardContext
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
Object[] filterMaps = (Object[]) invokeMethod(context, "findFilterMaps"); Object[] filterMaps = (Object[]) invokeMethod(context, "findFilterMaps");
if (filterMaps == null || filterMaps.length == 0) return Collections.emptyList(); if (filterMaps == null || filterMaps.length == 0) return Collections.emptyList();
Object[] filterDefs = (Object[]) invokeMethod(context, "findFilterDefs");
for (Object fm : filterMaps) { for (Object fm : filterMaps) {
// fm -> org.apache.tomcat.util.descriptor.web.FilterMap/org.apache.catalina.deploy.FilterMap
String name = (String) invokeMethod(fm, "getFilterName"); String name = (String) invokeMethod(fm, "getFilterName");
if (name == null) continue;
if (!aggregatedData.containsKey(name)) { if (!aggregatedData.containsKey(name)) {
String filterClass = "N/A"; // filterDef -> org.apache.tomcat.util.descriptor.web.FilterDef/org.apache.catalina.deploy.FilterDef
if (filterDefs != null) { Object filterDef = invokeMethod(context, "findFilterDef", new Class[]{String.class}, new Object[]{name});
Object filterDef = invokeMethod(context, "findFilterDef", new Class[]{String.class}, new Object[]{name}); String filterClass = (String) invokeMethod(filterDef, "getFilterClass");
filterClass = (String) invokeMethod(filterDef, "getFilterClass"); if (filterClass == null) {
if (filterClass == null) { // filterConfig -> org.apache.catalina.core.ApplicationFilterConfig
Object filterConfig = invokeMethod(context, "findFilterConfig", new Class[]{String.class}, new Object[]{name}); Object filterConfig = invokeMethod(context, "findFilterConfig", new Class[]{String.class}, new Object[]{name});
Object filter = invokeMethod(filterConfig, "getFilter"); Object filter = invokeMethod(filterConfig, "getFilter");
if (filter != null) filterClass = filter.getClass().getName(); if (filter != null) filterClass = filter.getClass().getName();
}
} }
Map<String, Object> info = new HashMap<>(); Map<String, Object> info = new HashMap<>();
info.put("filterName", name); info.put("filterName", name);
@@ -73,7 +70,7 @@ public class TomcatFilterProbe {
try { try {
urls = (String[]) invokeMethod(fm, "getURLPatterns"); urls = (String[]) invokeMethod(fm, "getURLPatterns");
} catch (Exception e) { } catch (Exception e) {
// Tomcat 5 // Tomcat 5 org.apache.catalina.deploy.FilterMap
String urlPattern = (String) invokeMethod(fm, "getURLPattern"); String urlPattern = (String) invokeMethod(fm, "getURLPattern");
if (urlPattern != null) { if (urlPattern != null) {
urls = new String[]{urlPattern}; urls = new String[]{urlPattern};
@@ -84,7 +81,7 @@ public class TomcatFilterProbe {
try { try {
servletNames = (String[]) invokeMethod(fm, "getServletNames"); servletNames = (String[]) invokeMethod(fm, "getServletNames");
} catch (Exception e) { } catch (Exception e) {
// Tomcat 5 // Tomcat 5 org.apache.catalina.deploy.FilterMap
String servletName = (String) invokeMethod(fm, "getServletName"); String servletName = (String) invokeMethod(fm, "getServletName");
if (servletName != null) { if (servletName != null) {
servletNames = new String[]{servletName}; servletNames = new String[]{servletName};
@@ -120,10 +117,10 @@ public class TomcatFilterProbe {
output.append(filters.get(0).get("error")).append("\n"); output.append(filters.get(0).get("error")).append("\n");
} else { } else {
for (Map<String, String> info : filters) { for (Map<String, String> info : filters) {
appendIfPresent(output, "", info.get("filterName"), ""); appendIfPresent(output, "", info.get("filterName"));
appendIfPresent(output, " -> ", info.get("filterClass"), ""); appendIfPresent(output, " -> ", info.get("filterClass"));
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); appendIfPresent(output, " -> URL:", info.get("urlPatterns"));
appendIfPresent(output, " -> Servlet:", info.get("servletNames"), ""); appendIfPresent(output, " -> Servlet:", info.get("servletNames"));
output.append("\n"); output.append("\n");
} }
} }
@@ -131,21 +128,12 @@ public class TomcatFilterProbe {
return output.toString(); return output.toString();
} }
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { private void appendIfPresent(StringBuilder sb, String prefix, String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
sb.append(prefix).append(value).append(suffix); sb.append(prefix).append(value);
} }
} }
@SuppressWarnings("all")
private static String repeatString(String str, int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(str);
}
return sb.toString();
}
@SuppressWarnings("all") @SuppressWarnings("all")
private String getContextRoot(Object context) { private String getContextRoot(Object context) {
String r = null; String r = null;
@@ -204,35 +192,30 @@ public class TomcatFilterProbe {
return contexts; return contexts;
} }
public static Object invokeMethod(Object obj, String methodName){ public static Object invokeMethod(Object obj, String methodName) throws Exception {
return invokeMethod(obj, methodName, null, null); return invokeMethod(obj, methodName, null, null);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -12,9 +12,9 @@ import java.util.*;
public class UndertowFilterProbe { public class UndertowFilterProbe {
@Override @Override
@SuppressWarnings("Duplicates")
public String toString() { public String toString() {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
Set<Object> contexts = null; Set<Object> contexts = null;
try { try {
contexts = getContext(); contexts = getContext();
@@ -24,6 +24,7 @@ public class UndertowFilterProbe {
if (contexts == null || contexts.isEmpty()) { if (contexts == null || contexts.isEmpty()) {
msg.append("context not found\n"); msg.append("context not found\n");
} else { } else {
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
for (Object context : contexts) { for (Object context : contexts) {
String contextRoot = getContextRoot(context); String contextRoot = getContextRoot(context);
try { try {
@@ -40,40 +41,24 @@ public class UndertowFilterProbe {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Map<String, String>> collectFiltersData(Object context) throws Exception { private List<Map<String, String>> collectFiltersData(Object context) throws Exception {
// context -> io.undertow.servlet.spec.ServletContextImpl
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
// deploymentInfo -> io.undertow.servlet.api.DeploymentInfo
Object deploymentInfo = getFieldValue(context, "deploymentInfo"); Object deploymentInfo = getFieldValue(context, "deploymentInfo");
Map<String, Object> filters = (Map<String, Object>) getFieldValue(deploymentInfo, "filters"); Map<String, Object> filters = (Map<String, Object>) getFieldValue(deploymentInfo, "filters");
List<Object> filterUrlMappings = (List<Object>) getFieldValue(deploymentInfo, "filterUrlMappings"); List<Object> filterUrlMappings = (List<Object>) getFieldValue(deploymentInfo, "filterUrlMappings");
List<Object> filterServletNameMappings = (List<Object>) getFieldValue(deploymentInfo, "filterServletNameMappings"); List<Object> filterServletNameMappings = (List<Object>) getFieldValue(deploymentInfo, "filterServletNameMappings");
for (Object filterUrlMapping : filterUrlMappings) { for (Object filterUrlMapping : filterUrlMappings) {
String filterName = (String) getFieldValue(filterUrlMapping, "filterName"); // filterUrlMapping -> io.undertow.servlet.api.FilterMappingInfo
Class<?> filterClass = (Class<?>) getFieldValue(filters.get(filterName), "filterClass"); Map<String, Object> info = fillFilterInfo(filterUrlMapping, filters, aggregatedData);
if (aggregatedData.get(filterName) == null) {
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
info.put("filterClass", filterClass.getName());
info.put("urlPatterns", new LinkedHashSet<String>());
info.put("servletNames", new LinkedHashSet<String>());
aggregatedData.put(filterName, info);
}
Map<String, Object> info = aggregatedData.get(filterName);
String urlPattern = (String) getFieldValue(filterUrlMapping, "mapping"); String urlPattern = (String) getFieldValue(filterUrlMapping, "mapping");
if (urlPattern != null) { if (urlPattern != null) {
((Set<String>) info.get("urlPatterns")).add(urlPattern); ((Set<String>) info.get("urlPatterns")).add(urlPattern);
} }
} }
for (Object filterServletNameMapping : filterServletNameMappings) { for (Object filterServletNameMapping : filterServletNameMappings) {
String filterName = (String) getFieldValue(filterServletNameMapping, "filterName"); // filterServletNameMapping -> io.undertow.servlet.api.FilterMappingInfo
Class<?> filterClass = (Class<?>) getFieldValue(filters.get(filterName), "filterClass"); Map<String, Object> info = fillFilterInfo(filterServletNameMapping, filters, aggregatedData);
if (aggregatedData.get(filterName) == null) {
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
info.put("filterClass", filterClass.getName());
info.put("urlPatterns", new LinkedHashSet<String>());
info.put("servletNames", new LinkedHashSet<String>());
aggregatedData.put(filterName, info);
}
Map<String, Object> info = aggregatedData.get(filterName);
String servletNames = (String) getFieldValue(filterServletNameMapping, "mapping"); String servletNames = (String) getFieldValue(filterServletNameMapping, "mapping");
if (servletNames != null) { if (servletNames != null) {
((Set<String>) info.get("servletNames")).add(servletNames); ((Set<String>) info.get("servletNames")).add(servletNames);
@@ -93,6 +78,20 @@ public class UndertowFilterProbe {
return result; return result;
} }
private static Map<String, Object> fillFilterInfo(Object filterServletNameMapping, Map<String, Object> filters, Map<String, Map<String, Object>> aggregatedData) throws NoSuchFieldException, IllegalAccessException {
String filterName = (String) getFieldValue(filterServletNameMapping, "filterName");
if (!aggregatedData.containsKey(filterName)) {
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
Class<?> filterClass = (Class<?>) getFieldValue(filters.get(filterName), "filterClass");
info.put("filterClass", filterClass.getName());
info.put("urlPatterns", new LinkedHashSet<String>());
info.put("servletNames", new LinkedHashSet<String>());
aggregatedData.put(filterName, info);
}
return aggregatedData.get(filterName);
}
@SuppressWarnings("all") @SuppressWarnings("all")
private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) { private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -106,10 +105,10 @@ public class UndertowFilterProbe {
output.append(filters.get(0).get("error")).append("\n"); output.append(filters.get(0).get("error")).append("\n");
} else { } else {
for (Map<String, String> info : filters) { for (Map<String, String> info : filters) {
appendIfPresent(output, "", info.get("filterName"), ""); appendIfPresent(output, "", info.get("filterName"));
appendIfPresent(output, " -> ", info.get("filterClass"), ""); appendIfPresent(output, " -> ", info.get("filterClass"));
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); appendIfPresent(output, " -> URL:", info.get("urlPatterns"));
appendIfPresent(output, " -> Servlet:", info.get("servletNames"), ""); appendIfPresent(output, " -> Servlet:", info.get("servletNames"));
output.append("\n"); output.append("\n");
} }
} }
@@ -117,9 +116,9 @@ public class UndertowFilterProbe {
return output.toString(); return output.toString();
} }
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { private void appendIfPresent(StringBuilder sb, String prefix, String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
sb.append(prefix).append(value).append(suffix); sb.append(prefix).append(value);
} }
} }
@@ -140,6 +139,7 @@ public class UndertowFilterProbe {
return c + "(" + r + ")"; return c + "(" + r + ")";
} }
@SuppressWarnings("Duplicates")
public Set<Object> getContext() throws Exception { public Set<Object> getContext() throws Exception {
Set<Object> contexts = new HashSet<Object>(); Set<Object> contexts = new HashSet<Object>();
Set<Thread> threads = Thread.getAllStackTraces().keySet(); Set<Thread> threads = Thread.getAllStackTraces().keySet();
@@ -159,30 +159,25 @@ public class UndertowFilterProbe {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -14,9 +14,9 @@ import java.util.*;
public class WebLogicFilterProbe { public class WebLogicFilterProbe {
@Override @Override
@SuppressWarnings("Duplicates")
public String toString() { public String toString() {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
Set<Object> contexts = null; Set<Object> contexts = null;
try { try {
contexts = getContext(); contexts = getContext();
@@ -26,6 +26,7 @@ public class WebLogicFilterProbe {
if (contexts == null || contexts.isEmpty()) { if (contexts == null || contexts.isEmpty()) {
msg.append("context not found\n"); msg.append("context not found\n");
} else { } else {
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
for (Object context : contexts) { for (Object context : contexts) {
String contextRoot = getContextRoot(context); String contextRoot = getContextRoot(context);
try { try {
@@ -42,13 +43,15 @@ public class WebLogicFilterProbe {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Map<String, String>> collectFiltersData(Object context) throws Exception { private List<Map<String, String>> collectFiltersData(Object context) throws Exception {
// context -> weblogic.servlet.internal.WebAppServletContext
// filterManager -> weblogic.servlet.internal.FilterManager
Object filterManager = getFieldValue(context, "filterManager"); Object filterManager = getFieldValue(context, "filterManager");
Map<String, Object> filters = (Map<String, Object>) getFieldValue(filterManager, "filters"); Map<String, Object> filters = (Map<String, Object>) getFieldValue(filterManager, "filters");
List<Object> filterPatternList = (ArrayList<Object>) getFieldValue(filterManager, "filterPatternList"); List<Object> filterPatternList = (ArrayList<Object>) getFieldValue(filterManager, "filterPatternList");
List<Object> filterServletList = (ArrayList<Object>) getFieldValue(filterManager, "filterServletList"); List<Object> filterServletList = (ArrayList<Object>) getFieldValue(filterManager, "filterServletList");
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
for (Object filterInfo : filterPatternList) { for (Object filterInfo : filterPatternList) {
// filterInfo -> weblogic.servlet.internal.FilterManager$FilterInfo
Object urlMap = getFieldValue(filterInfo, "map"); Object urlMap = getFieldValue(filterInfo, "map");
String filterName = (String) getFieldValue(filterInfo, "filterName"); String filterName = (String) getFieldValue(filterInfo, "filterName");
if (filterName == null) { if (filterName == null) {
@@ -56,59 +59,16 @@ public class WebLogicFilterProbe {
Object[] mapValues = (Object[]) invokeMethod(urlMap, "values", null, null); Object[] mapValues = (Object[]) invokeMethod(urlMap, "values", null, null);
filterName = ((String) mapValues[0]); filterName = ((String) mapValues[0]);
} }
if (aggregatedData.get(filterName) == null) { Map<String, Object> info = fillFilterInfo(aggregatedData, filterName, filters);
Object filterWrapper = filters.get(filterName);
String filterClassName = null;
try {
filterClassName = (String) getFieldValue(filterWrapper, "filterClassName");
} catch (NoSuchFieldException e) {
// WebLogic 10.3.6
filterClassName = (String) getFieldValue(filterWrapper, "filterclass");
}
if (filterClassName == null) {
Object filter = getFieldValue(filterWrapper, "filter");
if (filter != null) {
filterClassName = filter.getClass().getName();
}
}
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<String>());
info.put("servletNames", new LinkedHashSet<String>());
aggregatedData.put(filterName, info);
}
Map<String, Object> info = aggregatedData.get(filterName);
String[] urlPatterns = (String[]) invokeMethod(urlMap, "keys", null, null); String[] urlPatterns = (String[]) invokeMethod(urlMap, "keys", null, null);
if (urlPatterns != null) { if (urlPatterns != null) {
((Set<String>) info.get("urlPatterns")).addAll(Arrays.asList(urlPatterns)); ((Set<String>) info.get("urlPatterns")).addAll(Arrays.asList(urlPatterns));
} }
} }
for (Object filterInfo : filterServletList) { for (Object filterInfo : filterServletList) {
// filterInfo -> weblogic.servlet.internal.FilterManager$FilterInfo
String filterName = (String) getFieldValue(filterInfo, "filterName"); String filterName = (String) getFieldValue(filterInfo, "filterName");
if (aggregatedData.get(filterName) == null) { Map<String, Object> info = fillFilterInfo(aggregatedData, filterName, filters);
Object filterWrapper = filters.get(filterName);
String filterClassName = null;
try {
filterClassName = (String) getFieldValue(filterWrapper, "filterClassName");
} catch (NoSuchFieldException e) {
// WebLogic 10.3.6
filterClassName = (String) getFieldValue(filterWrapper, "filterclass");
}
if (filterClassName == null) {
Object filter = getFieldValue(filterWrapper, "filter");
if (filter != null) {
filterClassName = filter.getClass().getName();
}
}
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<String>());
info.put("servletNames", new LinkedHashSet<String>());
aggregatedData.put(filterName, info);
}
Map<String, Object> info = aggregatedData.get(filterName);
String servletName = (String) getFieldValue(filterInfo, "servletName"); String servletName = (String) getFieldValue(filterInfo, "servletName");
if (servletName != null) { if (servletName != null) {
((Set<String>) info.get("servletNames")).add(servletName); ((Set<String>) info.get("servletNames")).add(servletName);
@@ -128,6 +88,33 @@ public class WebLogicFilterProbe {
return result; return result;
} }
private static Map<String, Object> fillFilterInfo(Map<String, Map<String, Object>> aggregatedData, String filterName, Map<String, Object> filters) throws Exception {
if (!aggregatedData.containsKey(filterName)) {
// filterWrapper -> weblogic.servlet.internal.FilterWrapper
Object filterWrapper = filters.get(filterName);
String filterClassName = null;
try {
filterClassName = (String) getFieldValue(filterWrapper, "filterClassName");
} catch (NoSuchFieldException e) {
// WebLogic 10.3.6
filterClassName = (String) getFieldValue(filterWrapper, "filterclass");
}
if (filterClassName == null) {
Object filter = getFieldValue(filterWrapper, "filter");
if (filter != null) {
filterClassName = filter.getClass().getName();
}
}
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<String>());
info.put("servletNames", new LinkedHashSet<String>());
aggregatedData.put(filterName, info);
}
return aggregatedData.get(filterName);
}
@SuppressWarnings("all") @SuppressWarnings("all")
private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) { private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -141,10 +128,10 @@ public class WebLogicFilterProbe {
output.append(filters.get(0).get("error")).append("\n"); output.append(filters.get(0).get("error")).append("\n");
} else { } else {
for (Map<String, String> info : filters) { for (Map<String, String> info : filters) {
appendIfPresent(output, "", info.get("filterName"), ""); appendIfPresent(output, "", info.get("filterName"));
appendIfPresent(output, " -> ", info.get("filterClass"), ""); appendIfPresent(output, " -> ", info.get("filterClass"));
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); appendIfPresent(output, " -> URL:", info.get("urlPatterns"));
appendIfPresent(output, " -> Servlet:", info.get("servletNames"), ""); appendIfPresent(output, " -> Servlet:", info.get("servletNames"));
output.append("\n"); output.append("\n");
} }
} }
@@ -152,9 +139,9 @@ public class WebLogicFilterProbe {
return output.toString(); return output.toString();
} }
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { private void appendIfPresent(StringBuilder sb, String prefix, String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
sb.append(prefix).append(value).append(suffix); sb.append(prefix).append(value);
} }
} }
@@ -214,30 +201,25 @@ public class WebLogicFilterProbe {
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
@@ -12,9 +12,9 @@ import java.util.*;
public class WebSphereFilterProbe { public class WebSphereFilterProbe {
@Override @Override
@SuppressWarnings("Duplicates")
public String toString() { public String toString() {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
Set<Object> contexts = null; Set<Object> contexts = null;
try { try {
contexts = getContext(); contexts = getContext();
@@ -24,6 +24,7 @@ public class WebSphereFilterProbe {
if (contexts == null || contexts.isEmpty()) { if (contexts == null || contexts.isEmpty()) {
msg.append("context not found\n"); msg.append("context not found\n");
} else { } else {
Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>();
for (Object context : contexts) { for (Object context : contexts) {
String contextRoot = getContextRoot(context); String contextRoot = getContextRoot(context);
try { try {
@@ -38,94 +39,54 @@ public class WebSphereFilterProbe {
return msg.toString(); return msg.toString();
} }
@SuppressWarnings("unchecked")
private List<Map<String, String>> collectFiltersData(Object context) throws Exception { private List<Map<String, String>> collectFiltersData(Object context) throws Exception {
// context -> com.ibm.ws.webcontainer.webapp.WebAppImpl
Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>();
// filterManager -> com.ibm.ws.webcontainer.filter.WebAppFilterManager
Object filterManager = getFieldValue(context, "filterManager");
// webAppConfig -> com.ibm.ws.webcontainer.webapp.WebAppConfigurationImpl
Object webAppConfig = getFieldValue(context, "config");
try { try {
Object filterManager = getFieldValue(context, "filterManager"); // WebLogic 12+
Object webAppConfig = getFieldValue(context, "config"); List<Object> uriFilterMappingInfos = (List<Object>) getFieldValue(webAppConfig, "uriFilterMappingInfos");
try { for (Object uriFilterMappingInfo : uriFilterMappingInfos) {
List uriFilterMappingInfos = (List) getFieldValue(webAppConfig, "uriFilterMappingInfos"); // uriFilterMappingInfo -> com.ibm.ws.webcontainer.filter.FilterMapping
for (Object uriFilterMappingInfo : uriFilterMappingInfos) { Map<String, Object> info = filterFilterInfo1(uriFilterMappingInfo, aggregatedData);
Object filterConfig = getFieldValue(uriFilterMappingInfo, "filterConfig"); String urlPattern = (String) invokeMethod(uriFilterMappingInfo, "getUrlPattern", null, null);
String filterName = (String) invokeMethod(filterConfig, "getFilterName", null, null); if (urlPattern != null && !urlPattern.isEmpty()) {
if (!aggregatedData.containsKey(filterName)) { ((Set<String>) info.get("urlPatterns")).add(urlPattern);
String filterClassName = (String) invokeMethod(filterConfig, "getFilterClassName", null, null);
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<>());
info.put("servletNames", new LinkedHashSet<>());
aggregatedData.put(filterName, info);
}
Map<String, Object> info = aggregatedData.get(filterName);
String urlPattern = (String) invokeMethod(uriFilterMappingInfo, "getUrlPattern", null, null);
if (urlPattern != null && !urlPattern.isEmpty()) {
((Set) info.get("urlPatterns")).add(urlPattern);
}
} }
}
List servletFilterMappingInfos = (List) getFieldValue(webAppConfig, "servletFilterMappingInfos"); List<Object> servletFilterMappingInfos = (List<Object>) getFieldValue(webAppConfig, "servletFilterMappingInfos");
for (Object servletFilterMappingInfo : servletFilterMappingInfos) { for (Object servletFilterMappingInfo : servletFilterMappingInfos) {
Object filterConfig = getFieldValue(servletFilterMappingInfo, "filterConfig"); // servletFilterMappingInfo -> com.ibm.ws.webcontainer.filter.FilterMapping
String filterName = (String) invokeMethod(filterConfig, "getFilterName", null, null); Map<String, Object> info = filterFilterInfo1(servletFilterMappingInfo, aggregatedData);
if (!aggregatedData.containsKey(filterName)) { String servletName = (String) invokeMethod(invokeMethod(servletFilterMappingInfo, "getServletConfig"), "getServletName");
String filterClassName = (String) invokeMethod(filterConfig, "getFilterClassName", null, null); if (servletName != null && !servletName.isEmpty()) {
Map<String, Object> info = new HashMap<>(); ((Set<String>) info.get("servletNames")).add(servletName);
info.put("filterName", filterName);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<>());
info.put("servletNames", new LinkedHashSet<>());
aggregatedData.put(filterName, info);
}
Map<String, Object> info = aggregatedData.get(filterName);
String servletName = (String) invokeMethod(invokeMethod(servletFilterMappingInfo, "getServletConfig"), "getServletName");
if (servletName != null && !servletName.isEmpty()) {
((Set) info.get("servletNames")).add(servletName);
}
} }
} catch (Throwable throwable) { }
// WebLogic 10.3.6 } catch (Throwable throwable) {
List uriFilterMappings = (List) getFieldValue(filterManager, "_uriFilterMappings"); // WebLogic 10.3.6
for (Object uriFilterMapping : uriFilterMappings) { List<Object> uriFilterMappings = (List<Object>) getFieldValue(filterManager, "_uriFilterMappings");
String filterName = (String) getFieldValue(uriFilterMapping, "_filterName"); for (Object uriFilterMapping : uriFilterMappings) {
if (!aggregatedData.containsKey(filterName)) { // uriFilterMapping -> com.ibm.ws.webcontainer.filter.WebAppFilterManager$FilterMappingInfo
Object filterConfig = invokeMethod(webAppConfig, "getFilterInfo", new Class[]{String.class}, new Object[]{filterName}); Map<String, Object> info = fillFilterInfo2(uriFilterMapping, aggregatedData, webAppConfig);
String filterClassName = (String) invokeMethod(filterConfig, "getFilterClassName", null, null); String urlPattern = (String) getFieldValue(uriFilterMapping, "_filterURI");
Map<String, Object> info = new HashMap<>(); if (urlPattern != null && !urlPattern.isEmpty()) {
info.put("filterName", filterName); ((Set<String>) info.get("urlPatterns")).add(urlPattern);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<>());
info.put("servletNames", new LinkedHashSet<>());
aggregatedData.put(filterName, info);
}
Map<String, Object> info = aggregatedData.get(filterName);
String urlPattern = (String) getFieldValue(uriFilterMapping, "_filterURI");
if (urlPattern != null && !urlPattern.isEmpty()) {
((Set) info.get("urlPatterns")).add(urlPattern);
}
} }
List servletFilterMappings = (List) getFieldValue(filterManager, "_servletFilterMappings"); }
for (Object servletFilterMapping : servletFilterMappings) { List<Object> servletFilterMappings = (List<Object>) getFieldValue(filterManager, "_servletFilterMappings");
String filterName = (String) getFieldValue(servletFilterMapping, "_filterName"); for (Object servletFilterMapping : servletFilterMappings) {
if (!aggregatedData.containsKey(filterName)) { // servletFilterMapping -> com.ibm.ws.webcontainer.filter.WebAppFilterManager$FilterMappingInfo
Object filterConfig = invokeMethod(webAppConfig, "getFilterInfo", new Class[]{String.class}, new Object[]{filterName}); Map<String, Object> info = fillFilterInfo2(servletFilterMapping, aggregatedData, webAppConfig);
String filterClassName = (String) invokeMethod(filterConfig, "getFilterClassName", null, null); String servletName = (String) getFieldValue(servletFilterMapping, "_filterServlet");
Map<String, Object> info = new HashMap<>(); if (servletName != null && !servletName.isEmpty()) {
info.put("filterName", filterName); ((Set<String>) info.get("servletNames")).add(servletName);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<>());
info.put("servletNames", new LinkedHashSet<>());
aggregatedData.put(filterName, info);
}
Map<String, Object> info = aggregatedData.get(filterName);
String servletName = (String) getFieldValue(servletFilterMapping, "_filterServlet");
if (servletName != null && !servletName.isEmpty()) {
((Set) info.get("servletNames")).add(servletName);
}
} }
} }
} catch (Exception e) {
e.printStackTrace();
} }
List<Map<String, String>> result = new ArrayList<>(); List<Map<String, String>> result = new ArrayList<>();
for (Map<String, Object> entry : aggregatedData.values()) { for (Map<String, Object> entry : aggregatedData.values()) {
@@ -141,6 +102,36 @@ public class WebSphereFilterProbe {
return result; return result;
} }
private static Map<String, Object> filterFilterInfo1(Object uriFilterMappingInfo, Map<String, Map<String, Object>> aggregatedData) throws Exception {
Object filterConfig = getFieldValue(uriFilterMappingInfo, "filterConfig");
String filterName = (String) invokeMethod(filterConfig, "getFilterName", null, null);
if (!aggregatedData.containsKey(filterName)) {
String filterClassName = (String) invokeMethod(filterConfig, "getFilterClassName", null, null);
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<>());
info.put("servletNames", new LinkedHashSet<>());
aggregatedData.put(filterName, info);
}
return aggregatedData.get(filterName);
}
private static Map<String, Object> fillFilterInfo2(Object servletFilterMapping, Map<String, Map<String, Object>> aggregatedData, Object webAppConfig) throws Exception {
String filterName = (String) getFieldValue(servletFilterMapping, "_filterName");
if (!aggregatedData.containsKey(filterName)) {
Object filterConfig = invokeMethod(webAppConfig, "getFilterInfo", new Class[]{String.class}, new Object[]{filterName});
String filterClassName = (String) invokeMethod(filterConfig, "getFilterClassName", null, null);
Map<String, Object> info = new HashMap<>();
info.put("filterName", filterName);
info.put("filterClass", filterClassName);
info.put("urlPatterns", new LinkedHashSet<>());
info.put("servletNames", new LinkedHashSet<>());
aggregatedData.put(filterName, info);
}
return aggregatedData.get(filterName);
}
@SuppressWarnings("all") @SuppressWarnings("all")
private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) { private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -154,10 +145,10 @@ public class WebSphereFilterProbe {
output.append(filters.get(0).get("error")).append("\n"); output.append(filters.get(0).get("error")).append("\n");
} else { } else {
for (Map<String, String> info : filters) { for (Map<String, String> info : filters) {
appendIfPresent(output, "", info.get("filterName"), ""); appendIfPresent(output, "", info.get("filterName"));
appendIfPresent(output, " -> ", info.get("filterClass"), ""); appendIfPresent(output, " -> ", info.get("filterClass"));
appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); appendIfPresent(output, " -> URL:", info.get("urlPatterns"));
appendIfPresent(output, " -> Servlet:", info.get("servletNames"), ""); appendIfPresent(output, " -> Servlet:", info.get("servletNames"));
output.append("\n"); output.append("\n");
} }
} }
@@ -165,9 +156,9 @@ public class WebSphereFilterProbe {
return output.toString(); return output.toString();
} }
private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { private void appendIfPresent(StringBuilder sb, String prefix, String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
sb.append(prefix).append(value).append(suffix); sb.append(prefix).append(value);
} }
} }
@@ -236,35 +227,30 @@ public class WebSphereFilterProbe {
return contexts; return contexts;
} }
public static Object invokeMethod(Object obj, String methodName) { public static Object invokeMethod(Object obj, String methodName) throws Exception {
return invokeMethod(obj, methodName, null, null); return invokeMethod(obj, methodName, null, null);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
try { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); Method method = null;
Method method = null; while (clazz != null && method == null) {
while (clazz != null && method == null) { try {
try { if (paramClazz == null) {
if (paramClazz == null) { method = clazz.getDeclaredMethod(methodName);
method = clazz.getDeclaredMethod(methodName); } else {
} else { method = clazz.getDeclaredMethod(methodName, paramClazz);
method = clazz.getDeclaredMethod(methodName, paramClazz);
}
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} catch (Exception e) {
throw new RuntimeException("Error invoking method: " + methodName, e);
} }
if (method == null) {
throw new NoSuchMethodException("Method not found: " + methodName);
}
method.setAccessible(true);
return method.invoke(obj instanceof Class ? null : obj, param);
} }
@SuppressWarnings("all") @SuppressWarnings("all")