Package tests.jfun.yan.xml

Source Code of tests.jfun.yan.xml.MapNamingServiceWiring

package tests.jfun.yan.xml;

import java.util.Map;

import jfun.util.Misc;
import jfun.yan.Binder;
import jfun.yan.Component;
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.TypeMismatchException;
import jfun.yan.factory.ThreadLocalScope;
import jfun.yan.xml.NutsUtils;

class MapNamingServiceWiring implements PropertyBinder{
  private final String default_map_name;
  MapNamingServiceWiring(String default_name){
    this.default_map_name = default_name;
    this.detect_map = Components.autodetect(Map.class, new Object[]{default_name});
  }
  private final Component detect_map;
  private volatile Map the_map = null;
  private Creator performLookup(Class component_type, Object key, Class type){
    final String lookup_key = (Misc.getTypeName(component_type)+'.'+key)
    .replace('.', '/').replace('$', '/');
    Object v = the_map.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);
    }
  }
  public Creator bind(final Class component_type,
      final Object key, final Class type) {
    if(the_map!=null){
      return performLookup(component_type, key, type);
    }
    else{
      return detect_map.bind(new Binder(){
        public Creator bind(Object m){
          if(m instanceof Map){
            the_map = (Map)m;
            return performLookup(component_type, key, type);
          }
          else
            throw new TypeMismatchException(Map.class, m==null?null:m.getClass());
        }
      });
    }
  }

}
TOP

Related Classes of tests.jfun.yan.xml.MapNamingServiceWiring

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.