You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/11/11 02:55:47 UTC

cvs commit: jakarta-avalon-excalibur/instrument-manager/src/java/org/apache/excalibur/instrument/manager AbstractInstrumentSample.java

donaldp     2002/11/10 17:55:47

  Modified:    instrument-manager/src/java/org/apache/excalibur/instrument/manager
                        AbstractInstrumentSample.java
  Log:
  clean imports
  
  Revision  Changes    Path
  1.3       +162 -162  jakarta-avalon-excalibur/instrument-manager/src/java/org/apache/excalibur/instrument/manager/AbstractInstrumentSample.java
  
  Index: AbstractInstrumentSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/instrument-manager/src/java/org/apache/excalibur/instrument/manager/AbstractInstrumentSample.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractInstrumentSample.java	6 Sep 2002 02:10:12 -0000	1.2
  +++ AbstractInstrumentSample.java	11 Nov 2002 01:55:47 -0000	1.3
  @@ -7,17 +7,13 @@
    */
   package org.apache.excalibur.instrument.manager;
   
  -import java.util.ArrayList;
  -import java.util.Arrays;
   import java.util.StringTokenizer;
  -
  -import org.apache.excalibur.instrument.manager.interfaces.InstrumentSampleSnapshot;
  -import org.apache.excalibur.instrument.manager.interfaces.InstrumentSampleUtils;
  -
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.avalon.framework.configuration.DefaultConfiguration;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  +import org.apache.excalibur.instrument.manager.interfaces.InstrumentSampleSnapshot;
  +import org.apache.excalibur.instrument.manager.interfaces.InstrumentSampleUtils;
   
   /**
    * An AbstractInstrumentSample contains all of the functionality common to all
  @@ -33,55 +29,55 @@
   {
       /** The InstrumentProxy which owns the InstrumentSample. */
       private InstrumentProxy m_instrumentProxy;
  -    
  +
       /** Configured flag. */
       private boolean m_configured;
  -    
  +
       /** The name of the new InstrumentSample. */
       private String m_name;
  -    
  +
       /** The sample interval of the new InstrumentSample. */
       private long m_interval;
  -    
  +
       /** The number of samples to store as history. */
       private int m_size;
  -    
  +
       /** The description of the new InstrumentSample. */
       private String m_description;
  -    
  +
       /** The Descriptor for the InstrumentSample. */
       private InstrumentSampleDescriptorLocal m_descriptor;
  -    
  -    /** 
  +
  +    /**
        * The maximum amount of time between updates before history will be
        * wiped clean.
        */
       private long m_maxAge;
  -    
  +
       /** The UNIX time of the beginning of the sample. */
       protected long m_time;
  -    
  +
       /** The time that the current lease expires. */
       private long m_leaseExpirationTime;
  -    
  +
       /** True if the lease has expired. */
       private boolean m_expired;
  -    
  +
       /** The Index into the history arrays. */
       private int m_historyIndex;
  -    
  +
       /** The Old half of the history array. */
       private int[] m_historyOld;
  -    
  +
       /** The New half of the history array. */
       private int[] m_historyNew;
  -    
  +
       /** Array of registered InstrumentSampleListeners. */
       private InstrumentSampleListener[] m_listeners;
  -    
  +
       /** State Version. */
       private int m_stateVersion;
  -    
  +
       /*---------------------------------------------------------------
        * Constructors
        *-------------------------------------------------------------*/
  @@ -105,21 +101,21 @@
                                           long lease )
       {
           m_instrumentProxy = instrumentProxy;
  -        
  -        if ( interval < 1 )
  +
  +        if( interval < 1 )
           {
               throw new IllegalArgumentException( "interval must be at least 1." );
           }
  -        if ( size < 1 )
  +        if( size < 1 )
           {
               throw new IllegalArgumentException( "size must be at least 1." );
           }
  -        
  +
           m_name = name;
           m_interval = interval;
           m_size = size;
           m_description = description;
  -        if ( lease > 0 )
  +        if( lease > 0 )
           {
               m_leaseExpirationTime = System.currentTimeMillis() + lease;
           }
  @@ -128,16 +124,16 @@
               // Permanent lease.
               m_leaseExpirationTime = 0;
           }
  -        
  +
           // Calculate the maxAge
           m_maxAge = m_size * m_interval;
  -        
  +
           init();
  -        
  +
           // Create the descriptor
           m_descriptor = new InstrumentSampleDescriptorLocalImpl( this );
       }
  -    
  +
       /*---------------------------------------------------------------
        * InstrumentSample Methods
        *-------------------------------------------------------------*/
  @@ -150,7 +146,7 @@
       {
           return m_instrumentProxy;
       }
  -    
  +
       /**
        * Returns true if the Instrument was configured in the instrumentables
        *  section of the configuration.
  @@ -161,7 +157,7 @@
       {
           return m_configured;
       }
  -    
  +
       /**
        * Returns the name of the sample.
        *
  @@ -171,7 +167,7 @@
       {
           return m_name;
       }
  -    
  +
       /**
        * Returns the sample interval.  The period of each sample in millisends.
        *
  @@ -181,7 +177,7 @@
       {
           return m_interval;
       }
  -    
  +
       /**
        * Returns the number of samples in the sample history.
        *
  @@ -191,7 +187,7 @@
       {
           return m_size;
       }
  -    
  +
       /**
        * Returns the description of the sample.
        *
  @@ -201,7 +197,7 @@
       {
           return m_description;
       }
  -    
  +
       /**
        * Returns a Descriptor for the InstrumentSample.
        *
  @@ -211,7 +207,7 @@
       {
           return m_descriptor;
       }
  -    
  +
       /**
        * Obtain the value of the sample.  All samples are integers, so the profiled
        * objects must measure quantity (numbers of items), rate (items/period), time in
  @@ -224,22 +220,22 @@
           boolean update;
           int value;
           long time;
  -        
  -        synchronized(this)
  +
  +        synchronized( this )
           {
               long now = System.currentTimeMillis();
               update = update( now );
               value = getValueInner();
               time = m_time;
           }
  -        
  -        if ( update )
  +
  +        if( update )
           {
               updateListeners( value, time );
           }
           return value;
       }
  -    
  +
       /**
        * Obtain the UNIX time of the beginning of the sample.
        *
  @@ -250,22 +246,22 @@
           boolean update;
           int value;
           long time;
  -        
  -        synchronized(this)
  +
  +        synchronized( this )
           {
               long now = System.currentTimeMillis();
               update = update( now );
               value = getValueInner();
               time = m_time;
           }
  -        
  -        if ( update )
  +
  +        if( update )
           {
               updateListeners( value, time );
           }
           return time;
       }
  -    
  +
       /**
        * Returns the time that the current lease expires.  Permanent samples will
        *  return a value of 0.
  @@ -276,7 +272,7 @@
       {
           return m_leaseExpirationTime;
       }
  -    
  +
       /**
        * Extends the lease to be lease milliseconds from the current time.
        *  Ignored if the lease has already expired.
  @@ -288,23 +284,23 @@
        */
       public long extendLease( long lease )
       {
  -        synchronized(this)
  +        synchronized( this )
           {
               // Only extend the lease if it is not permanent.
  -            if ( ( m_leaseExpirationTime > 0 ) && ( !m_expired ) )
  +            if( ( m_leaseExpirationTime > 0 ) && ( !m_expired ) )
               {
                   long newLeaseExpirationTime = System.currentTimeMillis() + lease;
  -                if ( newLeaseExpirationTime > m_leaseExpirationTime )
  +                if( newLeaseExpirationTime > m_leaseExpirationTime )
                   {
                       m_leaseExpirationTime = newLeaseExpirationTime;
                       stateChanged();
                   }
               }
  -            
  +
               return m_leaseExpirationTime;
           }
       }
  -    
  +
       /**
        * Tells the sample that its lease has expired.  No new references to
        *  the sample will be made available, but clients which already have
  @@ -314,10 +310,10 @@
       {
           // Update to the time that we expire at.
           update( m_leaseExpirationTime );
  -        
  +
           m_expired = true;
       }
  -    
  +
       /**
        * Obtains a static snapshot of the InstrumentSample.
        *
  @@ -325,11 +321,11 @@
        */
       public final InstrumentSampleSnapshot getSnapshot()
       {
  -        synchronized(this)
  +        synchronized( this )
           {
               long time = System.currentTimeMillis();
               update( time );
  -            
  +
               return new InstrumentSampleSnapshot(
                   m_name,
                   m_interval,
  @@ -339,7 +335,7 @@
                   m_stateVersion );
           }
       }
  -    
  +
       /**
        * Returns the stateVersion of the sample.  The state version will be
        *  incremented each time any of the configuration of the sample is
  @@ -353,7 +349,7 @@
       {
           return m_stateVersion;
       }
  -    
  +
       /**
        * Registers a InstrumentSampleListener with a InstrumentSample given a name.
        *
  @@ -362,13 +358,13 @@
        */
       public void addInstrumentSampleListener( InstrumentSampleListener listener )
       {
  -        if ( getLogger().isDebugEnabled() )
  +        if( getLogger().isDebugEnabled() )
           {
               getLogger().debug( "A InstrumentSampleListener was added to sample, " + m_name + " : " +
  -                listener.getClass().getName() );
  +                               listener.getClass().getName() );
           }
  -        
  -        synchronized(this)
  +
  +        synchronized( this )
           {
               // Store the listeners in an array.  This makes it possible to
               //  avoid synchronization while propagating events.  Never change
  @@ -376,9 +372,9 @@
               //  m_listeners field.
               InstrumentSampleListener[] oldListeners = m_listeners;
               InstrumentSampleListener[] newListeners;
  -            if ( oldListeners == null )
  +            if( oldListeners == null )
               {
  -                newListeners = new InstrumentSampleListener[] { listener };
  +                newListeners = new InstrumentSampleListener[]{listener};
               }
               else
               {
  @@ -386,12 +382,12 @@
                   System.arraycopy( oldListeners, 0, newListeners, 0, oldListeners.length );
                   newListeners[ oldListeners.length ] = listener;
               }
  -            
  +
               // Update the m_listeners field.
               m_listeners = newListeners;
           }
       }
  -    
  +
       /**
        * Unregisters a InstrumentSampleListener from a InstrumentSample given a name.
        *
  @@ -400,13 +396,13 @@
        */
       public void removeInstrumentSampleListener( InstrumentSampleListener listener )
       {
  -        if ( getLogger().isDebugEnabled() )
  +        if( getLogger().isDebugEnabled() )
           {
               getLogger().debug( "A InstrumentSampleListener was removed from sample, " + m_name +
  -                " : " + listener.getClass().getName() );
  +                               " : " + listener.getClass().getName() );
           }
  -        
  -        synchronized(this)
  +
  +        synchronized( this )
           {
               // Store the listeners in an array.  This makes it possible to
               //  avoid synchronization while propagating events.  Never change
  @@ -414,15 +410,15 @@
               //  m_listeners field.
               InstrumentSampleListener[] oldListeners = m_listeners;
               InstrumentSampleListener[] newListeners;
  -            if ( oldListeners == null )
  +            if( oldListeners == null )
               {
                   // Means that this method should not have been called, but
                   //  don't throw an error.
                   newListeners = null;
               }
  -            else if ( oldListeners.length == 1 )
  +            else if( oldListeners.length == 1 )
               {
  -                if ( oldListeners[0] == listener )
  +                if( oldListeners[ 0 ] == listener )
                   {
                       newListeners = null;
                   }
  @@ -436,16 +432,16 @@
               {
                   // Look for the listener in the array.
                   int pos = -1;
  -                for ( int i = 0; i < oldListeners.length; i++ )
  +                for( int i = 0; i < oldListeners.length; i++ )
                   {
  -                    if ( oldListeners[i] == listener )
  +                    if( oldListeners[ i ] == listener )
                       {
                           pos = i;
                           break;
                       }
                   }
  -                
  -                if ( pos < 0 )
  +
  +                if( pos < 0 )
                   {
                       // The listener was not in the list.
                       newListeners = oldListeners;
  @@ -453,25 +449,25 @@
                   else
                   {
                       newListeners = new InstrumentSampleListener[ oldListeners.length - 1 ];
  -                    if ( pos > 0 )
  +                    if( pos > 0 )
                       {
                           // Copy the head of the array
                           System.arraycopy( oldListeners, 0, newListeners, 0, pos );
                       }
  -                    if ( pos < oldListeners.length - 1 )
  +                    if( pos < oldListeners.length - 1 )
                       {
                           // Copy the tail of the array
  -                        System.arraycopy( oldListeners, pos + 1, 
  -                            newListeners, pos, oldListeners.length - 1 - pos );
  +                        System.arraycopy( oldListeners, pos + 1,
  +                                          newListeners, pos, oldListeners.length - 1 - pos );
                       }
                   }
               }
  -            
  +
               // Update the m_listeners field.
               m_listeners = newListeners;
           }
       }
  -    
  +
       /**
        * Notifies any listeners of a change.
        * <p>
  @@ -484,15 +480,15 @@
       {
           // Get a local reference to the listeners, so that synchronization can be avoided.
           InstrumentSampleListener[] listeners = m_listeners;
  -        if ( listeners != null )
  +        if( listeners != null )
           {
  -            for ( int i = 0; i < listeners.length; i++ )
  +            for( int i = 0; i < listeners.length; i++ )
               {
  -                listeners[i].setValue( getName(), value, time );
  +                listeners[ i ].setValue( getName(), value, time );
               }
           }
       }
  -    
  +
       /**
        * Saves the current state into a Configuration.
        *
  @@ -503,48 +499,48 @@
       {
           // If this sample is not configured and its lease time is 0, then it
           //  is an artifact of a previous state file, so it should not be saved.
  -        if ( ( !isConfigured() ) && ( getLeaseExpirationTime() <= 0 ) )
  +        if( ( !isConfigured() ) && ( getLeaseExpirationTime() <= 0 ) )
           {
               return null;
           }
  -        
  -        synchronized(this)
  +
  +        synchronized( this )
           {
               DefaultConfiguration state = new DefaultConfiguration( "sample", "-" );
               state.setAttribute( "type",
  -                InstrumentSampleUtils.getInstrumentSampleTypeName( getType() ) );
  +                                InstrumentSampleUtils.getInstrumentSampleTypeName( getType() ) );
               state.setAttribute( "interval", Long.toString( m_interval ) );
               state.setAttribute( "size", Integer.toString( m_size ) );
  -            
  +
               state.setAttribute( "time", Long.toString( m_time ) );
  -            if ( getLeaseExpirationTime() > 0 )
  +            if( getLeaseExpirationTime() > 0 )
               {
                   state.setAttribute( "lease-expiration", Long.toString( getLeaseExpirationTime() ) );
                   state.setAttribute( "description", m_description );
               }
  -            
  +
               // Save the history samples so that the newest is first.
               DefaultConfiguration samples = new DefaultConfiguration( "history", "-" );
               int[] history = getHistorySnapshot();
  -            
  +
               // Build up a string of the sample points.
               StringBuffer sb = new StringBuffer();
               // Store the first value outside the loop to simplify the loop.
               sb.append( history[ history.length - 1 ] );
  -            for ( int i = history.length - 2; i >= 0; i-- )
  +            for( int i = history.length - 2; i >= 0; i-- )
               {
                   sb.append( ',' );
                   sb.append( history[ i ] );
               }
               samples.setValue( sb.toString() );
               state.addChild( samples );
  -            
  +
               saveState( state );
  -            
  +
               return state;
           }
       }
  -    
  +
       /**
        * Loads the state into the InstrumentSample.
        *
  @@ -559,47 +555,47 @@
           {
               // Set the time
               long savedTime = m_time = state.getAttributeAsLong( "time" );
  -            
  +
               // Load the lease expiration time
               m_leaseExpirationTime = state.getAttributeAsLong( "lease-expiration", 0 );
  -            
  +
               // Set the history index.
               m_historyIndex = 0;
  -            
  +
               // Read in the samples, don't trust that the number will be correct.
               //  First sample is the current value, following sames go back in
               //   time from newest to oldest.
               Configuration history = state.getChild( "history" );
  -            
  +
               String compactSamples = history.getValue();
  -            
  +
               // Sample values are stored in newest to oldest order.
               StringTokenizer st = new StringTokenizer( compactSamples, "," );
  -            int[] sampleValues = new int[st.countTokens()];
  -            
  -            for ( int i = 0; i < sampleValues.length; i++ )
  +            int[] sampleValues = new int[ st.countTokens() ];
  +
  +            for( int i = 0; i < sampleValues.length; i++ )
               {
                   try
                   {
  -                    sampleValues[i] = Integer.parseInt( st.nextToken() );
  +                    sampleValues[ i ] = Integer.parseInt( st.nextToken() );
                   }
  -                catch ( NumberFormatException e )
  +                catch( NumberFormatException e )
                   {
                       throw new ConfigurationException( "The compact sample data could not be " +
  -                        "loaded, because of a number format problem, for InstrumentSample: " +
  -                        m_name ); 
  +                                                      "loaded, because of a number format problem, for InstrumentSample: " +
  +                                                      m_name );
                   }
               }
  -            
  +
               // Get the current value
               int value;
  -            if ( sampleValues.length > 0 )
  +            if( sampleValues.length > 0 )
               {
  -                value = sampleValues[0];
  -                
  -                for ( int i = 0; i < m_size - 1; i++ )
  +                value = sampleValues[ 0 ];
  +
  +                for( int i = 0; i < m_size - 1; i++ )
                   {
  -                    if ( i < sampleValues.length - 1 )
  +                    if( i < sampleValues.length - 1 )
                       {
                           m_historyOld[ m_size - 2 - i ] = sampleValues[ i + 1 ];
                       }
  @@ -613,18 +609,18 @@
               {
                   value = 0;
               }
  -            
  +
               loadState( value, state );
   
  -            if ( calculateSampleTime( System.currentTimeMillis() ) > savedTime )
  +            if( calculateSampleTime( System.currentTimeMillis() ) > savedTime )
               {
                   // The sample period changed since the save.
                   //  This will usually happen, but not always for long
                   //  intervals.
                   postSaveNeedsReset();
               }
  -            
  -            if ( m_leaseExpirationTime > 0 )
  +
  +            if( m_leaseExpirationTime > 0 )
               {
                   // This is a sample that was leased in a previous JVM invocation
                   //  and needs to be registered with the InstrumentManager
  @@ -632,10 +628,10 @@
                       registerLeasedInstrumentSample( this );
               }
           }
  -        
  +
           stateChanged();
       }
  -    
  +
       /*---------------------------------------------------------------
        * Methods
        *-------------------------------------------------------------*/
  @@ -646,7 +642,7 @@
       {
           m_configured = true;
       }
  -    
  +
       /**
        * Initializes the sample
        */
  @@ -656,7 +652,7 @@
           //  value of the current time. This will allign the intervals to the start of computer
           //  time.
           m_time = calculateSampleTime( System.currentTimeMillis() );
  -        
  +
           // Create the arrays which will hold the history points.
           // History is build with m_value holding the current value and all previous values
           // spanning accross 2 arrays that switch places as time progresses.  This completely
  @@ -666,14 +662,16 @@
           m_historyOld = new int[ m_size - 1 ];
           m_historyNew = new int[ m_size - 1 ];
       }
  -    
  +
       /**
        * Allow subclasses to add information into the saved state.
        *
        * @param state State configuration.
        */
  -    protected void saveState( DefaultConfiguration state ) {}
  -    
  +    protected void saveState( DefaultConfiguration state )
  +    {
  +    }
  +
       /**
        * Used to load the state, called from AbstractInstrumentSample.loadState();
        * <p>
  @@ -687,13 +685,13 @@
        */
       protected abstract void loadState( int value, Configuration state )
           throws ConfigurationException;
  -    
  +
       /**
        * Called after a state is loaded if the sample period is not the same
        *  as the last period saved.
        */
       protected abstract void postSaveNeedsReset();
  -    
  +
       /**
        * Calculates the time of the sample which contains the specified time.
        *
  @@ -703,7 +701,7 @@
       {
           return ( time / m_interval ) * m_interval;
       }
  -    
  +
       /**
        * Gets the current value.  Does not update.
        * <p>
  @@ -712,7 +710,7 @@
        * @return The current value.
        */
       protected abstract int getValueInner();
  -    
  +
       /**
        * The current sample has already been stored.  Reset the current sample
        *  and move on to the next.
  @@ -720,7 +718,7 @@
        * Should only be called when synchronized.
        */
       protected abstract void advanceToNextSample();
  -    
  +
       /**
        * Brings the InstrumentSample's time up to date so that a new value can be added.
        * <p>
  @@ -734,16 +732,16 @@
       {
           //System.out.println("update(" + time + ")");
           // If the lease has already expired, then do nothing
  -        if ( m_expired )
  +        if( m_expired )
           {
               return false;
           }
  -        
  +
           // See if we are already up to date.
  -        if ( time - m_time >= m_interval )
  +        if( time - m_time >= m_interval )
           {
               // Needs to move to a new sample.
  -            if ( time - m_time >= m_maxAge )
  +            if( time - m_time >= m_maxAge )
               {
                   // The history is too old, reset the sample.
                   advanceToNextSample();
  @@ -752,34 +750,36 @@
               else
               {
                   // Advance the history index.
  -                while ( time - m_time >= m_interval )
  +                while( time - m_time >= m_interval )
                   {
                       // Store the current value into the end of the history.
                       m_historyNew[ m_historyIndex ] = getValueInner();
  -                    
  +
                       // Advance to the next sample.
                       m_time += m_interval;
                       advanceToNextSample();
                       m_historyIndex++;
  -                    
  -                    if ( m_historyIndex >= m_size - 1 )
  +
  +                    if( m_historyIndex >= m_size - 1 )
                       {
                           // Need to swap the history arrays
                           int[] tmp = m_historyOld;
                           m_historyOld = m_historyNew;
                           m_historyNew = tmp;
  -                        
  +
                           // Reset the history index
                           m_historyIndex = 0;
                       }
                   }
               }
               return true;
  -        } else {
  +        }
  +        else
  +        {
               return false;
           }
       }
  -    
  +
       /**
        * Gets a snapshot of the samples.
        * <p>
  @@ -792,42 +792,42 @@
           // Create a new array to hold the snapshot of the history data.
           // This method is a little slow but normal collection of sample points is
           // extremely fast.
  -        int[] history = new int[m_size];
  -        
  +        int[] history = new int[ m_size ];
  +
           int sizem1 = m_size - 1;
  -        
  -        if ( m_size > 1 )
  +
  +        if( m_size > 1 )
           {
               // Copy samples from the old history first.
  -            if ( m_historyIndex < sizem1 )
  +            if( m_historyIndex < sizem1 )
               {
                   // Copy the last (size - 1 - historyIndex) samples from the old history.
                   System.arraycopy( m_historyOld, m_historyIndex, history, 0, sizem1 - m_historyIndex );
               }
  -            
  -            if ( m_historyIndex > 0 )
  +
  +            if( m_historyIndex > 0 )
               {
                   // Copy the first (historyIndex) samples from the new history.
                   System.arraycopy( m_historyNew, 0, history, sizem1 - m_historyIndex, m_historyIndex );
               }
           }
           // Get the final sample from the current sample value.
  -        history[ m_size - 1] = getValueInner();
  -        
  +        history[ m_size - 1 ] = getValueInner();
  +
           return history;
       }
  -    
  +
       /**
        * Called whenever the state of the sample is changed.
        */
       protected void stateChanged()
       {
           m_stateVersion++;
  -        
  +
           // Propagate to the parent
           m_instrumentProxy.stateChanged();
       }
  -    
  +
       /**
        * Returns a string representation of the sample.
        *
  @@ -835,8 +835,8 @@
        */
       public String toString()
       {
  -        return "InstrumentSample[name=" + m_name + ", type=" + 
  -            InstrumentSampleUtils.getInstrumentSampleTypeName( getType() ) + ", interval=" + 
  +        return "InstrumentSample[name=" + m_name + ", type=" +
  +            InstrumentSampleUtils.getInstrumentSampleTypeName( getType() ) + ", interval=" +
               m_interval + ", size=" + m_size + ", lease=" + m_leaseExpirationTime + "]";
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>