DiagAgent2.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import java.io.FileWriter;
  2. import java.io.PrintWriter;
  3. import java.lang.instrument.Instrumentation;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.Method;
  6. import java.util.Map;
  7. public class DiagAgent2 {
  8. public static void agentmain(String args, Instrumentation inst) {
  9. try (PrintWriter out = new PrintWriter(new FileWriter("/tmp/forge-diag2.txt"))) {
  10. try {
  11. Class<?> transUtils = null;
  12. for (Class<?> c : inst.getAllLoadedClasses()) {
  13. if ("com.mdframe.forge.starter.trans.util.TransUtils".equals(c.getName())) { transUtils = c; break; }
  14. }
  15. if (transUtils == null) { out.println("TransUtils not loaded"); return; }
  16. Field tmField = transUtils.getDeclaredField("transManager");
  17. tmField.setAccessible(true);
  18. Object transManager = tmField.get(null);
  19. Field ctxField = transManager.getClass().getDeclaredField("applicationContext");
  20. ctxField.setAccessible(true);
  21. Object ctx = ctxField.get(transManager);
  22. Object env = ctx.getClass().getMethod("getEnvironment").invoke(ctx);
  23. Object sources = env.getClass().getMethod("getPropertySources").invoke(env);
  24. out.println("== main context env property sources ==");
  25. for (Object ps : (Iterable<?>) sources) {
  26. String name = (String) ps.getClass().getMethod("getName").invoke(ps);
  27. out.println(" - " + name + " (" + ps.getClass().getSimpleName() + ") @" + System.identityHashCode(ps));
  28. if ("forgeCryptoBootstrap".equals(name) || "dbPropertySource".equals(name)) {
  29. Object src = ps.getClass().getMethod("getSource").invoke(ps);
  30. if (src instanceof Map<?, ?> m) {
  31. for (Map.Entry<?, ?> e : m.entrySet()) {
  32. String k = String.valueOf(e.getKey());
  33. String v = String.valueOf(e.getValue());
  34. boolean secret = k.contains("key") || k.contains("secret");
  35. out.println(" " + k + " = " + (secret ? ("<len=" + v.length() + ">") : v));
  36. }
  37. }
  38. }
  39. }
  40. } catch (Throwable t) { t.printStackTrace(out); }
  41. } catch (Exception ignore) {}
  42. }
  43. }