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 org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.riotfamily.common.beans.override.OverrideNamespaceHandler.BeanReplacement;
029: import org.springframework.beans.BeansException;
030: import org.springframework.beans.MutablePropertyValues;
031: import org.springframework.beans.factory.config.BeanDefinition;
032: import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
033: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
034: import org.springframework.beans.factory.config.ConstructorArgumentValues;
035: import org.springframework.core.Ordered;
036:
037: /**
038: * BeanFactoryPostProcessor that can be used to replace beans that have been
039: * defined elsewhere. Use this class when you want to write a Riot module that
040: * needs to replace a bean which is provided by another module.
041: * <p>Simply defining a bean with the same id would not work, because the order
042: * in which the module configurations are processed is not defined.
043: *
044: * @author Felix Gnass [fgnass at neteye dot de]
045: * @since 6.5
046: */
047: public class BeanOverrideProcessor implements BeanFactoryPostProcessor,
048: Ordered {
049:
050: private static Log log = LogFactory
051: .getLog(BeanOverrideProcessor.class);
052:
053: private String ref;
054:
055: private BeanDefinition beanDefinition;
056:
057: private boolean merge;
058:
059: private int order = Ordered.LOWEST_PRECEDENCE;
060:
061: public void setRef(String ref) {
062: this .ref = ref;
063: }
064:
065: public void setBeanDefinition(BeanDefinition beanDefinition) {
066: this .beanDefinition = beanDefinition;
067: }
068:
069: public void setBeanReplacement(BeanReplacement replacement) {
070: setBeanDefinition(replacement.getBeanDefinition());
071: }
072:
073: public void setMerge(boolean merge) {
074: this .merge = merge;
075: }
076:
077: public int getOrder() {
078: return this .order;
079: }
080:
081: public void setOrder(int order) {
082: this .order = order;
083: }
084:
085: public void postProcessBeanFactory(
086: ConfigurableListableBeanFactory beanFactory)
087: throws BeansException {
088:
089: BeanDefinition target = beanFactory.getBeanDefinition(ref);
090: overwriteBeanDefinition(target, beanDefinition);
091: }
092:
093: private void overwriteBeanDefinition(BeanDefinition target,
094: BeanDefinition source) {
095: log.info("Replacing bean [" + ref + "] with a ["
096: + source.getBeanClassName() + "]");
097:
098: target.setBeanClassName(source.getBeanClassName());
099: ConstructorArgumentValues cas = target
100: .getConstructorArgumentValues();
101: cas.clear();
102: cas.addArgumentValues(source.getConstructorArgumentValues());
103:
104: MutablePropertyValues pvs = target.getPropertyValues();
105: if (!merge) {
106: pvs.clear();
107: }
108: pvs.addPropertyValues(source.getPropertyValues());
109: }
110:
111: }
|