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

Loader

public final class Loader extends LoaderBase
Implements loading of all MBean API MBeans WITHIN the DAS (Domain Admin Server).

Fields Summary
private boolean
mQueuedAll
private List
mLoaders
private Map
mOldToNewObjectNames
private LoaderRegThread
mRegThread
private final Set
JMX_DOMAINS_OF_INTEREST
private final DeferredRegistrationThread
mDeferredRegistrationThread
private static final long
WAIT_THRESHOLD_MILLIS
private static final boolean
WAIT_FOR_REGISTRATION
private final Object
CONFIG_SYNC
Constructors Summary
public Loader()

        JMX_DOMAINS_OF_INTEREST =
            GSetUtil.newUnmodifiableStringSet( ObjectNames.kDefaultIASDomainName );
        
		mOldToNewObjectNames	=
		    Collections.synchronizedMap( new HashMap<ObjectName,ObjectName>() );
		
		mQueuedAll	= false;
		
		mLoaders	= null;
		
		mRegThread      = null;
		
		mDeferredRegistrationThread	= new DeferredRegistrationThread();
		mDeferredRegistrationThread.start();
				
	
Methods Summary
private javax.management.ObjectName_sync(javax.management.ObjectName oldObjectName)

        if ( ! getMBeanServer().isRegistered( oldObjectName ) )
        {
			throw new RuntimeException( new InstanceNotFoundException() );
        }
        
        // out of order registration can result in trying to register the same MBean
        // more than once.
        synchronized( this )
        {
            ObjectName	result	= mOldToNewObjectNames.get( oldObjectName );
            if ( result == null )
            {
                try
                {
                    final LoaderOfOld	loaderOfOld	= findLoaderOfOld( oldObjectName );
                    
                    if ( loaderOfOld != null )
                    {
                        result	= loaderOfOld.syncWithOld( oldObjectName );
                        if ( result == null )
                        {
                            throw new IllegalArgumentException( oldObjectName.toString() );
                        }
                    }
                }
                catch( Exception e )
                {
                    final String msg    = ExceptionUtil.toString( e );
                    debug( msg );
                    getMBeanLogger().warning( msg );
                    
                    if ( e instanceof RuntimeException )
                    {
                        throw (RuntimeException)e;
                    }
                    else
                    {
                        throw new RuntimeException( e );
                    }
                }
            }
		
            return( result );
        }
	
private voidaddLoaders()

		assert( getMBeanServer() != null );
		assert( getMBeanLogger() != null );
		
		final List<LoaderOfOld>	loaders	= new ArrayList<LoaderOfOld>();
		loaders.add( new LoaderOfOldConfig( this ) );
		loaders.add( new LoaderOfOld77( this ) );
		loaders.add( new LoaderOfOldMonitor( this ) );
		mLoaders	= Collections.unmodifiableList( loaders );
	
protected java.lang.ObjectcreateDomainRoot()

        return new DomainRootImpl();
    
private LoaderOfOldfindLoaderOfOld(javax.management.ObjectName candidate)

		LoaderOfOld	oldLoader	= null;
		
		for( final LoaderOfOld loo : mLoaders )
		{
			if ( loo.shouldSync( candidate ) )
			{
				oldLoader	= loo;
				break;
			}
		}
		return( oldLoader );
	
public voidhandleMBeanRegistered(javax.management.ObjectName oldObjectName)

    
		 
	  	 
		 
	
		trace( "handleMBeanRegistered: " + oldObjectName );
		
		if ( shouldSync( oldObjectName ) )
		{
			final long	start	= now();
			
            if ( WAIT_FOR_REGISTRATION )
            {
                while ( ! getMBeanServer().isRegistered( oldObjectName ) &&
                        (now() - start) < WAIT_THRESHOLD_MILLIS )
                {
                    mySleep( 50 );
                    debug( "SLEPT for 50ms waiting for " + oldObjectName );
                }
            }
			
			if ( ! getMBeanServer().isRegistered( oldObjectName ) )
			{
				trace( "Loader.handleMBeanRegistered: not found: " + JMXUtil.toString(oldObjectName) );
				throw new InstanceNotFoundException( JMXUtil.toString(oldObjectName) );
			}
			
			try
			{
				sync( oldObjectName );
			}
			catch( Exception e )
			{
				final Throwable	rootCause	= ExceptionUtil.getRootCause( e );
				if ( rootCause instanceof DeferRegistrationException )
				{
					mDeferredRegistrationThread.defer( oldObjectName );
				}
			}
		}
	
public voidhandleMBeanUnregistered(javax.management.ObjectName oldObjectName)

		trace( "handleMBeanUnregistered: " + oldObjectName );
		
		final ObjectName	newObjectName	=
			mOldToNewObjectNames.remove( oldObjectName );
			
		if ( newObjectName != null && getMBeanServer().isRegistered( newObjectName ) )
		{
		    debug( "unregistering: " + newObjectName + " corresponding to " + oldObjectName );
			getMBeanServer().unregisterMBean( newObjectName );
		}
	
