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