Sun Java System Application Server Enterprise Edition 8.2 Developer's Guide

Displaying the AMX MBean Hierarchy

The displayAMX() method demonstrates how to display the AMX MBean hierarchy.


Example 16–6 Displaying the AMX MBean Hierarchy

[...]
displayAMX(
    final AMX amx,
    final int indentCount )
{
    final String indent = getIndent( indentCount );
    final String j2eeType = amx.getJ2EEType();
    final String name = amx.getName();
    if ( name.equals( AMX.NO_NAME ) )
    {
        println( indent + j2eeType );
    }
    else
    {
        println( indent + j2eeType + "=" + name );
    }
}
private void
displayHierarchy(
    final Collection amxSet,
    final int indentCount )
{
    final Iterator    iter    = amxSet.iterator();
    while ( iter.hasNext() )
    {
        final AMX amx = (AMX)iter.next();
        displayHierarchy( amx, indentCount );
    }
}
public void
displayHierarchy(
    final AMX amx,
    final int    indentCount )
{
    displayAMX( amx, indentCount );
    if ( amx instanceof Container )
    {
        final Map m = ((Container)amx).getMultiContaineeMap( null );
        final Set deferred = new HashSet();
        final Iterator mapsIter = m.values().iterator();
        while ( mapsIter.hasNext() )
        {
            final Map instancesMap = (Map)mapsIter.next();
            final AMX first = (AMX)instancesMap.values().iterator().next();
            if ( first instanceof Container )
            {
                deferred.add( instancesMap );
            }
            else
            {
                displayHierarchy( instancesMap.values(), indentCount + 2);
            }
        }
        // display deferred items
        final Iterator iter = deferred.iterator();
        while ( iter.hasNext() )
        {
            final Map instancesMap = (Map)iter.next();
            displayHierarchy( instancesMap.values(), indentCount + 2);
        }
    }
}
public void displayHierarchy()
{
    displayHierarchy( getDomainRoot(), 0);
}
public void
displayHierarchy( final String j2eeType )
{
    final Set items = getQueryMgr().queryJ2EETypeSet( j2eeType );
    if ( items.size() == 0 )
    {
        println( "No {@link AMX} of j2eeType "
            + SampleUtil.quote( j2eeType ) + " found" );
    }
    else
    {
        displayHierarchy( items, 0);
    }
}
[...]

The displayAMX() method obtains the J2EE type and the name of an AMX MBean by calling AMX.getJ2EEType and AMX.getName respectively.

The displayHierarchy() method defines a standard Java Collection instance, amxSet, which collects instances of AMX MBeans.

To display the hierarchy of MBeans within a particular MBean in the collection, displayHierarchy() checks whether the MBean is an instance of Container. If so, it creates a set of the MBeans it contains by calling the com.sun.appserv.management.base.Container.getMultiContaineeMap() method.

The MBean hierarchy for a particular J2EE type is displayed by calling the com.sun.appserv.management.base.QueryMgr.queryJ2EETypeSet(), and passing the result to displayHierarchy().

To display the entire AMX MBean hierarchy in a domain, displayHierarchy() calls getDomainRoot() to obtain the root AMX MBean in the domain.