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>
* <lifecycle name="disposer" reentrant="true">
* <method class="..." name="..." args="..."/>
* <lifecycle>
* </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);
}
}