001: /*
002: * $Id: AbstractChildDefinitionParser.java 11373 2008-03-15 05:03:10Z dfeist $
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.generic.AutoIdUtils;
014: import org.mule.config.spring.util.SpringXMLUtils;
015: import org.mule.util.StringUtils;
016:
017: import org.springframework.beans.factory.config.RuntimeBeanReference;
018: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
019: import org.springframework.beans.factory.xml.ParserContext;
020: import org.w3c.dom.Element;
021:
022: /**
023: * This definition parser supports the definition of beans that are then set on the parent bean -
024: * it extends {@link org.mule.config.spring.parsers.AbstractHierarchicalDefinitionParser} with
025: * methods that assume the data are associated with a single property.
026: *
027: * This supports collections and Maps. For collections if a child element is repeated it will be assumed
028: * that it is a collection.
029: *
030: * If the Bean Class for this element is set to
031: * {@link org.mule.config.spring.parsers.collection.ChildMapEntryDefinitionParser.KeyValuePair} it is assumed that a Map
032: * is being processed and any child elements will be added to the parent Map. Similarly for
033: * {@link org.mule.config.spring.parsers.collection.ChildListEntryDefinitionParser}.
034: *
035: * A single method needs to be overriden called {@link #getPropertyName} that determines the name of the property to
036: * set on the parent bean with this bean. Note that the property name can be dynamically resolved depending on the parent
037: * element.
038: *
039: * @see org.mule.config.spring.parsers.generic.ChildDefinitionParser
040: * @see org.mule.config.spring.parsers.collection.ChildMapEntryDefinitionParser.KeyValuePair
041: * @see AbstractMuleBeanDefinitionParser
042: */
043: public abstract class AbstractChildDefinitionParser extends
044: AbstractHierarchicalDefinitionParser implements
045: MuleChildDefinitionParser {
046:
047: protected final void doParse(Element element,
048: ParserContext parserContext, BeanDefinitionBuilder builder) {
049: setRegistry(parserContext.getRegistry());
050: parseChild(element, parserContext, builder);
051: }
052:
053: protected void parseChild(Element element,
054: ParserContext parserContext, BeanDefinitionBuilder builder) {
055: builder.setSingleton(isSingleton());
056: super .doParse(element, parserContext, builder);
057: }
058:
059: protected void postProcess(ParserContext context,
060: BeanAssembler assembler, Element element) {
061: super .postProcess(context, assembler, element);
062:
063: // legacy handling of orphan beans - avoid setting parent
064: String propertyName = getPropertyName(element);
065: if (null != propertyName) {
066: // If this is a singleton we need to inject it into parent using a
067: // RuntimeBeanReference so that the bean does not get created twice, once
068: // with a name and once as an (inner bean).
069: if (!assembler.getBean().getBeanDefinition().isSingleton()) {
070: assembler.insertBeanInTarget(propertyName);
071: } else {
072: assembler
073: .getTarget()
074: .getPropertyValues()
075: .addPropertyValue(
076: propertyName,
077: new RuntimeBeanReference(
078: element
079: .getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_NAME)));
080: }
081: }
082: }
083:
084: public String getBeanName(Element e) {
085: String name = SpringXMLUtils.getNameOrId(e);
086: if (StringUtils.isBlank(name)) {
087: String parentId = getParentBeanName(e);
088: if (!parentId.startsWith(".")) {
089: parentId = "." + parentId;
090: }
091: return AutoIdUtils.uniqueValue(parentId + ":"
092: + e.getLocalName());
093: } else {
094: return name;
095: }
096: }
097:
098: public abstract String getPropertyName(Element element);
099:
100: }
|