Package net.sourceforge.javautil.lifecycle.impl

Source Code of net.sourceforge.javautil.lifecycle.impl.ParentLifecycle

package net.sourceforge.javautil.lifecycle.impl;

import net.sourceforge.javautil.lifecycle.ILifecycle;
import net.sourceforge.javautil.lifecycle.ILifecycle.PhaseType;
import net.sourceforge.javautil.lifecycle.LifecycleEvent;
import net.sourceforge.javautil.lifecycle.LifecycleEvent.TransitionType;
import net.sourceforge.javautil.lifecycle.LifecycleException;

/**
* This will form a Parent-Child life cycle relationship which only
* affects when the parent is stopped or destroyed, which will then
* automatically cause the child to stop or be destroyed respectively.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class ParentLifecycle extends LifecycleAdapter {
 
  protected final ILifecycle child;
  protected final boolean autoStartChild;
 
  public ParentLifecycle (ILifecycle child, boolean autoStartChild) {
    super(new PhaseType[] {
      PhaseType.STOP, PhaseType.DESTROY, PhaseType.START
    }, new TransitionType[] {
      TransitionType.Before, TransitionType.After
    });
   
    this.child = child;
    this.autoStartChild = autoStartChild;
  }

  @Override public void before(LifecycleEvent event) {
    switch (event.getPhase()) {
      case STOP:
        if (child.getCurrentPhase() == PhaseType.START) {
            child.stop();
        }
        break;
       
      case DESTROY:
        switch (child.getCurrentPhase()) {
          case CREATE: case START: case STOP:
            this.child.destroy();
           
          default:
        }
        break;
       
      default:
    }
  }

  @Override public void after(LifecycleEvent event) {
    switch (event.getPhase()) {
      case START:
        switch (child.getCurrentPhase()) {
          case INSTANTIATED: case CREATE: case INITIALIZE: case STOP:
            child.start();
            break;
           
          case START:
            throw new LifecycleException("Child already started before parent", child);
           
          default:
            throw new LifecycleException("Child could not be started, invalid state: " + child.getCurrentPhase(), child);
        }
        break;
   
      default:
    }
  }

}
TOP

Related Classes of net.sourceforge.javautil.lifecycle.impl.ParentLifecycle

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.