01: /*
02: * $Id: MBeanPanelModel.java 6115 2006-01-30 04:50:47Z dfs $
03: *
04: * Copyright 2004-2006 Daniel F. Savarese
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.savarese.org/software/ApacheLicense-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package org.savarese.unicorn.ui;
20:
21: import java.io.IOException;
22: import javax.management.*;
23:
24: /**
25: * An {@link ObjectPanelModel} implementation for JMX MBeans.
26: */
27: public class MBeanPanelModel extends
28: AbstractObjectPanelModel<ObjectName> {
29:
30: private MBeanInfo __info;
31: private MBeanServerConnection __connection;
32:
33: /**
34: * Instantiates an MBeanPanelModel that wraps the provided MBean,
35: * accessed via the provided MBeanServerConnection.
36: *
37: * @param mbean The ObjectName of the MBean to wrap.
38: * @param conn The MBeanServerConnection via which the MBean can be accessed.
39: */
40: public MBeanPanelModel(ObjectName mbean, MBeanServerConnection conn) {
41: setConnection(conn);
42: setObject(mbean);
43: }
44:
45: public void setAttribute(String attribute, Object value)
46: throws JMException, IOException {
47: __connection.setAttribute(getObject(), new Attribute(attribute,
48: value));
49: }
50:
51: public Object getAttribute(String attribute) throws JMException,
52: IOException {
53: return __connection.getAttribute(getObject(), attribute);
54: }
55:
56: /**
57: * Sets the MBeanServerConnection via which the MBean can be accessed.
58: */
59: public void setConnection(MBeanServerConnection conn) {
60: __connection = conn;
61: }
62:
63: public void setObject(ObjectName mbean) {
64: if (mbean != null && __connection != null) {
65: try {
66: __info = __connection.getMBeanInfo(mbean);
67: } catch (Exception e) {
68: __info = null;
69: }
70: } else
71: __info = null;
72:
73: super .setObject(mbean);
74: }
75:
76: public Object invoke(String operation, Object[] params,
77: Class[] types, String[] signature) throws JMException,
78: IOException {
79: return __connection.invoke(getObject(), operation, params,
80: signature);
81: }
82:
83: public MBeanInfo getObjectInfo() {
84: return __info;
85: }
86:
87: public boolean isValid() {
88: return (__info != null);
89: }
90: }
|