1 8 14 package jfun.yan.containers; 15 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import jfun.util.Misc; 23 import jfun.yan.AmbiguousComponentResolutionException; 24 import jfun.yan.Component; 25 import jfun.yan.ComponentInstantiationException; 26 import jfun.yan.ComponentMap; 27 import jfun.yan.Components; 28 import jfun.yan.Container; 29 import jfun.yan.Creator; 30 import jfun.yan.CyclicDependencyException; 31 import jfun.yan.Dependency; 32 import jfun.yan.Registrar; 33 import jfun.yan.UnknownComponentTypeException; 34 import jfun.yan.UnresolvedComponentException; 35 import jfun.yan.UnsatisfiedComponentException; 36 import jfun.yan.YanException; 37 import jfun.yan.factory.Factory; 38 39 60 public class SimpleContainer implements Container, java.io.Serializable { 61 private final Registrar target; 62 63 67 public SimpleContainer(final Registrar target) { 68 this.target = target; 69 } 70 71 public void registerComponent(Object key, Component cc) { 72 target.registerComponent(key, cc); 73 } 74 public void registerComponent(Component cc){ 75 final Class ctype = cc.getType(); 76 if(ctype==null){ 77 throw new UnknownComponentTypeException( 78 "immature component cannot be registered"); 79 } 80 registerComponent(ctype, cc); 81 } 82 public void unregisterComponent(Object key) { 83 target.unregisterComponent(key); 84 } 85 86 public void unregisterComponentsOfType(Class type) { 87 target.unregisterComponentsOfType(type); 88 } 89 public boolean containsKey(Object key) { 90 return target.containsKey(key); 91 } 92 93 public boolean containsType(Class type) { 94 return target.containsType(type); 95 } 96 97 public Component getComponent(Object key) { 98 return target.getComponent(key); 99 } 100 101 public <T> Component<T> getComponentOfType(Class <T> type) { 102 return target.getComponentOfType(type); 103 } 104 public <T> List <Component<T>> getComponentsOfType(Class <T> type) { 105 return target.getComponentsOfType(type); 106 } 107 108 public <T> T getInstanceOfType(final Class <T> type, ComponentMap cmap){ 109 try{ 110 final Component<T> cc = getComponentOfType(type); 111 if(cc==null){ 112 throw new UnresolvedComponentException(type); 113 } 114 return cc.create(cmap.getDependencyOfType(type, cmap)); 115 } 116 catch(YanException e){ 117 e.push("getInstanceOfType <" + Misc.getTypeName(type)+">"); 118 throw e; 119 } 120 } 121 public <T> List <T> getInstancesOfType(final Class <T> type, ComponentMap cmap){ 122 try{ 123 java.util.List <Component<T>> ccs = getComponentsOfType(type); 124 final java.util.List <T> ret = new java.util.ArrayList <T>(); 125 for(final Component<T> cc : ccs){ 126 final Class ctype = cc.getType(); 127 if(ctype != null) 128 ret.add(cc.create(cmap.getDependencyOfType(ctype, cmap))); 129 } 130 return ret; 131 } 132 catch(YanException e){ 133 e.push("getInstancesOfType <" + Misc.getTypeName(type) + ">"); 134 throw e; 135 } 136 } 137 public <T> T getInstanceOfType(Class <T> type){ 138 return getInstanceOfType(type, this); 139 } 140 public <T> List <T> getInstancesOfType(Class <T> type){ 141 return getInstancesOfType(type, this); 142 } 143 public Dependency getDependencyOfType(Class type, ComponentMap cmap) { 144 return target.getDependencyOfType(type, cmap); 145 } 146 public Dependency getDependency(Object key, ComponentMap cmap){ 147 return target.getDependency(key, cmap); 148 } 149 public Dependency getDependency(Object key){ 150 return getDependency(key, this); 151 } 152 public Dependency getDependencyOfType(Class type){ 153 return getDependencyOfType(type, this); 154 } 155 public void verify(ComponentMap cmap) { 156 target.verify(cmap); 157 } 158 public void verify(){ 159 target.verify(this); 160 } 161 162 public Class verifyComponent(Component cc){ 163 try{ 164 169 return cc.verify(getDependency(null)); 170 } 171 catch(YanException e){ 172 e.push("verifyComponent <" + cc + ">"); 173 throw e; 174 } 175 } 176 public Class verifyKey(Object key){ 177 try{ 178 final Component c = getComponent(key); 179 if(c==null){ 180 throw new UnresolvedComponentException(key); 181 } 182 return c.verify(getDependency(key)); 183 } 184 catch(YanException e){ 185 e.push("verifyKey <" + key +">"); 186 throw e; 187 } 188 } 189 public Class verifyType(Class type){ 190 try{ 191 final Component c = getComponentOfType(type); 192 if(c==null) 193 throw new UnresolvedComponentException(type); 194 return c.verify(getDependencyOfType(type)); 195 } 196 catch(YanException e){ 197 e.push("verifyType <" + Misc.getTypeName(type) +">"); 198 throw e; 199 } 200 } 201 public Class getComponentType(Object key) { 202 final Component cc = getComponent(key); 203 if(cc==null) return null; 204 return cc.getType(); 205 } 206 207 208 public <T> T instantiateComponent(Creator<T> cc, ComponentMap cmap) 209 throws AmbiguousComponentResolutionException, 210 ComponentInstantiationException, CyclicDependencyException, 211 UnresolvedComponentException, UnsatisfiedComponentException, YanException { 212 try{ 213 return cc.create(getDependency(null, cmap)); 214 } 215 catch(YanException e){ 216 e.push("instantiateComponent <" + cc + ">"); 217 throw e; 218 } 219 } 220 public <T> T instantiateComponent(Object key, Creator<T> cc, ComponentMap cmap) 221 throws AmbiguousComponentResolutionException, 222 ComponentInstantiationException, CyclicDependencyException, 223 UnresolvedComponentException, UnsatisfiedComponentException, YanException { 224 try{ 225 return cc.create(getDependency(key, cmap)); 226 } 227 catch(YanException e){ 228 e.push("instantiateComponent <" + key + ">"); 229 throw e; 230 } 231 } 232 public <T> T instantiateComponent(Creator<T> cc) 233 throws AmbiguousComponentResolutionException, 234 ComponentInstantiationException, CyclicDependencyException, 235 UnresolvedComponentException, UnsatisfiedComponentException, YanException { 236 return instantiateComponent(cc, this); 237 } 238 public <T> T instantiateComponent(Object key, Creator<T> cc) 239 throws AmbiguousComponentResolutionException, 240 ComponentInstantiationException, CyclicDependencyException, 241 UnresolvedComponentException, UnsatisfiedComponentException, YanException { 242 return instantiateComponent(key, cc, this); 243 } 244 public Object getInstance(Object key, ComponentMap cmap) { 245 try{ 246 final Component cc = getComponent(key); 247 if(cc==null){ 248 throw new UnresolvedComponentException(key); 249 } 250 return cc.create(cmap.getDependency(key,cmap)); 251 } 252 catch(YanException e){ 253 e.push("getInstance <" + key + ">"); 254 throw e; 255 } 256 } 257 public Object getInstance(Object key){ 258 return getInstance(key, this); 259 } 260 261 public void registerValue(Object key, Object v){ 262 registerComponent(key, Components.value(v)); 263 } 264 public void registerValue(Object v){ 265 registerValue(v.getClass(), v); 267 } 268 274 public void registerConstructor(Class c){ 275 registerConstructor(c, c); 276 } 277 287 protected Component defaultTransform(Component comp){ 288 return comp.singleton(); 289 } 290 296 public void registerConstructor(Object key, Class c){ 297 registerComponent(key, defaultTransform(Components.ctor(c))); 298 } 299 305 public void registerConstructor(Class c, Class [] param_types){ 306 registerConstructor(c, c, param_types); 307 } 308 314 public void registerConstructor(Object key, Class c, Class [] param_types){ 315 registerComponent(key, 316 defaultTransform(Components.ctor(c, param_types))); 317 } 318 324 public void registerStaticMethod(Class c, String name){ 325 final Component comp = Components.static_method(c, name); 326 registerComponent(defaultTransform(comp)); 327 } 328 334 public void registerStaticMethod(Object key, Class c, String name){ 335 registerComponent(key, 336 defaultTransform(Components.static_method(c, name))); 337 } 338 344 public void registerStaticMethod(Class c, String name, Class [] param_types){ 345 final Component comp = Components.static_method(c, name, param_types); 346 registerComponent(defaultTransform(comp)); 347 } 348 354 public void registerStaticMethod(Object key, Class c, String name, Class [] param_types){ 355 registerComponent(key, 356 defaultTransform(Components.static_method(c, name, param_types))); 357 } 358 public Container inherit(Registrar parent){ 359 if(parent==this) return this; 360 return new SimpleContainer(new InheritedRegistrar(parent, this)); 361 } 362 public boolean equals(Object other) { 363 if(other instanceof SimpleContainer){ 364 final SimpleContainer cc2 = (SimpleContainer)other; 365 return target.equals(cc2.target); 366 } 367 else return target.equals(other); 368 } 369 public int hashCode() { 370 return target.hashCode(); 371 } 372 public String toString() { 373 return target.toString(); 374 } 375 376 public Set keys() { 377 return target.keys(); 378 } 379 public java.util.Collection getComponents(){ 380 return target.getComponents(); 381 } 382 public void getInstances(Map store){ 383 getInstances(store, this); 384 } 385 public java.util.List getInstances(){ 386 return getInstances(this); 387 } 388 public void getInstances(Map store, ComponentMap cmap){ 389 try{ 390 for(Iterator it = keys().iterator(); it.hasNext();){ 391 final Object ky = it.next(); 392 final Component cc = getComponent(ky); 393 store.put(ky, cc.create(cmap.getDependency(ky, cmap))); 394 } 395 } 396 catch(YanException e){ 397 e.push("getInstances"); 398 throw e; 399 } 400 } 401 public java.util.List getInstances(ComponentMap cmap){ 402 try{ 403 final java.util.Set ks = keys(); 404 final ArrayList ret = new ArrayList (); 405 for(Iterator it = ks.iterator(); it.hasNext();){ 406 final Object ky = it.next(); 407 final Component cc = getComponent(ky); 408 ret.add(cc.create(cmap.getDependency(ky, cmap))); 409 } 410 return ret; 411 } 412 catch(YanException e){ 413 e.push("getInstances"); 414 throw e; 415 } 416 } 417 public Factory getFactory(Object key, ComponentMap cmap) throws UnresolvedComponentException, YanException { 418 return instantiateFactory(key, getComponent(key), cmap); 419 } 420 421 public Factory getFactory(Object key) throws UnresolvedComponentException, YanException { 422 return instantiateFactory(key, getComponent(key)); 423 } 424 425 public <T> Factory<T> getFactoryOfType(Class <T> type, ComponentMap cmap) throws AmbiguousComponentResolutionException, UnresolvedComponentException, YanException { 426 return instantiateFactory(type, getComponentOfType(type), cmap); 427 } 428 429 public <T> Factory<T> getFactoryOfType(Class <T> type) throws AmbiguousComponentResolutionException, UnresolvedComponentException, YanException { 430 return instantiateFactory(type, getComponentOfType(type)); 431 } 432 private <T> Factory<T> instantiateFactory(Object key, Component<T> cc, ComponentMap src){ 433 return this.instantiateComponent(key, cc.factory(), src); 434 } 435 private <T> Factory<T> instantiateFactory(Object key, Component<T> cc){ 436 return this.instantiateComponent(key, cc.factory()); 437 } 438 442 protected final Registrar getDelegateTarget(){ 443 return target; 444 } 445 } 446 | Popular Tags |