1 24 package org.riotfamily.common.beans.override; 25 26 import java.util.Map ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.springframework.beans.BeansException; 31 import org.springframework.beans.PropertyValue; 32 import org.springframework.beans.factory.config.BeanDefinition; 33 import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 34 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 35 import org.springframework.beans.factory.config.MapFactoryBean; 36 import org.springframework.beans.factory.config.RuntimeBeanReference; 37 import org.springframework.beans.factory.support.ManagedMap; 38 import org.springframework.util.Assert; 39 import org.springframework.util.StringUtils; 40 41 45 public class MapMergeProcessor implements BeanFactoryPostProcessor { 46 47 private static Log log = LogFactory.getLog(MapMergeProcessor.class); 48 49 private String ref; 50 51 private String property; 52 53 private Map entries; 54 55 public void setRef(String ref) { 56 this.ref = ref; 57 } 58 59 public void setProperty(String property) { 60 this.property = property; 61 } 62 63 public void setEntries(Map entries) { 64 this.entries = entries; 65 } 66 67 public void postProcessBeanFactory( 68 ConfigurableListableBeanFactory beanFactory) 69 throws BeansException { 70 71 BeanDefinition bd = beanFactory.getBeanDefinition(ref); 72 if (property == null) { 73 Assert.state(MapFactoryBean.class.getName().equals(bd.getBeanClassName()), 74 "Bean [" + ref + "] must be a MapFactoryBean"); 75 76 property = "sourceMap"; 77 } 78 79 if (log.isInfoEnabled()) { 80 String keys = StringUtils.collectionToCommaDelimitedString(entries.keySet()); 81 log.info("Adding [" + keys + "] to " + ref + "." + property); 82 } 83 84 PropertyValue pv = bd.getPropertyValues().getPropertyValue(property); 85 if (pv == null) { 86 ManagedMap map = new ManagedMap(); 88 map.putAll(entries); 89 bd.getPropertyValues().addPropertyValue(property, map); 90 } 91 else { 92 Object value = pv.getValue(); 93 if (value instanceof RuntimeBeanReference) { 94 RuntimeBeanReference ref = (RuntimeBeanReference) value; 95 value = beanFactory.getBean(ref.getBeanName()); 96 } 97 Assert.isInstanceOf(Map .class, value); 98 Map map = (Map ) value; 99 map.putAll(entries); 100 } 101 } 102 } 103 | Popular Tags |