com.croftsoft.core.lang.lifecycle
Class LifecycleEnforcer

java.lang.Object
  extended by com.croftsoft.core.lang.lifecycle.LifecycleEnforcer
All Implemented Interfaces:
Commissionable, Destroyable, Initializable, Lifecycle, Resumable, Startable, Stoppable

public class LifecycleEnforcer
extends Object
implements Lifecycle

Strictly enforces the Lifecycle method calling order by frameworks.

Methods throw an IllegalStateException if they are called out of order. The acceptable state transition sequence is as follows:

 init() --> ( start() --> stop() )* --> destroy().
 
[* start() then stop() may be called zero or more times.]

LifecycleEnforcer can be used via inheritance, by delegation, or as a wrapper.

When used via inheritance, LifecycleEnforcer is subclassed and the subclass methods call the corresponding superclass methods as their first actions.

Inheritance example:

 public class  ActiveResource1
   extends LifecycleEnforcer
 {
   public  ActiveResource1 ( )
   {
     super ( ); // use the zero argument superclass constructor

     // insert subclass specific constructor code here
   }

   public void  init ( )
   {
     super.init ( ); // may throw IllegalStateException

     // insert subclass specific initialization here
   }

   public void  start ( )
   {
     super.start ( ); // may throw IllegalStateException

     // insert subclass specific start code here
   }

   public void  stop ( )
   {
     super.stop ( ); // may throw IllegalStateException

     // insert subclass specific stop code here
   }

   public void  destroy ( )
   {
     super.destroy ( ); // may throw IllegalStateException

     // insert subclass specific destroy code here
   }
 }
 

When used via delegation, it is much like inheritance except that a reference is maintained to a LifecycleEnforcer instead of subclassing from it. The containing Lifecycle instance will delegate Lifecycle method calls to the delegate LifecycleEnforcer as the first action.

Delegation example:

 public class  ActiveResource2
 {
   private final Lifecycle  lifecycleEnforcer;

   public  ActiveResource1 ( )
   {
     lifecycleEnforcer = new LifecycleEnforcer ( );
   }

   public void  init ( )
   {
     lifecycleEnforcer.init ( ); // may throw IllegalStateException

     // insert subclass specific initialization here
   }

   public void  start ( )
   {
     lifecycleEnforcer.start ( ); // may throw IllegalStateException

     // insert subclass specific start code here
   }

   public void  stop ( )
   {
     lifecycleEnforcer.stop ( ); // may throw IllegalStateException

     // insert subclass specific stop code here
   }

   public void  destroy ( )
   {
     lifecycleEnforcer.destroy ( ); // may throw IllegalStateException

     // insert subclass specific destroy code here
   }
 }
 

When used as a wrapper, LifecycleEnforcer acts as a protective exterior around a private Lifecycle instance. Calls to the lifecycle methods are delegated to the private instance only after checking for proper state transitions. The wrapper has the added benefit of effectively making all but the lifecycle methods of the private instance inaccessible by direct reference.

Wrapper example:

 Lifecycle  unprotectedLifecycle = new ActiveResource3 ( );

 Lifecycle  protectedLifecycle
   = new LifecycleEnforcer ( unprotectedLifecycle );

 untrustedFramework.manageLifecycleObject ( protectedLifecycle );
 

Since:
2001-03-08
Version:
2001-05-31
Author:
David W. Croft

Field Summary
static int STATE_DESTROYED
           
static int STATE_INITIALIZED
           
static int STATE_STARTED
           
static int STATE_STOPPED
           
static int STATE_UNINITIALIZED
           
 
Constructor Summary
LifecycleEnforcer()
           
LifecycleEnforcer(Lifecycle lifecycle)
           
 
Method Summary
 void destroy()
           
 int getState()
           
 void init()
           
 void start()
           
 void stop()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_UNINITIALIZED

public static final int STATE_UNINITIALIZED
See Also:
Constant Field Values

STATE_INITIALIZED

public static final int STATE_INITIALIZED
See Also:
Constant Field Values

STATE_STARTED

public static final int STATE_STARTED
See Also:
Constant Field Values

STATE_STOPPED

public static final int STATE_STOPPED
See Also:
Constant Field Values

STATE_DESTROYED

public static final int STATE_DESTROYED
See Also:
Constant Field Values
Constructor Detail

LifecycleEnforcer

public LifecycleEnforcer(Lifecycle lifecycle)

LifecycleEnforcer

public LifecycleEnforcer()
Method Detail

getState

public int getState()

init

public void init()
Specified by:
init in interface Initializable

start

public void start()
Specified by:
start in interface Startable

stop

public void stop()
Specified by:
stop in interface Stoppable

destroy

public void destroy()
Specified by:
destroy in interface Destroyable

CroftSoft Javadoc

CroftSoft Core Javadoc (2008-09-28 20:58:02)