avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/util ComponentStateValidator.java
Date Fri, 08 Feb 2002 08:16:20 GMT
leif        02/02/08 00:16:20

  Modified:    src/scratchpad/org/apache/avalon/excalibur/util
                        ComponentStateValidator.java
  Log:
  Rewrote the ComponentStateValidator to display error messages
  which give the exact problem when a component is misused.
  
  Revision  Changes    Path
  1.8       +529 -253  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/util/ComponentStateValidator.java
  
  Index: ComponentStateValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/util/ComponentStateValidator.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ComponentStateValidator.java	17 Jan 2002 15:02:01 -0000	1.7
  +++ ComponentStateValidator.java	8 Feb 2002 08:16:20 -0000	1.8
  @@ -12,8 +12,11 @@
   import org.apache.avalon.framework.activity.Startable;
   import org.apache.avalon.framework.activity.Suspendable;
   import org.apache.avalon.framework.component.Composable;
  +import org.apache.avalon.framework.component.Recomposable;
   import org.apache.avalon.framework.configuration.Configurable;
  +import org.apache.avalon.framework.configuration.Reconfigurable;
   import org.apache.avalon.framework.context.Contextualizable;
  +import org.apache.avalon.framework.context.Recontextualizable;
   import org.apache.avalon.framework.logger.LogEnabled;
   import org.apache.avalon.framework.logger.Loggable;
   import org.apache.avalon.framework.parameters.Parameterizable;
  @@ -22,51 +25,64 @@
    * This class provides basic facilities for enforcing Avalon's contracts
    * within your own code.
    *
  + * Based on Avalon version from Sandbox.
  + *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author <a href="mailto:mike@hihat.net">Michael McKibben</a>
  - * @version CVS $Revision: 1.7 $ $Date: 2002/01/17 15:02:01 $
  + * @author <a href="mailto:leif@silveregg.co.jp">Michael McKibben</a>
  + * @version CVS $Revision: 1.8 $ $Date: 2002/02/08 08:16:20 $
    */
   public final class ComponentStateValidator
   {
  -    private static final String OOO_FAIL       = "Initialization out of order";
  -    private static final String LOG_FAIL       = OOO_FAIL + ": LogEnabled";
  -    private static final String CONTEXT_FAIL   = OOO_FAIL + ": Contextualizable";
  -    private static final String PARAMETER_FAIL = OOO_FAIL + ": Parameterizable";
  -    private static final String CONFIGURE_FAIL = OOO_FAIL + ": Configurable";
  -    private static final String COMPOSE_FAIL   = OOO_FAIL + ": Composable";
  -    private static final String INIT_FAIL      = OOO_FAIL + ": Initializable";
  -    private static final String START_FAIL     = OOO_FAIL + ": Startable.start()";
  -    private static final String STOP_FAIL      = OOO_FAIL + ": Startable.stop()";
  -    private static final String SUSPEND_FAIL   = OOO_FAIL + ": Suspendable.suspend()";
  -    private static final String RESUME_FAIL    = OOO_FAIL + ": Suspendable.resume()";
  -    private static final String DISPOSE_FAIL   = OOO_FAIL + ": Disposable";
  -    private static final String ACTIVE_FAIL    = "Component is not Active";
       private static final String WRITE_FAIL      = "Value is already bound";
   
  -    private static final long LOG_ENABLED    = 0x00000001;
  -    private static final long CONTEXTUALIZED = 0x00000002;
  -    private static final long COMPOSED       = 0x00000004;
  -    private static final long CONFIGURED     = 0x00000008;
  -    private static final long PARAMETERIZED  = 0x00000010;
  -    private static final long INITIALIZED    = 0x00000020;
  -    private static final long STARTED        = 0x00000040;
  -    private static final long ACTIVE         = 0x10000000;
  -    private static final long SUSPENDED      = 0x01000000;
  -    private static final long STOPPED        = 0x00000080;
  -    private static final long DISPOSED       = 0x00000100;
  -
  -    private static final long LOG_MASK       = LOG_ENABLED;
  -    private static final long CONTEXT_MASK   = LOG_MASK | CONTEXTUALIZED;
  -    private static final long COMPOSE_MASK   = CONTEXT_MASK | COMPOSED;
  -    private static final long CONFIGURE_MASK = COMPOSE_MASK | CONFIGURED;
  -    private static final long PARAMETER_MASK = CONFIGURE_MASK | PARAMETERIZED;
  -    private static final long INIT_MASK      = PARAMETER_MASK | INITIALIZED;
  -    private static final long START_MASK     = INIT_MASK | STARTED;
  -    private static final long STOP_MASK      = START_MASK | STOPPED;
  -    private static final long DISPOSE_MASK   = STOP_MASK | DISPOSED;
  -
  -    private final long m_mask;
  -    private long m_state;
  +    // Interfaces
  +    private static final long LOG_ENABLED        = 0x00000001;
  +    private static final long LOGGABLE           = 0x00000002;
  +    private static final long CONTEXTUALIZABLE   = 0x00000004;
  +    private static final long COMPOSABLE         = 0x00000008;
  +    private static final long CONFIGURABLE       = 0x00000010;
  +    private static final long PARAMETERIZABLE    = 0x00000020;
  +    private static final long INITIALIZABLE      = 0x00000040;
  +    private static final long STARTABLE          = 0x00000060;
  +    private static final long SUSPENDABLE        = 0x00001000;
  +    private static final long RECONTEXTUALIZABLE = 0x00002000;
  +    private static final long RECOMPOSABLE       = 0x00004000;
  +    private static final long RECONFIGURABLE     = 0x00008000;
  +    private static final long DISPOSABLE         = 0x00100000;
  +    
  +    // Initialization Methods.
  +    private static final long ENABLE_LOGGING   = 0x00000001;
  +    private static final long SET_LOGGER       = 0x00000002;
  +    private static final long CONTEXTUALIZE    = 0x00000004;
  +    private static final long COMPOSE          = 0x00000008;
  +    private static final long CONFIGURE        = 0x00000010;
  +    private static final long PARAMETERIZE     = 0x00000020;
  +    private static final long INITIALIZE       = 0x00000040;
  +    private static final long START            = 0x00000080;
  +    private static final long INIT_COMPLETE    = 0x00000400;
  +    
  +    // Active Service Methods
  +    private static final long SUSPEND          = 0x00001000;
  +    private static final long RECONTEXTUALIZE  = 0x00002000;
  +    private static final long RECOMPOSE        = 0x00004000;
  +    private static final long RECONFIGURE      = 0x00008000;
  +    private static final long RESUME           = 0x00010000;
  +    
  +    // Destruction Methods
  +    private static final long STOP             = 0x00100000;
  +    private static final long DISPOSE          = 0x00200000;
  +    
  +    // Masks
  +    private static final long INIT_MASK = ENABLE_LOGGING | SET_LOGGER | 
  +        CONTEXTUALIZE | COMPOSE | CONFIGURE | PARAMETERIZE | INITIALIZE | 
  +        START;
  +    
  +    private final long m_interfaces;
  +    private final long m_methods;
  +    private       long m_state;
  +    private       boolean m_active;
  +    private final Object m_object;
   
       /**
        * Create state validator from object (this can be used for more than just
  @@ -74,96 +90,462 @@
        */
       public ComponentStateValidator( final Object object )
       {
  -        int mask = 0;
  +        m_object = object;
  +        
  +        long methods = 0;
  +        long interfaces = 0;
   
  -        if ( object instanceof LogEnabled ||
  -             object instanceof Loggable )
  +        if ( object instanceof LogEnabled )
           {
  -            mask |= LOG_ENABLED;
  +            interfaces |= LOG_ENABLED;
  +            methods |= ENABLE_LOGGING;
           }
  -
  +        
  +        if ( object instanceof Loggable )
  +        {
  +            interfaces |= LOGGABLE;
  +            methods |= SET_LOGGER;
  +        }
  +        
           if ( object instanceof Contextualizable )
           {
  -            mask |= CONTEXTUALIZED;
  +            interfaces |= CONTEXTUALIZABLE;
  +            methods |= CONTEXTUALIZE;
           }
  -
  -        if ( object instanceof Parameterizable )
  +        
  +        if ( object instanceof Composable )
           {
  -            mask |= PARAMETERIZED;
  +            interfaces |= COMPOSABLE;
  +            methods |= COMPOSE;
           }
  -
  +        
           if ( object instanceof Configurable )
           {
  -            mask |= CONFIGURED;
  +            interfaces |= CONFIGURABLE;
  +            methods |= CONFIGURE;
           }
  -
  -        if ( object instanceof Composable )
  +        
  +        if ( object instanceof Parameterizable )
           {
  -            mask |= COMPOSED;
  +            interfaces |= PARAMETERIZABLE;
  +            methods |= PARAMETERIZE;
           }
  -
  +        
           if ( object instanceof Initializable )
           {
  -            mask |= INITIALIZED;
  +            interfaces |= INITIALIZABLE;
  +            methods |= INITIALIZE;
           }
  -
  +        
  +        if ( object instanceof Startable )
  +        {
  +            interfaces |= STARTABLE;
  +            methods |= START | STOP;
  +        }
  +        
  +        if ( object instanceof Suspendable )
  +        {
  +            interfaces |= SUSPENDABLE;
  +            methods |= SUSPEND | RESUME;
  +        }
  +        
  +        if ( object instanceof Recontextualizable )
  +        {
  +            interfaces |= RECONTEXTUALIZABLE;
  +            methods |= RECONTEXTUALIZE;
  +        }
  +        
  +        if ( object instanceof Recomposable )
  +        {
  +            interfaces |= RECOMPOSABLE;
  +            methods |= RECOMPOSE;
  +        }
  +        
  +        if ( object instanceof Reconfigurable )
  +        {
  +            interfaces |= RECONFIGURABLE;
  +            methods |= RECONFIGURE;
  +        }
  +        
           if ( object instanceof Disposable )
           {
  -            mask |= DISPOSED;
  +            interfaces |= DISPOSABLE;
  +            methods |= DISPOSE;
           }
  -
  -        if ( object instanceof Startable )
  +        
  +        m_methods = methods;
  +        m_interfaces = interfaces;
  +        
  +        generalCheckInitComplete();
  +    }
  +    
  +    private String getInterfaceName( long interfaceId )
  +    {
  +        if ( interfaceId ==  LOG_ENABLED )
           {
  -            mask |= STARTED | STOPPED;
  +            return LogEnabled.class.getName();
           }
  -
  -        if ( object instanceof Suspendable )
  +        else if ( interfaceId == LOGGABLE )
           {
  -            mask |= SUSPENDED;
  +            return Loggable.class.getName();
  +        }
  +        else if ( interfaceId == CONTEXTUALIZABLE )
  +        {
  +            return Contextualizable.class.getName();
  +        }
  +        else if ( interfaceId == COMPOSABLE )
  +        {
  +            return Composable.class.getName();
  +        }
  +        else if ( interfaceId == CONFIGURABLE )
  +        {
  +            return Configurable.class.getName();
  +        }
  +        else if ( interfaceId == PARAMETERIZABLE )
  +        {
  +            return Parameterizable.class.getName();
  +        }
  +        else if ( interfaceId == INITIALIZABLE )
  +        {
  +            return Initializable.class.getName();
  +        }
  +        else if ( interfaceId == STARTABLE )
  +        {
  +            return Startable.class.getName();
  +        }
  +        else if ( interfaceId == SUSPENDABLE )
  +        {
  +            return Suspendable.class.getName();
  +        }
  +        else if ( interfaceId == RECONTEXTUALIZABLE )
  +        {
  +            return Recontextualizable.class.getName();
  +        }
  +        else if ( interfaceId == RECOMPOSABLE )
  +        {
  +            return Recomposable.class.getName();
  +        }
  +        else if ( interfaceId == RECONFIGURABLE )
  +        {
  +            return Reconfigurable.class.getName();
  +        }
  +        else if ( interfaceId == DISPOSABLE )
  +        {
  +            return Disposable.class.getName();
  +        }
  +        else
  +        {
  +            throw new IllegalStateException("Unknown Interface Id " + interfaceId);
           }
  -
  -        m_mask = mask & ~ACTIVE;
  +    }
  +    
  +    private String getMethodName( long methodId )
  +    {
  +        if ( methodId == ENABLE_LOGGING )
  +        {
  +            return "enableLogging()";
  +        }
  +        else if ( methodId == SET_LOGGER )
  +        {
  +            return "setLogger()";
  +        }
  +        else if ( methodId == CONTEXTUALIZE )
  +        {
  +            return "contextualize()";
  +        }
  +        else if ( methodId == COMPOSE )
  +        {
  +            return "compose()";
  +        }
  +        else if ( methodId == CONFIGURE )
  +        {
  +            return "configure()";
  +        }
  +        else if ( methodId == PARAMETERIZE )
  +        {
  +            return "parameterize()";
  +        }
  +        else if ( methodId == INITIALIZE )
  +        {
  +            return "initialize()";
  +        }
  +        else if ( methodId == START )
  +        {
  +            return "start()";
  +        }
  +        else if ( methodId == SUSPEND )
  +        {
  +            return "suspend()";
  +        }
  +        else if ( methodId == RECONTEXTUALIZE )
  +        {
  +            return "recontextualize()";
  +        }
  +        else if ( methodId == RECOMPOSE )
  +        {
  +            return "recompose()";
  +        }
  +        else if ( methodId == RECONFIGURE )
  +        {
  +            return "reconfigure()";
  +        }
  +        else if ( methodId == RESUME )
  +        {
  +            return "resume()";
  +        }
  +        else if ( methodId == STOP )
  +        {
  +            return "stop()";
  +        }
  +        else if ( methodId == DISPOSE )
  +        {
  +            return "dispose()";
  +        }
  +        else
  +        {
  +            throw new IllegalStateException("Unknown Method Id " + methodId);
  +        }
  +    }
  +    
  +    private String getLastMethod( long state ) {
  +        for ( int i = 31; i >= 0; i-- )
  +        {
  +            long methodId = 0x1 << i;
  +            if ( ( state & methodId ) != 0 )
  +            {
  +                return getMethodName(methodId);
  +            }
  +        }
  +        throw new IllegalStateException("No last state method found for state " + state);
  +    }
  +    
  +    /**
  +     * Test to see if this was the last initialization method.
  +     */
  +    private void generalCheckInitComplete() {
  +        if ( m_state == ( m_methods & INIT_MASK ) )
  +        {
  +            // All init methods called
  +            m_active = true;
  +        }
  +    }
  +    
  +    /**
  +     * Initialization methods must be called in order, must all be called, may
  +     *  not be called more than once, and may not be called once any of the
  +     *  Descruction methods have been called.
  +     */
  +    private void generalCheckInit( final String message, final long interfaceId, final
long methodId )
  +    {
  +        if ( ( m_interfaces & interfaceId ) == 0 )
  +        {
  +            // Interface not implemented
  +            if ( message == null)
  +            {
  +                throw new IllegalStateException( m_object.getClass().getName() + 
  +                    " does not implement " + getInterfaceName( interfaceId ) + "." );
  +            }
  +            else
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        }
  +        else if ( ( m_state & methodId ) > 0 )
  +        {
  +            // Method already called.
  +            if ( message == null )
  +            {
  +                throw new IllegalStateException( getMethodName( methodId ) + " already
called." );
  +            }
  +            else 
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        }
  +        else if ( m_state > methodId )
  +        {
  +            // Method called after a descruction method was called.
  +            if ( message == null )
  +            {
  +                throw new IllegalStateException( getMethodName( methodId ) + 
  +                    " can not be called after " + getLastMethod( m_state ) + "." );
  +            }
  +            else 
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        }
  +        else if ( ( m_state & ( methodId - 1 ) ) != ( m_methods & ( methodId -
1 ) ) )
  +        {
  +            // One or more of the methods that should have been called before
  +            //  this method was not.
  +            if ( message == null )
  +            {
  +                throw new IllegalStateException( getMethodName( methodId ) + 
  +                    " called out of order. " + getLastMethod( m_methods & ( methodId
- 1 ) ) +
  +                    " must be called first." );
  +            }
  +            else 
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        }
  +        
  +        // Add this method to the state
  +        m_state |= methodId;
  +        
  +        // See if the initialization is complete.
  +        generalCheckInitComplete();
       }
   
       /**
  +     * Active Service methods may only be called after all of the
  +     *  Initialization methods have been called, any before any of the
  +     *  Descruction methods have been called.  While in the active state,
  +     *  the contracts of the methods allow the active state methods to be
  +     *  called any number of times, in any order.
  +     * The resume() method should do nothing if suspend() has not yet been
  +     *  called for example.
  +     */
  +    private void generalCheckActive( final String message, final long interfaceId, final
long methodId )
  +    {
  +        if ( ( m_interfaces & interfaceId ) == 0 )
  +        {
  +            // Interface not implemented
  +            if ( message == null)
  +            {
  +                throw new IllegalStateException( m_object.getClass().getName() + 
  +                    " does not implement " + getInterfaceName( interfaceId ) + "." );
  +            }
  +            else
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        }
  +        else if ( !m_active )
  +        {
  +            // Component not in the active state.
  +            if ( m_state < INIT_COMPLETE )
  +            {
  +                // Still expecting initialization methods.
  +                if ( message == null )
  +                {
  +                    throw new IllegalStateException( getMethodName( methodId ) + 
  +                        " called before component was made active. " + 
  +                        getLastMethod( m_methods & ( INIT_COMPLETE - 1 ) ) +
  +                        " must be called first." );
  +                }
  +                else 
  +                {
  +                    throw new IllegalStateException( message );
  +                }
  +            } else {
  +                // One or more destruction methods have been called.
  +                if ( message == null )
  +                {
  +                    throw new IllegalStateException( getMethodName( methodId ) + 
  +                        " called after component was made inactive.  Cannot call after
" + 
  +                        getLastMethod( m_state ) + "." );
  +                }
  +                else 
  +                {
  +                    throw new IllegalStateException( message );
  +                }
  +            }
  +        } 
  +    }
  +    
  +    /**
  +     * Descruction Methods must be called in order.  They may be called before
  +     *  all of the Initialization methods have been called if there was an
  +     *  error.
  +     */
  +    private void generalCheckDest( final String message, final long interfaceId, final
long methodId )
  +    {
  +        if ( ( m_interfaces & interfaceId ) == 0 )
  +        {
  +            // Interface not implemented
  +            if ( message == null)
  +            {
  +                throw new IllegalStateException( m_object.getClass().getName() + 
  +                    " does not implement " + getInterfaceName( interfaceId ) + "." );
  +            }
  +            else
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        }
  +        else if ( m_state > methodId )
  +        {
  +            // Method called after a later descruction method was called.
  +            if ( message == null )
  +            {
  +                throw new IllegalStateException( getMethodName( methodId ) + 
  +                    " can not be called after " + getLastMethod( m_state ) + "." );
  +            }
  +            else 
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        }
  +        
  +        // Add this method to the state
  +        m_state |= methodId;
  +        
  +        // Deactivate
  +        m_active = false;
  +    }
  +    
  +    
  +    
  +    /**
        * Throw an exception if the initialization is out of order.  It tests to see
  -     * if the LOG_ENABLED state has already been set, if the component implements
  -     * LogEnabled or Logger, and if the state has progressed beyond the Logger
  -     * stage.
  +     * if the ENABLE_LOGGING state has already been set, if the component implements
  +     * LogEnabled, and if the state has progressed beyond the Logger stage.
        *
        * @throws IllegalStateException if the state is manage out of order
        */
       public void checkLogEnabled()
       {
  -        checkLogEnabled( LOG_FAIL );
  +        checkLogEnabled( null );
       }
   
       /**
        * Throw an exception if the initialization is out of order.  It tests to see
  -     * if the LOG_ENABLED state has already been set, if the component implements
  -     * LogEnabled or Logger, and if the state has progressed beyond the Logger
  -     * stage.
  +     * if the ENABLE_LOGGING state has already been set, if the component implements
  +     * LogEnabled, and if the state has progressed beyond the Logger stage.
        *
        * @param message the message to include in the thrown exception
        * @throws IllegalStateException if the state is manage out of order
        */
       public void checkLogEnabled( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & LOG_ENABLED) == 0;
  -        isValid = isValid && ((m_mask & LOG_MASK) | LOG_ENABLED) ==
  -                             ((m_state & DISPOSE_MASK) | LOG_ENABLED);
  +        generalCheckInit( message, LOG_ENABLED, ENABLE_LOGGING );
  +    }
   
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  +    /**
  +     * Throw an exception if the initialization is out of order.  It tests to see
  +     * if the SET_LOGGER state has already been set, if the component implements
  +     * Loggable, and if the state has progressed beyond the Logger stage.
  +     *
  +     * @throws IllegalStateException if the state is manage out of order
  +     */
  +    public void checkLoggable()
  +    {
  +        checkLogEnabled( null );
  +    }
   
  -        m_state |= LOG_ENABLED;
  -        if ( (m_state & START_MASK) == (m_mask & START_MASK) )
  -        {
  -            m_state |= ACTIVE;
  -        }
  +    /**
  +     * Throw an exception if the initialization is out of order.  It tests to see
  +     * if the SET_LOGGER state has already been set, if the component implements
  +     * Loggable, and if the state has progressed beyond the Logger stage.
  +     *
  +     * @param message the message to include in the thrown exception
  +     * @throws IllegalStateException if the state is manage out of order
  +     */
  +    public void checkLoggable( final String message )
  +    {
  +        generalCheckInit( message, LOGGABLE, SET_LOGGER );
       }
   
       /**
  @@ -175,7 +557,7 @@
        */
       public void checkContextualized()
       {
  -        checkContextualized(CONTEXT_FAIL);
  +        checkContextualized( null );
       }
   
       /**
  @@ -188,60 +570,32 @@
        */
       public void checkContextualized( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & CONTEXTUALIZED) == 0;
  -        isValid = isValid && ((m_mask & CONTEXT_MASK) | CONTEXTUALIZED) ==
  -                             ((m_state & DISPOSE_MASK) | CONTEXTUALIZED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state |= CONTEXTUALIZED;
  -        if ( (m_state & START_MASK) == (m_mask & START_MASK) )
  -        {
  -            m_state |= ACTIVE;
  -        }
  +        generalCheckInit( message, CONTEXTUALIZABLE, CONTEXTUALIZE );
       }
  -
  +    
       /**
        * Throw an exception if the initialization is out of order.  It tests to see
  -     * if the PARAMETERIZED state has already been set, if the component implements
  -     * Parameterizable, and if the state has progressed beyond the Parameters stage.
  +     * if the COMPOSED state has already been set, if the component implements
  +     * Composable, and if the state has progressed beyond the Configuration stage.
        *
        * @throws IllegalStateException if the state is manage out of order
        */
  -    public void checkParameterized()
  +    public void checkComposed()
       {
  -        checkParameterized( PARAMETER_FAIL );
  +        checkComposed( null );
       }
   
       /**
        * Throw an exception if the initialization is out of order.  It tests to see
  -     * if the PARAMETERIZED state has already been set, if the component implements
  -     * Parameterizable, and if the state has progressed beyond the Parameters stage.
  +     * if the COMPOSED state has already been set, if the component implements
  +     * Composable, and if the state has progressed beyond the Configuration stage.
        *
        * @param message the message to include in the thrown exception
        * @throws IllegalStateException if the state is manage out of order
        */
  -    public void checkParameterized( final String message )
  +    public void checkComposed( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & PARAMETERIZED) == 0;
  -        isValid = isValid && ((m_mask & PARAMETER_MASK) | PARAMETERIZED) ==
  -                             ((m_state & DISPOSE_MASK) | PARAMETERIZED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state |= PARAMETERIZED;
  -        if ( (m_state & START_MASK) == (m_mask & START_MASK) )
  -        {
  -            m_state |= ACTIVE;
  -        }
  +        generalCheckInit( message, COMPOSABLE, COMPOSE );
       }
   
       /**
  @@ -253,7 +607,7 @@
        */
       public void checkConfigured()
       {
  -        checkConfigured( CONFIGURE_FAIL );
  +        checkConfigured( null );
       }
   
       /**
  @@ -266,60 +620,32 @@
        */
       public void checkConfigured( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & CONFIGURED) == 0;
  -        isValid = isValid && ((m_mask & CONFIGURE_MASK) | CONFIGURED) ==
  -                             ((m_state & DISPOSE_MASK) | CONFIGURED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state |= CONFIGURED;
  -        if ( (m_state & START_MASK) == (m_mask & START_MASK) )
  -        {
  -            m_state |= ACTIVE;
  -        }
  +        generalCheckInit( message, CONFIGURABLE, CONFIGURE );
       }
   
       /**
        * Throw an exception if the initialization is out of order.  It tests to see
  -     * if the COMPOSED state has already been set, if the component implements
  -     * Composable, and if the state has progressed beyond the Configuration stage.
  +     * if the PARAMETERIZED state has already been set, if the component implements
  +     * Parameterizable, and if the state has progressed beyond the Parameters stage.
        *
        * @throws IllegalStateException if the state is manage out of order
        */
  -    public void checkComposed()
  +    public void checkParameterized()
       {
  -        checkComposed( COMPOSE_FAIL );
  +        checkParameterized( null );
       }
   
       /**
        * Throw an exception if the initialization is out of order.  It tests to see
  -     * if the COMPOSED state has already been set, if the component implements
  -     * Composable, and if the state has progressed beyond the Configuration stage.
  +     * if the PARAMETERIZED state has already been set, if the component implements
  +     * Parameterizable, and if the state has progressed beyond the Parameters stage.
        *
        * @param message the message to include in the thrown exception
        * @throws IllegalStateException if the state is manage out of order
        */
  -    public void checkComposed( final String message )
  +    public void checkParameterized( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & COMPOSED) == 0;
  -        isValid = isValid && ((m_mask & COMPOSE_MASK) | COMPOSED) ==
  -                             ((m_state & DISPOSE_MASK) | COMPOSED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state |= COMPOSED;
  -        if ( (m_state & START_MASK) == (m_mask & START_MASK) )
  -        {
  -            m_state |= ACTIVE;
  -        }
  +        generalCheckInit( message, PARAMETERIZABLE, PARAMETERIZE );
       }
   
       /**
  @@ -331,7 +657,7 @@
        */
       public void checkInitialized()
       {
  -        checkInitialized( INIT_FAIL );
  +        checkInitialized( null );
       }
   
       /**
  @@ -344,21 +670,7 @@
        */
       public void checkInitialized( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & INITIALIZED) == 0;
  -        isValid = isValid && ((m_mask & INIT_MASK) | INITIALIZED) ==
  -                             ((m_state & DISPOSE_MASK) | INITIALIZED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state |= INITIALIZED;
  -        if ( (m_state & START_MASK) == (m_mask & START_MASK) )
  -        {
  -            m_state |= ACTIVE;
  -        }
  +        generalCheckInit( message, INITIALIZABLE, INITIALIZE );
       }
   
       /**
  @@ -370,7 +682,7 @@
        */
       public void checkStarted()
       {
  -        checkStarted( START_FAIL );
  +        checkStarted( null );
       }
   
       /**
  @@ -383,23 +695,11 @@
        */
       public void checkStarted( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & STARTED) == 0;
  -        isValid = isValid && ((m_mask & START_MASK) | STARTED) ==
  -                             ((m_state & DISPOSE_MASK) | STARTED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state |= STARTED;
  -        if ( (m_state & START_MASK) == (m_mask & START_MASK) )
  -        {
  -            m_state |= ACTIVE;
  -        }
  +        generalCheckInit( message, STARTABLE, START );
       }
   
  +    
  +    
       /**
        * Throw an exception if the initialization is out of order.  It tests to see
        * if the SUSPENDED state has already been set, if the component implements
  @@ -409,7 +709,7 @@
        */
       public void checkSuspended()
       {
  -        checkSuspended( SUSPEND_FAIL );
  +        checkSuspended( null );
       }
   
       /**
  @@ -422,18 +722,7 @@
        */
       public void checkSuspended( final String message )
       {
  -        checkActive( message );
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & SUSPENDED) == 0;
  -        isValid = isValid && ((m_mask & START_MASK) & ~SUSPENDED) ==
  -                             ((m_state & DISPOSE_MASK) & ~SUSPENDED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state |= SUSPENDED;
  +        generalCheckActive( message, SUSPENDABLE, SUSPEND );
       }
   
       /**
  @@ -445,7 +734,7 @@
        */
       public void checkResumed()
       {
  -        checkResumed( RESUME_FAIL );
  +        checkResumed( null );
       }
   
       /**
  @@ -458,18 +747,7 @@
        */
       public void checkResumed( final String message )
       {
  -        checkActive( message );
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & SUSPENDED) > 0;
  -        isValid = isValid && ((m_mask & START_MASK) | SUSPENDED) ==
  -                             ((m_state & DISPOSE_MASK) | SUSPENDED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state &= ~SUSPENDED;
  +        generalCheckActive( message, SUSPENDABLE, RESUME );
       }
   
       /**
  @@ -481,7 +759,7 @@
        */
       public void checkStopped()
       {
  -        checkStopped( STOP_FAIL );
  +        checkStopped( null );
       }
   
       /**
  @@ -494,18 +772,7 @@
        */
       public void checkStopped( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & STOPPED) == 0;
  -        isValid = isValid && ((m_mask & STOP_MASK) | STOPPED) ==
  -                             ((m_state & DISPOSE_MASK) | STOPPED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state &= ~ACTIVE;
  -        m_state |= STOPPED;
  +        generalCheckDest( message, STARTABLE, STOP );
       }
   
       /**
  @@ -517,7 +784,7 @@
        */
       public void checkDisposed()
       {
  -        checkDisposed( DISPOSE_FAIL );
  +        checkDisposed( null );
       }
   
       /**
  @@ -530,18 +797,7 @@
        */
       public void checkDisposed( final String message )
       {
  -        boolean isValid = false;
  -        isValid = (m_state & m_mask & DISPOSED) == 0;
  -        isValid = isValid && ((m_mask & DISPOSE_MASK) | DISPOSED) ==
  -                             ((m_state & DISPOSE_MASK) | DISPOSED);
  -
  -        if ( ! isValid )
  -        {
  -            throw new IllegalStateException( message );
  -        }
  -
  -        m_state &= ~ACTIVE;
  -        m_state |= DISPOSED;
  +        generalCheckDest( message, DISPOSABLE, DISPOSE );
       }
   
       /**
  @@ -551,7 +807,7 @@
        */
       public void checkActive()
       {
  -        checkActive( ACTIVE_FAIL );
  +        checkActive( null );
       }
   
       /**
  @@ -567,7 +823,32 @@
               return;
           }
   
  -        throw new IllegalStateException( message );
  +        // Component not in the active state.
  +        if ( m_state < INIT_COMPLETE )
  +        {
  +            // Still expecting initialization methods.
  +            if ( message == null )
  +            {
  +                throw new IllegalStateException( "Component not in the active state. "
+ 
  +                    getLastMethod( m_methods & ( INIT_COMPLETE - 1 ) ) +
  +                    " was not called." );
  +            }
  +            else 
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        } else {
  +            // One or more destruction methods have been called.
  +            if ( message == null )
  +            {
  +                throw new IllegalStateException( "Component not in the active state because
" + 
  +                    getLastMethod( m_state ) + " was called." );
  +            }
  +            else 
  +            {
  +                throw new IllegalStateException( message );
  +            }
  +        }
       }
   
       /**
  @@ -577,12 +858,7 @@
        */
       public boolean isActive()
       {
  -        if ( (ACTIVE & m_state) > 0 )
  -        {
  -            return true;
  -        }
  -
  -        return false;
  +        return m_active;
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message