Filter ObjectNames based on the value of a particular Attribute. The value
may be null or anything else. This is essentially a crude form of using
the QueryMgr. A value which is a Class object succeeds if the result is
an object whose class is assignable to the specfied class. Typically this
is used to detect a thrown Exception.
For example, to select all MBeans which have a [bB]oolean Attribute named "Enabled",
which is set to true, call:
filterByAttributeValue( objectNameSet, "Enabled", Boolean.TRUE)
The query for the Attribute value is performed as a bulk operation; thus this
routine may be used with confidence that it is fast.
final ObjectName[] objectNames = new ObjectName[ objectNameSet.size() ];
objectNameSet.toArray( objectNames );
final Object[] values = mBulkAccess.bulkGetAttribute( objectNames, attributeName );
final Set<ObjectName> filtered = new HashSet<ObjectName>();
for( int i = 0; i < values.length; ++i )
{
final Object idxValue = values[ i ];
boolean matches = false;
if ( valueToMatch == null && idxValue == null )
{
matches = true;
}
else if ( valueToMatch instanceof Class &&
((Class<?>)valueToMatch).isAssignableFrom( idxValue.getClass() ) )
{
matches = true;
}
else if ( valueToMatch != null && valueToMatch.equals( idxValue ) )
{
matches = true;
}
else
{
// no match
}
if ( matches )
{
filtered.add( objectNames[ i ] );
}
}
return( filtered );