only SigarRegistry needs to implement MBeanRegistration

This commit is contained in:
Doug MacEachern 2009-04-29 15:16:17 -07:00
parent 25241fcc7a
commit 448b595e30
2 changed files with 12 additions and 71 deletions

View File

@ -24,8 +24,6 @@ import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean; import javax.management.DynamicMBean;
import javax.management.MBeanException; import javax.management.MBeanException;
import javax.management.MBeanRegistration; import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
@ -43,7 +41,7 @@ import org.hyperic.sigar.SigarProxy;
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.5 * @since 1.5
*/ */
public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration { public abstract class AbstractMBean implements DynamicMBean {
public static final String MBEAN_DOMAIN = SigarInvokerJMX.DOMAIN_NAME; public static final String MBEAN_DOMAIN = SigarInvokerJMX.DOMAIN_NAME;
public static final String MBEAN_ATTR_TYPE = SigarInvokerJMX.PROP_TYPE; public static final String MBEAN_ATTR_TYPE = SigarInvokerJMX.PROP_TYPE;
@ -58,16 +56,6 @@ public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration {
*/ */
protected final SigarProxy sigar; protected final SigarProxy sigar;
/**
* The MBean server this MBean is registered to. Set during the MBean's
* registration to the MBean server and unset to <code>null</code> when
* the deregistration finished.
*
* @see #preRegister(MBeanServer, ObjectName)
* @see #postDeregister()
*/
protected MBeanServer mbeanServer;
/** /**
* <p>Creates a new instance of this class. The SigarProxy instance is stored (and * <p>Creates a new instance of this class. The SigarProxy instance is stored (and
* accessible) via the {@link #sigar} member. * accessible) via the {@link #sigar} member.
@ -145,59 +133,6 @@ public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration {
return result; return result;
} }
// -------
// Implementation of the MBeanRegistration interface
// -------
/**
* <p>Returns <code>new ObjectName(this.getObjectName())</code> to guarantee
* a reliable and reproducable object name.</p>
*
* <p><b>Note:</b> Make sure any subclass does a super call to this method,
* otherwise the implementation might be broken.</p>
*
* @see MBeanRegistration#preRegister(MBeanServer, ObjectName)
*/
public ObjectName preRegister(MBeanServer server, ObjectName name)
throws Exception {
this.mbeanServer = server;
return new ObjectName(getObjectName());
}
/**
* Empty implementation, allowing subclasses to ignore the interface.
*
* <p><b>Note:</b> Make sure any subclass does a super call to this method,
* otherwise the implementation might be broken.</p>
*
* @see MBeanRegistration#postRegister(Boolean)
*/
public void postRegister(Boolean success) {
}
/**
* Empty implementation, allowing subclasses to ignore the interface.
*
* <p><b>Note:</b> Make sure any subclass does a super call to this method,
* otherwise the implementation might be broken.</p>
*
* @see MBeanRegistration#preDeregister()
*/
public void preDeregister() throws Exception {
}
/**
* Empty implementation, allowing subclasses to ignore the interface.
*
* <p><b>Note:</b> Make sure any subclass does a super call to this method,
* otherwise the implementation might be broken.</p>
*
* @see MBeanRegistration#postDeregister()
*/
public void postDeregister() {
this.mbeanServer = null;
}
public void setAttribute(Attribute attr) throws AttributeNotFoundException { public void setAttribute(Attribute attr) throws AttributeNotFoundException {
throw new AttributeNotFoundException(attr.getName()); throw new AttributeNotFoundException(attr.getName());
} }

View File

@ -22,6 +22,7 @@ import java.util.ArrayList;
import javax.management.AttributeNotFoundException; import javax.management.AttributeNotFoundException;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.management.ObjectInstance; import javax.management.ObjectInstance;
import javax.management.ObjectName; import javax.management.ObjectName;
@ -57,7 +58,7 @@ import org.hyperic.sigar.SigarProxy;
* @author Bjoern Martin * @author Bjoern Martin
* @since 1.5 * @since 1.5
*/ */
public class SigarRegistry extends AbstractMBean { public class SigarRegistry extends AbstractMBean implements MBeanRegistration {
private static final String MBEAN_TYPE = "SigarRegistry"; private static final String MBEAN_TYPE = "SigarRegistry";
@ -79,6 +80,7 @@ public class SigarRegistry extends AbstractMBean {
private final String objectName; private final String objectName;
private final ArrayList managedBeans; private final ArrayList managedBeans;
private MBeanServer mbeanServer;
public SigarRegistry(SigarProxy sigar) { public SigarRegistry(SigarProxy sigar) {
super(sigar); super(sigar);
@ -117,6 +119,11 @@ public class SigarRegistry extends AbstractMBean {
// ------- // -------
// Implementation of the MBeanRegistration interface // Implementation of the MBeanRegistration interface
// ------- // -------
public ObjectName preRegister(MBeanServer server, ObjectName name)
throws Exception {
this.mbeanServer = server;
return new ObjectName(getObjectName());
}
/** /**
* Registers the default set of Sigar MBeans. Before doing so, a super call * Registers the default set of Sigar MBeans. Before doing so, a super call
@ -127,8 +134,6 @@ public class SigarRegistry extends AbstractMBean {
public void postRegister(Boolean success) { public void postRegister(Boolean success) {
ReflectedMBean mbean; ReflectedMBean mbean;
super.postRegister(success);
if (!success.booleanValue()) if (!success.booleanValue())
return; return;
@ -239,8 +244,9 @@ public class SigarRegistry extends AbstractMBean {
} }
} }
} }
}
// do the super call public void postDeregister() {
super.preDeregister(); this.mbeanServer = null;
} }
} }