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