The base agent does very little filtering because it does very little management. Usually, filters are applied programmatically in order to get a list of MBeans to which some operations will apply. There are no management operations in the MBean server which apply to a list of MBeans: you must loop through your list and apply the desired operation to each MBean.
Before exiting the agent application, we do a query of all MBeans so that we can unregister them properly. MBeans should be unregistered before being destroyed since they may need to perform some actions before or after being unregistered. See "MBean Registration Control" in the JMX agent specification for more information.
public void removeAllMBeans() { try { Set allMBeans = myMBeanServer.queryNames(null, null); for (Iterator i = allMBeans.iterator(); i.hasNext();) { ObjectName name = (ObjectName) i.next(); // Don't unregister the MBean server delegate if ( ! name.toString().equals( ServiceName.DELEGATE ) ) { println("Unregistering " + name.toString() ); myMBeanServer.unregisterMBean(name); } } } catch (Exception e) { e.printStackTrace(); System.exit(0); } } |
We use the queryNames method because we only need the object names to operate on MBeans. The null object name as a filter gives us all MBeans in the MBean server. We then iterate through the resulting set and unregister each one, except for the MBean server delegate. As described in "The MBean Server Delegate", the delegate is also an MBean and so it will be returned by the query. However, if we unregister it, the MBean server can no longer function and remove the rest of our MBeans.
We recognized the delegate by its standard name which is given by the static field ServiceName.DELEGATE. The ServiceName class provides standard names and other default properties for communications and service MBeans. It also provides the version strings that are exposed by the delegate MBean. Note that, since the delegate is the only MBean created directly by the MBean server, it is the only one whose name cannot be overridden during its registration. This is why the delegate always has the same object name, and we are always sure to detect it.