FileDocCategorySizeDatePackage
CoverageInfoImpl.javaAPI DocGlassfish v2 API20981Fri May 04 22:23:42 BST 2007com.sun.enterprise.management.support

CoverageInfoImpl

public final class CoverageInfoImpl extends Object implements Serializable, CoverageInfo
Used to record access to AMX.
see
AMXDebugStuff

Fields Summary
public static final long
serialVersionUID
private final Set
mAttributesRead
private final Set
mAttributesWritten
private final Set
mOperationsInvoked
private final Map
mAttributeGetFailures
private final Map
mAttributeSetFailures
private final Map
mUnknownAttributes
private final Map
mUnknownOperations
private final Map
mInvocationFailures
private Set
mLegalReadableAttributes
private Set
mLegalWriteableAttributes
private Set
mLegalOperations
private MBeanInfo
mMBeanInfo
private static final String[]
EMPTY_SIG
Constructors Summary
public CoverageInfoImpl(MBeanInfo mbeanInfo)

    
        
        
    
        mLegalReadableAttributes   = null;
        mLegalWriteableAttributes  = null;
        mLegalOperations           = null;
        mMBeanInfo  = mbeanInfo;
        setMBeanInfo( mMBeanInfo );
        
        mAttributesRead    = new HashSet<String>();
        mAttributesWritten = new HashSet<String>();
        mOperationsInvoked = new HashSet<String>();
        
        mAttributeGetFailures = new HashMap<String,Integer>();
        mAttributeSetFailures = new HashMap<String,Integer>();
        mUnknownAttributes    = new HashMap<String,Integer>();
        mUnknownOperations    = new HashMap<String,Integer>();
        mInvocationFailures   = new HashMap<String,Integer>();
        
    
Methods Summary
public voidattributeGetFailure(java.lang.String name)

        Integer count   = mAttributeGetFailures.get( name );
        count   = Integer.valueOf(count == null ? 1
                                      : 1 + count.intValue());
        
        mAttributeGetFailures.put( name, count );
    
public voidattributeSetFailure(java.lang.String name)

        Integer count   = mAttributeSetFailures.get( name );
        count   = Integer.valueOf(count == null ? 1
                                      : 1 + count.intValue());
        
        mAttributeSetFailures.put( name, count );
    
public voidattributeWasRead(java.lang.String name)

        if ( mLegalReadableAttributes.contains( name ) )
        {
            mAttributesRead.add( name );
        }
        else
        {
            unknownAttribute( name );
        }
    
public voidattributeWasWritten(java.lang.String name)

        if ( mLegalWriteableAttributes.contains( name ) )
        {
            mAttributesWritten.add( name );
        }
        else
        {
            unknownAttribute( name );
        }
    
public voidattributesWereRead(java.lang.String[] names)

        for( final String name : names )
        {
            attributeWasRead( name );
        }
    
private voidcheckHaveMBeanInfo()

        if ( getMBeanInfo() == null )
        {
            throw new IllegalArgumentException(
                "MBeanInfo must be set using setMBeanInfo() prior to call" );
        }
    
public voidclear()

        mAttributesRead.clear();
        mAttributesWritten.clear();
        mOperationsInvoked.clear();
        
        mAttributeGetFailures.clear();
        mAttributeSetFailures.clear();
        mUnknownAttributes.clear();
        mUnknownOperations.clear();
        mInvocationFailures.clear();
    
public java.util.MapgetAttributeGetFailures()

        return new HashMap<String,Integer>( mAttributeGetFailures );
    
public intgetAttributeReadCoverage()

        return percent( mAttributesRead.size(), mLegalReadableAttributes.size() );
    
public java.util.MapgetAttributeSetFailures()

        return new HashMap<String,Integer>( mAttributeSetFailures );
    
public intgetAttributeWriteCoverage()

        return percent( mAttributesWritten.size(), getNumWriteableAttributes() );
    
public java.util.SetgetAttributesNotRead()

        checkHaveMBeanInfo();
        
        // remove all Attributes which were written from the legal set
        final Set<String>   notRead = new HashSet<String>( mLegalReadableAttributes );
        notRead.removeAll( mAttributesRead );

        return notRead;
    
