Package net.sourceforge.javautil.lifecycle.impl

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

package net.sourceforge.javautil.lifecycle.impl;

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

/**
* This will form a Child-Parent life cycle relationship which only
* affects when the child is going to start, which will then ensure
* that the parent is started before this happens.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class ChildLifecycle extends LifecycleAdapter<ILifecycle<?>> {
 
  protected final ILifecycle<?> parent;
 
  public ChildLifecycle (ILifecycle<?> parent) {
    super(new PhaseType[] {
      PhaseType.PRESTART, PhaseType.CREATE, PhaseType.INITIALIZE, PhaseType.START
    }, new TransitionType[] {
      TransitionType.Before
    });
   
    this.parent = parent;
  }

  @Override public void before(LifecycleEvent<ILifecycle<?>> event) {
    switch (event.getPhase()) {
      case PRESTART: case CREATE: case INITIALIZE: case START:
        switch (parent.getCurrentPhase()) {
          case INITIALIZE: case CREATE: case STOP: case INSTANTIATED:
            parent.start();
            break;
           
          case DESTROY:
            throw new LifecycleException("Parent has been destroyed, child cannot start", event.getSource());
           
          default:
        }
       
      default:
    }
  }

}
TOP

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

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.