Hamilton Verissimo de Oliveira (Engenharia - SPO) wrote:
Something like:

chain = CreationDestruction --> SetupTeardown --> StartupShutdown


Too abstract.
Put that in code, please.

A lifecycle runs in two directions - setup (forward) and teardown (backwards). If your going to create an abstract chain then the chain needs to represent the logical steps taken in both directions. So in the above example you have a phase dealing with CreationDesctruction. If setup is applied to that phase, then you know that to teardown the component you need to invoke teardown on the CreationDesctruction object.


Example (change names to whatever you want):

  List list = new ArrayList();
  list.add( new CreationPhaseHandler() );
  list.add( new InitializationPhaseHandler() );
  list.add( new ExecutionPhaseHandler() );
  PhaseHandler[] chain = convertToArray( list );

So now we have an order list of phase handlers. From this list we can process the in two directions - forward and backwards.

  public void setupChain( PhaseHandler[] chain )
  {
       ArrayList list = new ArrayList(); // list of processed handlers
       for( int i=0; i<chain.length; i++ )
       {
            PhaseHandler handler = chain[i];
            try
            {
                 handler.setup();
                 list.add( handler );
            }
            catch( Throwable e )
            {
                 PhaseHandler[] rollback = convertToArray( list );
                 tearDownChain( rollback );
                 throw e;
            }
       }
  }

  public void tearDownChain( PhaseHandler[] chain )
  {
       for( int i=0; i<chain.length; i++ )
       {
            PhaseHandler handler = chain[i];
            handler.teardown();
       }
  }


Steve.


--

|------------------------------------------------|
| Magic by Merlin                                |
| Production by Avalon                           |
|                                                |
| http://avalon.apache.org/merlin                |
| http://dpml.net/merlin/distributions/latest    |
|------------------------------------------------|

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to