public java.util.SetgetAttributesNotWritten()

        checkHaveMBeanInfo();
        
        // remove all Attributes which were read from the legal set
        final Set<String>   notWritten = new HashSet<String>( mLegalWriteableAttributes );
        notWritten.removeAll( mAttributesWritten );

        return notWritten;
    
public java.util.SetgetAttributesRead()

        return new HashSet<String>( mAttributesRead );
    
public java.util.SetgetAttributesWritten()

        return  new HashSet<String>( mAttributesWritten );
    
public booleangetFullCoverage()

        return getAttributeReadCoverage() == 100 &&
                getAttributeWriteCoverage() == 100 &&
                getOperationCoverage() == 100;
    
private java.lang.StringgetFullOperationName(java.lang.String name, java.lang.String[] sig)

        final String    sigString   = StringUtil.toString( ",", sig == null ? EMPTY_SIG : sig  );
        
        final String    s   = name + "(" + sigString + ")";
        
        return s;
    
public java.util.MapgetInvocationFailures()

        return new HashMap<String,Integer>( mInvocationFailures );
    
public javax.management.MBeanInfogetMBeanInfo()

        return mMBeanInfo;
    
public intgetNumOperations()

        return mLegalOperations.size();
    
public intgetNumReadableAttributes()

        checkHaveMBeanInfo();
        
        return mLegalReadableAttributes.size();
    
public intgetNumWriteableAttributes()

        checkHaveMBeanInfo();
        
        return mLegalWriteableAttributes.size();
    
public intgetOperationCoverage()

return
percent of legal operations which were invoked

        checkHaveMBeanInfo();
        
        final int   numOperations   = mLegalOperations.size();
        
        // do it this way; some illegal operations get invoked, too
        final Set<String> remaining   = new HashSet<String>( mLegalOperations );
        for( final String invoked : mOperationsInvoked )
        {
            remaining.remove( invoked );
        }

        final int   numInvoked      = numOperations - remaining.size();
        
        return percent( numInvoked, numOperations );
    
public java.util.SetgetOperations()

        return mLegalOperations;
    
public java.util.SetgetOperationsInvoked()

        return new HashSet<String>( mOperationsInvoked );
    
public java.util.SetgetOperationsNotInvoked()

        checkHaveMBeanInfo();
        
        // remove all Attributes which were read from the legal set
        final Set<String>   notInvoked = new HashSet<String>( mLegalOperations );
        notInvoked.removeAll( getOperationsInvoked() );

        return notInvoked;
    
public java.util.SetgetReadableAttributes()

        return mLegalReadableAttributes;
    
public java.util.MapgetUnknownAttributes()

        return new HashMap<String,Integer>( mUnknownAttributes );
    
public java.util.MapgetUnknownOperations()

        return new HashMap<String,Integer>( mUnknownOperations );
    
public java.util.SetgetWriteableAttributes()

        return mLegalWriteableAttributes;
    
public voidignoreUnknownAttribute(java.lang.String name)

        mUnknownAttributes.remove( name );
        mAttributeGetFailures.remove( name );
        mAttributeSetFailures.remove( name );
    
public voidmarkAsInvoked(java.lang.String fullName)

        if ( (! fullName.endsWith( ")" )) || fullName.indexOf( "(" ) < 0 )
        {
            throw new IllegalArgumentException( fullName );
        }
        
        mOperationsInvoked.add( fullName );
    
public voidmerge(CoverageInfo info)

        mAttributesRead.addAll( info.getAttributesRead() );
        mAttributesWritten.addAll( info.getAttributesWritten() );
        mOperationsInvoked.addAll( info.getOperationsInvoked() );
        
        mergeCounts( info.getAttributeGetFailures(), mAttributeGetFailures );
        mergeCounts( info.getAttributeSetFailures(), mAttributeSetFailures );
        mergeCounts( info.getUnknownAttributes(), mUnknownAttributes );
        mergeCounts( info.getUnknownOperations(), mUnknownOperations );
        mergeCounts( info.getInvocationFailures(), mInvocationFailures );
    
