package sun.tools.jconsole.inspector;
import java.awt.EventQueue;
import java.util.*;
import javax.management.*;
import javax.swing.*;
import javax.swing.tree.*;
import sun.tools.jconsole.JConsole;
import sun.tools.jconsole.MBeansTab;
import sun.tools.jconsole.Messages;
import sun.tools.jconsole.inspector.XNodeInfo;
import sun.tools.jconsole.inspector.XNodeInfo.Type;
@SuppressWarnings("serial")
public class XTree extends JTree {
private static final List<String> orderedKeyPropertyList =
new ArrayList<String>();
static {
String keyPropertyList =
System.getProperty("com.sun.tools.jconsole.mbeans.keyPropertyList");
if (keyPropertyList == null) {
orderedKeyPropertyList.add("type");
orderedKeyPropertyList.add("j2eeType");
} else {
StringTokenizer st = new StringTokenizer(keyPropertyList, ",");
while (st.hasMoreTokens()) {
orderedKeyPropertyList.add(st.nextToken());
}
}
}
private MBeansTab mbeansTab;
private Map<String, DefaultMutableTreeNode> nodes =
new HashMap<String, DefaultMutableTreeNode>();
public XTree(MBeansTab mbeansTab) {
this(new DefaultMutableTreeNode("MBeanTreeRootNode"), mbeansTab);
}
public XTree(TreeNode root, MBeansTab mbeansTab) {
super(root);
this.mbeansTab = mbeansTab;
setRootVisible(false);
setShowsRootHandles(true);
ToolTipManager.sharedInstance().registerComponent(this);
}
private synchronized void removeChildNode(DefaultMutableTreeNode child) {
DefaultTreeModel model = (DefaultTreeModel) getModel();
model.removeNodeFromParent(child);
}
private synchronized void addChildNode(
DefaultMutableTreeNode parent,
DefaultMutableTreeNode child,
int index) {
DefaultTreeModel model = (DefaultTreeModel) getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
boolean rootLeaf = root.isLeaf();
model.insertNodeInto(child, parent, index);
if (rootLeaf) {
model.nodeStructureChanged(root);
}
}
private synchronized void addChildNode(
DefaultMutableTreeNode parent, DefaultMutableTreeNode child) {
int childCount = parent.getChildCount();
if (childCount == 0) {
addChildNode(parent, child, 0);
} else if (child instanceof ComparableDefaultMutableTreeNode) {
ComparableDefaultMutableTreeNode comparableChild =
(ComparableDefaultMutableTreeNode)child;
int i = 0;
for (; i < childCount; i++) {
DefaultMutableTreeNode brother =
(DefaultMutableTreeNode) parent.getChildAt(i);
if (comparableChild.compareTo(brother) < 0) {
addChildNode(parent, child, i);
break;
}
else if (comparableChild.compareTo(brother) == 0) {
addChildNode(parent, child, i);
break;
}
}
if (i == childCount) {
addChildNode(parent, child, childCount);
}
} else {
addChildNode(parent, child, childCount);
}
}
public synchronized void removeAll() {
DefaultTreeModel model = (DefaultTreeModel) getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
root.removeAllChildren();
model.nodeStructureChanged(root);
nodes.clear();
}
public void delMBeanFromView(final ObjectName mbean) {
EventQueue.invokeLater(new Runnable() {
public void run() {
synchronized (XTree.this) {
DefaultMutableTreeNode node = null;
Dn dn = buildDn(mbean);
if (dn.size() > 0) {
DefaultTreeModel model = (DefaultTreeModel) getModel();
Token token = dn.getToken(0);
String hashKey = dn.getHashKey(token);
node = nodes.get(hashKey);
if ((node != null) && (!node.isRoot())) {
if (hasMBeanChildren(node)) {
removeNonMBeanChildren(node);
String label = token.getValue().toString();
XNodeInfo userObject = new XNodeInfo(
Type.NONMBEAN, label,
label, token.toString());
changeNodeValue(node, userObject);
} else {
DefaultMutableTreeNode parent =
(DefaultMutableTreeNode) node.getParent();
model.removeNodeFromParent(node);
nodes.remove(hashKey);
delParentFromView(dn, 1, parent);
}
}
}
}
}
});
}
private boolean hasMBeanChildren(DefaultMutableTreeNode node) {
for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode) e.nextElement();
if (((XNodeInfo) n.getUserObject()).getType().equals(Type.MBEAN)) {
return true;
}
}
return false;
}
private void removeNonMBeanChildren(DefaultMutableTreeNode node) {
Set<DefaultMutableTreeNode> metadataNodes =
new HashSet<DefaultMutableTreeNode>();
DefaultTreeModel model = (DefaultTreeModel) getModel();
for (Enumeration e = node.children(); e.hasMoreElements(); ) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode) e.nextElement();
if (!((XNodeInfo) n.getUserObject()).getType().equals(Type.MBEAN)) {
metadataNodes.add(n);
}
}
for (DefaultMutableTreeNode n : metadataNodes) {
model.removeNodeFromParent(n);
}
}
private DefaultMutableTreeNode delParentFromView(
Dn dn, int index, DefaultMutableTreeNode node) {
if ((!node.isRoot()) && node.isLeaf() &&
(!(((XNodeInfo) node.getUserObject()).getType().equals(Type.MBEAN)))) {
DefaultMutableTreeNode parent =
(DefaultMutableTreeNode) node.getParent();
removeChildNode(node);
String hashKey = dn.getHashKey(dn.getToken(index));
nodes.remove(hashKey);
delParentFromView(dn, index + 1, parent);
}
return node;
}
public synchronized void addMBeanToView(final ObjectName mbean) {
final XMBean xmbean;
try {
xmbean = new XMBean(mbean, mbeansTab);
if (xmbean == null) {
return;
}
} catch (Exception e) {
if (JConsole.isDebug()) {
e.printStackTrace();
}
return;
}
EventQueue.invokeLater(new Runnable() {
public void run() {
synchronized (XTree.this) {
Dn dn = buildDn(mbean);
if (dn.size() == 0) return;
Token token = dn.getToken(0);
DefaultMutableTreeNode node = null;
boolean nodeCreated = true;
String hashKey = dn.getHashKey(token);
if (nodes.containsKey(hashKey)) {
node = nodes.get(hashKey);
final Object data = createNodeValue(xmbean, token);
final String label = data.toString();
final XNodeInfo userObject =
new XNodeInfo(Type.MBEAN, data, label, mbean.toString());
changeNodeValue(node, userObject);
nodeCreated = false;
} else {
node = createDnNode(dn, token, xmbean);
if (node != null) {
nodes.put(hashKey, node);
nodeCreated = true;
} else {
return;
}
}
for (int i = 1; i < dn.size(); i++) {
DefaultMutableTreeNode currentNode = null;
token = dn.getToken(i);
hashKey = dn.getHashKey(token);
if (nodes.containsKey(hashKey)) {
if (nodeCreated) {
currentNode = nodes.get(hashKey);
addChildNode(currentNode, node);
return;
} else {
return;
}
} else {
if (token.getKeyDn().equals("domain")) {
currentNode = createDomainNode(dn, token);
if (currentNode != null) {
final DefaultMutableTreeNode root =
(DefaultMutableTreeNode) getModel().getRoot();
addChildNode(root, currentNode);
}
} else {
currentNode = createSubDnNode(dn, token);
if (currentNode == null) {
continue;
}
}
nodes.put(hashKey, currentNode);
addChildNode(currentNode, node);
nodeCreated = true;
}
node = currentNode;
}
}
}
});
}
private synchronized void changeNodeValue(
final DefaultMutableTreeNode node, XNodeInfo nodeValue) {
if (node instanceof ComparableDefaultMutableTreeNode) {
DefaultMutableTreeNode clone =
(DefaultMutableTreeNode) node.clone();
clone.setUserObject(nodeValue);
if (((ComparableDefaultMutableTreeNode) node).compareTo(clone) == 0) {
node.setUserObject(nodeValue);
DefaultTreeModel model = (DefaultTreeModel) getModel();
model.nodeChanged(node);
} else {
DefaultMutableTreeNode parent =
(DefaultMutableTreeNode) node.getParent();
removeChildNode(node);
node.setUserObject(nodeValue);
addChildNode(parent, node);
}
} else {
node.setUserObject(nodeValue);
DefaultTreeModel model = (DefaultTreeModel) getModel();
model.nodeChanged(node);
}
if (nodeValue.getType().equals(Type.MBEAN)) {
XMBeanInfo.loadInfo(node);
DefaultTreeModel model = (DefaultTreeModel) getModel();
model.nodeStructureChanged(node);
}
if (node == getLastSelectedPathComponent()) {
TreePath selectionPath = getSelectionPath();
clearSelection();
setSelectionPath(selectionPath);
}
}
private DefaultMutableTreeNode createDomainNode(Dn dn, Token token) {
DefaultMutableTreeNode node = new ComparableDefaultMutableTreeNode();
String label = dn.getDomain();
XNodeInfo userObject =
new XNodeInfo(Type.NONMBEAN, label, label, label);
node.setUserObject(userObject);
return node;
}
private DefaultMutableTreeNode createDnNode(
Dn dn, Token token, XMBean xmbean) {
DefaultMutableTreeNode node = new ComparableDefaultMutableTreeNode();
Object data = createNodeValue(xmbean, token);
String label = data.toString();
XNodeInfo userObject = new XNodeInfo(Type.MBEAN, data, label,
xmbean.getObjectName().toString());
node.setUserObject(userObject);
XMBeanInfo.loadInfo(node);
return node;
}
private DefaultMutableTreeNode createSubDnNode(Dn dn, Token token) {
DefaultMutableTreeNode node = new ComparableDefaultMutableTreeNode();
String label = isKeyValueView() ? token.toString() :
token.getValue().toString();
XNodeInfo userObject =
new XNodeInfo(Type.NONMBEAN, label, label, token.toString());
node.setUserObject(userObject);
return node;
}
private Object createNodeValue(XMBean xmbean, Token token) {
String label = isKeyValueView() ? token.toString() :
token.getValue().toString();
xmbean.setText(label);
return xmbean;
}
private Map<String,String> (
String properties, ObjectName mbean) {
String props = properties;
Map<String,String> map = new LinkedHashMap<String,String>();
int eq = props.indexOf("=");
while (eq != -1) {
String key = props.substring(0, eq);
String value = mbean.getKeyProperty(key);
map.put(key, value);
props = props.substring(key.length() + 1 + value.length());
if (props.startsWith(",")) {
props = props.substring(1);
}
eq = props.indexOf("=");
}
return map;
}
private String getKeyPropertyListString(ObjectName mbean) {
String props = mbean.getKeyPropertyListString();
Map<String,String> map = extractKeyValuePairs(props, mbean);
StringBuilder sb = new StringBuilder();
for (String key : orderedKeyPropertyList) {
if (map.containsKey(key)) {
sb.append(key + "=" + map.get(key) + ",");
map.remove(key);
}
}
for (Map.Entry<String,String> entry : map.entrySet()) {
sb.append(entry.getKey() + "=" + entry.getValue() + ",");
}
String orderedKeyPropertyListString = sb.toString();
orderedKeyPropertyListString = orderedKeyPropertyListString.substring(
0, orderedKeyPropertyListString.length() - 1);
return orderedKeyPropertyListString;
}
private Dn buildDn(ObjectName mbean) {
String domain = mbean.getDomain();
String globalDn = getKeyPropertyListString(mbean);
Dn dn = buildDn(domain, globalDn, mbean);
dn.updateDn();
dn.reverseOrder();
dn.computeHashDn();
return dn;
}
private Dn buildDn(String domain, String globalDn, ObjectName mbean) {
Dn dn = new Dn(domain, globalDn);
String keyDn = "no_key";
if (isTreeView()) {
String props = globalDn;
Map<String,String> map = extractKeyValuePairs(props, mbean);
for (Map.Entry<String,String> entry : map.entrySet()) {
dn.addToken(new Token(keyDn,
entry.getKey() + "=" + entry.getValue()));
}
} else {
dn.addToken(new Token(keyDn, "properties=" + globalDn));
}
return dn;
}
public static class ComparableDefaultMutableTreeNode
extends DefaultMutableTreeNode
implements Comparable<DefaultMutableTreeNode> {
public int compareTo(DefaultMutableTreeNode node) {
return (this.toString().compareTo(node.toString()));
}
}
private boolean treeView;
private boolean treeViewInit = false;
public boolean isTreeView() {
if (!treeViewInit) {
treeView = getTreeViewValue();
treeViewInit = true;
}
return treeView;
}
private boolean getTreeViewValue() {
String treeView = System.getProperty("treeView");
return ((treeView == null) ? true : !(treeView.equals("false")));
}
private boolean keyValueView = Boolean.getBoolean("keyValueView");
public boolean isKeyValueView() {
return keyValueView;
}
public static class Dn {
private String domain;
private String dn;
private String hashDn;
private ArrayList<Token> tokens = new ArrayList<Token>();
public Dn(String domain, String dn) {
this.domain = domain;
this.dn = dn;
}
public void clearTokens() {
tokens.clear();
}
public void addToken(Token token) {
tokens.add(token);
}
public void addToken(int index, Token token) {
tokens.add(index, token);
}
public void setToken(int index, Token token) {
tokens.set(index, token);
}
public void removeToken(int index) {
tokens.remove(index);
}
public Token getToken(int index) {
return tokens.get(index);
}
public void reverseOrder() {
ArrayList<Token> newOrder = new ArrayList<Token>(tokens.size());
for (int i = tokens.size() - 1; i >= 0; i--) {
newOrder.add(tokens.get(i));
}
tokens = newOrder;
}
public int size() {
return tokens.size();
}
public String getDomain() {
return domain;
}
public String getDn() {
return dn;
}
public String getHashDn() {
return hashDn;
}
public String getHashKey(Token token) {
final int begin = getHashDn().indexOf(token.getHashToken());
return getHashDn().substring(begin, getHashDn().length());
}
public void computeHashDn() {
final StringBuilder hashDn = new StringBuilder();
final int tokensSize = tokens.size();
for (int i = 0; i < tokensSize; i++) {
Token token = tokens.get(i);
String hashToken = token.getHashToken();
if (hashToken == null) {
hashToken = token.getToken() + (tokensSize - i);
token.setHashToken(hashToken);
}
hashDn.append(hashToken);
hashDn.append(",");
}
if (tokensSize > 0) {
this.hashDn = hashDn.substring(0, hashDn.length() - 1);
} else {
this.hashDn = "";
}
}
public void updateDn() {
addToken(0, new Token("domain", "domain=" + getDomain()));
}
public String toString() {
return tokens.toString();
}
}
public static class Token {
private String keyDn;
private String token;
private String hashToken;
private String key;
private String value;
public Token(String keyDn, String token) {
this.keyDn = keyDn;
this.token = token;
buildKeyValue();
}
public Token(String keyDn, String token, String hashToken) {
this.keyDn = keyDn;
this.token = token;
this.hashToken = hashToken;
buildKeyValue();
}
public String getKeyDn() {
return keyDn;
}
public String getToken() {
return token;
}
public void setValue(String value) {
this.value = value;
this.token = key + "=" + value;
}
public void setKey(String key) {
this.key = key;
this.token = key + "=" + value;
}
public void setKeyDn(String keyDn) {
this.keyDn = keyDn;
}
public void setHashToken(String hashToken) {
this.hashToken = hashToken;
}
public String getHashToken() {
return hashToken;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public String toString(){
return getToken();
}
public boolean equals(Object object) {
if (object instanceof Token) {
return token.equals(((Token) object));
} else {
return false;
}
}
private void buildKeyValue() {
int index = token.indexOf("=");
if (index < 0) {
key = token;
value = token;
} else {
key = token.substring(0, index);
value = token.substring(index + 1, token.length());
}
}
}
}