mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor StatViewServlet support for jndi: unfinished
This commit is contained in:
@@ -126,6 +126,20 @@ public class DruidStatManagerFacade {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
||||
for (DruidDataSource datasource : getDruidDataSourceInstances()) {
|
||||
for (JdbcSqlStat sqlStat : datasource.getDataSourceStat().getSqlStatMap().values()) {
|
||||
if (sqlStat.getExecuteCount() == 0 && sqlStat.getRunningCount() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(getSqlStatData(sqlStat));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
package com.alibaba.druid.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.stat.DruidStatManagerFacade;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.util.MapComparator;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
/**
|
||||
* 注意:避免直接调用Druid相关对象例如DruidDataSource等,相关调用要到DruidStatManagerFacade里用反射实现
|
||||
*
|
||||
* @author sandzhang<sandzhangtoo@gmail.com>
|
||||
*/
|
||||
public class JSONDruidStatService {
|
||||
@@ -96,16 +95,7 @@ public class JSONDruidStatService {
|
||||
if (page == null) page = DEFAULT_PAGE;
|
||||
if (perPageCount == null) perPageCount = DEFAULT_PER_PAGE_COUNT;
|
||||
|
||||
List<Map<String, Object>> array = new ArrayList<Map<String, Object>>();
|
||||
for (DruidDataSource datasource : druidStatManager.getDruidDataSourceInstances()) {
|
||||
for (JdbcSqlStat sqlStat : datasource.getDataSourceStat().getSqlStatMap().values()) {
|
||||
if (sqlStat.getExecuteCount() == 0 && sqlStat.getRunningCount() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
array.add(druidStatManager.getSqlStatData(sqlStat));
|
||||
}
|
||||
}
|
||||
List<Map<String, Object>> array = druidStatManager.getSqlStatDataList();
|
||||
|
||||
// orderby
|
||||
if (orderBy != null && orderBy.trim().length() != 0) {
|
||||
|
||||
@@ -2,23 +2,24 @@ package com.alibaba.druid.support.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.DruidStatManagerFacade;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.support.JSONDruidStatService;
|
||||
import com.alibaba.druid.util.IOUtils;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
/**
|
||||
* 注意:避免直接调用Druid相关对象例如DruidDataSource等,相关调用要到DruidStatManagerFacade里用反射实现
|
||||
*
|
||||
* @author sandzhang<sandzhangtoo@gmail.com>
|
||||
*/
|
||||
public class StatViewServlet extends HttpServlet {
|
||||
@@ -73,22 +74,19 @@ public class StatViewServlet extends HttpServlet {
|
||||
|
||||
if (path.startsWith("/connectionInfo-") && path.endsWith(".html")) {
|
||||
Integer id = StringUtils.subStringToInteger(path, "connectionInfo-", ".");
|
||||
DruidDataSource datasource = druidStatManager.getDruidDataSourceById(id);
|
||||
returnViewConnectionInfo(datasource, request, response);
|
||||
returnViewConnectionInfo(id, request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.startsWith("/activeConnectionStackTrace-") && path.endsWith(".html")) {
|
||||
Integer id = StringUtils.subStringToInteger(path, "activeConnectionStackTrace-", ".");
|
||||
DruidDataSource datasource = druidStatManager.getDruidDataSourceById(id);
|
||||
returnViewActiveConnectionStackTrace(datasource, request, response);
|
||||
returnViewActiveConnectionStackTrace(id, request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.startsWith("/sql-") && path.endsWith(".html")) {
|
||||
Integer id = StringUtils.subStringToInteger(path, "sql-", ".");
|
||||
JdbcSqlStat sqlStat = druidStatManager.getSqlStatById(id);
|
||||
returnViewSqlStat(sqlStat, response);
|
||||
returnViewSqlStat(druidStatManager.getSqlStatData(id), response);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -96,48 +94,41 @@ public class StatViewServlet extends HttpServlet {
|
||||
returnResourceFile(path, response);
|
||||
}
|
||||
|
||||
private void returnViewActiveConnectionStackTrace(DruidDataSource datasource, HttpServletRequest request,
|
||||
private void returnViewActiveConnectionStackTrace(Integer id, HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException {
|
||||
if (datasource == null) return;
|
||||
|
||||
if (!datasource.isRemoveAbandoned()) {
|
||||
response.getWriter().print(mergeTemplatePage("ERROR", "require set removeAbandoned=true"));
|
||||
return;
|
||||
}
|
||||
|
||||
String text = IOUtils.readFromResource(RESOURCE_PATH + "/activeConnectionStackTrace.html");
|
||||
text = text.replaceAll("\\{datasourceId\\}", String.valueOf(System.identityHashCode(datasource)));
|
||||
text = text.replaceAll("\\{datasourceId\\}", id.toString());
|
||||
response.getWriter().print(text);
|
||||
}
|
||||
|
||||
private void returnViewConnectionInfo(DruidDataSource datasource, HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException {
|
||||
if (datasource == null) return;
|
||||
|
||||
private void returnViewConnectionInfo(Integer id, HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException {
|
||||
String text = IOUtils.readFromResource(RESOURCE_PATH + "/connectionInfo.html");
|
||||
text = text.replaceAll("\\{datasourceId\\}", String.valueOf(System.identityHashCode(datasource)));
|
||||
text = text.replaceAll("\\{datasourceId\\}", id.toString());
|
||||
response.getWriter().print(text);
|
||||
}
|
||||
|
||||
private void returnViewSqlStat(JdbcSqlStat sqlStat, HttpServletResponse response) throws IOException {
|
||||
private void returnViewSqlStat(Map<String, Object> sqlStat, HttpServletResponse response) throws IOException {
|
||||
if (sqlStat == null) return;
|
||||
|
||||
StringBuilder content = new StringBuilder();
|
||||
|
||||
content.append("<h2>FULL SQL</h2> <h4>" + sqlStat.getSql() + "</h4>");
|
||||
content.append("<h2>FULL SQL</h2> <h4>" + sqlStat.get("SQL") + "</h4>");
|
||||
content.append("<h2>Format View:</h2>");
|
||||
content.append("<textarea style='width:99%;height:120px;;border:1px #A8C7CE solid;line-height:20px;font-size:12px;'>");
|
||||
content.append(SQLUtils.format(sqlStat.getSql(), sqlStat.getDbType()));
|
||||
content.append(SQLUtils.format((String) sqlStat.get("SQL"), (String) sqlStat.get("DbType")));
|
||||
content.append("</textarea><br />");
|
||||
content.append("<p>API:com.alibaba.druid.sql.SQLUtils.format(sql,DBType);</p>");
|
||||
content.append("<br />");
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sqlStat.getSql(), sqlStat.getDbType());
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements((String) sqlStat.get("SQL"),
|
||||
(String) sqlStat.get("DbType"));
|
||||
if (!statementList.isEmpty()) {
|
||||
content.append("<h2>Parse View:</h2>");
|
||||
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(statementList, sqlStat.getDbType());
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(statementList, (String) sqlStat.get("DbType"));
|
||||
statemen.accept(visitor);
|
||||
content.append("<table cellpadding='5' cellspacing='1' width='99%'>");
|
||||
content.append("<tr>");
|
||||
|
||||
@@ -33,13 +33,23 @@ public class ReflectionUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static Object callPublicMethod(Object obj, String methodName) {
|
||||
|
||||
try {
|
||||
Method m = obj.getClass().getMethod(methodName);
|
||||
return m.invoke(obj);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTest() {
|
||||
return "test";
|
||||
public void getTest() {
|
||||
System.out.println("dd");
|
||||
// return "test";
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
System.out.println(callStaticMethod(ReflectionUtils.class, "getTest"));
|
||||
// System.out.println(callStaticMethod(new ReflectionUtils(), "getTest"));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user