Skip to content

Commit

Permalink
add AliDruidDataSourceWrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
whwlsfb committed Jan 5, 2022
1 parent 2e504cb commit 77589bc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 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 @@ -24,6 +24,7 @@ public static void main(String[] args) {
new DataSource01(),
new DataSource02(),
new DataSource03(),
new DataSource04(),
new Redis01(),
new Redis02(),
new PropertySource01(),
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/cn/wanghw/spider/DataSource04.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.wanghw.spider;

import cn.wanghw.ISpider;
import cn.wanghw.utils.HashMapUtils;
import org.graalvm.visualvm.lib.jfluid.heap.Heap;
import org.graalvm.visualvm.lib.profiler.oql.engine.api.OQLEngine;

import java.util.HashMap;

public class DataSource04 implements ISpider {
@Override
public String getName() {
return "AliDruidDataSourceWrapper";
}

@Override
public String sniff(Heap heap) {
final String[] result = {""};
try {
OQLEngine oqlEngine = new OQLEngine(heap);
oqlEngine.executeQuery("select {'username':x.username.toString(), 'password':x.password.toString(), 'jdbcUrl': x.jdbcUrl.toString()} from com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper x", o -> {
if (o instanceof HashMap) {
HashMap<String, String> hashMap = (HashMap<String, String>) o;
result[0] += HashMapUtils.dumpString(hashMap, false) + "\r\n";
}
return false;
});
} catch (Exception ex) {
if (!result[0].equals("")) {
result[0] = "not found!";
}
}
return result[0];
}
}
25 changes: 21 additions & 4 deletions src/main/java/cn/wanghw/spider/PropertySource01.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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 PropertySource01 implements ISpider {

Expand All @@ -17,14 +20,28 @@ public String getName() {
public String sniff(Heap heap) {
final String[] result = {""};
try {
List<Long> listObjId = new ArrayList<>();
OQLEngine oqlEngine = new OQLEngine(heap);
oqlEngine.executeQuery("select map(filter(map(x.source.m.table, \"it\"), \"it != null\"),\"{'key':it.key.value.toString(),'value':it.value.value.value.toString()}\") from org.springframework.boot.env.OriginTrackedMapPropertySource x", o -> {
if (o instanceof HashMap) {
HashMap<String, String> hashMap = (HashMap<String, String>) o;
result[0] += hashMap.get("key") + " = " + hashMap.get("value") + "\r\n";
oqlEngine.executeQuery("select map(filter(map(x.source.m ? x.source.m.table : x.source.table, 'it'), 'it != null'),\"it.id\") from org.springframework.boot.env.OriginTrackedMapPropertySource x", o -> {
if (o instanceof Long) {
listObjId.add((Long) o);
}
return false;
});
List<String> seenKeys = new ArrayList<>();
for (Long objId : listObjId) {
oqlEngine.executeQuery(OQLSnippets.getValue + "map(filter(map(heap.findObject(" + objId.toString() + "), 'it'), 'it != null'), \"{'key':it.key.value && it.key.value.toString(),'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("")) {
result[0] = "not found!";
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/cn/wanghw/spider/PropertySource02.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;

Expand All @@ -27,11 +28,16 @@ public String sniff(Heap heap) {
}
return false;
});
List<String> seenKeys = new ArrayList<>();
for (Long objId : listObjId) {
oqlEngine.executeQuery("select map(filter(map(heap.findObject(" + objId.toString() + "), 'it'), 'it != null'), \"{'key':it.key.value && it.key.value.toString(),'value':it.value.value.toString()}\")", o -> {
oqlEngine.executeQuery(OQLSnippets.getValue + "map(filter(map(heap.findObject(" + objId.toString() + "), 'it'), 'it != null'), \"{'key':it.key.value && it.key.value.toString(),'value':getValue(it.value)}\")", o -> {
if (o instanceof HashMap) {
HashMap<String, String> hashMap = (HashMap<String, String>) o;
result[0] += hashMap.get("key") + " = " + hashMap.get("value") + "\r\n";
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;
});
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/wanghw/utils/OQLSnippets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cn.wanghw.utils;

public class OQLSnippets {
public static final String isNullOrUndefined = "function isNullOrUndefined(val) {\n" +
" return val == null || val == undefined\n" +
"}\n";
public static final String getValue = isNullOrUndefined + "function getValue(val) {\n" +
" return !isNullOrUndefined(val.value) ? !isNullOrUndefined(val.value.value) ? !isNullOrUndefined(val.value.value.value) ? val.value.value.value.toString() : val.value.value.toString() : val.value.toString() : val.toString();\n" +
"}\n";
}

0 comments on commit 77589bc

Please sign in to comment.