001: /*
002: * $Id: AbstractHierarchicalDefinitionParser.java 10494 2008-01-23 21:09:56Z acooke $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010: package org.mule.config.spring.parsers;
011:
012: import org.mule.config.spring.parsers.assembly.BeanAssembler;
013: import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
014: import org.mule.config.spring.parsers.assembly.configuration.ReusablePropertyConfiguration;
015: import org.mule.config.spring.parsers.assembly.configuration.TempWrapperPropertyConfiguration;
016: import org.mule.config.spring.util.SpringXMLUtils;
017: import org.mule.util.StringUtils;
018:
019: import org.springframework.beans.factory.config.BeanDefinition;
020: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
021: import org.springframework.beans.factory.xml.ParserContext;
022: import org.w3c.dom.Element;
023:
024: /**
025: * This definition parser introduces the notion of Hierarchical processing to nested XML elements. Definition
026: * parsers that extend this can refer to parent beans. It does not assume that the parser is restricted
027: * to a single property.
028: *
029: * Calling classes must set the registry at the start of processing.
030: *
031: * @see org.mule.config.spring.parsers.generic.ChildDefinitionParser
032: * @see org.mule.config.spring.parsers.collection.ChildMapEntryDefinitionParser.KeyValuePair
033: * @see org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser
034: */
035: public abstract class AbstractHierarchicalDefinitionParser extends
036: AbstractMuleBeanDefinitionParser {
037:
038: private ReusablePropertyConfiguration targetPropertyConfiguration = new ReusablePropertyConfiguration(
039: new TempWrapperPropertyConfiguration(
040: beanPropertyConfiguration, false));
041: private BeanDefinition forcedParent = null;
042:
043: public PropertyConfiguration getTargetPropertyConfiguration() {
044: return targetPropertyConfiguration;
045: }
046:
047: protected String getParentBeanName(Element element) {
048: return ((Element) element.getParentNode())
049: .getAttribute(ATTRIBUTE_NAME);
050: }
051:
052: public BeanDefinition getParentBeanDefinition(Element element) {
053: if (null != forcedParent) {
054: return forcedParent;
055: } else {
056: String parentBean = getParentBeanName(element);
057: if (StringUtils.isBlank(parentBean)) {
058: throw new IllegalStateException("No parent for "
059: + SpringXMLUtils.elementToString(element));
060: }
061: return getRegistry().getBeanDefinition(parentBean);
062: }
063: }
064:
065: /**
066: * The bean assembler gives more reliable/automatic processing of collections, maps, etc.
067: *
068: * @param element The current element
069: * @param bean The bean being constructed
070: * @return An assembler that includes Mule-specific construction logic
071: */
072: protected BeanAssembler getBeanAssembler(Element element,
073: BeanDefinitionBuilder bean) {
074: BeanDefinition target = getParentBeanDefinition(element);
075: return getBeanAssemblerFactory().newBeanAssembler(
076: beanPropertyConfiguration, bean,
077: targetPropertyConfiguration, target);
078: }
079:
080: /**
081: * Provide access to bean assembler from non-hierarchical case. Legacy support for
082: * "mixed" definition parsers.
083: *
084: * @deprecated
085: * @param element
086: * @param bean
087: * @return
088: */
089: protected BeanAssembler getOrphanBeanAssembler(Element element,
090: BeanDefinitionBuilder bean) {
091: return super .getBeanAssembler(element, bean);
092: }
093:
094: public void forceParent(BeanDefinition parent) {
095: forcedParent = parent;
096: }
097:
098: protected void preProcess(Element element) {
099: super .preProcess(element);
100: targetPropertyConfiguration.reset();
101: }
102:
103: // reset the forced parent
104: protected void postProcess(ParserContext context,
105: BeanAssembler assembler, Element element) {
106: super.postProcess(context, assembler, element);
107: forcedParent = null;
108: }
109:
110: }
|