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

DottedNamesBase

public abstract class DottedNamesBase extends AMXImplBase implements com.sun.appserv.management.base.DottedNames
Base class from which the DottedNames (config and monitoring) MBeans inherit.

Fields Summary
private static final String
DOTTED_NAMES
protected volatile OldDottedNamesProxy
mOldDottedNamesProxy
private MBeanInfo
mMBeanInfo
private Map
mAttributes
private Set
mParentAttributeNames
protected final com.sun.appserv.management.helper.AMXDebugHelper
mDebugHelper
private static final String
ALL_DOTTED_NAMES
protected volatile boolean
mRefreshNeeded
protected final ReentrantLock
mRefreshLock
Constructors Summary
public DottedNamesBase()

		mMBeanInfo	= null;
		
		mParentAttributeNames	= null;
        
        debugMsg( "DottedNamesBase: " + this.getClass().getName() );
        mDebugHelper.setEchoToStdOut( true );
	
Methods Summary
private static java.lang.StringattributeToNamePair(javax.management.Attribute attr)

		return( attr.getName() + "=" + attr.getValue() );
	
protected javax.management.MBeanAttributeInfo[]buildAttributeInfos(javax.management.MBeanAttributeInfo[] parentAttributeInfos)

		final String[]	parentAttributeNames	=
			JMXUtil.getAttributeNames( parentAttributeInfos );
		mParentAttributeNames	=
		    GSetUtil.newUnmodifiableStringSet( parentAttributeNames );
		
		final Map<String,Attribute>		attributes	=	getAttributes();
		
		final MBeanAttributeInfo[]	infos =
			new MBeanAttributeInfo[ attributes.size()  + parentAttributeInfos.length ];
		
		// make info for every Attribute
		
		int i	= 0;
		for( final String name : attributes.keySet() )
		{
			final Attribute	attr	= attributes.get( name );
		
			final Object	value	= attr.getValue();
			final Class	theClass	= value == null ? String.class : attr.getValue().getClass();
			
			infos[ i ]	= new MBeanAttributeInfo( name, theClass.getName(), "",
				true, isWriteableDottedName( name ), false );
			++i;
		}
		
		System.arraycopy( parentAttributeInfos, 0,
			infos, attributes.size(), parentAttributeInfos.length );

		return( infos );
	
protected javax.management.MBeanInfobuildMBeanInfo()

		final MBeanInfo	superMBeanInfo	= super.getMBeanInfo();
		
		final MBeanAttributeInfo[]		attributeInfos		=
				buildAttributeInfos( superMBeanInfo.getAttributes() );
		final MBeanOperationInfo[]		operationInfos		=
				buildOperationInfos( superMBeanInfo.getOperations() );
		
		final MBeanInfo	info	= new MBeanInfo( this.getClass().getName(),
									"exposes dotted-names as Attributes",
									attributeInfos,
									superMBeanInfo.getConstructors(),
									operationInfos,
									superMBeanInfo.getNotifications() );
		
		return( info );
	
protected javax.management.MBeanOperationInfo[]buildOperationInfos(javax.management.MBeanOperationInfo[] existing)

		final MBeanOperationInfo	refreshInfo	= new MBeanOperationInfo( "refresh",
			"update MBeanInfo to reflect all available dotted names",
			null,
			Void.class.getName(),
			MBeanOperationInfo.ACTION );
		
		final MBeanOperationInfo[]	infos	= new MBeanOperationInfo[ existing.length + 1 ];
		System.arraycopy( existing, 0, infos, 0, existing.length );
		infos[ infos.length -1 ]	= refreshInfo;
		
		return( infos );
	
private final voidcheckLegalName(java.lang.String name)

        checkRefresh();
        
		if ( ! isLegalAttributeName( name ) )
		{	    
			throw new AttributeNotFoundException( "illegal attribute name: " + name );
		}
	
private voidcheckRefresh()

        if ( mRefreshNeeded )
        {
            refreshAttributes();
        }
    
protected voiddebugMsg(java.lang.Object args)

             mDebugHelper.println( args ); 
public abstract java.lang.Object[]dottedNameGet(java.lang.String[] names)

public abstract java.lang.ObjectdottedNameGet(java.lang.String name)

public abstract java.lang.Object[]dottedNameList(java.lang.String[] names)

public abstract java.lang.Object[]dottedNameSet(java.lang.String[] nameValuePairs)

protected final voidensureAttributes()

		if ( mAttributes == null )
		{
			refreshAttributes();
		}
	
private synchronized voidensureMBeanInfo()

		if ( mMBeanInfo == null )
		{
			refresh();
			assert( mMBeanInfo != null );
		}
	
