KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > beans > override > ListMergeProcessor


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.beans.override;
25
26 import java.util.List JavaDoc;
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 /**
42  * @author Felix Gnass [fgnass at neteye dot de]
43  * @since 6.5
44  */

45 public class ListMergeProcessor implements BeanFactoryPostProcessor {
46
47     private static Log log = LogFactory.getLog(ListMergeProcessor.class);
48             
49     private String JavaDoc ref;
50     
51     private String JavaDoc property;
52     
53     private List JavaDoc values;
54     
55     private boolean append = false;
56     
57     public void setRef(String JavaDoc ref) {
58         this.ref = ref;
59     }
60     
61     public void setProperty(String JavaDoc property) {
62         this.property = property;
63     }
64
65     public void setValues(List JavaDoc 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             // No list set on the target bean, create a new one ...
90
ManagedList list = new ManagedList();
91             list.addAll(values);
92             bd.getPropertyValues().addPropertyValue(property, list);
93         }
94         else {
95             Object JavaDoc 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 JavaDoc.class, value);
109             List JavaDoc list = (List JavaDoc) 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