KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.riotfamily.common.beans.override.OverrideNamespaceHandler.BeanReplacement;
29 import org.springframework.beans.BeansException;
30 import org.springframework.beans.MutablePropertyValues;
31 import org.springframework.beans.factory.config.BeanDefinition;
32 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
33 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
34 import org.springframework.beans.factory.config.ConstructorArgumentValues;
35 import org.springframework.core.Ordered;
36
37 /**
38  * BeanFactoryPostProcessor that can be used to replace beans that have been
39  * defined elsewhere. Use this class when you want to write a Riot module that
40  * needs to replace a bean which is provided by another module.
41  * <p>Simply defining a bean with the same id would not work, because the order
42  * in which the module configurations are processed is not defined.
43  *
44  * @author Felix Gnass [fgnass at neteye dot de]
45  * @since 6.5
46  */

47 public class BeanOverrideProcessor implements BeanFactoryPostProcessor, Ordered {
48
49     private static Log log = LogFactory.getLog(BeanOverrideProcessor.class);
50     
51     private String JavaDoc ref;
52
53     private BeanDefinition beanDefinition;
54     
55     private boolean merge;
56     
57     private int order = Ordered.LOWEST_PRECEDENCE;
58     
59     public void setRef(String JavaDoc ref) {
60         this.ref = ref;
61     }
62
63     public void setBeanDefinition(BeanDefinition beanDefinition) {
64         this.beanDefinition = beanDefinition;
65     }
66
67     public void setBeanReplacement(BeanReplacement replacement) {
68         setBeanDefinition(replacement.getBeanDefinition());
69     }
70     
71     public void setMerge(boolean merge) {
72         this.merge = merge;
73     }
74     
75     public int getOrder() {
76         return this.order;
77     }
78
79     public void setOrder(int order) {
80         this.order = order;
81     }
82
83     public void postProcessBeanFactory(
84             ConfigurableListableBeanFactory beanFactory)
85             throws BeansException {
86         
87         BeanDefinition target = beanFactory.getBeanDefinition(ref);
88         overwriteBeanDefinition(target, beanDefinition);
89     }
90     
91     private void overwriteBeanDefinition(BeanDefinition target, BeanDefinition source) {
92         log.info("Replacing bean [" + ref + "] with a ["
93                 + source.getBeanClassName() + "]");
94         
95         target.setBeanClassName(source.getBeanClassName());
96         ConstructorArgumentValues cas = target.getConstructorArgumentValues();
97         cas.clear();
98         cas.addArgumentValues(source.getConstructorArgumentValues());
99         
100         MutablePropertyValues pvs = target.getPropertyValues();
101         if (!merge) {
102             pvs.clear();
103         }
104         pvs.addPropertyValues(source.getPropertyValues());
105     }
106     
107 }
108
Popular Tags