org.jvnet.hk2.component.internal.runlevel
Class DefaultRunLevelService

java.lang.Object
  extended by org.jvnet.hk2.component.internal.runlevel.DefaultRunLevelService
All Implemented Interfaces:
java.util.EventListener, HabitatListener, InhabitantListener, RunLevelService<java.lang.Void>, RunLevelState<java.lang.Void>

public class DefaultRunLevelService
extends java.lang.Object
implements RunLevelService<java.lang.Void>, RunLevelState<java.lang.Void>, InhabitantListener, HabitatListener

The default environment RunLevelService implementation. Assume ServiceA, ServiceB, and ServiceC are all in the same RunLevel and the dependencies are: ServiceA -> ServiceB -> ServiceC Expected start order: ServiceC, ServiceB, ServiceA Expected shutdown / PreDestroy order: ServiceA, ServiceB, ServiceC So, how do we decide when to record events to the recorder? The answer is hook into (post) PostConstruct activations. Since the model is not in the habitat, we will just need to randomly pick a start service. Case 1: A, B, then C by RLS. get ServiceA (called by RLS) Start ServiceA: get ServiceB Start ServiceB: get ServiceC Start ServiceC wire ServiceC PostConstruct ServiceC wire ServiceB PostConstruct ServiceB wire ServiceA PostConstruct ServiceA get ServiceB (called by RLS) get ServiceC (called by RLS) Case 2: B, C, then A by RLS. get ServiceB (called by RLS) Start ServiceB: get ServiceC Start ServiceC wire ServiceC PostConstruct ServiceC wire ServiceB PostConstruct ServiceB get ServiceC (called by RLS) get ServiceA (called by RLS) Start ServiceA: get ServiceB wire ServiceA PostConstruct ServiceA Case 3: B, A, then C by RLS. get ServiceB (called by RLS) Start ServiceB: get ServiceC Start ServiceC wire ServiceC PostConstruct ServiceC wire ServiceB PostConstruct ServiceB get ServiceA (called by RLS) Start ServiceA: get ServiceB wire ServiceA PostConstruct ServiceA get ServiceC (called by RLS) Case 4: C, B, then A by RLS. get ServiceC (called by RLS) Start ServiceC: wire ServiceC PostConstruct ServiceC get ServiceB (called by RLS) Start ServiceB: get ServiceC wire ServiceB PostConstruct ServiceB get ServiceA (called by RLS) Start ServiceA: get ServiceB wire ServiceA PostConstruct ServiceA get ServiceA (called by RLS) ~~~ Note that the implementation performs some level of constraint checking during injection. For example, - It is an error to have a RunLevel-annotated service at RunLevel X to depend on (i.e., be injected with) a RunLevel-annotated service at RunLevel Y when Y > X. - It is an error to have a non-RunLevel-annotated service to depend on a RunLevel-annotated service at any RunLevel. Note that the implementation does not handle Holder and Collection injection constraint validations. ~~~ The implementation will automatically proceedTo(-1) after the habitat has been initialized. Note that all RunLevel values less than -1 will be ignored.

Since:
3.1
Author:
Jeff Trent

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jvnet.hk2.component.InhabitantListener
InhabitantListener.EventType
 
Nested classes/interfaces inherited from interface org.jvnet.hk2.component.HabitatListener
HabitatListener.EventType
 
Constructor Summary
DefaultRunLevelService(Habitat habitat)
           
 
Method Summary
protected  void activateRunLevel()
           
 java.lang.Integer getCurrentRunLevel()
          The current run level state.
 java.lang.Class<java.lang.Void> getEnvironment()
          The environment value for this state.
 java.lang.Integer getPlannedRunLevel()
          The planned run level state.
 RunLevelState<java.lang.Void> getState()
          Returns the current state of this RunLevelService instance.
 boolean inhabitantChanged(HabitatListener.EventType eventType, Habitat habitat, Inhabitant<?> inhabitant)
          Called when the habitat has changed.
 boolean inhabitantChanged(InhabitantListener.EventType eventType, Inhabitant<?> inhabitant)
          Called when the inhabitant has changed.
 boolean inhabitantIndexChanged(HabitatListener.EventType eventType, Habitat habitat, Inhabitant<?> inhabitant, java.lang.String index, java.lang.String name, java.lang.Object service)
          Called when the habitat index has changed.
 void proceedTo(int runLevel)
          Causes this RunLevelService to move to the specified run level for all RunLevel instances (identified by environment), orchestrating the appropriate lifecycle events based on the given implementation strategy.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultRunLevelService

