1 package tests.jfun.yan.xml; 2 3 import java.util.Map ; 4 5 import jfun.util.Misc; 6 import jfun.yan.Binder; 7 import jfun.yan.Component; 8 import jfun.yan.ComponentResolutionException; 9 import jfun.yan.Components; 10 import jfun.yan.Creator; 11 import jfun.yan.Monad; 12 import jfun.yan.PropertyBinder; 13 import jfun.yan.Recovery; 14 import jfun.yan.TypeMismatchException; 15 import jfun.yan.factory.ThreadLocalScope; 16 import jfun.yan.xml.NutsUtils; 17 18 class MapNamingServiceWiring implements PropertyBinder{ 19 private final String default_map_name; 20 MapNamingServiceWiring(String default_name){ 21 this.default_map_name = default_name; 22 this.detect_map = Components.autodetect(Map .class, new Object []{default_name}); 23 } 24 private final Component detect_map; 25 private volatile Map the_map = null; 26 private Creator performLookup(Class component_type, Object key, Class type){ 27 final String lookup_key = (Misc.getTypeName(component_type)+'.'+key) 28 .replace('.', '/').replace('$', '/'); 29 Object v = the_map.get(lookup_key); 30 if(v==null){ 31 final Recovery rec = Monad.onException(ComponentResolutionException.class, 32 Monad.fail("failed to lookup " + lookup_key)); 33 return Components.useProperty(component_type, key, type) 34 .recover(rec); 35 } 36 else{ 37 return NutsUtils.asComponent(v); 38 } 39 } 40 public Creator bind(final Class component_type, 41 final Object key, final Class type) { 42 if(the_map!=null){ 43 return performLookup(component_type, key, type); 44 } 45 else{ 46 return detect_map.bind(new Binder(){ 47 public Creator bind(Object m){ 48 if(m instanceof Map ){ 49 the_map = (Map )m; 50 return performLookup(component_type, key, type); 51 } 52 else 53 throw new TypeMismatchException(Map .class, m==null?null:m.getClass()); 54 } 55 }); 56 } 57 } 58 59 } 60 | Popular Tags |