1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.spring.parsers.specific; |
8 | |
|
9 | |
import org.mule.config.spring.parsers.assembly.BeanAssembler; |
10 | |
import org.mule.config.spring.parsers.assembly.BeanAssemblerFactory; |
11 | |
import org.mule.config.spring.parsers.assembly.DefaultBeanAssembler; |
12 | |
import org.mule.config.spring.parsers.assembly.DefaultBeanAssemblerFactory; |
13 | |
import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration; |
14 | |
import org.mule.config.spring.parsers.generic.ParentDefinitionParser; |
15 | |
import org.mule.routing.MessageFilter; |
16 | |
|
17 | |
import java.util.List; |
18 | |
|
19 | |
import org.springframework.beans.MutablePropertyValues; |
20 | |
import org.springframework.beans.PropertyValue; |
21 | |
import org.springframework.beans.factory.config.BeanDefinition; |
22 | |
import org.springframework.beans.factory.config.RuntimeBeanReference; |
23 | |
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
24 | |
import org.springframework.beans.factory.support.ManagedList; |
25 | |
import org.w3c.dom.Element; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class FilterRefDefinitionParser extends ParentDefinitionParser |
32 | |
{ |
33 | |
public static final String FILTER = "filter"; |
34 | |
|
35 | |
@Override |
36 | |
protected void preProcess(Element element) |
37 | |
{ |
38 | 0 | super.preProcess(element); |
39 | 0 | if (isWrapWithMessageFilter(element)) |
40 | |
{ |
41 | 0 | setBeanAssemblerFactory(new MessageProcessorWrappingBeanAssemblerFactory()); |
42 | 0 | addAlias(ATTRIBUTE_REF, "messageProcessor"); |
43 | |
} |
44 | |
else |
45 | |
{ |
46 | 0 | setBeanAssemblerFactory(new DefaultBeanAssemblerFactory()); |
47 | 0 | addAlias(ATTRIBUTE_REF, FILTER); |
48 | |
} |
49 | 0 | } |
50 | |
|
51 | |
private boolean isWrapWithMessageFilter(Element e) |
52 | |
{ |
53 | 0 | String parentName = e.getParentNode().getLocalName().toLowerCase(); |
54 | 0 | String grandParentName = e.getParentNode().getParentNode().getLocalName().toLowerCase(); |
55 | |
|
56 | 0 | return !("message-filter".equals(parentName) || "and-filter".equals(parentName) |
57 | |
|| "or-filter".equals(parentName) || "not-filter".equals(parentName) |
58 | |
|| "outbound".equals(grandParentName) || "selective-consumer-router".equals(parentName) |
59 | |
|| "error-filter".equals(parentName) || "when".equals(parentName)); |
60 | |
} |
61 | |
|
62 | 0 | private static class MessageProcessorWrappingBeanAssemblerFactory implements BeanAssemblerFactory |
63 | |
{ |
64 | |
public BeanAssembler newBeanAssembler(PropertyConfiguration beanConfig, |
65 | |
BeanDefinitionBuilder bean, |
66 | |
PropertyConfiguration targetConfig, |
67 | |
BeanDefinition target) |
68 | |
{ |
69 | 0 | return new MessageProcessorWrappingBeanAssembler(beanConfig, bean, targetConfig, target); |
70 | |
} |
71 | |
} |
72 | |
|
73 | 0 | private static class MessageProcessorWrappingBeanAssembler extends DefaultBeanAssembler |
74 | |
{ |
75 | |
public MessageProcessorWrappingBeanAssembler(PropertyConfiguration beanConfig, |
76 | |
BeanDefinitionBuilder bean, |
77 | |
PropertyConfiguration targetConfig, |
78 | |
BeanDefinition target) |
79 | |
{ |
80 | 0 | super(beanConfig, bean, targetConfig, target); |
81 | 0 | } |
82 | |
|
83 | |
@Override |
84 | |
public void copyBeanToTarget() |
85 | |
{ |
86 | 0 | String oldName = "messageProcessor"; |
87 | 0 | assertTargetPresent(); |
88 | 0 | String newName = bestGuessName(targetConfig, oldName, target.getBeanClassName()); |
89 | 0 | MutablePropertyValues targetProperties = target.getPropertyValues(); |
90 | 0 | MutablePropertyValues beanProperties = bean.getBeanDefinition().getPropertyValues(); |
91 | 0 | Object value = beanProperties.getPropertyValue(newName).getValue(); |
92 | 0 | RuntimeBeanReference ref = (RuntimeBeanReference) ((ManagedList<?>) value).get(0); |
93 | |
|
94 | 0 | BeanDefinitionBuilder messageFilter = BeanDefinitionBuilder.genericBeanDefinition(MessageFilter.class); |
95 | 0 | messageFilter.addPropertyValue(FILTER, ref); |
96 | |
|
97 | 0 | PropertyValue pv = targetProperties.getPropertyValue(newName); |
98 | 0 | Object oldValue = null == pv ? null : pv.getValue(); |
99 | |
|
100 | 0 | if (oldValue == null) |
101 | |
{ |
102 | 0 | oldValue = new ManagedList<Object>(); |
103 | 0 | pv = new PropertyValue(newName, oldValue); |
104 | 0 | targetProperties.addPropertyValue(pv); |
105 | |
} |
106 | 0 | if (targetConfig.isCollection(oldName)) |
107 | |
{ |
108 | 0 | List list = retrieveList(oldValue); |
109 | 0 | list.add(messageFilter.getBeanDefinition()); |
110 | 0 | } |
111 | |
else |
112 | |
{ |
113 | 0 | targetProperties.addPropertyValue(newName, messageFilter.getBeanDefinition()); |
114 | |
} |
115 | 0 | } |
116 | |
} |
117 | |
|
118 | |
} |