001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2007
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.beans.override;
025:
026: import java.util.List;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.springframework.beans.BeansException;
031: import org.springframework.beans.PropertyValue;
032: import org.springframework.beans.factory.config.BeanDefinition;
033: import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
034: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
035: import org.springframework.beans.factory.config.ListFactoryBean;
036: import org.springframework.beans.factory.config.RuntimeBeanReference;
037: import org.springframework.beans.factory.config.TypedStringValue;
038: import org.springframework.beans.factory.support.ManagedList;
039: import org.springframework.util.Assert;
040:
041: /**
042: * @author Felix Gnass [fgnass at neteye dot de]
043: * @since 6.5
044: */
045: public class ListMergeProcessor implements BeanFactoryPostProcessor {
046:
047: private static Log log = LogFactory
048: .getLog(ListMergeProcessor.class);
049:
050: private String ref;
051:
052: private String property;
053:
054: private List values;
055:
056: private boolean append = false;
057:
058: public void setRef(String ref) {
059: this .ref = ref;
060: }
061:
062: public void setProperty(String property) {
063: this .property = property;
064: }
065:
066: public void setValues(List values) {
067: this .values = values;
068: }
069:
070: public void setAppend(boolean append) {
071: this .append = append;
072: }
073:
074: public void postProcessBeanFactory(
075: ConfigurableListableBeanFactory beanFactory)
076: throws BeansException {
077:
078: BeanDefinition bd = beanFactory.getBeanDefinition(ref);
079: if (property == null) {
080: Assert.state(ListFactoryBean.class.getName().equals(
081: bd.getBeanClassName()), "Bean [" + ref
082: + "] must be a ListFactoryBean");
083:
084: property = "sourceList";
085: }
086:
087: log.info("Adding " + values.size() + " items to " + ref + "."
088: + property);
089:
090: PropertyValue pv = bd.getPropertyValues().getPropertyValue(
091: property);
092: if (pv == null) {
093: // No list set on the target bean, create a new one ...
094: ManagedList list = new ManagedList();
095: list.addAll(values);
096: bd.getPropertyValues().addPropertyValue(property, list);
097: } else {
098: Object value = pv.getValue();
099: if (value instanceof RuntimeBeanReference) {
100: RuntimeBeanReference ref = (RuntimeBeanReference) value;
101: value = beanFactory.getBean(ref.getBeanName());
102: } else if (value instanceof TypedStringValue) {
103: TypedStringValue tsv = (TypedStringValue) value;
104: ManagedList list = new ManagedList();
105: list.addAll(values);
106: list.add(tsv.getValue());
107: bd.getPropertyValues().addPropertyValue(property, list);
108: return;
109: }
110: Assert.isInstanceOf(List.class, value);
111: List list = (List) value;
112: if (append) {
113: list.addAll(values);
114: } else {
115: for (int i = values.size() - 1; i >= 0; i--) {
116: list.add(0, values.get(i));
117: }
118: }
119: }
120: }
121:
122: }
|