usr/src/java/vpanels/panel/org/opensolaris/os/vp/panel/common/MXBeanTracker.java
changeset 545 7a29a25b92e2
child 677 fbc09f84f958
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/java/vpanels/panel/org/opensolaris/os/vp/panel/common/MXBeanTracker.java	Wed Jul 21 20:26:39 2010 -0400
@@ -0,0 +1,121 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ */
+
+package org.opensolaris.os.vp.panel.common;
+
+import java.beans.*;
+import javax.management.*;
+import org.opensolaris.os.vp.util.misc.event.PropertyChangeListeners;
+
+/**
+ * The {@code MXBeanTracker} is a {@link ConnectionTracker} that automatically
+ * creates and re-creates an {@link #getBean MXBean} after changes in the {@link
+ * #setMBeanServerConnection MBeanServerConnection} or {@link #setObjectName
+ * ObjectName}.
+ * </p>
+ * A property change notification is sent out when the tracked {@code MXBean}
+ * changes.
+ */
+public class MXBeanTracker<T> extends ConnectionTracker {
+    //
+    // Static data
+    //
+
+    /**
+     * The name of the property that changes with {@link #setBean}.
+     */
+    public static final String PROPERTY_BEAN = "bean";
+
+    //
+    // Instance data
+    //
+
+    private Class<T> clazz;
+    private T bean;
+
+    private PropertyChangeListener pListener =
+	new PropertyChangeListener() {
+	    @Override
+	    public void propertyChange(PropertyChangeEvent event) {
+		setBean();
+	    }
+	};
+
+    //
+    // Constructors
+    //
+
+    public MXBeanTracker(Class<T> clazz) {
+	init(clazz);
+    }
+
+    public MXBeanTracker(Class<T> clazz, ClientContext context,
+	ObjectName oName) {
+	super(context, oName);
+	init(clazz);
+    }
+
+    //
+    // MXBeanTracker methods
+    //
+
+    public T getBean() {
+	return bean;
+    }
+
+    protected void setBean(T bean) {
+	if (this.bean != bean) {
+	    PropertyChangeEvent e = new PropertyChangeEvent(
+		this, PROPERTY_BEAN, this.bean, bean);
+	    this.bean = bean;
+	    getPropertyChangeListeners().propertyChange(e);
+	}
+    }
+
+    //
+    // Private methods
+    //
+
+    private void init(Class<T> clazz) {
+	this.clazz = clazz;
+	addPropertyChangeListener(PROPERTY_MBSC, pListener);
+	addPropertyChangeListener(PROPERTY_OBJECTNAME, pListener);
+	setBean();
+    }
+
+    private void setBean() {
+	T bean = null;
+
+	MBeanServerConnection mbsc = getMBeanServerConnection();
+	if (mbsc != null) {
+	    ObjectName oName = getObjectName();
+	    if (oName != null) {
+		bean = JMX.newMXBeanProxy(mbsc, oName, clazz);
+	    }
+	}
+
+	setBean(bean);
+    }
+}