mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
使用策略模式重构DruidStatManagerFacade.java。
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
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 javax.management.JMException;
|
||||
|
||||
import com.alibaba.druid.VERSION;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DruidDataSourceStatDefaultStrategy implements DruidDataSourceStatStrategy {
|
||||
|
||||
private Set<DruidDataSource> getDruidDataSourceInstances() {
|
||||
return DruidDataSourceStatManager.getDruidDataSourceInstances();
|
||||
}
|
||||
|
||||
public void resetDataSourceStat() {
|
||||
DruidDataSourceStatManager.getInstance().reset();
|
||||
}
|
||||
|
||||
public void resetSqlStat() {
|
||||
JdbcStatManager.getInstance().reset();
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
resetSqlStat();
|
||||
resetDataSourceStat();
|
||||
}
|
||||
|
||||
private JdbcSqlStat getSqlStatById(Integer id) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private DruidDataSource getDruidDataSourceById(Integer identity) {
|
||||
|
||||
if (identity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (DruidDataSource datasource : getDruidDataSourceInstances()) {
|
||||
if (System.identityHashCode(datasource) == identity) {
|
||||
return datasource;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
JdbcSqlStat sqlStat = getSqlStatById(id);
|
||||
return sqlStat == null ? null : getSqlStatData(sqlStat);
|
||||
}
|
||||
|
||||
private 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,53 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.util.ReflectionUtils;
|
||||
|
||||
public class DruidDataSourceStatJNDIStatStrategy implements DruidDataSourceStatStrategy{
|
||||
|
||||
private Object getStrategyB() {
|
||||
return ReflectionUtils.getClassFromWebContainerOrCurrentClassLoader("com.alibaba.druid.stat.DruidDataSourceStatDefaultStrategy");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
return (Map<String, Object>) ReflectionUtils.callObjectMethod(getStrategyB(), "getSqlStatData", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
return (List<Map<String, Object>>) ReflectionUtils.callObjectMethod(getStrategyB(), "getSqlStatDataList");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
return (List<String>) ReflectionUtils.callObjectMethod(getStrategyB(), "getActiveConnectionStackTraceByDataSourceId", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
return (Map<String, Object>) ReflectionUtils.callObjectMethod(getStrategyB(), "returnJSONBasicStat");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> getDataSourceStatList() {
|
||||
return (List<Object>) ReflectionUtils.callObjectMethod(getStrategyB(), "getDataSourceStatList");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
return (Map<String, Object>) ReflectionUtils.callObjectMethod(getStrategyB(), "getDataSourceStatData", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
return (List<Map<String, Object>>) ReflectionUtils.callObjectMethod(getStrategyB(), "getPoolingConnectionInfoByDataSourceId", id);
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
ReflectionUtils.callObjectMethod(getStrategyB(), "resetAll");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface DruidDataSourceStatStrategy {
|
||||
public Map<String, Object> getSqlStatData(Integer id);
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList();
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id);
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat();
|
||||
|
||||
public List<Object> getDataSourceStatList();
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id);
|
||||
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id);
|
||||
|
||||
public void resetAll();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DruidDataSourceStatStrategyContext {
|
||||
private DruidDataSourceStatStrategy druidDataSourceStatStrategy;
|
||||
|
||||
public DruidDataSourceStatStrategyContext(DruidDataSourceStatStrategy druidDataSourceStatStrategy) {
|
||||
this.druidDataSourceStatStrategy = druidDataSourceStatStrategy;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
return druidDataSourceStatStrategy.getSqlStatData(id);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
return druidDataSourceStatStrategy.getSqlStatDataList();
|
||||
}
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
return druidDataSourceStatStrategy.getActiveConnectionStackTraceByDataSourceId(id);
|
||||
}
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
return druidDataSourceStatStrategy.returnJSONBasicStat();
|
||||
}
|
||||
|
||||
public List<Object> getDataSourceStatList() {
|
||||
return druidDataSourceStatStrategy.getDataSourceStatList();
|
||||
}
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
return druidDataSourceStatStrategy.getDataSourceStatData(id);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
return druidDataSourceStatStrategy.getPoolingConnectionInfoByDataSourceId(id);
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
druidDataSourceStatStrategy.resetAll();
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,7 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* 监控相关的对外数据暴露
|
||||
@@ -28,252 +15,53 @@ import com.alibaba.druid.util.ReflectionUtils;
|
||||
*/
|
||||
public class DruidStatManagerFacade {
|
||||
|
||||
private final static DruidStatManagerFacade instance = new DruidStatManagerFacade();
|
||||
private final static DruidStatManagerFacade instance = new DruidStatManagerFacade();
|
||||
|
||||
private boolean inited = false;
|
||||
private static DruidDataSourceStatStrategyContext ctx = null;
|
||||
|
||||
protected final ReentrantLock lock = new ReentrantLock(true);
|
||||
private static DruidDataSourceStatStrategy strategy = null;
|
||||
|
||||
private Object druidDataSourceStatManager;
|
||||
private Object jdbcStatManager;
|
||||
static {
|
||||
strategy = new DruidDataSourceStatJNDIStatStrategy();
|
||||
ctx = new DruidDataSourceStatStrategyContext(strategy);
|
||||
}
|
||||
|
||||
// TODO shoud be Object
|
||||
private Set<DruidDataSource> datasourceInstances;
|
||||
private DruidStatManagerFacade() {
|
||||
}
|
||||
|
||||
private DruidStatManagerFacade() {
|
||||
}
|
||||
public static DruidStatManagerFacade getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static DruidStatManagerFacade getInstance() {
|
||||
return instance;
|
||||
}
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
return ctx.getSqlStatData(id);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
if (inited) {
|
||||
return;
|
||||
}
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
return ctx.getSqlStatDataList();
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
try {
|
||||
if (inited) {
|
||||
return;
|
||||
}
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
return ctx.getActiveConnectionStackTraceByDataSourceId(id);
|
||||
}
|
||||
|
||||
initByClassLoader();
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
return ctx.returnJSONBasicStat();
|
||||
}
|
||||
|
||||
inited = true;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
public List<Object> getDataSourceStatList() {
|
||||
return ctx.getDataSourceStatList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void initByClassLoader() {
|
||||
Class<?> druidDataSourceStatManagerClass = ReflectionUtils.getClassFromWebContainerOrCurrentClassLoader("com.alibaba.druid.stat.DruidDataSourceStatManager");
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
return ctx.getDataSourceStatData(id);
|
||||
}
|
||||
|
||||
druidDataSourceStatManager = (DruidDataSourceStatManager) ReflectionUtils.callStaticMethod(druidDataSourceStatManagerClass,
|
||||
"getInstance");
|
||||
datasourceInstances = (Set<DruidDataSource>) ReflectionUtils.callStaticMethod(druidDataSourceStatManagerClass,
|
||||
"getDruidDataSourceInstances");
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
return ctx.getPoolingConnectionInfoByDataSourceId(id);
|
||||
}
|
||||
|
||||
Class<?> jdbcStatManagerClass = ReflectionUtils.getClassFromWebContainerOrCurrentClassLoader("com.alibaba.druid.stat.JdbcStatManager");
|
||||
jdbcStatManager = (JdbcStatManager) ReflectionUtils.callStaticMethod(jdbcStatManagerClass, "getInstance");
|
||||
|
||||
}
|
||||
|
||||
private Set<DruidDataSource> getDruidDataSourceInstances() {
|
||||
init();
|
||||
return datasourceInstances;
|
||||
}
|
||||
|
||||
public void resetDataSourceStat() {
|
||||
init();
|
||||
ReflectionUtils.callObjectMethod(druidDataSourceStatManager, "reset");
|
||||
}
|
||||
|
||||
public void resetSqlStat() {
|
||||
init();
|
||||
ReflectionUtils.callObjectMethod(jdbcStatManager, "reset");
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
init();
|
||||
resetSqlStat();
|
||||
resetDataSourceStat();
|
||||
}
|
||||
|
||||
private JdbcSqlStat getSqlStatById(Integer id) {
|
||||
|
||||
for (DruidDataSource ds : getDruidDataSourceInstances()) {
|
||||
JdbcSqlStat sqlStat = ds.getDataSourceStat().getSqlStat(id);
|
||||
if (sqlStat != null) return sqlStat;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
init();
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
return datasource == null ? null : dataSourceToMapData(datasource);
|
||||
}
|
||||
|
||||
private DruidDataSource getDruidDataSourceById(Integer identity) {
|
||||
|
||||
if (identity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (DruidDataSource datasource : getDruidDataSourceInstances()) {
|
||||
if (System.identityHashCode(datasource) == identity) {
|
||||
return datasource;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
init();
|
||||
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) {
|
||||
init();
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
JdbcSqlStat sqlStat = getSqlStatById(id);
|
||||
return sqlStat == null ? null : getSqlStatData(sqlStat);
|
||||
}
|
||||
|
||||
private Map<String, Object> getSqlStatData(JdbcSqlStat sqlStat) {
|
||||
try {
|
||||
return sqlStat.getData();
|
||||
} catch (JMException e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Object> getDataSourceStatList() {
|
||||
init();
|
||||
List<Object> datasourceList = new ArrayList<Object>();
|
||||
for (DruidDataSource dataSource : getDruidDataSourceInstances()) {
|
||||
datasourceList.add(dataSourceToMapData(dataSource));
|
||||
}
|
||||
return datasourceList;
|
||||
}
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
init();
|
||||
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) {
|
||||
init();
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
if (datasource == null) return null;
|
||||
return datasource.getPoolingConnectionInfo();
|
||||
}
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
init();
|
||||
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;
|
||||
}
|
||||
public void resetAll() {
|
||||
ctx.resetAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,49 +7,86 @@ import javax.servlet.http.HttpServletRequest;
|
||||
/**
|
||||
* @author sandzhang<sandzhangtoo@gmail.com>
|
||||
*/
|
||||
public class ReflectionUtils {
|
||||
public class ReflectionUtils
|
||||
{
|
||||
|
||||
public static Class<?> getClassFromWebContainerOrCurrentClassLoader(String className) {
|
||||
public static Class<?> getClassFromWebContainerOrCurrentClassLoader(String className)
|
||||
{
|
||||
Class<?> result = null;
|
||||
try {
|
||||
try
|
||||
{
|
||||
result = HttpServletRequest.class.getClassLoader().loadClass(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
if (result == null) {
|
||||
try {
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
}
|
||||
if (result == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Thread.currentThread().getContextClassLoader().loadClass(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Object callStaticMethod(Class<?> classObject, String methodName) {
|
||||
|
||||
try {
|
||||
public static Object callStaticMethod(Class<?> classObject, String methodName)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
Method m = classObject.getMethod(methodName);
|
||||
return m.invoke(classObject);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object callObjectMethod(Object obj, String methodName) {
|
||||
|
||||
try {
|
||||
public static Object callObjectMethod(Object obj, String methodName)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
Method m = obj.getClass().getMethod(methodName);
|
||||
return m.invoke(obj);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void getTest() {
|
||||
|
||||
public static Object callObjectMethod(Object obj, String methodName, Object methodParam)
|
||||
{
|
||||
try
|
||||
{
|
||||
Method m = obj.getClass().getMethod(methodName);
|
||||
return m.invoke(obj,methodParam);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void getTest()
|
||||
{
|
||||
System.out.println("dd");
|
||||
// return "test";
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
public static void main(String args[])
|
||||
{
|
||||
System.out.println(callObjectMethod(new ReflectionUtils(), "getTest"));
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user