protected final voidfilterNames(java.lang.String[] in, java.util.Set dotted, java.util.Set parent)

		for( int i = 0; i < in.length; ++i )
		{
			final String	name	= in[ i ];
			
			if ( isDottedName( name ) )
			{
				dotted.add( name );
			}
			else if ( isParentAttributeName( name ) )
			{
				parent.add( name );
			}
		}
	
public java.lang.ObjectgetAttribute(java.lang.String name)

		checkLegalName( name );
	    mCoverage.attributeWasRead( name );
		
		Object	result	= null;
		
		if ( isDottedName( name ) )
		{
			result	= dottedNameGet( name );
			assert( !(result instanceof Attribute) );
		}
		else if ( isParentAttributeName( name ) )
		{
			result	= super.getAttribute( name );
		}
		else
		{
			throw new AttributeNotFoundException( name );
		}
		
		return( result );
	
public javax.management.AttributeListgetAttributes(java.lang.String[] names)

        checkRefresh();
        
    	mCoverage.attributesWereRead( names );
	    
		final Set<String>	dotted	= new HashSet<String>();
		final Set<String>	parent	= new HashSet<String>();
		filterNames( names, dotted, parent);

		final Object[]	dottedResults	=
			dottedNameGet( (String[])dotted.toArray( new String[ dotted.size() ] ) );
		
		final String[]		namesForParent	= new String[ parent.size() ];
		final AttributeList	parentResults	=
			super.getAttributes( (String[])parent.toArray( namesForParent ) );
		
		final AttributeList	successList	= new AttributeList();
		successList.addAll( parentResults );
		
		// add all the dotted name results
		for( int i = 0; i < dottedResults.length; ++i )
		{
			if ( dottedResults[ i ] instanceof Attribute )
			{
				successList.add( (Attribute)dottedResults[ i ] );
			}
			else
			{
				assert( dottedResults[ i ] instanceof Exception );
			}
		}
		
		
		return( successList );
	
protected final java.util.MapgetAttributes()

		ensureAttributes();
		
		return( mAttributes );
	
protected final java.lang.String[]getDottedNamesArray()

		final Map<String,Attribute>	map	= getAttributes();
		
		final Set<String>	keySet	= map.keySet();
		
		return( (String[])keySet.toArray( new String[ keySet.size() ] ) );
	
public javax.management.MBeanInfogetMBeanInfo()
Note the during registration, we won't have setup our delegate, and our MBeanInfo will therefore not contain any Attributes.

		MBeanInfo	mbeanInfo	= null;
        
        checkRefresh();
        
		if ( getOldDottedNames() != null )
		{
			ensureMBeanInfo();
			mbeanInfo	= mMBeanInfo;
		}
		else
		{
			mbeanInfo	= super.getMBeanInfo();
		}
		
		return( mbeanInfo );
	
public booleangetMBeanInfoIsInvariant()
MBeanInfo Attributes change whenever the dotted-namespace changes.

		return( false );
	
protected com.sun.enterprise.management.support.DottedNamesBase$OldDottedNamesProxygetOldDottedNames()

        // synchronization not needed; making more than one proxy is not an issue
        if ( mOldDottedNamesProxy == null ) {
            setupOldDottedNamesProxy();
        }
        
		return( mOldDottedNamesProxy );
	
public java.lang.ObjectinvokeManually(java.lang.String operationName, java.lang.Object[] params, java.lang.String[] types)

		final boolean	noParams	= params == null || params.length == 0;
		Object			result	= null;
		
		if ( operationName.equals( "refresh" ) && noParams )
		{
			refresh();
			result	= null;
		}
		else
		{
			result	= super.invokeManually( operationName, params, types );
		}
		return( result );
	
protected final booleanisDottedName(java.lang.String name)

		return( getAttributes().keySet().contains( name ) );
	
protected final booleanisLegalAttributeName(java.lang.String name)

		ensureAttributes();
		return( getAttributes().keySet().contains( name ) ||
				mParentAttributeNames.contains( name ) );
	
protected final booleanisParentAttributeName(java.lang.String name)

		return( mParentAttributeNames.contains( name ) );
	
protected abstract booleanisWriteableDottedName(java.lang.String name)

private static javax.management.AttributenamePairToAttribute(java.lang.String pair)

		final int			delimIndex	= pair.indexOf( "=" );
		assert( delimIndex >= 1 );
		final String		name	= pair.substring( 0, delimIndex );
		final String		value	= pair.substring( delimIndex + 1, pair.length() );
		
		return( new Attribute( name, value ) );
	
