Skip to content

Commit

Permalink
add ConsulPropertySources spider.
Browse files Browse the repository at this point in the history
  • Loading branch information
whwlsfb committed Apr 13, 2022
1 parent 568b2eb commit 3c3b53c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/cn/wanghw/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void main(String[] args) {
new PropertySource01(),
new PropertySource02(),
new PropertySource03(),
new PropertySource04(),
new OSS01()
};

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/cn/wanghw/spider/PropertySource04.java
Original file line number Diff line number Diff line change
@@ -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<Long> 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<String> 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<String, String> hashMap = (HashMap<String, String>) 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];
}
}
4 changes: 2 additions & 2 deletions src/main/java/cn/wanghw/utils/OQLSnippets.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down

0 comments on commit 3c3b53c

Please sign in to comment.