jdk/src/share/classes/sun/tools/jconsole/inspector/XMBean.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 697 fb86c99b92a2
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 697
diff changeset
     2
 * Copyright 2004-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole.inspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.Icon;
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    31
import sun.tools.jconsole.JConsole;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.tools.jconsole.MBeansTab;
697
fb86c99b92a2 6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents: 55
diff changeset
    33
import sun.tools.jconsole.ProxyClient.SnapshotMBeanServerConnection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    35
public class XMBean {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    36
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    37
    private final MBeansTab mbeansTab;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    38
    private final ObjectName objectName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private Icon icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private String text;
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    41
    private Boolean broadcaster;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    42
    private final Object broadcasterLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private MBeanInfo mbeanInfo;
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    44
    private final Object mbeanInfoLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    46
    public XMBean(ObjectName objectName, MBeansTab mbeansTab) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.mbeansTab = mbeansTab;
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    48
        this.objectName = objectName;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    49
        text = objectName.getKeyProperty("name");
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    50
        if (text == null) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    51
            text = objectName.getDomain();
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    52
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (MBeanServerDelegate.DELEGATE_NAME.equals(objectName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            icon = IconManager.MBEANSERVERDELEGATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            icon = IconManager.MBEAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    MBeanServerConnection getMBeanServerConnection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return mbeansTab.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
697
fb86c99b92a2 6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents: 55
diff changeset
    64
    SnapshotMBeanServerConnection getSnapshotMBeanServerConnection() {
fb86c99b92a2 6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents: 55
diff changeset
    65
        return mbeansTab.getSnapshotMBeanServerConnection();
fb86c99b92a2 6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents: 55
diff changeset
    66
    }
fb86c99b92a2 6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents: 55
diff changeset
    67
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    68
    public Boolean isBroadcaster() {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    69
        synchronized (broadcasterLock) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    70
            if (broadcaster == null) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    71
                try {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    72
                    broadcaster = getMBeanServerConnection().isInstanceOf(
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    73
                            getObjectName(),
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    74
                            "javax.management.NotificationBroadcaster");
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    75
                } catch (Exception e) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    76
                    if (JConsole.isDebug()) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    77
                        System.err.println("Couldn't check if MBean [" +
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    78
                                objectName + "] is a notification broadcaster");
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    79
                        e.printStackTrace();
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    80
                    }
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    81
                    return false;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    82
                }
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    83
            }
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    84
            return broadcaster;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public Object invoke(String operationName) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Object result = getMBeanServerConnection().invoke(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                getObjectName(), operationName, new Object[0], new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public Object invoke(String operationName, Object params[], String sig[])
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    95
            throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Object result = getMBeanServerConnection().invoke(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                getObjectName(), operationName, params, sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public void setAttribute(Attribute attribute)
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   102
            throws AttributeNotFoundException, InstanceNotFoundException,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            InvalidAttributeValueException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            ReflectionException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        getMBeanServerConnection().setAttribute(getObjectName(), attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public Object getAttribute(String attributeName)
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   109
            throws AttributeNotFoundException, InstanceNotFoundException,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            MBeanException, ReflectionException, IOException {
697
fb86c99b92a2 6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents: 55
diff changeset
   111
        return getSnapshotMBeanServerConnection().getAttribute(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                getObjectName(), attributeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public AttributeList getAttributes(String attributeNames[])
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   116
            throws AttributeNotFoundException, InstanceNotFoundException,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            MBeanException, ReflectionException, IOException {
697
fb86c99b92a2 6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents: 55
diff changeset
   118
        return getSnapshotMBeanServerConnection().getAttributes(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                getObjectName(), attributeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public AttributeList getAttributes(MBeanAttributeInfo attributeNames[])
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   123
            throws AttributeNotFoundException, InstanceNotFoundException,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            MBeanException, ReflectionException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String attributeString[] = new String[attributeNames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (int i = 0; i < attributeNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            attributeString[i] = attributeNames[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return getAttributes(attributeString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public ObjectName getObjectName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return objectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   136
    public MBeanInfo getMBeanInfo() throws InstanceNotFoundException,
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   137
            IntrospectionException, ReflectionException, IOException {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   138
        synchronized (mbeanInfoLock) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   139
            if (mbeanInfo == null) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   140
                mbeanInfo = getMBeanServerConnection().getMBeanInfo(objectName);
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   141
            }
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   142
            return mbeanInfo;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   143
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   146
    @Override
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   147
    public boolean equals(Object obj) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   148
        if (obj == null) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   149
            return false;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   150
        }
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   151
        if (obj == this) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   152
            return true;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   153
        }
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   154
        if (!(obj instanceof XMBean)) {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   155
            return false;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   156
        }
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   157
        XMBean that = (XMBean) obj;
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   158
        return getObjectName().equals(that.getObjectName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   161
    @Override
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   162
    public int hashCode() {
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   163
        return (objectName == null ? 0 : objectName.hashCode());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void setText(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public Icon getIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void setIcon(Icon icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        this.icon = icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
   182
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}