Package jfun.yan.xml.nuts

Source Code of jfun.yan.xml.nuts.LifecycleNut

package jfun.yan.xml.nuts;

import java.util.Locale;

import jfun.yan.Component;
import jfun.yan.lifecycle.DefaultLifecycleDescriptor;
import jfun.yan.lifecycle.Lifecycle;
import jfun.yan.lifecycle.LifecycleManager;


/**
* The <lifecycle> tag.
* For example:
* <pre>
* &lt;lifecycle name="disposer" reentrant="true"&gt;
*   &lt;method class="..." name="..." args="..."/&gt;
* &lt;lifecycle&gt;
* </pre>
* <p>
* @author Ben Yu
* Nov 28, 2005 10:27:58 AM
*/
public class LifecycleNut extends DelegatingNut {
  private String name;
  private boolean reentrant;
  private boolean reentrant_set = false;
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public boolean isReentrant() {
    return reentrant;
  }

  public void setReentrant(boolean reentrant) {
    this.reentrant = reentrant;
    this.reentrant_set = true;
  }

  public Component eval(){
    checkMandatory("name", name);
    final Component target = getMandatory();
    final String phase = name.toLowerCase(Locale.US)
      .trim();
    if(DefaultLifecycleDescriptor
        .DEFAULT_INITIALIZER_NAME.equals(phase)){
      //initializer runs right away.
      return target;
    }
    else{
      //The object identity of the factory determines if this factory
      //can be called multiple times by the lifecycle manager.
      //so cannot use singleton even if it carries no state.
      final Component factory =
        target.factory();
      final LifecycleManager man = super.getNutEnvironment()
      .getLifecycleManager();
      final Lifecycle lifecycle = new Lifecycle();
      lifecycle.put(phase, FactoryProcedure.instance(), isReentrant(phase));
      return man.withLifecycle(factory, lifecycle);
    }
  }
  private boolean isReentrant(String phase){
    if(reentrant_set){
      return reentrant;
    }
    return !DefaultLifecycleDescriptor.DEFAULT_DISPOSER_NAME
        .equals(phase);
  }
}
TOP

Related Classes of jfun.yan.xml.nuts.LifecycleNut

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.