J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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.MBeansTab; |
| 32 | |
| 33 | public class XMBean extends Object { |
| 34 | private ObjectName objectName; |
| 35 | private Icon icon; |
| 36 | private String text; |
| 37 | private boolean broadcaster; |
| 38 | private MBeanInfo mbeanInfo; |
| 39 | private MBeansTab mbeansTab; |
| 40 | |
| 41 | public XMBean(ObjectName objectName, MBeansTab mbeansTab) |
| 42 | throws InstanceNotFoundException, IntrospectionException, |
| 43 | ReflectionException, IOException { |
| 44 | this.mbeansTab = mbeansTab; |
| 45 | setObjectName(objectName); |
| 46 | if (MBeanServerDelegate.DELEGATE_NAME.equals(objectName)) { |
| 47 | icon = IconManager.MBEANSERVERDELEGATE; |
| 48 | } else { |
| 49 | icon = IconManager.MBEAN; |
| 50 | } |
| 51 | this.broadcaster = isBroadcaster(objectName); |
| 52 | this.mbeanInfo = getMBeanInfo(objectName); |
| 53 | } |
| 54 | |
| 55 | MBeanServerConnection getMBeanServerConnection() { |
| 56 | return mbeansTab.getMBeanServerConnection(); |
| 57 | } |
| 58 | |
| 59 | public boolean isBroadcaster() { |
| 60 | return broadcaster; |
| 61 | } |
| 62 | |
| 63 | private boolean isBroadcaster(ObjectName name) { |
| 64 | try { |
| 65 | return getMBeanServerConnection().isInstanceOf( |
| 66 | name, "javax.management.NotificationBroadcaster"); |
| 67 | } catch (Exception e) { |
| 68 | System.out.println("Error calling isBroadcaster: " + |
| 69 | e.getMessage()); |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | public Object invoke(String operationName) throws Exception { |
| 75 | Object result = getMBeanServerConnection().invoke( |
| 76 | getObjectName(), operationName, new Object[0], new String[0]); |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | public Object invoke(String operationName, Object params[], String sig[]) |
| 81 | throws Exception { |
| 82 | Object result = getMBeanServerConnection().invoke( |
| 83 | getObjectName(), operationName, params, sig); |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | public void setAttribute(Attribute attribute) |
| 88 | throws AttributeNotFoundException, InstanceNotFoundException, |
| 89 | InvalidAttributeValueException, MBeanException, |
| 90 | ReflectionException, IOException { |
| 91 | getMBeanServerConnection().setAttribute(getObjectName(), attribute); |
| 92 | } |
| 93 | |
| 94 | public Object getAttribute(String attributeName) |
| 95 | throws AttributeNotFoundException, InstanceNotFoundException, |
| 96 | MBeanException, ReflectionException, IOException { |
| 97 | return getMBeanServerConnection().getAttribute( |
| 98 | getObjectName(), attributeName); |
| 99 | } |
| 100 | |
| 101 | public AttributeList getAttributes(String attributeNames[]) |
| 102 | throws AttributeNotFoundException, InstanceNotFoundException, |
| 103 | MBeanException, ReflectionException, IOException { |
| 104 | return getMBeanServerConnection().getAttributes( |
| 105 | getObjectName(), attributeNames); |
| 106 | } |
| 107 | |
| 108 | public AttributeList getAttributes(MBeanAttributeInfo attributeNames[]) |
| 109 | throws AttributeNotFoundException, InstanceNotFoundException, |
| 110 | MBeanException, ReflectionException, IOException { |
| 111 | String attributeString[] = new String[attributeNames.length]; |
| 112 | for (int i = 0; i < attributeNames.length; i++) { |
| 113 | attributeString[i] = attributeNames[i].getName(); |
| 114 | } |
| 115 | return getAttributes(attributeString); |
| 116 | } |
| 117 | |
| 118 | public ObjectName getObjectName() { |
| 119 | return objectName; |
| 120 | } |
| 121 | |
| 122 | private void setObjectName(ObjectName objectName) { |
| 123 | this.objectName = objectName; |
| 124 | // generate a readable name now |
| 125 | String name = getObjectName().getKeyProperty("name"); |
| 126 | if (name == null) |
| 127 | setText(getObjectName().getDomain()); |
| 128 | else |
| 129 | setText(name); |
| 130 | } |
| 131 | |
| 132 | public MBeanInfo getMBeanInfo() { |
| 133 | return mbeanInfo; |
| 134 | } |
| 135 | |
| 136 | private MBeanInfo getMBeanInfo(ObjectName name) |
| 137 | throws InstanceNotFoundException, IntrospectionException, |
| 138 | ReflectionException, IOException { |
| 139 | return getMBeanServerConnection().getMBeanInfo(name); |
| 140 | } |
| 141 | |
| 142 | public boolean equals(Object o) { |
| 143 | if (o instanceof XMBean) { |
| 144 | XMBean mbean = (XMBean) o; |
| 145 | return getObjectName().equals((mbean).getObjectName()); |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | public String getText() { |
| 151 | return text; |
| 152 | } |
| 153 | |
| 154 | public void setText(String text) { |
| 155 | this.text = text; |
| 156 | } |
| 157 | |
| 158 | public Icon getIcon() { |
| 159 | return icon; |
| 160 | } |
| 161 | |
| 162 | public void setIcon(Icon icon) { |
| 163 | this.icon = icon; |
| 164 | } |
| 165 | |
| 166 | public String toString() { |
| 167 | return getText(); |
| 168 | } |
| 169 | } |