public DefaultRunLevelService(Habitat habitat)
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

proceedTo

public void proceedTo(int runLevel)
Description copied from interface: RunLevelService
Causes this RunLevelService to move to the specified run level for all RunLevel instances (identified by environment), orchestrating the appropriate lifecycle events based on the given implementation strategy. See the javadoc for each implementation for details.

If the RunLevel specified is the same as the current RunLevel then the RunLevelService will return immediately.

Note that the underlying implementation may perform this operation asynchronously. Implementors who choose the asynchronous approach are expected to treat a subsequent proceedTo(newRunLevel) call as an implicit cancellation of any currently running proceedTo() that is running on one or more managed threads. Again, see the javadoc for each implementation for details.

Specified by:
proceedTo in interface RunLevelService<java.lang.Void>
Parameters:
runLevel - the run level to move to.

activateRunLevel

protected void activateRunLevel()

getState

public RunLevelState<java.lang.Void> getState()
Description copied from interface: RunLevelService
Returns the current state of this RunLevelService instance.

Specified by:
getState in interface RunLevelService<java.lang.Void>

getEnvironment

public java.lang.Class<java.lang.Void> getEnvironment()
Description copied from interface: RunLevelState
The environment value for this state.

Specified by:
getEnvironment in interface RunLevelState<java.lang.Void>
Returns:
the class type used to uniquely identify the environment in context, or null representing the default environment.

getCurrentRunLevel

public java.lang.Integer getCurrentRunLevel()
Description copied from interface: RunLevelState
The current run level state. This represents the last run level successfully achieved by the underlying RunLevelService responsible for this environment.

Specified by:
getCurrentRunLevel in interface RunLevelState<java.lang.Void>
Returns:
the current run level, or null if no run level has been been achieved.

getPlannedRunLevel

public java.lang.Integer getPlannedRunLevel()
Description copied from interface: RunLevelState
The planned run level state. If this value is different from current run level, this signifies movement of the underlying RunLevelService.

Specified by:
getPlannedRunLevel in interface RunLevelState<java.lang.Void>
Returns:
the planned run level, or null if there is no planned level. This value is established by a call to RunLevelService.proceedTo(int).

inhabitantChanged

public boolean inhabitantChanged(InhabitantListener.EventType eventType,
                                 Inhabitant<?> inhabitant)
Description copied from interface: InhabitantListener
Called when the inhabitant has changed.

Specified by:
inhabitantChanged in interface InhabitantListener
Returns:
callee should return true to continue receiving notification, false otherwise

inhabitantChanged

public boolean inhabitantChanged(HabitatListener.EventType eventType,
                                 Habitat habitat,
                                 Inhabitant<?> inhabitant)
Description copied from interface: HabitatListener
Called when the habitat has changed.

Specified by:
inhabitantChanged in interface HabitatListener
Returns:
callee should return true to continue receiving notification, false otherwise

inhabitantIndexChanged

public boolean inhabitantIndexChanged(HabitatListener.EventType eventType,
                                      Habitat habitat,
                                      Inhabitant<?> inhabitant,
                                      java.lang.String index,
                                      java.lang.String name,
                                      java.lang.Object service)
Description copied from interface: HabitatListener
Called when the habitat index has changed.

Specified by:
inhabitantIndexChanged in interface HabitatListener
Returns:
callee should return true to continue receiving notification, false otherwise


Copyright © 2010 Sun Microsystems, Inc.. All Rights Reserved.