Component state - Code cleanup + method reordering

This commit is contained in:
Emmanuel BENOîT 2015-09-15 17:05:21 +02:00
parent 92ebf8dad2
commit 2fa4227e5a

View file

@ -29,7 +29,7 @@ public final class ComponentState
private final ThrowingConsumer[] lcActions;
ComponentState( ComponentRegistry registry , final NewComponentInfo< ? > ci )
ComponentState( final ComponentRegistry registry , final NewComponentInfo< ? > ci )
throws ComponentCreationException
{
this.registry = registry;
@ -58,7 +58,7 @@ public final class ComponentState
this.autostart = ci.getAutostart( );
this.lcActions = new ThrowingConsumer[ 4 ];
for ( LifecycleStage stage : LifecycleStage.values( ) ) {
for ( final LifecycleStage stage : LifecycleStage.values( ) ) {
this.lcActions[ stage.ordinal( ) ] = ci.getLifecycleAction( stage );
}
}
@ -67,7 +67,7 @@ public final class ComponentState
@Override
public String toString( )
{
StringBuilder sb = new StringBuilder( );
final StringBuilder sb = new StringBuilder( );
if ( this.name == null ) {
sb.append( "anonymous component" );
} else {
@ -98,25 +98,25 @@ public final class ComponentState
public Set< ComponentState > getDependencies( )
{
return Collections.unmodifiableSet( dependencies );
return Collections.unmodifiableSet( this.dependencies );
}
public void getDependencies( Collection< ComponentState > output )
public void getDependencies( final Collection< ComponentState > output )
{
output.addAll( dependencies );
output.addAll( this.dependencies );
}
public Set< ComponentState > getReverseDependencies( )
{
return Collections.unmodifiableSet( reverseDependencies );
return Collections.unmodifiableSet( this.reverseDependencies );
}
public void getReverseDependencies( Collection< ComponentState > output )
public void getReverseDependencies( final Collection< ComponentState > output )
{
output.addAll( reverseDependencies );
output.addAll( this.reverseDependencies );
}
@ -145,7 +145,7 @@ public final class ComponentState
throw new IllegalStateException( "registry is inactive" );
}
startNoChecks( );
this.startNoChecks( );
}
@ -160,8 +160,8 @@ public final class ComponentState
return null;
}
for ( ComponentState rdepState : this.reverseDependencies ) {
Throwable t = rdepState.stop( );
for ( final ComponentState rdepState : this.reverseDependencies ) {
final Throwable t = rdepState.stop( );
if ( t != null ) {
return t;
}
@ -169,7 +169,7 @@ public final class ComponentState
try {
this.runLifecycleAction( LifecycleStage.START );
} catch ( Throwable t ) {
} catch ( final Throwable t ) {
return t;
}
@ -185,7 +185,7 @@ public final class ComponentState
throw new IllegalStateException( "registry has failed" );
}
Throwable t = this.stop( );
final Throwable t = this.stop( );
if ( t != null ) {
throw new ComponentRestartException( "failed to stop" , t );
}
@ -194,20 +194,55 @@ public final class ComponentState
}
void setInitialised( final boolean initialised )
{
this.initialised = initialised;
}
void setActive( final boolean active )
{
this.active = active;
}
void addDependency( final ComponentState dep )
{
this.dependencies.add( dep );
dep.reverseDependencies.add( this );
}
void runLifecycleAction( final LifecycleStage stage )
throws Throwable
{
@SuppressWarnings( "unchecked" )
final ThrowingConsumer< Object > tc = this.lcActions[ stage.ordinal( ) ];
if ( tc != null ) {
try {
tc.accept( this.component );
} catch ( final FunctionException e ) {
throw e.getCause( );
}
}
}
private void startNoChecks( )
throws ComponentStartupException
{
if ( this.active ) {
return;
}
for ( ComponentState depState : this.dependencies ) {
for ( final ComponentState depState : this.dependencies ) {
depState.startNoChecks( );
}
try {
this.runLifecycleAction( LifecycleStage.START );
} catch ( ComponentStartupException e ) {
} catch ( final ComponentStartupException e ) {
throw e;
} catch ( Throwable t ) {
} catch ( final Throwable t ) {
throw new ComponentStartupException( t );
}
this.active = true;
@ -220,50 +255,15 @@ public final class ComponentState
if ( this.wasActive ) {
try {
this.startNoChecks( );
} catch ( ComponentStartupException e ) {
} catch ( final ComponentStartupException e ) {
throw new ComponentRestartException( "failed to restart " + this , e );
}
for ( ComponentState rDepState : this.reverseDependencies ) {
for ( final ComponentState rDepState : this.reverseDependencies ) {
rDepState.startIfPreviouslyActive( );
}
}
}
void setInitialised( boolean initialised )
{
this.initialised = initialised;
}
void setActive( boolean active )
{
this.active = active;
}
void addDependency( ComponentState dep )
{
this.dependencies.add( dep );
dep.reverseDependencies.add( this );
}
void runLifecycleAction( LifecycleStage stage )
throws Throwable
{
@SuppressWarnings( "unchecked" )
ThrowingConsumer< Object > tc = this.lcActions[ stage.ordinal( ) ];
if ( tc != null ) {
try {
tc.accept( this.component );
} catch ( FunctionException e ) {
throw e.getCause( );
}
}
}
}