public voidhandleNotification(javax.management.Notification notifIn, java.lang.Object handback)

        
		final String	type	= notifIn.getType();
		
		if ( notifIn instanceof MBeanServerNotification )
		{
			final MBeanServerNotification	notif	= (MBeanServerNotification)notifIn;
			final ObjectName objectName	= notif.getMBeanName();
            
            if ( type == MBeanServerNotification.REGISTRATION_NOTIFICATION )
            {
                if ( JMXUtil.toString(objectName).equals( "amx:j2eeType=J2EEServer,name=server" ) )
                {
                    debug( "Loader.handleNotification:  REGISTER for: " + JMXUtil.toString(objectName) );
                }
            }
            else if ( type == MBeanServerNotification.UNREGISTRATION_NOTIFICATION )
            {
                //debug( "Loader.handleNotification:  UNREGISTER for: " + JMXUtil.toString(objectName) );
            }
            
			if ( JMX_DOMAINS_OF_INTEREST.contains( objectName.getDomain() ) &&
                    shouldSync( objectName ) )
			{
				final boolean	register	=
					type == MBeanServerNotification.REGISTRATION_NOTIFICATION;
                
                // put it at the end of the registration or unregistration queue
				mRegThread.enqueue( register, objectName );
			}
		}
	
public booleanisDAS()

	    return true;
	
public booleanisStarted()

		return super.isStarted() && mQueuedAll &&
			mRegThread.isQueueEmpty();
	
protected javax.management.ObjectNameloadSystemInfo(javax.management.MBeanServer server)

		final BootUtil	bootUtil	= BootUtil.getInstance();
		
		final SystemInfoImpl	systemInfo	= new SystemInfoImpl( server, bootUtil );
		
		final ObjectName	tempName	= mObjectNames.getSingletonObjectName( systemInfo.J2EE_TYPE );
		
		final ObjectName objectName	= mServer.registerMBean( systemInfo, tempName ).getObjectName();
		
		debug( "loaded SystemInfo" );
		return( objectName );
	
protected final voidpostRegisterHook()

        super.postRegisterHook();
        
        FeatureAvailability.getInstance().registerFeature(
            FeatureAvailability.AMX_LOADER_FEATURE, getObjectName() );
    
protected voidpreRegisterHook()

		mRegThread	= new LoaderRegThread( this, mLogger );
		mRegThread.start();
	
private voidqueueAll()

		for( final LoaderOfOld oldLoader : mLoaders )
		{
			final List<ObjectName>	oldObjectNames	= oldLoader.findAllOld();
			mRegThread.enqueue( true, oldObjectNames );
			
			getMBeanLogger().fine( "Loader: Queued " + oldObjectNames.size() +
				" MBeans for loader " + oldLoader.getClass().getName() );
		}
	
synchronized javax.management.ObjectNameregisterNew(java.lang.Object impl, javax.management.ObjectName implObjectName, javax.management.ObjectName oldObjectName)

		//debug( "registering: ", JMXUtil.toString(implObjectName), " corresponding to ", JMXUtil.toString(oldObjectName) );
		    
		final ObjectName	resultName	=
			getMBeanServer().registerMBean( impl, implObjectName ).getObjectName();
			
		mOldToNewObjectNames.put( oldObjectName, resultName );
		
		return( resultName );
	
public javax.management.ObjectNameresyncAMXMBean(javax.management.ObjectName amx)

	    if ( ! getMBeanServer().isRegistered( amx ) )
	    {
	        throw new InstanceNotFoundException();
	    }
	    if ( ! getAMXJMXDomainName().equals( amx.getDomain() ) )
	    {
	        throw new IllegalArgumentException( "" + amx );
	    }
	    
	    debug( "resyncAMXMBean: looking for matching delegate MBean" );
	    ObjectName    old    = null;
	    for( final ObjectName oldTemp : mOldToNewObjectNames.keySet() )
	    {
	        if ( mOldToNewObjectNames.get( oldTemp ).equals( amx ) )
	        {
	            old = oldTemp;
	            debug( "resyncAMXMBean: found matching delegate MBean: " + old );
	            break;
	        }
	    }
	    
	    if ( old == null )
	    {
	        throw new IllegalArgumentException( "" + amx );
	    }
	    
	    debug( "resyncAMXMBean: removing mapping from: " + old + " TO " + amx );
        mOldToNewObjectNames.remove( old );
	    debug( "resyncAMXMBean: unregistering: " + amx );
        getMBeanServer().unregisterMBean( amx );
	    debug( "resyncAMXMBean: handleMBeanRegistered: " + amx );
        handleMBeanRegistered( old );
	        
	    final ObjectName    newAMX = mOldToNewObjectNames.get( old );
	    assert( newAMX != null );
	    
	    debug( "resyncAMXMBean: new ObjectName: " + newAMX );
	    return newAMX;
	
private booleanshouldSync(javax.management.ObjectName oldObjectName)

        final String  jmxDomain   = oldObjectName.getDomain();
        final boolean applicable  = ObjectNames.kDefaultIASDomainName.equals( jmxDomain );
        
		return applicable ? (findLoaderOfOld( oldObjectName ) != null) : false;
	
protected voidstartHook()

	    super.startHook();
	    
	    addLoaders();
		queueAll();
		mQueuedAll	= true;
	
public final javax.management.ObjectNamesync(javax.management.ObjectName oldObjectName)

    
		   
	  	 
	
		if ( ! mStarted )
		{
			throw new IllegalStateException();
		}
        
		if ( ! shouldSync( oldObjectName ) )
		{
			throw new IllegalArgumentException( oldObjectName.toString() );
		}
        
        ObjectName  result = null;
        /*
            Synchronize for config; ConfigFactory can call sync() explicitly, and there
            is a race condition that can attempt duplicate registration with the
            one to be done or already done or in progress by handleMBeanRegistered().
         */
        final String category = oldObjectName.getKeyProperty( "category" );
        if ( category != null && category.equals( "config" ) )
        {
            synchronized( CONFIG_SYNC )
            {
               result = _sync( oldObjectName );
            }
        }
        else
        {
             result = _sync( oldObjectName );
        }
        return result;
    
public final voidwaitAll()

        mRegThread.waitAll();
        debug( "waitAll() DONE" );