KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map 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.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 /**
42  * @author Felix Gnass [fgnass at neteye dot de]
43  * @since 6.5
44  */

45 public class MapMergeProcessor implements BeanFactoryPostProcessor {
46
47     private static Log log = LogFactory.getLog(MapMergeProcessor.class);
48             
49     private String JavaDoc ref;
50     
51     private String JavaDoc property;
52     
53     private Map JavaDoc entries;
54     
55     public void setRef(String JavaDoc ref) {
56         this.ref = ref;
57     }
58     
59     public void setProperty(String JavaDoc property) {
60         this.property = property;
61     }
62
63     public void setEntries(Map JavaDoc 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 JavaDoc 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             // No map set on the target bean, create a new one ...
87
ManagedMap map = new ManagedMap();
88             map.putAll(entries);
89             bd.getPropertyValues().addPropertyValue(property, map);
90         }
91         else {
92             Object JavaDoc value = pv.getValue();
93             if (value instanceof RuntimeBeanReference) {
94                 RuntimeBeanReference ref = (RuntimeBeanReference) value;
95                 value = beanFactory.getBean(ref.getBeanName());
96             }
97             Assert.isInstanceOf(Map JavaDoc.class, value);
98             Map JavaDoc map = (Map JavaDoc) value;
99             map.putAll(entries);
100         }
101     }
102 }
103
Popular Tags