1 package jfun.yan.spring; 2 3 import java.io.IOException ; 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.LinkedHashMap ; 7 import java.util.Locale ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 import jfun.yan.Component; 12 import jfun.yan.Container; 13 import jfun.yan.util.ReflectionUtil; 14 15 import org.springframework.beans.BeansException; 16 import org.springframework.beans.factory.BeanCreationException; 17 import org.springframework.beans.factory.BeanFactory; 18 import org.springframework.beans.factory.BeanFactoryUtils; 19 import org.springframework.beans.factory.BeanNotOfRequiredTypeException; 20 import org.springframework.beans.factory.FactoryBean; 21 import org.springframework.beans.factory.ListableBeanFactory; 22 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 23 import org.springframework.context.ApplicationContext; 24 import org.springframework.context.ApplicationEvent; 25 import org.springframework.context.ApplicationEventPublisher; 26 import org.springframework.context.HierarchicalMessageSource; 27 import org.springframework.context.MessageSource; 28 import org.springframework.context.MessageSourceResolvable; 29 import org.springframework.context.NoSuchMessageException; 30 import org.springframework.context.event.ApplicationEventMulticaster; 31 import org.springframework.context.event.SimpleApplicationEventMulticaster; 32 import org.springframework.context.support.AbstractApplicationContext; 33 import org.springframework.core.io.Resource; 34 import org.springframework.core.io.support.ResourcePatternResolver; 35 36 47 public class Container2ApplicationContext implements ListableBeanFactory, 48 ApplicationContext{ 49 50 private final String name; 51 private final long startup_timestamp; 52 private final Container yan; 53 private final MessageSource parent_message_source; 54 private final ResourcePatternResolver resource_loader; 55 private final ApplicationEventPublisher parent_publisher; 56 private static final String prefix = BeanFactory.FACTORY_BEAN_PREFIX;; 57 private MessageSource message_source = null; 58 private ApplicationEventMulticaster multicaster = null; 59 private synchronized MessageSource getMessageSource(){ 60 if(message_source!=null) return message_source; 61 final String key = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME; 62 final Class type = yan.getComponentType(key); 63 if(type!=null && MessageSource.class.isAssignableFrom(type)){ 64 message_source = (MessageSource)yan.getInstance(key); 65 if (parent_message_source != null && message_source instanceof HierarchicalMessageSource) { 66 HierarchicalMessageSource hms = (HierarchicalMessageSource) message_source; 67 if (hms.getParentMessageSource() == null) { 68 hms.setParentMessageSource(parent_message_source); 71 } 72 } 73 } 74 else{ 75 return parent_message_source; 76 } 77 return message_source; 78 } 79 private synchronized ApplicationEventMulticaster getApplicationEventMulticaster(){ 80 if(multicaster!=null) return multicaster; 81 final String key = AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME; 82 final Class type = yan.getComponentType(key); 83 if(type!=null && ApplicationEventMulticaster.class.isAssignableFrom(type)){ 84 multicaster = (ApplicationEventMulticaster)yan.getInstance(key); 85 } 86 else{ 87 multicaster = new SimpleApplicationEventMulticaster(); 88 } 89 return multicaster; 90 } 91 100 public Container2ApplicationContext( 101 MessageSource message_source, ResourcePatternResolver resource_loader, 102 ApplicationEventPublisher publisher, 103 String name, long startup_timestamp, 104 Container yan 105 ) 106 { 107 this.parent_message_source = message_source; 108 this.name = name; 109 this.resource_loader = resource_loader; 110 this.startup_timestamp = startup_timestamp; 111 this.yan = yan; 112 this.parent_publisher = publisher; 113 } 114 115 118 public Container getContainer() { 119 return yan; 120 } 121 122 123 126 public int getBeanDefinitionCount() { 127 final Set keys = yan.keys(); 128 int ret = 0; 129 for(Iterator it=keys.iterator(); it.hasNext();){ 130 final Object key = it.next(); 131 if(key instanceof String ){ 132 ret++; 133 } 134 } 135 return ret; 136 } 137 138 141 public String [] getBeanDefinitionNames() { 142 final Set keys = yan.keys(); 143 final ArrayList names = new ArrayList (); 144 for(Iterator it=keys.iterator(); it.hasNext();){ 145 final Object key = it.next(); 146 if(key instanceof String ){ 147 names.add((String )key); 148 } 149 } 150 final String [] ret = new String [names.size()]; 151 names.toArray(ret); 152 return ret; 153 } 154 155 164 public String [] getBeanDefinitionNames(Class type) { 165 return getBeanNamesForType(type, true, false); 166 } 167 176 public String [] getBeanNamesForType(Class type){ 177 return getBeanNamesForType(type, true, true); 178 } 179 private interface BeanListener{ 180 183 void onBean(String key, Component c); 184 187 void onFactoryBean(String key, FactoryBean fb); 188 191 void onFactoryBean(String key, Component fbc); 192 } 193 202 private void foreachBean(Class type, 203 boolean includePrototypes, boolean includeFactoryBeans, 204 BeanListener listener){ 205 final boolean for_factorybean = isFactoryBeanType(type); 206 for(Iterator it=yan.keys().iterator();it.hasNext();){ 207 final Object key = it.next(); 208 if(!(key instanceof String )) continue; 209 final jfun.yan.Component c = yan.getComponent(key); 210 final Component fbc = SpringAdapter.getFactoryBeanComponent(c); 211 if(fbc!=null){ 212 if(includeFactoryBeans){ 214 final FactoryBean fb = (FactoryBean)instantiate(fbc); 216 if(!includePrototypes && !fb.isSingleton()){ 217 continue; 218 } 219 final Class fbt = fb.getObjectType(); 220 if(type==null || ReflectionUtil.isAssignableFrom(type, fbt)){ 221 final String beanname = (String )key; 222 listener.onFactoryBean(beanname, fb); 223 continue; 224 } 225 } 226 if(!includePrototypes && !c.isSingleton()){ 227 continue; 228 } 229 if(type==null || for_factorybean){ 230 listener.onFactoryBean((String )key, fbc); 232 continue; 233 } 234 } 235 else{ 236 if(!includePrototypes && !c.isSingleton()){ 237 continue; 238 } 239 if(type==null){ 240 listener.onBean((String )key, c); 241 continue; 242 } 243 final Class ctype = c.getType(); 244 if(ctype!=null&&ReflectionUtil.isAssignableFrom(type, ctype)){ 245 listener.onBean((String )key, c); 246 continue; 247 } 248 } 249 } 250 } 251 252 public String [] getBeanNamesForType(Class type, boolean includePrototypes, 253 boolean includeFactoryBeans){ 254 302 final ArrayList buf = new ArrayList (); 303 final BeanListener listener = new BeanListener(){ 304 public void onBean(String key, Component c) { 305 buf.add(key); 306 } 307 public void onFactoryBean(String key, Component fbc) { 308 buf.add(prefix+key); 309 } 310 public void onFactoryBean(String key, FactoryBean fb) { 311 buf.add(key); 312 } 313 }; 314 foreachBean(type, includePrototypes, includeFactoryBeans, listener); 315 final String [] ret = new String [buf.size()]; 316 buf.toArray(ret); 317 return ret; 318 } 319 325 public boolean containsBeanDefinition(String beanName) { 326 return yan.containsKey(getRealBeanName(beanName)); 327 } 328 private static String getRealBeanName(String name){ 329 return BeanFactoryUtils.transformedBeanName(name); 330 } 331 332 private static boolean isFactoryBeanType(Class type){ 333 return type!=null && FactoryBean.class.equals(type); 334 } 335 339 private Object instantiate(Component c){ 340 return yan.instantiateComponent(c); 341 } 342 350 public Map getBeansOfType(Class type) throws BeansException { 351 return getBeansOfType(type, true, true); 352 } 353 360 public Map getBeansOfType(Class type, boolean includePrototypes, 361 boolean includeFactoryBeans) throws BeansException { 362 416 final Map result = new LinkedHashMap (); 417 final BeanListener listener = new BeanListener(){ 418 public void onBean(String key, Component c) { 419 result.put(key, instantiate(c)); 420 } 421 public void onFactoryBean(String key, Component fbc) { 422 result.put(prefix+key, instantiate(fbc)); 423 } 424 public void onFactoryBean(String key, FactoryBean fb) { 425 result.put(key, instantiate(key, fb)); 426 } 427 }; 428 return result; 429 } 430 private Component findComponent(String name){ 431 final Component c = yan.getComponent(name); 432 if(c==null){ 433 throw new NoSuchBeanDefinitionException(name, "bean "+name+" not found."); 434 } 435 return c; 436 } 437 private Component getComponent(String name){ 438 return findComponent(BeanFactoryUtils.transformedBeanName(name)); 439 } 440 447 public Object getBean(final String name) throws BeansException { 448 456 return getBean(name, null); 457 } 458 459 472 public Object getBean(final String beanname, Class requiredType) throws BeansException { 473 if(BeanFactoryUtils.isFactoryDereference(beanname)){ 474 final String factoryname = BeanFactoryUtils.transformedBeanName(beanname); 476 final Component c = findComponent(factoryname); 477 final Component fbc = SpringAdapter.getFactoryBeanComponent(c); 478 if(fbc!=null){ 479 throw new NoSuchBeanDefinitionException(beanname, 480 "FactoryBean "+factoryname+" not found."); 481 } 482 final Object result = instantiate(fbc); 483 checkBeanInstanceType(beanname, requiredType, result); 484 return result; 485 } 486 else{ 487 final Component c = findComponent(beanname); 488 final Component fbc = SpringAdapter.getFactoryBeanComponent(c); 489 if(requiredType==null){ 490 return instantiate(c); 491 } 492 if(fbc!=null){ 493 final FactoryBean fb = (FactoryBean)instantiate(fbc); 494 final Class ctype = fb.getObjectType(); 495 checkBeanType(beanname, requiredType, ctype); 496 return instantiate(beanname, fb); 497 } 498 else{ 499 final Class ctype = c.getType(); 500 if(c.isConcrete()){ 501 checkBeanType(beanname, requiredType, ctype); 502 } 503 final Object result = instantiate(c); 504 checkBeanInstanceType(beanname, requiredType, result); 505 return result; 506 } 507 } 508 } 509 510 515 public boolean containsBean(String name) { 516 return this.containsBeanDefinition(name); 517 } 519 520 525 public boolean isSingleton(final String name) 526 throws NoSuchBeanDefinitionException { 527 final Component c = getComponent(name); 528 if(!BeanFactoryUtils.isFactoryDereference(name)){ 529 final Component fbc = SpringAdapter.getFactoryBeanComponent(c); 530 if(fbc!=null){ 531 final FactoryBean fb = (FactoryBean)instantiate(fbc); 532 return fb.isSingleton(); 533 } 534 } 535 return c.isSingleton(); 536 } 537 538 543 public Class getType(final String name) 544 throws NoSuchBeanDefinitionException { 545 final Component c = getComponent(name); 546 if(!BeanFactoryUtils.isFactoryDereference(name)){ 547 final Component fbc = SpringAdapter.getFactoryBeanComponent(c); 548 if(fbc!=null){ 549 final FactoryBean fb = (FactoryBean)instantiate(fbc); 550 return fb.getObjectType(); 551 } 552 } 553 return c.getType(); 554 } 555 private static final String [] no_aliases = {}; 556 559 public String [] getAliases(String name) 560 throws NoSuchBeanDefinitionException { 561 getComponent(name); 562 return no_aliases; 563 } 564 565 public String getDisplayName() { 566 return name; 567 } 568 569 public ApplicationContext getParent() { 570 return null; 571 } 572 573 public long getStartupDate() { 574 return startup_timestamp; 575 } 576 577 public void publishEvent(ApplicationEvent event) { 578 final ApplicationEventMulticaster multicaster = getApplicationEventMulticaster(); 579 multicaster.multicastEvent(event); 580 if(parent_publisher!=null) 581 parent_publisher.publishEvent(event); 582 } 583 584 585 586 public String getMessage(String code, Object [] args, Locale locale) throws NoSuchMessageException { 587 return getMessageSource().getMessage(code, args, locale); 588 } 589 590 public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { 591 return getMessageSource().getMessage(resolvable, locale); 592 } 593 594 public String getMessage(String code, Object [] args, String defaultMessage, Locale locale) { 595 return getMessageSource().getMessage(code, args, defaultMessage, locale); 596 } 597 598 public Resource[] getResources(String locationPattern) throws IOException { 599 return resource_loader.getResources(locationPattern); 600 } 601 602 public Resource getResource(String location) { 603 return resource_loader.getResource(location); 604 } 605 606 public BeanFactory getParentBeanFactory() { 607 return null; 608 } 609 private void checkBeanType(String beanname, Class required, Class actual){ 610 if(actual!=null && required!=null && 611 !ReflectionUtil.isAssignableFrom(required, actual)){ 612 throw new BeanNotOfRequiredTypeException(beanname, required, actual); 613 } 614 } 615 private void checkBeanInstanceType(String beanname, Class required, Object instance){ 616 if(required!=null && 617 !ReflectionUtil.isInstance(required, instance)){ 618 throw new BeanNotOfRequiredTypeException(beanname, required, 619 instance==null?null:instance.getClass()); 620 } 621 } 622 private static Object instantiate(String beanname, FactoryBean fb){ 623 try{ 624 return fb.getObject(); 625 } 626 catch(RuntimeException e){ 627 throw e; 628 } 629 catch(Error e){ 630 throw e; 631 } 632 catch(Exception e){ 633 throw new BeanCreationException(beanname, "FactoryBean failed", e); 634 } 635 } 636 } 637 | Popular Tags |