
com.alibaba.druid.stat.DruidStatService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of druid Show documentation
Show all versions of druid Show documentation
An JDBC datasource implementation.
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.stat;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.support.http.stat.WebAppStatManager;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.support.spring.stat.SpringStatManager;
import com.alibaba.druid.util.MapComparator;
import com.alibaba.druid.util.StringUtils;
import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 注意:避免直接调用Druid相关对象例如DruidDataSource等,相关调用要到DruidStatManagerFacade里用反射实现
*
* @author sandzhang[[email protected]]
*/
public final class DruidStatService implements DruidStatServiceMBean {
private static final Log LOG = LogFactory.getLog(DruidStatService.class);
public static final String MBEAN_NAME = "com.alibaba.druid:type=DruidStatService";
private static final DruidStatService instance = new DruidStatService();
private static DruidStatManagerFacade statManagerFacade = DruidStatManagerFacade.getInstance();
public static final int RESULT_CODE_SUCCESS = 1;
public static final int RESULT_CODE_ERROR = -1;
private static final int DEFAULT_PAGE = 1;
private static final int DEFAULT_PER_PAGE_COUNT = Integer.MAX_VALUE;
private static final String ORDER_TYPE_DESC = "desc";
private static final String ORDER_TYPE_ASC = "asc";
private static final String DEFAULT_ORDER_TYPE = ORDER_TYPE_ASC;
private static final String DEFAULT_ORDERBY = "SQL";
private DruidStatService() {
}
public static DruidStatService getInstance() {
return instance;
}
public boolean isResetEnable() {
return statManagerFacade.isResetEnable();
}
public void setResetEnable(boolean value) {
statManagerFacade.setResetEnable(value);
}
public String service(String url) {
Map parameters = getParameters(url);
if (url.equals("/basic.json")) {
return returnJSONResult(RESULT_CODE_SUCCESS, statManagerFacade.returnJSONBasicStat());
}
if (url.equals("/reset-all.json")) {
statManagerFacade.resetAll();
return returnJSONResult(RESULT_CODE_SUCCESS, null);
}
if (url.equals("/log-and-reset.json")) {
statManagerFacade.logAndResetDataSource();
return returnJSONResult(RESULT_CODE_SUCCESS, null);
}
if (url.equals("/datasource.json")) {
return returnJSONResult(RESULT_CODE_SUCCESS, statManagerFacade.getDataSourceStatDataList());
}
if (url.equals("/activeConnectionStackTrace.json")) {
return returnJSONResult(RESULT_CODE_SUCCESS, statManagerFacade.getActiveConnStackTraceList());
}
if (url.startsWith("/datasource-")) {
Integer id = StringUtils.subStringToInteger(url, "datasource-", ".");
Object result = statManagerFacade.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 = statManagerFacade.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("/wall.json")) {
return returnJSONResult(RESULT_CODE_SUCCESS, getWallStatMap(parameters));
}
if (url.startsWith("/wall-") && url.indexOf(".json") > 0) {
Integer dataSourceId = StringUtils.subStringToInteger(url, "wall-", ".json");
Object result = statManagerFacade.getWallStatMap(dataSourceId);
return returnJSONResult(result == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, result);
}
if (url.startsWith("/sql-") && url.indexOf(".json") > 0) {
Integer id = StringUtils.subStringToInteger(url, "sql-", ".json");
return getSqlStat(id);
}
if (url.startsWith("/weburi.json")) {
return returnJSONResult(RESULT_CODE_SUCCESS, getWebURIStatDataList(parameters));
}
if (url.startsWith("/weburi-") && url.indexOf(".json") > 0) {
String uri = StringUtils.subString(url, "weburi-", ".json", true);
return returnJSONResult(RESULT_CODE_SUCCESS, getWebURIStatData(uri));
}
if (url.startsWith("/webapp.json")) {
return returnJSONResult(RESULT_CODE_SUCCESS, getWebAppStatDataList(parameters));
}
if (url.startsWith("/websession.json")) {
return returnJSONResult(RESULT_CODE_SUCCESS, getWebSessionStatDataList(parameters));
}
if (url.startsWith("/websession-") && url.indexOf(".json") > 0) {
String id = StringUtils.subString(url, "websession-", ".json");
return returnJSONResult(RESULT_CODE_SUCCESS, getWebSessionStatData(id));
}
if (url.startsWith("/spring.json")) {
return returnJSONResult(RESULT_CODE_SUCCESS, getSpringStatDataList(parameters));
}
if (url.startsWith("/spring-detail.json")) {
String clazz = parameters.get("class");
String method = parameters.get("method");
return returnJSONResult(RESULT_CODE_SUCCESS, getSpringMethodStatData(clazz, method));
}
return returnJSONResult(RESULT_CODE_ERROR, "Do not support this request, please contact with administrator.");
}
private List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy