01: /*
02: * $Id: MapDefinitionParserMutator.java 10494 2008-01-23 21:09:56Z acooke $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.config.spring.parsers.delegate;
12:
13: import org.mule.config.spring.parsers.MuleChildDefinitionParser;
14: import org.mule.config.spring.parsers.MuleDefinitionParser;
15: import org.mule.config.spring.parsers.assembly.BeanAssembler;
16: import org.mule.config.spring.parsers.assembly.TwoStageMapBeanAssemblerFactory;
17: import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
18: import org.mule.config.spring.parsers.generic.ChildDefinitionParser;
19:
20: import java.util.HashMap;
21: import java.util.Map;
22:
23: import org.springframework.beans.factory.config.BeanDefinition;
24: import org.springframework.beans.factory.support.AbstractBeanDefinition;
25: import org.springframework.beans.factory.xml.ParserContext;
26: import org.w3c.dom.Element;
27:
28: /**
29: * This changes a {@link org.mule.config.spring.parsers.generic.ChildDefinitionParser}
30: * so that it generates a map instead of a bean definition. This is useful for converting
31: * parsers to work with the object factory (which requires a map).
32: */
33: public class MapDefinitionParserMutator extends
34: AbstractDelegatingDefinitionParser implements
35: TwoStageMapBeanAssemblerFactory.BeanAssemblerStore,
36: MuleChildDefinitionParser {
37:
38: private String setter;
39: private Element currentElement;
40: private Map pendingAssemblers = new HashMap();
41:
42: public MapDefinitionParserMutator(String setter,
43: ChildDefinitionParser delegate) {
44: super (new MuleDefinitionParser[] { delegate });
45: this .setter = setter;
46: // this is where we set the callback
47: delegate
48: .setBeanAssemblerFactory(new TwoStageMapBeanAssemblerFactory(
49: this ));
50: }
51:
52: public AbstractBeanDefinition muleParse(Element element,
53: ParserContext parserContext) {
54: if (pendingAssemblers.containsKey(element)) {
55: // this is the second call, after the children have been processed
56: BeanAssembler beanAssembler = (BeanAssembler) pendingAssemblers
57: .get(element);
58: pendingAssemblers.remove(element);
59: beanAssembler.insertBeanInTarget(setter);
60: return null;
61: } else {
62: // first call, so process in normal manner, but set current element so that
63: // when the store callback is called, we can associate the assembler with this
64: // element
65: currentElement = element;
66: return getChildDelegate().muleParse(element, parserContext);
67: }
68: }
69:
70: public void saveBeanAssembler(BeanAssembler beanAssembler) {
71: // this is called by the bean assembler from inside parseDelegate above.
72: pendingAssemblers.put(currentElement, beanAssembler);
73: }
74:
75: protected ChildDefinitionParser getChildDelegate() {
76: return (ChildDefinitionParser) getDelegate(0);
77: }
78:
79: public void forceParent(BeanDefinition parent) {
80: getChildDelegate().forceParent(parent);
81: }
82:
83: public PropertyConfiguration getTargetPropertyConfiguration() {
84: return getChildDelegate().getTargetPropertyConfiguration();
85: }
86:
87: }
|