private voidmergeCounts(java.util.Map src, java.util.Map dest)

        for( final String key : src.keySet() )
        {
            final Integer srcValue = src.get( key );
            final Integer destValue = dest.get( key );
            
            final int sum   = srcValue.intValue() +
                    (destValue == null ? 0 : destValue.intValue());
            
            dest.put(key, Integer.valueOf(sum));
        }
    
public voidoperationFailed(java.lang.String name, java.lang.String[] sig)

        final String    fullName    = getFullOperationName( name, sig );
        if ( ! getOperations().contains( fullName ) )
        {
            throw new IllegalArgumentException( fullName );
        }
        
        Integer count   = mInvocationFailures.get( name );
        count   = Integer.valueOf(count == null ? 1
                                      : 1 + count.intValue());
        
        mInvocationFailures.put( fullName, count );
    
public voidoperationWasInvoked(java.lang.String name, java.lang.String[] sig)

        final String fullName   = getFullOperationName( name, sig );

        if ( mLegalOperations.contains( fullName ) )
        {
            mOperationsInvoked.add( fullName );
        }
        else
        {
            unknownOperation( fullName, sig );
            assert( ! mOperationsInvoked.contains( fullName ) );
        }
    
private intpercent(int numerator, int denominator)

        return (int)((((float)numerator / (float)denominator))*100.0);
    
private voidsdebug(java.lang.Object o)

    
          
    
        System.out.println( "" + o );
    
public voidsetMBeanInfo(javax.management.MBeanInfo mbeanInfo)

        mLegalOperations            = new HashSet<String>();
        mLegalReadableAttributes    = new HashSet<String>();
        mLegalWriteableAttributes   = new HashSet<String>();
        
        if ( mbeanInfo != null ) try
        {
            final MBeanOperationInfo[]  ops = mbeanInfo.getOperations();
            for( final MBeanOperationInfo opInfo : ops  )
            {
                final String[]  sig = JMXUtil.getSignature( opInfo.getSignature() );
                final String    fullName    =
                    getFullOperationName( opInfo.getName(), sig );
                mLegalOperations.add( fullName );
            }
            mLegalOperations    = Collections.unmodifiableSet( mLegalOperations );
            
            final MBeanAttributeInfo[] allAttrInfos    = getMBeanInfo().getAttributes();
                
            final MBeanAttributeInfo[]  readables  =
                JMXUtil.filterAttributeInfos( allAttrInfos,
                    ReadWriteAttributeFilter.READABLE_FILTER );
                    
            final MBeanAttributeInfo[]  writeables  =
                JMXUtil.filterAttributeInfos( allAttrInfos,
                    ReadWriteAttributeFilter.WRITEABLE_FILTER );
            
            mLegalReadableAttributes    =
                GSetUtil.newUnmodifiableStringSet( JMXUtil.getAttributeNames( readables ) );
                
            mLegalWriteableAttributes    =
                GSetUtil.newUnmodifiableStringSet( JMXUtil.getAttributeNames( writeables ) );
        }
        catch( Exception e )
        {
            System.out.println( ExceptionUtil.toString( e ) );
            throw new RuntimeException( e );
        }
    
public java.lang.StringtoString()

        return toString( true );
    
private java.lang.StringtoString(java.util.Collection c)

        return CollectionUtil.toString( c );
    
private java.lang.StringtoString(java.util.Collection c, java.lang.String sep)

        return CollectionUtil.toString( c, sep );
    
