package tests.jfun.yan.xml;
import java.util.Map;
import jfun.util.Misc;
import jfun.yan.ComponentResolutionException;
import jfun.yan.Components;
import jfun.yan.Creator;
import jfun.yan.Monad;
import jfun.yan.PropertyBinder;
import jfun.yan.Recovery;
import jfun.yan.xml.NutsUtils;
final class NamingServiceWiring implements PropertyBinder {
private final Map names;
public Creator bind(Class component_type, Object key, Class type) {
final String lookup_key = (Misc.getTypeName(component_type)+'.'+key)
.replace('.', '/').replace('$', '/');
Object v = names.get(lookup_key);
if(v==null){
final Recovery rec = Monad.onException(ComponentResolutionException.class,
Monad.fail("failed to lookup " + lookup_key));
return Components.useProperty(component_type, key, type)
.recover(rec);
}
else{
return NutsUtils.asComponent(v);
}
}
NamingServiceWiring(Map names) {
this.names = names;
}
}