mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
删除用处不大的jconsole plugin
This commit is contained in:
@@ -1,89 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
|
||||
import javax.management.MBeanServerConnection;
|
||||
import javax.management.ObjectInstance;
|
||||
import javax.management.openmbean.CompositeData;
|
||||
import javax.management.openmbean.TabularData;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
|
||||
public class ConnectionsPanel extends JPanel {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(ConnectionsPanel.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private MBeanServerConnection connection;
|
||||
private ObjectInstance objectInstance;
|
||||
private DataSourceInfo dataSourceInfo;
|
||||
|
||||
private JTable table;
|
||||
|
||||
private String[] columns = new String[] { "id", "connectTime", "connectTimespan",
|
||||
"establishTime", "aliveTimespan", "lastSql", "lastError", "lastErrorTime", "connectStatckTrace",
|
||||
"lastStatementStackTrace", "dataSource" };
|
||||
|
||||
public ConnectionsPanel(MBeanServerConnection connection, ObjectInstance objectInstance,
|
||||
DataSourceInfo dataSourceInfo){
|
||||
super();
|
||||
this.connection = connection;
|
||||
this.objectInstance = objectInstance;
|
||||
this.dataSourceInfo = dataSourceInfo;
|
||||
|
||||
try {
|
||||
TabularData connectionTabularData = (TabularData) connection.getAttribute(objectInstance.getObjectName(),
|
||||
"ConnectionList");
|
||||
|
||||
Object[][] rows = new Object[connectionTabularData.size()][];
|
||||
int rowIndex = 0;
|
||||
for (Object item : connectionTabularData.values()) {
|
||||
CompositeData rowData = (CompositeData) item;
|
||||
Object[] row = new Object[columns.length];
|
||||
row[0] = rowData.get("id");
|
||||
row[1] = rowData.get("connectTime");
|
||||
row[2] = rowData.get("connectTimespan");
|
||||
row[3] = rowData.get("establishTime");
|
||||
row[4] = rowData.get("aliveTimespan");
|
||||
|
||||
row[5] = rowData.get("lastSql");
|
||||
row[6] = rowData.get("lastError");
|
||||
row[7] = rowData.get("lastErrorTime");
|
||||
row[8] = rowData.get("connectStatckTrace");
|
||||
row[9] = rowData.get("lastStatementStackTrace");
|
||||
|
||||
row[10] = rowData.get("dataSource");
|
||||
|
||||
rows[rowIndex++] = row;
|
||||
}
|
||||
|
||||
table = new JTable(rows, columns);
|
||||
|
||||
JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
|
||||
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
|
||||
this.setLayout(new BorderLayout());
|
||||
this.add(tableScrollPane, BorderLayout.CENTER);
|
||||
} catch (Exception e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public MBeanServerConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public ObjectInstance getObjectInstance() {
|
||||
return objectInstance;
|
||||
}
|
||||
|
||||
public DataSourceInfo getDataSourceInfo() {
|
||||
return dataSourceInfo;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.MBeanServerConnection;
|
||||
import javax.management.openmbean.CompositeData;
|
||||
|
||||
public class DataSourceInfo {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
private String url;
|
||||
private List<String> filters = new ArrayList<String>();
|
||||
private Date createdTime;
|
||||
|
||||
private String rawDriverClassName;
|
||||
private String rawUrl;
|
||||
private int rawDriverMajorVersion;
|
||||
private int rawDriverMinorVersion;
|
||||
private String properties;
|
||||
private MBeanServerConnection connection;
|
||||
|
||||
public DataSourceInfo(){
|
||||
|
||||
}
|
||||
|
||||
public DataSourceInfo(MBeanServerConnection conn, CompositeData data){
|
||||
this.connection = conn;
|
||||
this.id = ((Number) data.get("ID")).longValue();
|
||||
this.name = (String) data.get("Name");
|
||||
this.url = (String) data.get("URL");
|
||||
for (String item : (String[]) data.get("FilterClasses")) {
|
||||
filters.add(item);
|
||||
}
|
||||
this.createdTime = (Date) data.get("CreatedTime");
|
||||
|
||||
this.rawDriverClassName = (String) data.get("RawDriverClassName");
|
||||
this.rawUrl = (String) data.get("RawUrl");
|
||||
this.rawDriverMajorVersion = ((Number) data.get("RawDriverMajorVersion")).intValue();
|
||||
this.rawDriverMinorVersion = ((Number) data.get("RawDriverMinorVersion")).intValue();
|
||||
this.properties = (String) data.get("Properties");
|
||||
}
|
||||
|
||||
public MBeanServerConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void setConnection(MBeanServerConnection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public List<String> getFilters() {
|
||||
return filters;
|
||||
}
|
||||
|
||||
public void setFilters(List<String> filters) {
|
||||
this.filters = filters;
|
||||
}
|
||||
|
||||
public Date getCreatedTime() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public void setCreatedTime(Date createdTime) {
|
||||
this.createdTime = createdTime;
|
||||
}
|
||||
|
||||
public String getRawDriverClassName() {
|
||||
return rawDriverClassName;
|
||||
}
|
||||
|
||||
public void setRawDriverClassName(String rawDriverClassName) {
|
||||
this.rawDriverClassName = rawDriverClassName;
|
||||
}
|
||||
|
||||
public String getRawUrl() {
|
||||
return rawUrl;
|
||||
}
|
||||
|
||||
public void setRawUrl(String rawUrl) {
|
||||
this.rawUrl = rawUrl;
|
||||
}
|
||||
|
||||
public int getRawDriverMajorVersion() {
|
||||
return rawDriverMajorVersion;
|
||||
}
|
||||
|
||||
public void setRawDriverMajorVersion(int rawDriverMajorVersion) {
|
||||
this.rawDriverMajorVersion = rawDriverMajorVersion;
|
||||
}
|
||||
|
||||
public int getRawDriverMinorVersion() {
|
||||
return rawDriverMinorVersion;
|
||||
}
|
||||
|
||||
public void setRawDriverMinorVersion(int rawDriverMinorVersion) {
|
||||
this.rawDriverMinorVersion = rawDriverMinorVersion;
|
||||
}
|
||||
|
||||
public String getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void setProperties(String properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Panel;
|
||||
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
|
||||
public class DataSourcePanel extends Panel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private DataSourceInfo dataSourceInfo;
|
||||
|
||||
private JTable table;
|
||||
|
||||
private String[] columns = new String[] { "Name", "Value" };
|
||||
|
||||
public DataSourcePanel(DataSourceInfo dataSourceInfo){
|
||||
this.dataSourceInfo = dataSourceInfo;
|
||||
|
||||
Object[][] rows = new Object[][] { { "ID", dataSourceInfo.getId() }, { "Name", dataSourceInfo.getName() },
|
||||
{ "URL", dataSourceInfo.getUrl() }, { "Filters", dataSourceInfo.getFilters() },
|
||||
{ "CreatedTime", dataSourceInfo.getCreatedTime() },
|
||||
{ "RawDriverClassName", dataSourceInfo.getRawDriverClassName() },
|
||||
{ "RawUrl", dataSourceInfo.getRawUrl() },
|
||||
{ "RawDriverMajorVersion", dataSourceInfo.getRawDriverMajorVersion() },
|
||||
{ "RawDriverMinorVersion", dataSourceInfo.getRawDriverMinorVersion() },
|
||||
{ "Properties", dataSourceInfo.getProperties() }, };
|
||||
table = new JTable(rows, columns);
|
||||
|
||||
JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
|
||||
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
|
||||
this.setLayout(new BorderLayout());
|
||||
this.add(tableScrollPane, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
public DataSourceInfo getDataSourceInfo() {
|
||||
return dataSourceInfo;
|
||||
}
|
||||
|
||||
public void setDataSourceInfo(DataSourceInfo dataSourceInfo) {
|
||||
this.dataSourceInfo = dataSourceInfo;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,230 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.EventQueue;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.management.MBeanInfo;
|
||||
import javax.management.MBeanServerConnection;
|
||||
import javax.management.ObjectInstance;
|
||||
import javax.management.ObjectName;
|
||||
import javax.management.openmbean.CompositeData;
|
||||
import javax.management.openmbean.TabularData;
|
||||
import javax.management.remote.JMXConnector;
|
||||
import javax.management.remote.JMXConnectorFactory;
|
||||
import javax.management.remote.JMXServiceURL;
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JSplitPane;
|
||||
import javax.swing.JTree;
|
||||
import javax.swing.event.TreeSelectionEvent;
|
||||
import javax.swing.event.TreeSelectionListener;
|
||||
import javax.swing.tree.DefaultMutableTreeNode;
|
||||
import javax.swing.tree.TreeSelectionModel;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.stat.JdbcStatManager;
|
||||
import com.sun.tools.jconsole.JConsoleContext;
|
||||
|
||||
public class DruidPanel extends JPanel {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidPanel.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected JSplitPane mainSplit;
|
||||
protected JTree tree;
|
||||
protected JPanel sheet;
|
||||
protected DefaultMutableTreeNode rootNode;
|
||||
protected DefaultMutableTreeNode dataSourcesNode;
|
||||
|
||||
public DruidPanel(){
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
mainSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
|
||||
mainSplit.setDividerLocation(160);
|
||||
mainSplit.setBorder(BorderFactory.createEmptyBorder());
|
||||
|
||||
rootNode = new DefaultMutableTreeNode("root", true);
|
||||
dataSourcesNode = new DefaultMutableTreeNode(
|
||||
new NodeInfo(null, null, NodeType.DataSources, null, "DataSources"),
|
||||
true);
|
||||
rootNode.add(dataSourcesNode);
|
||||
|
||||
tree = new JTree(rootNode);
|
||||
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||||
tree.setRootVisible(false);
|
||||
|
||||
tree.addTreeSelectionListener(new TreeSelectionListener() {
|
||||
|
||||
@Override
|
||||
public void valueChanged(TreeSelectionEvent e) {
|
||||
treeSelect(e);
|
||||
}
|
||||
});
|
||||
|
||||
JScrollPane theScrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
|
||||
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
JPanel treePanel = new JPanel(new BorderLayout());
|
||||
treePanel.add(theScrollPane, BorderLayout.CENTER);
|
||||
mainSplit.setLeftComponent(treePanel);
|
||||
|
||||
sheet = new JPanel();
|
||||
mainSplit.setRightComponent(sheet);
|
||||
|
||||
add(mainSplit);
|
||||
|
||||
// init();
|
||||
}
|
||||
|
||||
private void treeSelect(TreeSelectionEvent e) {
|
||||
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
|
||||
|
||||
Object userObject = node.getUserObject();
|
||||
NodeInfo nodeInfo = (NodeInfo) userObject;
|
||||
|
||||
if (nodeInfo == null) {
|
||||
mainSplit.setRightComponent(sheet);
|
||||
return;
|
||||
}
|
||||
|
||||
if (nodeInfo.getType() == NodeType.DataSource) {
|
||||
DataSourceInfo dataSourceInfo = (DataSourceInfo) nodeInfo.getData();
|
||||
mainSplit.setRightComponent(new DataSourcePanel(dataSourceInfo));
|
||||
} else if (nodeInfo.getType() == NodeType.Connections) {
|
||||
DataSourceInfo dataSourceInfo = (DataSourceInfo) nodeInfo.getData();
|
||||
ConnectionsPanel connectionsPanel = new ConnectionsPanel(nodeInfo.getConnection(),
|
||||
nodeInfo.getObjectInstance(), dataSourceInfo);
|
||||
mainSplit.setRightComponent(connectionsPanel);
|
||||
} else if (nodeInfo.getType() == NodeType.SQL) {
|
||||
DataSourceInfo dataSourceInfo = (DataSourceInfo) nodeInfo.getData();
|
||||
SQLPanel connectionsPanel = new SQLPanel(nodeInfo.getConnection(), nodeInfo.getObjectInstance(),
|
||||
dataSourceInfo);
|
||||
mainSplit.setRightComponent(connectionsPanel);
|
||||
} else {
|
||||
mainSplit.setRightComponent(sheet);
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean(JdbcStatManager.getInstance(),
|
||||
new ObjectName(
|
||||
"com.alibaba.druid:type=JdbcStatManager"));
|
||||
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource,
|
||||
new ObjectName("com.alibaba.druid:type=DataSource"));
|
||||
|
||||
dataSource.setUrl("jdbc:mock:");
|
||||
dataSource.setFilters("stat,trace");
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT 1");
|
||||
while (rs.next()) {
|
||||
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
} catch (Exception ex) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected Object doInBackground(JConsoleContext context) throws Exception {
|
||||
doInBackground(context.getMBeanServerConnection());
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void doInBackground(MBeanServerConnection conn) throws Exception {
|
||||
// dataSourcesNode.removeAllChildren();
|
||||
|
||||
List<ObjectInstance> stats = new ArrayList<ObjectInstance>();
|
||||
|
||||
Set<ObjectInstance> instances = conn.queryMBeans(null, null);
|
||||
for (ObjectInstance instance : instances) {
|
||||
MBeanInfo info = conn.getMBeanInfo(instance.getObjectName());
|
||||
if ("com.alibaba.druid.stat.JdbcStatManager".equals(info.getClassName())) {
|
||||
stats.add(instance);
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (stats.size() == 0) {
|
||||
|
||||
}
|
||||
|
||||
for (ObjectInstance statInstance : stats) {
|
||||
TabularData tabularValue = (TabularData) conn.getAttribute(statInstance.getObjectName(), "DataSourceList");
|
||||
for (Object item : tabularValue.values()) {
|
||||
CompositeData rowData = (CompositeData) item;
|
||||
|
||||
DataSourceInfo dataSourceInfo = new DataSourceInfo(conn, rowData);
|
||||
|
||||
NodeInfo dataSourceNodeInfo = new NodeInfo(conn, statInstance, NodeType.DataSource, dataSourceInfo,
|
||||
dataSourceInfo.getName());
|
||||
DefaultMutableTreeNode dataSourceNode = new DefaultMutableTreeNode(dataSourceNodeInfo, true);
|
||||
|
||||
NodeInfo connectionsNodeInfo = new NodeInfo(conn, statInstance, NodeType.Connections, dataSourceInfo,
|
||||
"Connections");
|
||||
DefaultMutableTreeNode connections = new DefaultMutableTreeNode(connectionsNodeInfo, true);
|
||||
{
|
||||
}
|
||||
dataSourceNode.add(connections);
|
||||
|
||||
NodeInfo sqlNodeInfo = new NodeInfo(conn, statInstance, NodeType.SQL, dataSourceInfo, "SQL");
|
||||
DefaultMutableTreeNode sqlListNode = new DefaultMutableTreeNode(sqlNodeInfo, true);
|
||||
dataSourceNode.add(sqlListNode);
|
||||
|
||||
dataSourcesNode.add(dataSourceNode);
|
||||
}
|
||||
}
|
||||
|
||||
// TreePath path = new TreePath(dataSourcesNode.children());
|
||||
// tree.scrollPathToVisible(path);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
JFrame frame = new JFrame();
|
||||
|
||||
DruidPanel panel = new DruidPanel();
|
||||
|
||||
frame.getContentPane().add(panel);
|
||||
frame.pack();
|
||||
frame.setSize(1024, 768);
|
||||
|
||||
// final String urlPath = "/jndi/rmi://192.168.1.103:9005/jmxrmi";
|
||||
final String urlPath = "/jndi/rmi://10.20.138.25:9006/jmxrmi";
|
||||
JMXServiceURL jmxUrl = new JMXServiceURL("rmi", "", 0, urlPath);
|
||||
|
||||
JMXConnector connector = JMXConnectorFactory.connect(jmxUrl);
|
||||
panel.doInBackground(connector.getMBeanServerConnection());
|
||||
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingWorker;
|
||||
|
||||
import com.sun.tools.jconsole.JConsolePlugin;
|
||||
|
||||
public class DruidPlugin extends JConsolePlugin {
|
||||
|
||||
private final Map<String, JPanel> tabs = new HashMap<String, JPanel>();
|
||||
private DruidPanel panel;
|
||||
|
||||
public DruidPlugin(){
|
||||
panel = new DruidPanel();
|
||||
tabs.put("Druid", panel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, JPanel> getTabs() {
|
||||
return tabs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SwingWorker<?, ?> newSwingWorker() {
|
||||
SwingWorker<?, ?> worer = new SwingWorker<Object, Object>() {
|
||||
|
||||
@Override
|
||||
protected Object doInBackground() throws Exception {
|
||||
return DruidPlugin.this.doInBackground();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return worer;
|
||||
}
|
||||
|
||||
protected Object doInBackground() throws Exception {
|
||||
return panel.doInBackground(this.getContext());
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import javax.management.MBeanServerConnection;
|
||||
import javax.management.ObjectInstance;
|
||||
|
||||
public class NodeInfo {
|
||||
|
||||
private MBeanServerConnection connection;
|
||||
private ObjectInstance objectInstance;
|
||||
private NodeType type;
|
||||
private Object data;
|
||||
private String name;
|
||||
|
||||
public NodeInfo(MBeanServerConnection connection, ObjectInstance objectInstance, NodeType type, Object data,
|
||||
String name){
|
||||
this.connection = connection;
|
||||
this.objectInstance = objectInstance;
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ObjectInstance getObjectInstance() {
|
||||
return objectInstance;
|
||||
}
|
||||
|
||||
public MBeanServerConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public NodeType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
public enum NodeType {
|
||||
DataSources, DataSource, SQL, Connections
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.openmbean.CompositeData;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor;
|
||||
|
||||
public class SQLDetailDialog extends JDialog {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private JTextArea textArea;
|
||||
|
||||
public SQLDetailDialog(CompositeData rowData){
|
||||
textArea = new JTextArea();
|
||||
JScrollPane textAreaScrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
|
||||
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
this.getContentPane().add(textAreaScrollPane);
|
||||
|
||||
String sql = (String) rowData.get("SQL");
|
||||
|
||||
sql = format(sql);
|
||||
|
||||
textArea.setText(sql);
|
||||
}
|
||||
|
||||
public static String format(String sql) {
|
||||
if (sql == null || sql.length() == 0) {
|
||||
return sql;
|
||||
}
|
||||
|
||||
try {
|
||||
return mergeMySql(sql);
|
||||
} catch (Exception ex) {
|
||||
// skip
|
||||
}
|
||||
|
||||
try {
|
||||
return mergeOracle(sql);
|
||||
} catch (Exception ex) {
|
||||
// skip
|
||||
}
|
||||
|
||||
return sql; // 返回原来的SQL
|
||||
}
|
||||
|
||||
public static String mergeMySql(String sql) {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.isTrue(1 == statementList.size());
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
|
||||
statemen.accept(visitor);
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static String mergeOracle(String sql) {
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.isTrue(1 == statementList.size());
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
OracleOutputVisitor visitor = new OracleOutputVisitor(out);
|
||||
statemen.accept(visitor);
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class SQLDetailPanel extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Object[] row;
|
||||
|
||||
public SQLDetailPanel(Object[] row){
|
||||
this.row = row;
|
||||
}
|
||||
|
||||
public Object[] getRow() {
|
||||
return row;
|
||||
}
|
||||
}
|
||||
@@ -1,375 +0,0 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.MBeanServerConnection;
|
||||
import javax.management.ObjectInstance;
|
||||
import javax.management.openmbean.CompositeData;
|
||||
import javax.management.openmbean.TabularData;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
import javax.swing.table.DefaultTableCellRenderer;
|
||||
import javax.swing.table.DefaultTableColumnModel;
|
||||
import javax.swing.table.TableColumn;
|
||||
import javax.swing.table.TableRowSorter;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
|
||||
public class SQLPanel extends JPanel {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(SQLPanel.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private MBeanServerConnection connection;
|
||||
private ObjectInstance objectInstance;
|
||||
private DataSourceInfo dataSourceInfo;
|
||||
|
||||
private JTable table;
|
||||
private SQLTableModel tableModel;
|
||||
private SQLTableRowSorter sorter;
|
||||
|
||||
private String[] columnNames = { "ID", "File", "Name", "SQL", "ExecCount",
|
||||
|
||||
"TotalTime", "EffectedRows", "FetchRowCount", "Running",
|
||||
"ConcurrentMax",
|
||||
|
||||
"MaxTimespan", "LastTime", "LastError", "ErrorCount", };
|
||||
|
||||
public SQLPanel(MBeanServerConnection connection, ObjectInstance objectInstance, DataSourceInfo dataSourceInfo){
|
||||
super();
|
||||
this.connection = connection;
|
||||
this.objectInstance = objectInstance;
|
||||
this.dataSourceInfo = dataSourceInfo;
|
||||
|
||||
try {
|
||||
TabularData connectionTabularData = (TabularData) connection.getAttribute(objectInstance.getObjectName(),
|
||||
"SqlList");
|
||||
|
||||
List<Object[]> rowList = new ArrayList<Object[]>();
|
||||
|
||||
for (Object item : connectionTabularData.values()) {
|
||||
CompositeData rowData = (CompositeData) item;
|
||||
|
||||
String url = (String) rowData.get("DataSource");
|
||||
|
||||
if (!dataSourceInfo.getUrl().equals(url)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Object[] row = new Object[columnNames.length];
|
||||
int columnIndex = 0;
|
||||
row[columnIndex++] = rowData;
|
||||
row[columnIndex++] = rowData.get("File");
|
||||
row[columnIndex++] = rowData.get("Name");
|
||||
row[columnIndex++] = rowData.get("SQL");
|
||||
row[columnIndex++] = rowData.get("ExecuteCount");
|
||||
|
||||
row[columnIndex++] = rowData.get("TotalTime");
|
||||
row[columnIndex++] = rowData.get("EffectedRowCount");
|
||||
row[columnIndex++] = rowData.get("FetchRowCount");
|
||||
row[columnIndex++] = rowData.get("RunningCount");
|
||||
row[columnIndex++] = rowData.get("ConcurrentMax");
|
||||
|
||||
row[columnIndex++] = rowData.get("MaxTimespan");
|
||||
row[columnIndex++] = rowData.get("LastTime");
|
||||
row[columnIndex++] = rowData.get("LastError");
|
||||
row[columnIndex++] = rowData.get("ErrorCount");
|
||||
|
||||
rowList.add(row);
|
||||
}
|
||||
|
||||
Object[][] rows = new Object[rowList.size()][];
|
||||
rowList.toArray(rows);
|
||||
TableColumnModel columnModel = new TableColumnModel();
|
||||
|
||||
tableModel = new SQLTableModel(rows);
|
||||
sorter = new SQLTableRowSorter(tableModel);
|
||||
table = new JTable(tableModel, columnModel);
|
||||
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
|
||||
table.setRowSorter(sorter);
|
||||
table.addMouseListener(new MouseAdapter() {
|
||||
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
tableMouseClicked(e);
|
||||
}
|
||||
});
|
||||
|
||||
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
|
||||
JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
|
||||
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
|
||||
this.setLayout(new BorderLayout());
|
||||
this.add(tableScrollPane, BorderLayout.CENTER);
|
||||
} catch (Exception e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
try {
|
||||
TabularData connectionTabularData = (TabularData) connection.getAttribute(objectInstance.getObjectName(),
|
||||
"SqlList");
|
||||
|
||||
List<Object[]> rowList = new ArrayList<Object[]>();
|
||||
|
||||
for (Object item : connectionTabularData.values()) {
|
||||
CompositeData rowData = (CompositeData) item;
|
||||
|
||||
String url = (String) rowData.get("DataSource");
|
||||
|
||||
if (!dataSourceInfo.getUrl().equals(url)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Object[] row = new Object[columnNames.length];
|
||||
int columnIndex = 0;
|
||||
row[columnIndex++] = rowData;
|
||||
row[columnIndex++] = rowData.get("File");
|
||||
row[columnIndex++] = rowData.get("Name");
|
||||
row[columnIndex++] = rowData.get("SQL");
|
||||
row[columnIndex++] = rowData.get("ExecuteCount");
|
||||
|
||||
row[columnIndex++] = rowData.get("ErrorCount");
|
||||
row[columnIndex++] = rowData.get("TotalTime");
|
||||
row[columnIndex++] = rowData.get("LastTime");
|
||||
row[columnIndex++] = rowData.get("MaxTimespan");
|
||||
row[columnIndex++] = rowData.get("LastError");
|
||||
|
||||
row[columnIndex++] = rowData.get("EffectedRowCount");
|
||||
row[columnIndex++] = rowData.get("FetchRowCount");
|
||||
row[columnIndex++] = rowData.get("ConcurrentMax");
|
||||
row[columnIndex++] = rowData.get("RunningCount");
|
||||
|
||||
rowList.add(row);
|
||||
}
|
||||
|
||||
Object[][] rows = new Object[rowList.size()][];
|
||||
rowList.toArray(rows);
|
||||
|
||||
int rowCount = tableModel.getRowCount();
|
||||
tableModel.setRowData(rows);
|
||||
tableModel.fireTableRowsDeleted(0, rowCount - 1);
|
||||
|
||||
tableModel.setRowData(rows);
|
||||
tableModel.fireTableRowsInserted(0, rows.length);
|
||||
} catch (Exception e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void tableMouseClicked(MouseEvent e) {
|
||||
if (e.getClickCount() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
int rowIndex = table.getSelectedRow();
|
||||
if (rowIndex < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
CompositeData rowData = (CompositeData) table.getValueAt(rowIndex, 0);
|
||||
|
||||
SQLDetailDialog dialog = new SQLDetailDialog(rowData);
|
||||
|
||||
Toolkit kit = Toolkit.getDefaultToolkit();
|
||||
Dimension screenSize = kit.getScreenSize();
|
||||
int width = (int) screenSize.getWidth();
|
||||
int height = (int) screenSize.getHeight();
|
||||
dialog.setSize(800, 600);
|
||||
int w = dialog.getWidth();
|
||||
int h = dialog.getHeight();
|
||||
dialog.setLocation((width - w) / 2, (height - h) / 2);
|
||||
dialog.setVisible(true);
|
||||
}
|
||||
|
||||
public MBeanServerConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public ObjectInstance getObjectInstance() {
|
||||
return objectInstance;
|
||||
}
|
||||
|
||||
public DataSourceInfo getDataSourceInfo() {
|
||||
return dataSourceInfo;
|
||||
}
|
||||
|
||||
class SQLTableModel extends AbstractTableModel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Object[][] rowData;
|
||||
|
||||
public SQLTableModel(Object[][] rows){
|
||||
this.rowData = rows;
|
||||
}
|
||||
|
||||
public String getColumnName(int column) {
|
||||
return columnNames[column].toString();
|
||||
}
|
||||
|
||||
public int getRowCount() {
|
||||
return rowData.length;
|
||||
}
|
||||
|
||||
public int getColumnCount() {
|
||||
return columnNames.length;
|
||||
}
|
||||
|
||||
public Object getValueAt(int row, int col) {
|
||||
return rowData[row][col];
|
||||
}
|
||||
|
||||
public boolean isCellEditable(int row, int column) {
|
||||
if (column == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setRowData(Object[][] rowData) {
|
||||
this.rowData = rowData;
|
||||
}
|
||||
|
||||
public void setValueAt(Object value, int row, int col) {
|
||||
rowData[row][col] = value;
|
||||
fireTableCellUpdated(row, col);
|
||||
}
|
||||
}
|
||||
|
||||
class TableColumnModel extends DefaultTableColumnModel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public TableColumnModel(){
|
||||
for (int i = 0; i < columnNames.length; ++i) {
|
||||
TableColumn column = new TableColumn();
|
||||
column.setModelIndex(i);
|
||||
column.setHeaderValue(columnNames[i]);
|
||||
this.addColumn(column);
|
||||
}
|
||||
|
||||
{
|
||||
TableColumn column = getColumn(0);
|
||||
column.setMinWidth(50);
|
||||
column.setMaxWidth(50);
|
||||
column.setCellRenderer(new IDRender());
|
||||
}
|
||||
|
||||
{
|
||||
TableColumn column = getColumn(3);
|
||||
column.setPreferredWidth(400);
|
||||
column.setCellRenderer(new SQLRenderer());
|
||||
}
|
||||
|
||||
{
|
||||
TableColumn column = getColumn(4);
|
||||
column.setPreferredWidth(60);
|
||||
}
|
||||
|
||||
{
|
||||
TableColumn column = getColumn(11);
|
||||
column.setCellRenderer(new DateRenderer());
|
||||
column.setPreferredWidth(120);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class SQLRenderer extends DefaultTableCellRenderer {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SQLRenderer(){
|
||||
super();
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
String sql = (String) value;
|
||||
String formattedSql = SQLDetailDialog.format(sql);
|
||||
setText(sql);
|
||||
setToolTipText("<html><pre>" + formattedSql + "</pre></html>");
|
||||
}
|
||||
}
|
||||
|
||||
static class DateRenderer extends DefaultTableCellRenderer {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
DateFormat formatter;
|
||||
|
||||
public DateRenderer(){
|
||||
super();
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
if (formatter == null) {
|
||||
formatter = new SimpleDateFormat("MM-dd HH:mm:ss");
|
||||
}
|
||||
setText((value == null) ? "" : formatter.format(value));
|
||||
}
|
||||
}
|
||||
|
||||
static class IDRender extends DefaultTableCellRenderer {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public void setValue(Object value) {
|
||||
CompositeData rowData = (CompositeData) value;
|
||||
Object id = rowData.get("ID");
|
||||
setText((id == null) ? "" : id.toString());
|
||||
}
|
||||
}
|
||||
|
||||
static class SQLTableRowSorter extends TableRowSorter<SQLTableModel> {
|
||||
|
||||
public SQLTableRowSorter(SQLTableModel model){
|
||||
super(model);
|
||||
}
|
||||
|
||||
public Comparator<?> getComparator(int column) {
|
||||
switch (column) {
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
case 13:
|
||||
return new LongComparator();
|
||||
default:
|
||||
return super.getComparator(column);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static class LongComparator implements Comparator<Object> {
|
||||
|
||||
@Override
|
||||
public int compare(Object a, Object b) {
|
||||
long aValue = Long.parseLong(a.toString());
|
||||
long bValue = Long.parseLong(b.toString());
|
||||
|
||||
if (aValue == bValue) {
|
||||
return 0;
|
||||
}
|
||||
return aValue > bValue ? 1 : -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user