mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
StatViewServlet refactor
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import javax.management.JMException;
|
||||
|
||||
import com.alibaba.druid.VERSION;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.ReflectionUtils;
|
||||
|
||||
@@ -35,11 +45,11 @@ public class DruidStatManagerFacade {
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
if (inited) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (inited) {
|
||||
return;
|
||||
}
|
||||
|
||||
initByClassLoader();
|
||||
|
||||
inited = true;
|
||||
@@ -81,6 +91,162 @@ public class DruidStatManagerFacade {
|
||||
init();
|
||||
resetSqlStat();
|
||||
resetDataSourceStat();
|
||||
}
|
||||
|
||||
public JdbcSqlStat getSqlStatById(Integer id) {
|
||||
init();
|
||||
|
||||
for (DruidDataSource ds : getDruidDataSourceInstances()) {
|
||||
JdbcSqlStat sqlStat = ds.getDataSourceStat().getSqlStat(id);
|
||||
if (sqlStat != null) return sqlStat;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
return datasource == null ? null : dataSourceToMapData(datasource);
|
||||
}
|
||||
|
||||
public DruidDataSource getDruidDataSourceById(Integer identity) {
|
||||
init();
|
||||
|
||||
if (identity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (DruidDataSource datasource : getDruidDataSourceInstances()) {
|
||||
if (System.identityHashCode(datasource) == identity) {
|
||||
return datasource;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
JdbcSqlStat sqlStat = getSqlStatById(id);
|
||||
return sqlStat == null ? null : getSqlStatData(sqlStat);
|
||||
}
|
||||
|
||||
public Map<String, Object> getSqlStatData(JdbcSqlStat sqlStat) {
|
||||
try {
|
||||
return sqlStat.getData();
|
||||
} catch (JMException e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Object> getDataSourceStatList() {
|
||||
List<Object> datasourceList = new ArrayList<Object>();
|
||||
for (DruidDataSource dataSource : getDruidDataSourceInstances()) {
|
||||
datasourceList.add(dataSourceToMapData(dataSource));
|
||||
}
|
||||
return datasourceList;
|
||||
}
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("Version", VERSION.getVersionNumber());
|
||||
dataMap.put("Drivers", getDriversData());
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
private List<String> getDriversData() {
|
||||
List<String> drivers = new ArrayList<String>();
|
||||
for (Enumeration<Driver> e = DriverManager.getDrivers(); e.hasMoreElements();) {
|
||||
Driver driver = e.nextElement();
|
||||
drivers.add(driver.getClass().getName());
|
||||
}
|
||||
return drivers;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
if (datasource == null) return null;
|
||||
return datasource.getPoolingConnectionInfo();
|
||||
}
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
if (datasource == null || !datasource.isRemoveAbandoned()) return null;
|
||||
|
||||
return datasource.getActiveConnectionStackTrace();
|
||||
}
|
||||
|
||||
private Map<String, Object> dataSourceToMapData(DruidDataSource dataSource) {
|
||||
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("Identity", System.identityHashCode(dataSource));
|
||||
dataMap.put("Name", dataSource.getName());
|
||||
dataMap.put("DbType", dataSource.getDbType());
|
||||
dataMap.put("DriverClassName", dataSource.getDriverClassName());
|
||||
|
||||
dataMap.put("URL", dataSource.getUrl());
|
||||
dataMap.put("UserName", dataSource.getUsername());
|
||||
dataMap.put("FilterClassNames", dataSource.getFilterClassNames());
|
||||
|
||||
dataMap.put("WaitThreadCount", dataSource.getWaitThreadCount());
|
||||
dataMap.put("NotEmptyWaitCount", dataSource.getNotEmptyWaitCount());
|
||||
dataMap.put("NotEmptyWaitMillis", dataSource.getNotEmptyWaitMillis());
|
||||
|
||||
dataMap.put("PoolingCount", dataSource.getPoolingCount());
|
||||
dataMap.put("PoolingPeak", dataSource.getPoolingPeak());
|
||||
dataMap.put("PoolingPeakTime",
|
||||
dataSource.getPoolingPeakTime() == null ? null : dataSource.getPoolingPeakTime().toString());
|
||||
|
||||
dataMap.put("ActiveCount", dataSource.getActiveCount());
|
||||
dataMap.put("ActivePeak", dataSource.getActivePeak());
|
||||
dataMap.put("ActivePeakTime",
|
||||
dataSource.getActivePeakTime() == null ? null : dataSource.getActivePeakTime().toString());
|
||||
|
||||
dataMap.put("InitialSize", dataSource.getInitialSize());
|
||||
dataMap.put("MinIdle", dataSource.getMinIdle());
|
||||
dataMap.put("MaxActive", dataSource.getMaxActive());
|
||||
|
||||
dataMap.put("QueryTimeout", dataSource.getQueryTimeout());
|
||||
dataMap.put("TransactionQueryTimeout", dataSource.getTransactionQueryTimeout());
|
||||
dataMap.put("LoginTimeout", dataSource.getLoginTimeout());
|
||||
dataMap.put("ValidConnectionCheckerClassName", dataSource.getValidConnectionCheckerClassName());
|
||||
dataMap.put("ExceptionSorterClassName", dataSource.getExceptionSorterClassName());
|
||||
|
||||
dataMap.put("TestOnBorrow", dataSource.isTestOnBorrow());
|
||||
dataMap.put("TestOnReturn", dataSource.isTestOnReturn());
|
||||
dataMap.put("TestWhileIdle", dataSource.isTestWhileIdle());
|
||||
|
||||
dataMap.put("DefaultAutoCommit", dataSource.isDefaultAutoCommit());
|
||||
dataMap.put("DefaultReadOnly", dataSource.isDefaultAutoCommit());
|
||||
dataMap.put("DefaultTransactionIsolation", dataSource.getDefaultTransactionIsolation());
|
||||
|
||||
dataMap.put("LogicConnectCount", dataSource.getConnectCount());
|
||||
dataMap.put("LogicCloseCount", dataSource.getCloseCount());
|
||||
dataMap.put("LogicConnectErrorCount", dataSource.getConnectErrorCount());
|
||||
|
||||
dataMap.put("PhysicalConnectCount", dataSource.getCreateCount());
|
||||
dataMap.put("PhysicalCloseCount", dataSource.getDestroyCount());
|
||||
dataMap.put("PhysicalConnectErrorCount", dataSource.getCreateErrorCount());
|
||||
|
||||
dataMap.put("ExecuteCount", dataSource.getExecuteCount());
|
||||
dataMap.put("ErrorCount", dataSource.getErrorCount());
|
||||
dataMap.put("CommitCount", dataSource.getCommitCount());
|
||||
dataMap.put("RollbackCount", dataSource.getRollbackCount());
|
||||
|
||||
dataMap.put("PSCacheAccessCount", dataSource.getCachedPreparedStatementAccessCount());
|
||||
dataMap.put("PSCacheHitCount", dataSource.getCachedPreparedStatementHitCount());
|
||||
dataMap.put("PSCacheMissCount", dataSource.getCachedPreparedStatementMissCount());
|
||||
|
||||
dataMap.put("StartTransactionCount", dataSource.getStartTransactionCount());
|
||||
dataMap.put("TransactionHistogram", dataSource.getTransactionHistogramValues());
|
||||
|
||||
dataMap.put("ConnectionHoldTimeHistogram",
|
||||
dataSource.getDataSourceStat().getConnectionHoldHistogram().toArray());
|
||||
dataMap.put("RemoveAbandoned", dataSource.isRemoveAbandoned());
|
||||
|
||||
return dataMap;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author sandzhang<sandzhangtoo@gmail.com>
|
||||
*/
|
||||
public class JSONDruidStatService {
|
||||
|
||||
private final static JSONDruidStatService instance = new JSONDruidStatService();
|
||||
|
||||
private static DruidStatManagerFacade druidStatManager = DruidStatManagerFacade.getInstance();
|
||||
|
||||
public final static int RESULT_CODE_SUCCESS = 1;
|
||||
public final static int RESULT_CODE_ERROR = -1;
|
||||
|
||||
private final static int DEFAULT_PAGE = 1;
|
||||
private final static int DEFAULT_PER_PAGE_COUNT = Integer.MAX_VALUE;
|
||||
private static final String DEFAULT_ORDER_TYPE = "asc";
|
||||
|
||||
private JSONDruidStatService() {
|
||||
}
|
||||
|
||||
public static JSONDruidStatService getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public String service(String url) {
|
||||
|
||||
Map<String, String> parameters = StringUtils.getParameters(url);
|
||||
|
||||
if (url.equals("/basic.json")) {
|
||||
return returnJSONResult(RESULT_CODE_SUCCESS, druidStatManager.returnJSONBasicStat());
|
||||
}
|
||||
|
||||
if (url.equals("/reset-all.json")) {
|
||||
druidStatManager.resetAll();
|
||||
return returnJSONResult(RESULT_CODE_SUCCESS, null);
|
||||
}
|
||||
|
||||
if (url.equals("/datasource.json")) {
|
||||
return returnJSONResult(RESULT_CODE_SUCCESS, druidStatManager.getDataSourceStatList());
|
||||
}
|
||||
|
||||
if (url.startsWith("/datasource-")) {
|
||||
Integer id = StringUtils.subStringToInteger(url, "datasource-", ".");
|
||||
Object result = druidStatManager.getDataSourceStatData(id);
|
||||
return returnJSONResult(result == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, result);
|
||||
}
|
||||
|
||||
if (url.startsWith("/connectionInfo-") && url.endsWith(".json")) {
|
||||
Integer id = StringUtils.subStringToInteger(url, "connectionInfo-", ".");
|
||||
List<?> connectionInfoList = druidStatManager.getPoolingConnectionInfoByDataSourceId(id);
|
||||
return returnJSONResult(connectionInfoList == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS,
|
||||
connectionInfoList);
|
||||
}
|
||||
|
||||
if (url.startsWith("/activeConnectionStackTrace-") && url.endsWith(".json")) {
|
||||
Integer id = StringUtils.subStringToInteger(url, "activeConnectionStackTrace-", ".");
|
||||
return returnJSONActiveConnectionStackTrace(id);
|
||||
}
|
||||
|
||||
if (url.startsWith("/sql.json")) {
|
||||
return returnJSONResult(RESULT_CODE_SUCCESS, getSqlStatDataList(parameters));
|
||||
}
|
||||
|
||||
if (url.startsWith("/sql-") && url.indexOf(".json") > 0) {
|
||||
Integer id = StringUtils.subStringToInteger(url, "sql-", ".json");
|
||||
|
||||
Object result = druidStatManager.getSqlStatData(id);
|
||||
return returnJSONResult(result == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, result);
|
||||
}
|
||||
|
||||
return returnJSONResult(RESULT_CODE_ERROR, "Do not support this request, please contact with administrator.");
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> getSqlStatDataList(Map<String, String> parameters) {
|
||||
String orderBy = parameters.get("orderBy");
|
||||
String orderType = parameters.get("orderType");
|
||||
Integer page = Integer.parseInt(parameters.get("page"));
|
||||
Integer perPageCount = Integer.parseInt(parameters.get("perPageCount"));
|
||||
|
||||
if (!"desc".equals(orderType)) orderType = DEFAULT_ORDER_TYPE;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
// orderby
|
||||
if (orderBy != null && orderBy.trim().length() != 0) {
|
||||
Collections.sort(array, new MapComparator<String, Object>(orderBy, DEFAULT_ORDER_TYPE.equals(orderType)));
|
||||
}
|
||||
|
||||
// page
|
||||
int fromIndex = (page - 1) * perPageCount;
|
||||
int toIndex = page * perPageCount;
|
||||
if (toIndex > array.size()) toIndex = array.size();
|
||||
|
||||
return array.subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
private String returnJSONActiveConnectionStackTrace(Integer id) {
|
||||
List<String> result = druidStatManager.getActiveConnectionStackTraceByDataSourceId(id);
|
||||
|
||||
if (result == null) {
|
||||
return returnJSONResult(RESULT_CODE_ERROR, "require set removeAbandoned=true");
|
||||
}
|
||||
return returnJSONResult(RESULT_CODE_SUCCESS, result);
|
||||
}
|
||||
|
||||
private String returnJSONResult(int resultCode, Object content) {
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("ResultCode", resultCode);
|
||||
dataMap.put("Content", content);
|
||||
return JSON.toJSONString(dataMap);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +1,22 @@
|
||||
package com.alibaba.druid.support.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.management.JMException;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.alibaba.druid.VERSION;
|
||||
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.MapComparator;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
/**
|
||||
* @author sandzhang<sandzhangtoo@gmail.com>
|
||||
@@ -37,22 +26,15 @@ public class StatViewServlet extends HttpServlet {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final static int RESULT_CODE_SUCCESS = 1;
|
||||
private final static int RESULT_CODE_ERROR = -1;
|
||||
private final static String RESOURCE_PATH = "support/http/resources";
|
||||
private final static String TEMPLATE_PAGE_RESOURCE_PATH = RESOURCE_PATH + "/template.html";
|
||||
|
||||
private final static int DEFAULT_PAGE = 1;
|
||||
private final static int DEFAULT_PER_PAGE_COUNT = Integer.MAX_VALUE;
|
||||
private static DruidStatManagerFacade druidStatManager = DruidStatManagerFacade.getInstance();
|
||||
private static JSONDruidStatService jsonDruidStatService = JSONDruidStatService.getInstance();
|
||||
|
||||
private final static String RESOURCE_PATH = "support/http/resources";
|
||||
private final static String TEMPLATE_PAGE_RESOURCE_PATH = RESOURCE_PATH + "/template.html";
|
||||
|
||||
private static final String DEFAULT_ORDER_TYPE = "asc";
|
||||
|
||||
private static DruidStatManagerFacade druidStatManager=DruidStatManagerFacade.getInstance();
|
||||
|
||||
public String templatePage;
|
||||
public String templatePage;
|
||||
|
||||
public void init() throws ServletException {
|
||||
try {
|
||||
@@ -80,74 +62,33 @@ public class StatViewServlet extends HttpServlet {
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.equals("/basic.json")) {
|
||||
returnJSONBasicStat(request, response);
|
||||
if (path.indexOf(".json") >= 0) {
|
||||
String fullUrl = path;
|
||||
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
|
||||
fullUrl += "?" + request.getQueryString();
|
||||
}
|
||||
response.getWriter().print(jsonDruidStatService.service(fullUrl));
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.equals("/reset-all.json")) {
|
||||
resetAllStat();
|
||||
returnJSONResult(request, response, RESULT_CODE_SUCCESS, null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.equals("/datasource.json")) {
|
||||
returnJSONResult(request, response, RESULT_CODE_SUCCESS, getDataSourceStatList());
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.startsWith("/datasource-")) {
|
||||
Integer id = StringUtils.subStringToInteger(path, "datasource-", ".");
|
||||
Object result = getDataSourceStatData(id);
|
||||
returnJSONResult(request, response, result == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, result);
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.startsWith("/connectionInfo-")) {
|
||||
if (path.startsWith("/connectionInfo-") && path.endsWith(".html")) {
|
||||
Integer id = StringUtils.subStringToInteger(path, "connectionInfo-", ".");
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
|
||||
if (path.endsWith(".json")) {
|
||||
if (datasource == null) returnJSONResult(request, response, RESULT_CODE_ERROR, null);
|
||||
else returnJSONResult(request, response, RESULT_CODE_SUCCESS, datasource.getPoolingConnectionInfo());
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.endsWith(".html")) {
|
||||
returnViewConnectionInfo(datasource, request, response);
|
||||
return;
|
||||
}
|
||||
DruidDataSource datasource = druidStatManager.getDruidDataSourceById(id);
|
||||
returnViewConnectionInfo(datasource, request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.startsWith("/activeConnectionStackTrace-")) {
|
||||
if (path.startsWith("/activeConnectionStackTrace-") && path.endsWith(".html")) {
|
||||
Integer id = StringUtils.subStringToInteger(path, "activeConnectionStackTrace-", ".");
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
|
||||
if (path.endsWith(".json")) returnJSONActiveConnectionStackTrace(datasource, request, response);
|
||||
if (path.endsWith(".html")) returnViewActiveConnectionStackTrace(datasource, request, response);
|
||||
|
||||
DruidDataSource datasource = druidStatManager.getDruidDataSourceById(id);
|
||||
returnViewActiveConnectionStackTrace(datasource, request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.equals("/sql.json")) {
|
||||
returnJSONResult(request, response, RESULT_CODE_SUCCESS, getSqlStatDataList(request));
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.startsWith("/sql-")) {
|
||||
if (path.startsWith("/sql-") && path.endsWith(".html")) {
|
||||
Integer id = StringUtils.subStringToInteger(path, "sql-", ".");
|
||||
|
||||
if (path.endsWith(".json")) {
|
||||
Object result = getSqlStatData(id);
|
||||
returnJSONResult(request, response, result == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, result);
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.endsWith(".html")) {
|
||||
JdbcSqlStat sqlStat = getSqlStatById(id);
|
||||
returnViewSqlStat(sqlStat, response);
|
||||
return;
|
||||
}
|
||||
JdbcSqlStat sqlStat = druidStatManager.getSqlStatById(id);
|
||||
returnViewSqlStat(sqlStat, response);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -155,16 +96,6 @@ public class StatViewServlet extends HttpServlet {
|
||||
returnResourceFile(path, response);
|
||||
}
|
||||
|
||||
private void returnJSONActiveConnectionStackTrace(DruidDataSource datasource, HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException {
|
||||
if (datasource.isRemoveAbandoned()) {
|
||||
List<String> result = datasource.getActiveConnectionStackTrace();
|
||||
returnJSONResult(request, response, RESULT_CODE_SUCCESS, result);
|
||||
} else {
|
||||
returnJSONResult(request, response, RESULT_CODE_ERROR, "require set removeAbandoned=true");
|
||||
}
|
||||
}
|
||||
|
||||
private void returnViewActiveConnectionStackTrace(DruidDataSource datasource, HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException {
|
||||
if (datasource == null) return;
|
||||
@@ -251,197 +182,6 @@ public class StatViewServlet extends HttpServlet {
|
||||
|
||||
}
|
||||
|
||||
private void resetAllStat() {
|
||||
druidStatManager.resetAll();
|
||||
}
|
||||
|
||||
private List<String> getDriversData() {
|
||||
List<String> drivers = new ArrayList<String>();
|
||||
for (Enumeration<Driver> e = DriverManager.getDrivers(); e.hasMoreElements();) {
|
||||
Driver driver = e.nextElement();
|
||||
drivers.add(driver.getClass().getName());
|
||||
}
|
||||
return drivers;
|
||||
}
|
||||
|
||||
private void returnJSONBasicStat(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("Version", VERSION.getVersionNumber());
|
||||
dataMap.put("Drivers", getDriversData());
|
||||
|
||||
returnJSONResult(request, response, RESULT_CODE_SUCCESS, dataMap);
|
||||
|
||||
}
|
||||
|
||||
private List<Object> getDataSourceStatList() {
|
||||
List<Object> datasourceList = new ArrayList<Object>();
|
||||
for (DruidDataSource dataSource : druidStatManager.getDruidDataSourceInstances()) {
|
||||
datasourceList.add(dataSourceToMapData(dataSource));
|
||||
}
|
||||
return datasourceList;
|
||||
}
|
||||
|
||||
private Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
return datasource == null ? null : dataSourceToMapData(datasource);
|
||||
}
|
||||
|
||||
private Map<String, Object> getSqlStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
JdbcSqlStat sqlStat = getSqlStatById(id);
|
||||
return sqlStat == null ? null : getSqlStatData(sqlStat);
|
||||
}
|
||||
|
||||
private JdbcSqlStat getSqlStatById(Integer id) {
|
||||
for (DruidDataSource ds : druidStatManager.getDruidDataSourceInstances()) {
|
||||
JdbcSqlStat sqlStat = ds.getDataSourceStat().getSqlStat(id);
|
||||
if (sqlStat != null) return sqlStat;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private DruidDataSource getDruidDataSourceById(Integer identity) {
|
||||
if (identity == null) {
|
||||
return null;
|
||||
}
|
||||
for (DruidDataSource datasource : druidStatManager.getDruidDataSourceInstances()) {
|
||||
if (System.identityHashCode(datasource) == identity) {
|
||||
return datasource;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map<String, Object> getSqlStatData(JdbcSqlStat sqlStat) {
|
||||
try {
|
||||
return sqlStat.getData();
|
||||
} catch (JMException e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> getSqlStatDataList(HttpServletRequest request) {
|
||||
String orderBy = request.getParameter("orderBy");
|
||||
String orderType = request.getParameter("orderType");
|
||||
Integer page = StringUtils.stringToInteger(request.getParameter("page"));
|
||||
Integer perPageCount = StringUtils.stringToInteger(request.getParameter("perPageCount"));
|
||||
|
||||
if (!"desc".equals(orderType)) orderType = DEFAULT_ORDER_TYPE;
|
||||
|
||||
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(getSqlStatData(sqlStat));
|
||||
}
|
||||
}
|
||||
|
||||
// orderby
|
||||
if (orderBy != null && orderBy.trim().length() != 0) {
|
||||
Collections.sort(array, new MapComparator<String, Object>(orderBy, DEFAULT_ORDER_TYPE.equals(orderType)));
|
||||
}
|
||||
|
||||
// page
|
||||
int fromIndex = (page - 1) * perPageCount;
|
||||
int toIndex = page * perPageCount;
|
||||
if (toIndex > array.size()) toIndex = array.size();
|
||||
|
||||
return array.subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
private Map<String, Object> dataSourceToMapData(DruidDataSource dataSource) {
|
||||
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("Identity", System.identityHashCode(dataSource));
|
||||
dataMap.put("Name", dataSource.getName());
|
||||
dataMap.put("DbType", dataSource.getDbType());
|
||||
dataMap.put("DriverClassName", dataSource.getDriverClassName());
|
||||
|
||||
dataMap.put("URL", dataSource.getUrl());
|
||||
dataMap.put("UserName", dataSource.getUsername());
|
||||
dataMap.put("FilterClassNames", dataSource.getFilterClassNames());
|
||||
|
||||
dataMap.put("WaitThreadCount", dataSource.getWaitThreadCount());
|
||||
dataMap.put("NotEmptyWaitCount", dataSource.getNotEmptyWaitCount());
|
||||
dataMap.put("NotEmptyWaitMillis", dataSource.getNotEmptyWaitMillis());
|
||||
|
||||
dataMap.put("PoolingCount", dataSource.getPoolingCount());
|
||||
dataMap.put("PoolingPeak", dataSource.getPoolingPeak());
|
||||
dataMap.put("PoolingPeakTime",
|
||||
dataSource.getPoolingPeakTime() == null ? null : dataSource.getPoolingPeakTime().toString());
|
||||
|
||||
dataMap.put("ActiveCount", dataSource.getActiveCount());
|
||||
dataMap.put("ActivePeak", dataSource.getActivePeak());
|
||||
dataMap.put("ActivePeakTime",
|
||||
dataSource.getActivePeakTime() == null ? null : dataSource.getActivePeakTime().toString());
|
||||
|
||||
dataMap.put("InitialSize", dataSource.getInitialSize());
|
||||
dataMap.put("MinIdle", dataSource.getMinIdle());
|
||||
dataMap.put("MaxActive", dataSource.getMaxActive());
|
||||
|
||||
dataMap.put("QueryTimeout", dataSource.getQueryTimeout());
|
||||
dataMap.put("TransactionQueryTimeout", dataSource.getTransactionQueryTimeout());
|
||||
dataMap.put("LoginTimeout", dataSource.getLoginTimeout());
|
||||
dataMap.put("ValidConnectionCheckerClassName", dataSource.getValidConnectionCheckerClassName());
|
||||
dataMap.put("ExceptionSorterClassName", dataSource.getExceptionSorterClassName());
|
||||
|
||||
dataMap.put("TestOnBorrow", dataSource.isTestOnBorrow());
|
||||
dataMap.put("TestOnReturn", dataSource.isTestOnReturn());
|
||||
dataMap.put("TestWhileIdle", dataSource.isTestWhileIdle());
|
||||
|
||||
dataMap.put("DefaultAutoCommit", dataSource.isDefaultAutoCommit());
|
||||
dataMap.put("DefaultReadOnly", dataSource.isDefaultAutoCommit());
|
||||
dataMap.put("DefaultTransactionIsolation", dataSource.getDefaultTransactionIsolation());
|
||||
|
||||
dataMap.put("LogicConnectCount", dataSource.getConnectCount());
|
||||
dataMap.put("LogicCloseCount", dataSource.getCloseCount());
|
||||
dataMap.put("LogicConnectErrorCount", dataSource.getConnectErrorCount());
|
||||
|
||||
dataMap.put("PhysicalConnectCount", dataSource.getCreateCount());
|
||||
dataMap.put("PhysicalCloseCount", dataSource.getDestroyCount());
|
||||
dataMap.put("PhysicalConnectErrorCount", dataSource.getCreateErrorCount());
|
||||
|
||||
dataMap.put("ExecuteCount", dataSource.getExecuteCount());
|
||||
dataMap.put("ErrorCount", dataSource.getErrorCount());
|
||||
dataMap.put("CommitCount", dataSource.getCommitCount());
|
||||
dataMap.put("RollbackCount", dataSource.getRollbackCount());
|
||||
|
||||
dataMap.put("PSCacheAccessCount", dataSource.getCachedPreparedStatementAccessCount());
|
||||
dataMap.put("PSCacheHitCount", dataSource.getCachedPreparedStatementHitCount());
|
||||
dataMap.put("PSCacheMissCount", dataSource.getCachedPreparedStatementMissCount());
|
||||
|
||||
dataMap.put("StartTransactionCount", dataSource.getStartTransactionCount());
|
||||
dataMap.put("TransactionHistogram", dataSource.getTransactionHistogramValues());
|
||||
|
||||
dataMap.put("ConnectionHoldTimeHistogram",
|
||||
dataSource.getDataSourceStat().getConnectionHoldHistogram().toArray());
|
||||
dataMap.put("RemoveAbandoned", dataSource.isRemoveAbandoned());
|
||||
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
private void returnJSONResult(HttpServletRequest request, HttpServletResponse response, int resultCode,
|
||||
Object content) throws IOException {
|
||||
PrintWriter out = response.getWriter();
|
||||
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("ResultCode", resultCode);
|
||||
dataMap.put("Content", content);
|
||||
|
||||
out.print(JSON.toJSONString(dataMap));
|
||||
}
|
||||
|
||||
private void returnResourceFile(String fileName, HttpServletResponse response) throws ServletException, IOException {
|
||||
String text = IOUtils.readFromResource(RESOURCE_PATH + fileName);
|
||||
if (fileName.endsWith(".css")) {
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
/**
|
||||
* @author sandzhang<sandzhangtoo@gmail.com>
|
||||
*/
|
||||
@@ -21,13 +26,13 @@ public class StringUtils {
|
||||
* Example: subString("abcd","a","c")="b"
|
||||
*
|
||||
* @param src
|
||||
* @param start
|
||||
* @param to
|
||||
* @param start null while start from index=0
|
||||
* @param to null while to index=src.length
|
||||
* @return
|
||||
*/
|
||||
public static String subString(String src, String start, String to) {
|
||||
int indexFrom = src.indexOf(start);
|
||||
int indexTo = src.indexOf(to);
|
||||
int indexFrom = start == null ? 0 : src.indexOf(start);
|
||||
int indexTo = to == null ? src.length() : src.indexOf(to);
|
||||
if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
|
||||
return null;
|
||||
}
|
||||
@@ -66,4 +71,27 @@ public class StringUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, String> getParameters(String url) {
|
||||
if (url == null || (url = url.trim()).length() == 0) return null;
|
||||
|
||||
String parametersStr = subString(url, "?", null);
|
||||
if (parametersStr == null || parametersStr.length() == 0) return null;
|
||||
|
||||
String[] parametersArray = parametersStr.split("&");
|
||||
Map<String, String> parameters = new LinkedHashMap<String, String>();
|
||||
|
||||
for (String parameterStr : parametersArray) {
|
||||
int index = parameterStr.indexOf("=");
|
||||
if (index <= 0) continue;
|
||||
|
||||
String name = parameterStr.substring(0, index);
|
||||
String value = parameterStr.substring(index + 1);
|
||||
parameters.put(name, value);
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
System.out.println(JSON.toJSONString(getParameters("test?t=1&f=").get("t")));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user