1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.spring; |
8 | |
|
9 | |
import org.mule.config.spring.util.SpringXMLUtils; |
10 | |
import org.mule.util.StringUtils; |
11 | |
|
12 | |
import javax.xml.parsers.DocumentBuilderFactory; |
13 | |
import javax.xml.parsers.ParserConfigurationException; |
14 | |
|
15 | |
import org.apache.commons.logging.Log; |
16 | |
import org.apache.commons.logging.LogFactory; |
17 | |
import org.springframework.beans.factory.config.BeanDefinition; |
18 | |
import org.springframework.beans.factory.config.BeanDefinitionHolder; |
19 | |
import org.springframework.beans.factory.parsing.BeanComponentDefinition; |
20 | |
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; |
21 | |
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate; |
22 | |
import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader; |
23 | |
import org.springframework.beans.factory.xml.NamespaceHandler; |
24 | |
import org.springframework.beans.factory.xml.ParserContext; |
25 | |
import org.springframework.beans.factory.xml.XmlReaderContext; |
26 | |
import org.w3c.dom.Document; |
27 | |
import org.w3c.dom.Element; |
28 | |
import org.w3c.dom.NodeList; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class MuleHierarchicalBeanDefinitionParserDelegate extends BeanDefinitionParserDelegate |
40 | |
{ |
41 | |
|
42 | |
public static final String BEANS = "beans"; |
43 | |
public static final String MULE_REPEAT_PARSE = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_REPEAT_PARSE"; |
44 | |
public static final String MULE_NO_RECURSE = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_RECURSE"; |
45 | |
public static final String MULE_FORCE_RECURSE = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_FORCE_RECURSE"; |
46 | |
public static final String MULE_NO_REGISTRATION = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_REGISTRATION"; |
47 | |
public static final String MULE_POST_CHILDREN = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_POST_CHILDREN"; |
48 | |
private DefaultBeanDefinitionDocumentReader spring; |
49 | |
|
50 | 0 | protected static final Log logger = LogFactory.getLog(MuleHierarchicalBeanDefinitionParserDelegate.class); |
51 | |
|
52 | |
public MuleHierarchicalBeanDefinitionParserDelegate(XmlReaderContext readerContext, |
53 | |
DefaultBeanDefinitionDocumentReader spring) |
54 | |
{ |
55 | 0 | super(readerContext); |
56 | 0 | this.spring = spring; |
57 | 0 | } |
58 | |
|
59 | |
public BeanDefinition parseCustomElement(Element element, BeanDefinition parent) |
60 | |
{ |
61 | 0 | if (logger.isDebugEnabled()) |
62 | |
{ |
63 | 0 | logger.debug("parsing: " + SpringXMLUtils.elementToString(element)); |
64 | |
} |
65 | 0 | if (SpringXMLUtils.isBeansNamespace(element)) |
66 | |
{ |
67 | 0 | return handleSpringElements(element, parent); |
68 | |
} |
69 | |
else |
70 | |
{ |
71 | 0 | String namespaceUri = element.getNamespaceURI(); |
72 | 0 | NamespaceHandler handler = getReaderContext().getNamespaceHandlerResolver().resolve(namespaceUri); |
73 | 0 | if (handler == null) |
74 | |
{ |
75 | 0 | getReaderContext().error("Unable to locate NamespaceHandler for namespace [" + namespaceUri + "]", element); |
76 | 0 | return null; |
77 | |
} |
78 | |
|
79 | 0 | boolean noRecurse = false; |
80 | 0 | boolean forceRecurse = false; |
81 | |
BeanDefinition finalChild; |
82 | |
|
83 | |
do { |
84 | 0 | ParserContext parserContext = new ParserContext(getReaderContext(), this, parent); |
85 | 0 | finalChild = handler.parse(element, parserContext); |
86 | 0 | registerBean(element, finalChild); |
87 | 0 | noRecurse = noRecurse || testFlag(finalChild, MULE_NO_RECURSE); |
88 | 0 | forceRecurse = forceRecurse || testFlag(finalChild, MULE_FORCE_RECURSE); |
89 | 0 | } while (null != finalChild && testFlag(finalChild, MULE_REPEAT_PARSE)); |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
boolean isRecurse; |
102 | 0 | if (noRecurse) |
103 | |
{ |
104 | |
|
105 | 0 | isRecurse = false; |
106 | |
} |
107 | |
else |
108 | |
{ |
109 | 0 | if (forceRecurse) |
110 | |
{ |
111 | 0 | isRecurse = true; |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | |
|
116 | 0 | isRecurse = SpringXMLUtils.isMuleNamespace(element); |
117 | |
} |
118 | |
} |
119 | |
|
120 | 0 | if (isRecurse) |
121 | |
{ |
122 | 0 | NodeList list = element.getChildNodes(); |
123 | 0 | for (int i = 0; i < list.getLength(); i++) |
124 | |
{ |
125 | 0 | if (list.item(i) instanceof Element) |
126 | |
{ |
127 | 0 | parseCustomElement((Element) list.item(i), finalChild); |
128 | |
} |
129 | |
} |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | 0 | if (testFlag(finalChild, MULE_POST_CHILDREN)) |
135 | |
{ |
136 | 0 | ParserContext parserContext = new ParserContext(getReaderContext(), this, parent); |
137 | 0 | finalChild = handler.parse(element, parserContext); |
138 | |
} |
139 | |
|
140 | 0 | return finalChild; |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
protected BeanDefinition handleSpringElements(Element element, BeanDefinition parent) |
145 | |
{ |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | 0 | if (SpringXMLUtils.isLocalName(element, BEANS)) |
151 | |
{ |
152 | |
|
153 | |
|
154 | |
|
155 | |
try |
156 | |
{ |
157 | 0 | Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); |
158 | 0 | doc.appendChild(doc.importNode(element, true)); |
159 | 0 | spring.registerBeanDefinitions(doc, getReaderContext()); |
160 | |
} |
161 | 0 | catch (ParserConfigurationException e) |
162 | |
{ |
163 | 0 | throw new RuntimeException(e); |
164 | 0 | } |
165 | 0 | return parent; |
166 | |
} |
167 | |
|
168 | 0 | else if (SpringXMLUtils.isLocalName(element, PROPERTY_ELEMENT)) |
169 | |
{ |
170 | 0 | parsePropertyElement(element, parent); |
171 | 0 | return parent; |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | 0 | else if (SpringXMLUtils.isLocalName(element, BEAN_ELEMENT)) |
194 | |
{ |
195 | 0 | BeanDefinitionHolder holder = parseBeanDefinitionElement(element, parent); |
196 | 0 | registerBeanDefinitionHolder(holder); |
197 | 0 | return holder.getBeanDefinition(); |
198 | |
} |
199 | |
else |
200 | |
{ |
201 | 0 | throw new IllegalStateException("Unexpected Spring element: " + SpringXMLUtils.elementToString(element)); |
202 | |
} |
203 | |
} |
204 | |
|
205 | |
protected void registerBean(Element ele, BeanDefinition bd) |
206 | |
{ |
207 | 0 | if (bd == null) |
208 | |
{ |
209 | 0 | return; |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | 0 | if (! testFlag(bd, MULE_NO_REGISTRATION)) |
216 | |
{ |
217 | 0 | String name = generateChildBeanName(ele); |
218 | 0 | logger.debug("register " + name + ": " + bd.getBeanClassName()); |
219 | 0 | registerBeanDefinitionHolder(new BeanDefinitionHolder(bd, name)); |
220 | |
} |
221 | 0 | } |
222 | |
|
223 | |
protected void registerBeanDefinitionHolder(BeanDefinitionHolder bdHolder) |
224 | |
{ |
225 | |
|
226 | |
|
227 | 0 | BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry()); |
228 | |
|
229 | 0 | getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder)); |
230 | 0 | } |
231 | |
|
232 | |
protected String generateChildBeanName(Element e) |
233 | |
{ |
234 | 0 | String id = SpringXMLUtils.getNameOrId(e); |
235 | 0 | if (StringUtils.isBlank(id)) |
236 | |
{ |
237 | 0 | String parentId = SpringXMLUtils.getNameOrId((Element) e.getParentNode()); |
238 | 0 | return "." + parentId + ":" + e.getLocalName(); |
239 | |
} |
240 | |
else |
241 | |
{ |
242 | 0 | return id; |
243 | |
} |
244 | |
} |
245 | |
|
246 | |
public static void setFlag(BeanDefinition bean, String flag) |
247 | |
{ |
248 | 0 | bean.setAttribute(flag, Boolean.TRUE); |
249 | 0 | } |
250 | |
|
251 | |
public static boolean testFlag(BeanDefinition bean, String flag) |
252 | |
{ |
253 | 0 | return null != bean |
254 | |
&& bean.hasAttribute(flag) |
255 | |
&& bean.getAttribute(flag) instanceof Boolean |
256 | |
&& ((Boolean) bean.getAttribute(flag)).booleanValue(); |
257 | |
} |
258 | |
|
259 | |
} |