avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/util ComponentStateValidator.java
Date Sun, 02 Dec 2001 15:03:04 GMT
donaldp     01/12/02 07:03:04

  Added:       src/scratchpad/org/apache/avalon/excalibur/util
                        ComponentStateValidator.java
  Log:
  Utility object used to ensure a Component passes through the lifecycle correctly. Based
on Berins work on ComponentUtil in framework and then reorganized and resubmitted again.
  
  Submitted By: "Michael McKibben" <mike@hihat.net>
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/util/ComponentStateValidator.java
  
  Index: ComponentStateValidator.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.avalon.framework;
  
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.activity.Initializable;
  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.configuration.Configurable;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.logger.LogEnabled;
  import org.apache.avalon.framework.logger.Loggable;
  import org.apache.avalon.framework.parameters.Parameterizable;
  
  /**
   * This class provides basic facilities for enforcing Avalon's contracts
   * within your own code.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="mailto:mike@hihat.net">Michael McKibben</a>
   * @version CVS $Revision: 1.1 $ $Date: 2001/12/02 15:03:04 $
   */
  public final class ComponentStateValidator
  {
      private static final long LOG_ENABLED    = 0x00000001;
      private static final long CONTEXTUALIZED = 0x00000002;
      private static final long PARAMETERIZED  = 0x00000004;
      private static final long CONFIGURED     = 0x00000008;
      private static final long COMPOSED       = 0x00000010;
      private static final long ACTIVE         = 0x10000000;
      private static final long INITIALIZED    = 0x00000012;
      private static final long STARTED        = 0x00000014;
      private static final long SUSPENDED      = 0x01000000;
      private static final long STOPPED        = 0x00000018;
      private static final long DISPOSED       = 0x00000020;
      private static final long INIT_MASK      = LOG_ENABLED | CONTEXTUALIZED | 
          PARAMETERIZED | CONFIGURED | COMPOSED | INITIALIZED | STARTED;
  
      private long m_mask;
      private long m_state;
  
      /**
       * Create state validator from object (this can be used for more than just
       * components).
       */
      public ComponentStateValidator( final Object object )
      {
          if ( object instanceof LogEnabled ||
               object instanceof Loggable )
          {
              m_mask |= LOG_ENABLED;
          }
  
          if ( object instanceof Contextualizable )
          {
              m_mask |= CONTEXTUALIZED;
          }
  
          if ( object instanceof Parameterizable )
          {
              m_mask |= PARAMETERIZED;
          }
  
          if ( object instanceof Configurable )
          {
              m_mask |= CONFIGURED;
          }
  
          if ( object instanceof Composable )
          {
              m_mask |= COMPOSED;
          }
  
          if ( object instanceof Initializable )
          {
              m_mask |= INITIALIZED;
          }
  
          if ( object instanceof Disposable )
          {
              m_mask |= DISPOSED;
          }
  
          if ( object instanceof Startable )
          {
              m_mask |= STARTED | STOPPED;
          }
  
          if ( object instanceof Suspendable )
          {
              m_mask |= SUSPENDED;
          }
  
          m_state = m_mask & ~ACTIVE;
      }
  
      /**
       * 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.
       *
       * @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 )
      {
          if ( ( (m_state & m_mask & LOG_ENABLED) > 0 ) ||
               ( (m_mask & LOG_ENABLED) == 0 ) || ( m_state > LOG_ENABLED ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state |= LOG_ENABLED;
          if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
          {
              m_state |= ACTIVE;
          }
      }
  
      /**
       * Throw an exception if the initialization is out of order.  It tests to see
       * if the CONTEXTUALIZED state has already been set, if the component implements
       * Contextualizable, and if the state has progressed beyond the Context stage.
       *
       * @param message the message to include in the thrown exception
       * @throws IllegalStateException if the state is manage out of order
       */
      public void checkContextualized( final String message )
      {
          if ( ( (m_state & m_mask & CONTEXTUALIZED) > 0 ) ||
               ( (m_mask & CONTEXTUALIZED) == 0 ) || ( m_state > CONTEXTUALIZED ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state |= CONTEXTUALIZED;
          if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
          {
              m_state |= ACTIVE;
          }
      }
  
      /**
       * 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.
       *
       * @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 )
      {
          if ( ( (m_state & m_mask & PARAMETERIZED) > 0 ) ||
               ( (m_mask & PARAMETERIZED) == 0 ) || ( m_state > PARAMETERIZED ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state |= PARAMETERIZED;
          if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
          {
              m_state |= ACTIVE;
          }
      }
  
      /**
       * Throw an exception if the initialization is out of order.  It tests to see
       * if the CONFIGURED state has already been set, if the component implements
       * Configurable, 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 checkConfigured( final String message )
      {
          if ( ( (m_state & m_mask & CONFIGURED) > 0 ) ||
               ( (m_mask & CONFIGURED) == 0 ) || ( m_state > CONFIGURED ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state |= CONFIGURED;
          if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
          {
              m_state |= ACTIVE;
          }
      }
  
      /**
       * Throw an exception if the initialization is out of order.  It tests to see
       * if the INITIALIZED state has already been set, if the component implements
       * Initializable, and if the state has progressed beyond the <code>initialize</code>
stage.
       *
       * @param message the message to include in the thrown exception
       * @throws IllegalStateException if the state is manage out of order
       */
      public void checkInitialized( final String message )
      {
          if ( ( (m_state & m_mask & INITIALIZED) > 0 ) ||
               ( (m_mask & INITIALIZED) == 0 ) || ( m_state > INITIALIZED ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state |= INITIALIZED;
          if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
          {
              m_state |= ACTIVE;
          }
      }
  
      /**
       * Throw an exception if the initialization is out of order.  It tests to see
       * if the STARTED state has already been set, if the component implements
       * Startable, and if the state has progressed beyond the <code>start</code>
stage.
       *
       * @param message the message to include in the thrown exception
       * @throws IllegalStateException if the state is manage out of order
       */
      public void checkStarted( final String message )
      {
          if ( ( (m_state & m_mask & STARTED) > 0 ) ||
               ( (m_mask & STARTED) == 0 ) || ( m_state > STARTED ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state |= STARTED;
          if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
          {
              m_state |= ACTIVE;
          }
      }
  
      /**
       * 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
       * Suspendable, and if the Component is active.
       *
       * @param message the message to include in the thrown exception
       * @throws IllegalStateException if the state is manage out of order
       */
      public void checkSuspended( final String message )
      {
          checkActive( message );
          if ( ( (m_state & m_mask & SUSPENDED) > 0 ) ||
               ( (m_mask & SUSPENDED) == 0 ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state |= SUSPENDED;
      }
  
      /**
       * Throw an exception if the initialization is out of order.  It tests to see
       * if the SUSPENDED state has not been set, if the component implements
       * Suspendable, and if the Component is active.
       *
       * @param message the message to include in the thrown exception
       * @throws IllegalStateException if the state is manage out of order
       */
      public void checkResumed( final String message )
      {
          checkActive( message );
          if ( ( (m_state & m_mask & SUSPENDED) == 0 ) ||
               ( (m_mask & SUSPENDED) == 0 ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state &= ~SUSPENDED;
      }
  
      /**
       * Throw an exception if the initialization is out of order.  It tests to see
       * if the STOPPED state has not been set, if the component implements
       * Startable, and if the Component is active.
       *
       * @param message the message to include in the thrown exception
       * @throws IllegalStateException if the state is manage out of order
       */
      public void checkStopped( final String message )
      {
          if ( ( (m_state & m_mask & STOPPED) > 0 ) ||
               ( (m_mask & STOPPED) == 0 ) ||
               ( (m_state & m_mask) > STOPPED ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state &= ~ACTIVE;
          m_state |= STOPPED;
      }
  
      /**
       * Throw an exception if the initialization is out of order.  It tests to see
       * if the DISPOSED state has not been set, if the component implements
       * Disposable.
       *
       * @param message the message to include in the thrown exception
       * @throws IllegalStateException if the state is manage out of order
       */
      public void checkDisposed( final String message )
      {
          if ( ( (m_state & m_mask & DISPOSED) > 0 ) || ( (m_mask & DISPOSED)
== 0 ) )
          {
              throw new IllegalStateException( message );
          }
  
          m_state &= ~ACTIVE;
          m_state |= DISPOSED;
      }
  
      /**
       * Checks to see if the state is active.
       *
       * @param message the message to include in the thrown exception
       * @throws IllegalStateException if the component is not active
       */
      public void checkActive( final String message )
      {
          if ( (ACTIVE & m_state) > 0 )
          {
              return;
          }
  
          throw new IllegalStateException( message );
      }
  }
  
  
  
  

--
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