1 package jfun.yan.xml; 2 3 import java.util.HashMap ; 4 5 import jfun.util.Misc; 6 import jfun.yan.Component; 7 import jfun.yan.ComponentMap; 8 import jfun.yan.Components; 9 import jfun.yan.Container; 10 import jfun.yan.Creator; 11 import jfun.yan.DelegatingComponent; 12 import jfun.yan.Dependency; 13 import jfun.yan.ParameterBinder; 14 import jfun.yan.PropertyBinder; 15 import jfun.yan.factory.ThreadLocalScope; 16 import jfun.yan.function.Signature; 17 18 24 public class Modes { 25 28 public static final SingletonMode simple_singleton = new SingletonMode(){ 29 public Component decorate(Component c) { 30 return c.singleton(); 31 } 32 public String toString(){ 33 return Constants.ON; 34 } 35 }; 36 39 public static final SingletonMode thread_local_singleton = new SingletonMode(){ 40 public Component decorate(Component c) { 41 return c.singleton(new ThreadLocalScope()); 42 } 43 public String toString(){ 44 return Constants.ON; 45 } 46 }; 47 50 public static ParameterBinder params_bytype = new ParameterBinder(){ 51 private final HashMap cache = new HashMap (); 52 public Creator bind(Signature src, int ind, Class type) { 53 return useType(type, cache); 54 } 55 public String toString(){ 56 return Constants.BYTYPE; 57 } 58 }; 59 62 public static PropertyBinder props_byname = new PropertyBinder(){ 63 private final HashMap cache = new HashMap (); 64 public Creator bind(Class component_type, Object key, Class type) { 65 return useKey(key, cache); 66 } 67 public String toString(){ 68 return Constants.BYNAME; 69 } 70 }; 71 74 public static PropertyBinder props_bytype = new PropertyBinder(){ 75 private final HashMap cache = new HashMap (); 76 public Creator bind(Class component_type, Object key, Class type) { 77 return useType(type, cache); 78 } 79 public String toString(){ 80 return Constants.BYTYPE; 81 } 82 }; 83 90 public static PropertyBinder props_byqualifiedname = new PropertyBinder(){ 91 private final HashMap cache = new HashMap (); 92 public Creator bind(Class component_type, Object key, Class type) { 93 return useKey(Misc.getTypeName(component_type)+"."+key, cache); 94 } 95 public String toString(){ 96 return Constants.BYQUALIFIEDNAME; 97 } 98 }; 99 106 public static PropertyBinder props_autodetect = new PropertyBinder(){ 107 public Creator bind(final Class component_type, final Object key, final Class type) { 108 final Object [] alt_keys = {key, Misc.getTypeName(component_type)+'.'+key}; 109 return Components.autodetect(type, alt_keys); 110 } 111 public String toString(){ 112 return Constants.AUTODETECT; 113 } 114 }; 115 private static Component useKey(Object key, java.util.Map cache){ 116 synchronized(cache){ 117 Component result = (Component)cache.get(key); 118 if(result == null){ 119 result = useKey(key); 120 cache.put(key, result); 121 } 122 return result; 123 } 124 } 125 private static Component useType(Class type, java.util.Map cache){ 126 synchronized(cache){ 127 Component result = (Component)cache.get(type); 128 if(result == null){ 129 result = useType(type); 130 cache.put(type, result); 131 } 132 return result; 133 } 134 } 135 private static Component useKey(final Object key){ 137 return new DelegatingComponent(Components.useKey(key)){ 138 public Object create(Dependency dep){ 139 final ComponentMap cmap = dep.getComponentMap(); 140 if(cmap instanceof Container){ 141 return ((Container)cmap).getInstance(key); 142 } 143 else return super.create(dep); 144 } 145 }; 146 } 147 private static Component useType(final Class type){ 149 return new DelegatingComponent(Components.useType(type)){ 150 public Object create(Dependency dep){ 151 final ComponentMap cmap = dep.getComponentMap(); 152 if(cmap instanceof Container){ 153 return ((Container)cmap).getInstanceOfType(type); 154 } 155 else return super.create(dep); 156 } 157 }; 158 } 159 } 160 | Popular Tags |