1 24 package org.riotfamily.common.beans.override; 25 26 import java.util.List ; 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.ListFactoryBean; 36 import org.springframework.beans.factory.config.RuntimeBeanReference; 37 import org.springframework.beans.factory.config.TypedStringValue; 38 import org.springframework.beans.factory.support.ManagedList; 39 import org.springframework.util.Assert; 40 41 45 public class ListMergeProcessor implements BeanFactoryPostProcessor { 46 47 private static Log log = LogFactory.getLog(ListMergeProcessor.class); 48 49 private String ref; 50 51 private String property; 52 53 private List values; 54 55 private boolean append = false; 56 57 public void setRef(String ref) { 58 this.ref = ref; 59 } 60 61 public void setProperty(String property) { 62 this.property = property; 63 } 64 65 public void setValues(List values) { 66 this.values = values; 67 } 68 69 public void setAppend(boolean append) { 70 this.append = append; 71 } 72 73 public void postProcessBeanFactory( 74 ConfigurableListableBeanFactory beanFactory) 75 throws BeansException { 76 77 BeanDefinition bd = beanFactory.getBeanDefinition(ref); 78 if (property == null) { 79 Assert.state(ListFactoryBean.class.getName().equals(bd.getBeanClassName()), 80 "Bean [" + ref + "] must be a ListFactoryBean"); 81 82 property = "sourceList"; 83 } 84 85 log.info("Adding " + values.size() + " items to " + ref + "." + property); 86 87 PropertyValue pv = bd.getPropertyValues().getPropertyValue(property); 88 if (pv == null) { 89 ManagedList list = new ManagedList(); 91 list.addAll(values); 92 bd.getPropertyValues().addPropertyValue(property, list); 93 } 94 else { 95 Object value = pv.getValue(); 96 if (value instanceof RuntimeBeanReference) { 97 RuntimeBeanReference ref = (RuntimeBeanReference) value; 98 value = beanFactory.getBean(ref.getBeanName()); 99 } 100 else if (value instanceof TypedStringValue) { 101 TypedStringValue tsv = (TypedStringValue) value; 102 ManagedList list = new ManagedList(); 103 list.addAll(values); 104 list.add(tsv.getValue()); 105 bd.getPropertyValues().addPropertyValue(property, list); 106 return; 107 } 108 Assert.isInstanceOf(List .class, value); 109 List list = (List ) value; 110 if (append) { 111 list.addAll(values); 112 } 113 else { 114 for (int i = values.size() - 1; i >= 0; i--) { 115 list.add(0, values.get(i)); 116 } 117 } 118 } 119 } 120 121 } 122 | Popular Tags |