001: /*
002: * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole.inspector;
027:
028: import java.io.IOException;
029: import javax.management.*;
030: import javax.swing.Icon;
031: import sun.tools.jconsole.MBeansTab;
032:
033: public class XMBean extends Object {
034: private ObjectName objectName;
035: private Icon icon;
036: private String text;
037: private boolean broadcaster;
038: private MBeanInfo mbeanInfo;
039: private MBeansTab mbeansTab;
040:
041: public XMBean(ObjectName objectName, MBeansTab mbeansTab)
042: throws InstanceNotFoundException, IntrospectionException,
043: ReflectionException, IOException {
044: this .mbeansTab = mbeansTab;
045: setObjectName(objectName);
046: if (MBeanServerDelegate.DELEGATE_NAME.equals(objectName)) {
047: icon = IconManager.MBEANSERVERDELEGATE;
048: } else {
049: icon = IconManager.MBEAN;
050: }
051: this .broadcaster = isBroadcaster(objectName);
052: this .mbeanInfo = getMBeanInfo(objectName);
053: }
054:
055: MBeanServerConnection getMBeanServerConnection() {
056: return mbeansTab.getMBeanServerConnection();
057: }
058:
059: public boolean isBroadcaster() {
060: return broadcaster;
061: }
062:
063: private boolean isBroadcaster(ObjectName name) {
064: try {
065: return getMBeanServerConnection().isInstanceOf(name,
066: "javax.management.NotificationBroadcaster");
067: } catch (Exception e) {
068: System.out.println("Error calling isBroadcaster: "
069: + e.getMessage());
070: }
071: return false;
072: }
073:
074: public Object invoke(String operationName) throws Exception {
075: Object result = getMBeanServerConnection().invoke(
076: getObjectName(), operationName, new Object[0],
077: new String[0]);
078: return result;
079: }
080:
081: public Object invoke(String operationName, Object params[],
082: String sig[]) throws Exception {
083: Object result = getMBeanServerConnection().invoke(
084: getObjectName(), operationName, params, sig);
085: return result;
086: }
087:
088: public void setAttribute(Attribute attribute)
089: throws AttributeNotFoundException,
090: InstanceNotFoundException, InvalidAttributeValueException,
091: MBeanException, ReflectionException, IOException {
092: getMBeanServerConnection().setAttribute(getObjectName(),
093: attribute);
094: }
095:
096: public Object getAttribute(String attributeName)
097: throws AttributeNotFoundException,
098: InstanceNotFoundException, MBeanException,
099: ReflectionException, IOException {
100: return getMBeanServerConnection().getAttribute(getObjectName(),
101: attributeName);
102: }
103:
104: public AttributeList getAttributes(String attributeNames[])
105: throws AttributeNotFoundException,
106: InstanceNotFoundException, MBeanException,
107: ReflectionException, IOException {
108: return getMBeanServerConnection().getAttributes(
109: getObjectName(), attributeNames);
110: }
111:
112: public AttributeList getAttributes(
113: MBeanAttributeInfo attributeNames[])
114: throws AttributeNotFoundException,
115: InstanceNotFoundException, MBeanException,
116: ReflectionException, IOException {
117: String attributeString[] = new String[attributeNames.length];
118: for (int i = 0; i < attributeNames.length; i++) {
119: attributeString[i] = attributeNames[i].getName();
120: }
121: return getAttributes(attributeString);
122: }
123:
124: public ObjectName getObjectName() {
125: return objectName;
126: }
127:
128: private void setObjectName(ObjectName objectName) {
129: this .objectName = objectName;
130: // generate a readable name now
131: String name = getObjectName().getKeyProperty("name");
132: if (name == null)
133: setText(getObjectName().getDomain());
134: else
135: setText(name);
136: }
137:
138: public MBeanInfo getMBeanInfo() {
139: return mbeanInfo;
140: }
141:
142: private MBeanInfo getMBeanInfo(ObjectName name)
143: throws InstanceNotFoundException, IntrospectionException,
144: ReflectionException, IOException {
145: return getMBeanServerConnection().getMBeanInfo(name);
146: }
147:
148: public boolean equals(Object o) {
149: if (o instanceof XMBean) {
150: XMBean mbean = (XMBean) o;
151: return getObjectName().equals((mbean).getObjectName());
152: }
153: return false;
154: }
155:
156: public String getText() {
157: return text;
158: }
159:
160: public void setText(String text) {
161: this .text = text;
162: }
163:
164: public Icon getIcon() {
165: return icon;
166: }
167:
168: public void setIcon(Icon icon) {
169: this .icon = icon;
170: }
171:
172: public String toString() {
173: return getText();
174: }
175: }
|