public java.lang.StringtoString(boolean verbose)

        final String    NEWLINE    = System.getProperty( "line.separator" );
        final String    INDENT  = "  ";
        final String    ITEM_SEP  = NEWLINE + INDENT;
        
        final StringBuilder b   = new StringBuilder();
        
        b.append( "Attribute read coverage " +
            getAttributesRead().size() + "/" + getNumReadableAttributes() +
            " = " + getAttributeReadCoverage() + "%" + NEWLINE );
            
        b.append( "Attribute write coverage " +
            getAttributesWritten().size() + "/" + getNumWriteableAttributes() +
            " = " + getAttributeWriteCoverage() + "%" + NEWLINE );
            
        b.append( "Operation invocation coverage " +
                    getOperationsInvoked().size() + "/" + getNumOperations() +
                    " = " + getOperationCoverage()  + "%" + NEWLINE );
        
        if ( verbose )
        {
            b.append( mAttributesRead.size() +
                " Attributes read: " + ITEM_SEP +
                    toString( getAttributesRead(), ITEM_SEP) + NEWLINE );
                
            b.append( mAttributesWritten.size() +
                " Attributes written: " + ITEM_SEP +
                    toString( getAttributesWritten(), ITEM_SEP) + NEWLINE );
                
            b.append( mOperationsInvoked.size() +
                " operations invoked: " + ITEM_SEP +
                    toString( getOperationsInvoked(), ITEM_SEP) + NEWLINE );
        }
        
        if ( getMBeanInfo() != null )
        {
            Set<String> not = null;
            
            not = getAttributesNotRead();
            if ( not.size() != 0 || verbose )
            {
                b.append( not.size() +
                    " Attributes NOT read:" + ITEM_SEP + toString( not, ITEM_SEP ) + NEWLINE );
            }
                
            not = getAttributesNotWritten();
            if ( not.size() != 0 || verbose )
            {
                b.append( not.size() +
                    " Attributes NOT written:" + ITEM_SEP + toString( not, ITEM_SEP) + NEWLINE );
            }
            
            not = getOperationsNotInvoked();
            if ( not.size() != 0 || verbose )
            {
                b.append( not.size() +
                    " operations NOT invoked:" + ITEM_SEP + 
                        CollectionUtil.toString( not, ITEM_SEP ) + NEWLINE );
            }
        }
        else
        {
            b.append( "WARNING: MBeanInfo not supplied, " +
                "can't emit Attributes/operations not read/written/invoked" + NEWLINE );
        }
        
        Map<String,Integer> failures    = null;
        
        failures    = getAttributeGetFailures();
        if ( failures.size() != 0 || verbose )
        {
            b.append( failures.keySet().size() +
                " getAttribute failures: " + ITEM_SEP +
                    toString( failures.keySet(), ITEM_SEP) + NEWLINE );
        }
            
        failures    = getAttributeSetFailures();
        if ( failures.size() != 0 || verbose )
        {
            b.append( failures.keySet().size() +
                " setAttribute failures: " + ITEM_SEP +
                    toString( failures.keySet(), ITEM_SEP) + NEWLINE );
        }
            
        failures    = getUnknownAttributes();
        if ( failures.size() != 0 || verbose )
        {
            b.append( failures.keySet().size() +
                " unknown Attributes: " + ITEM_SEP +
                    toString( failures.keySet(), ITEM_SEP) + NEWLINE );
        }
        
        failures    = getUnknownOperations();
        if ( failures.size() != 0 || verbose )
        {
            b.append( failures.keySet().size() +
                " unknown operations: " + ITEM_SEP +
                    toString( failures.keySet(), ITEM_SEP) + NEWLINE );
        }
            
        failures    = getInvocationFailures();
        if ( failures.size() != 0 || verbose )
        {
            b.append( failures.keySet().size() +
                " invoke() failures: " + ITEM_SEP +
                CollectionUtil.toString( failures.keySet(), ITEM_SEP) + NEWLINE );
        }
        
        return b.toString();
    
private voidunknownAttribute(java.lang.String name)

        Integer count   = mUnknownAttributes.get( name );
        count   = Integer.valueOf(count == null ? 1
                                      : 1 + count.intValue());
        
        mUnknownAttributes.put( name, count );
    
private voidunknownOperation(java.lang.String name, java.lang.String[] sig)

        final String fullName   =
            getFullOperationName( name, sig );
        
        Integer count   = mUnknownOperations.get( fullName );
        count   = Integer.valueOf(count == null ? 1
                                      : 1 + count.intValue());
        
        mUnknownOperations.put( fullName, count );