public voidpostRegisterHook(java.lang.Boolean registrationSucceeded)

        super.postRegisterHook( registrationSucceeded );
        
	    if ( registrationSucceeded.booleanValue() )
		{
            // start a thread upon completion of server startup that will mark things as needing
            // a refresh, providing an accurate view of all available attributes without requiring
            // the client to make an explicit call to refresh().  Failure to do this means
            // that an empty or minimal state will be improperly returned.
            final RunnableBase refresher = new RunnableBase() {
                protected void doRun() {
                    FeatureAvailability.getInstance().waitForFeature(
                            SERVER_STARTED_FEATURE, "DottedNamesBase");
                    
                    setupOldDottedNamesProxy();
                    mRefreshNeeded   = true; // refresh lazily
                }
            };
            refresher.submit( RunnableBase.HowToRun.RUN_IN_SEPARATE_THREAD );
		}
	
public final voidrefresh()

        refreshAttributes();
	
final voidrefreshAttributes()
Refresh the MBeanInfo to reflect the currently available attributes.

			        	 
		 
	
	
		Object	result	= null;
		
        // if the lock cannot be acquired, that's just fine--another thread is already
        // doing the work.
        if ( mRefreshLock.tryLock() )
        {
            try
            {
                //trace( "##### DottedNamesBase.refreshAttributeNames" );
                result	= dottedNameGet( ALL_DOTTED_NAMES );
                
                // results is an array of length 1.  It should contain an Object[] containing
                // everything obtained from ALL_DOTTED_NAMES
                final Attribute[]	values	= (Attribute[])result;
                
                // extract the name of each attribute
                final Map<String,Attribute>	map	= new HashMap<String,Attribute>();
                for( final Attribute attr : values )
                {
                    map.put( attr.getName(), attr );
                }
                
                mAttributes	= map;
                
                mMBeanInfo	= buildMBeanInfo();
                
                mRefreshNeeded = false;
            }
            finally
            {
                mRefreshLock.unlock();
            }
        }
        else
        {
            // wait till the other thread has finished, in order to allow
            // the refresh to finish.
            mRefreshLock.lock();
            
            // release it
            mRefreshLock.unlock();
        }
	
public voidsetAttribute(javax.management.Attribute attr)

	    final String    name    = attr.getName();
	    
	    if ( isParentAttributeName( name ) )
	    {
	        super.setAttribute( attr );
	    }
	    else
	    {
    		checkLegalName( name );
    	    mCoverage.attributeWasWritten( name );
    		
    		final AttributeList		inList	= new AttributeList();
    		inList.add( attr );
    		final AttributeList	result	= setAttributes( inList );
    		if ( result.size() != 1)
    		{
    			throw new InvalidAttributeValueException( attr.getName() );
    		}
		}
	
public javax.management.AttributeListsetAttributes(javax.management.AttributeList attributes)

		/*
			Convert each attribute to a name/value pair.
			Omit any attributes that don't have a legal attribute name
		 */
		final int	numAttrsIn	= attributes.size();
		final List<String>	legalPairs	= new ArrayList<String>();
		for( int i = 0; i < numAttrsIn; ++i )
		{
			final Attribute	attr	= (Attribute)attributes.get( i );
			
			final String    name    = attr.getName();
			mCoverage.attributeWasWritten( name );
			
			if ( isLegalAttributeName( name ) )
			{
				legalPairs.add( attributeToNamePair( attr ) );
			}
		}
		
		final String[]	pairs	= (String[])legalPairs.toArray( new String[ legalPairs.size() ] );

		final Object[] results	= dottedNameSet( pairs );
		
		final AttributeList	attributeList	= new AttributeList();
		for( int i = 0; i < results.length; ++i )
		{
			if ( results[ i ] instanceof Attribute )
			{
				attributeList.add( (Attribute)results[ i ] );
			}
			else
			{
				assert( results[ i ] instanceof Exception );
				// it's an exception
			}
		}
		
		return( attributeList );
	
protected voidsetupOldDottedNamesProxy()

		setupOldDottedNamesProxy( Util.newObjectName( DOTTED_NAMES ) );
	
protected voidsetupOldDottedNamesProxy(javax.management.ObjectName target)

		final MBeanServer	server	= getMBeanServer();
        
        // server can be null early at startup
		if ( server != null && server.isRegistered( target ) )
		{
			mOldDottedNamesProxy	= (OldDottedNamesProxy)
                MBeanServerInvocationHandler.newProxyInstance(
                    server, target, OldDottedNamesProxy.class, false );
		}