diff --git a/src/main/java/cn/wanghw/Main.java b/src/main/java/cn/wanghw/Main.java index 0073fe3..e9b78b1 100644 --- a/src/main/java/cn/wanghw/Main.java +++ b/src/main/java/cn/wanghw/Main.java @@ -31,6 +31,7 @@ public static void main(String[] args) { new PropertySource01(), new PropertySource02(), new PropertySource03(), + new PropertySource04(), new OSS01() }; diff --git a/src/main/java/cn/wanghw/spider/PropertySource04.java b/src/main/java/cn/wanghw/spider/PropertySource04.java new file mode 100644 index 0000000..0972459 --- /dev/null +++ b/src/main/java/cn/wanghw/spider/PropertySource04.java @@ -0,0 +1,56 @@ +package cn.wanghw.spider; + +import cn.wanghw.ISpider; +import cn.wanghw.utils.OQLSnippets; +import org.graalvm.visualvm.lib.jfluid.heap.Heap; +import org.graalvm.visualvm.lib.profiler.oql.engine.api.OQLEngine; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class PropertySource04 implements ISpider { + + @Override + public String getName() { + return "ConsulPropertySources"; + } + + @Override + public String sniff(Heap heap) { + final String[] result = {""}; + long currentObjId = 0; + try { + List listObjId = new ArrayList<>(); + OQLEngine oqlEngine = new OQLEngine(heap); + oqlEngine.executeQuery(OQLSnippets.getTable + OQLSnippets.isMap + "map(filter(map(filter(map(heap.objects('org.springframework.cloud.consul.config.ConsulPropertySource'), 'it.properties'), 'isMap(it)'), 'getTable(it)'), 'it != null'), 'it.id');", o -> { + if (o instanceof Long) { + listObjId.add((Long) o); + } + return false; + }); + List seenKeys = new ArrayList<>(); + for (Long objId : listObjId) { + currentObjId = objId; + oqlEngine.executeQuery(OQLSnippets.getValue + "map(filter(map(heap.findObject(" + objId.toString() + "), 'it'), 'it != null'), \"{'key': getValue(it.key),'value':getValue(it.value)}\")", o -> { + if (o instanceof HashMap) { + HashMap hashMap = (HashMap) o; + String key = hashMap.get("key"); + if (!seenKeys.contains(key)) { + result[0] += hashMap.get("key") + " = " + hashMap.get("value") + "\r\n"; + seenKeys.add(hashMap.get("key")); + } + } + return false; + }); + } + } catch (Exception ex) { + if (result[0].equals("") && ex.getMessage().contains("is not found!")) { + result[0] = "not found!\r\n"; + } else { + System.out.println(ex + " objId: " + currentObjId); + } + } + return result[0]; + } +} diff --git a/src/main/java/cn/wanghw/utils/OQLSnippets.java b/src/main/java/cn/wanghw/utils/OQLSnippets.java index f9c9c82..72e0633 100644 --- a/src/main/java/cn/wanghw/utils/OQLSnippets.java +++ b/src/main/java/cn/wanghw/utils/OQLSnippets.java @@ -7,8 +7,8 @@ public class OQLSnippets { "}\n"; public static final String getValue = isNullOrUndefined + "function getValue(val) {\n" + - " if (!isNullOrUndefined(val)) {\n" + - " if (classof(val) == undefined || classof(val).name == \"java.lang.String\") {\n" + + " if (val != null && !isNullOrUndefined(val)) {\n" + + " if (unwrapJavaObject(val) != null && (classof(val) == undefined || classof(val).name == \"java.lang.String\") ) {\n" + " return val.toString();\n" + " } else if (!isNullOrUndefined(val.str)) {\n" + " return val.str.toString();\n" +