|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.croftsoft.core.lang.lifecycle.LifecycleEnforcer
public class LifecycleEnforcer
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:
[* start() then stop() may be called zero or more times.]
init() --> ( start() --> stop() )* --> destroy().
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 );
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 |
---|
public static final int STATE_UNINITIALIZED
public static final int STATE_INITIALIZED
public static final int STATE_STARTED
public static final int STATE_STOPPED
public static final int STATE_DESTROYED
Constructor Detail |
---|
public LifecycleEnforcer(Lifecycle lifecycle)
public LifecycleEnforcer()
Method Detail |
---|
public int getState()
public void init()
init
in interface Initializable
public void start()
start
in interface Startable
public void stop()
stop
in interface Stoppable
public void destroy()
destroy
in interface Destroyable
|
CroftSoft Javadoc | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |