| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.spring.xml; |
| 19 |
|
|
| 20 |
|
import org.apache.camel.spring.CamelContextFactoryBean; |
| 21 |
|
import org.apache.camel.spring.EndpointFactoryBean; |
| 22 |
|
import org.apache.camel.spring.CamelBeanPostProcessor; |
| 23 |
|
import static org.apache.camel.util.ObjectHelper.isNotNullOrBlank; |
| 24 |
|
import org.apache.camel.builder.xml.XPathBuilder; |
| 25 |
|
import org.springframework.beans.factory.config.BeanDefinition; |
| 26 |
|
import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 27 |
|
import org.springframework.beans.factory.parsing.BeanComponentDefinition; |
| 28 |
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 29 |
|
import org.springframework.beans.factory.xml.NamespaceHandlerSupport; |
| 30 |
|
import org.springframework.beans.factory.xml.ParserContext; |
| 31 |
|
import org.springframework.util.xml.DomUtils; |
| 32 |
|
import org.w3c.dom.Element; |
| 33 |
|
import org.w3c.dom.Node; |
| 34 |
|
import org.w3c.dom.NodeList; |
| 35 |
|
|
| 36 |
|
import java.util.Set; |
| 37 |
|
import java.util.HashSet; |
| 38 |
|
|
| 39 |
24 |
public class CamelNamespaceHandler extends NamespaceHandlerSupport { |
| 40 |
24 |
protected CamelBeanDefinitionParser routesParser = new CamelBeanDefinitionParser(this); |
| 41 |
24 |
protected BeanDefinitionParser endpointParser = new BeanDefinitionParser(EndpointFactoryBean.class); |
| 42 |
24 |
protected BeanDefinitionParser beanPostProcessorParser = new BeanDefinitionParser(CamelBeanPostProcessor.class); |
| 43 |
24 |
protected Set<String> parserElementNames = new HashSet<String>(); |
| 44 |
|
|
| 45 |
|
public void init() { |
| 46 |
24 |
registerParser("routes", routesParser); |
| 47 |
24 |
registerParser("routeBuilder", routesParser); |
| 48 |
24 |
registerParser("endpoint", endpointParser); |
| 49 |
|
|
| 50 |
24 |
registerParser("camelContext", new BeanDefinitionParser(CamelContextFactoryBean.class) { |
| 51 |
|
@Override |
| 52 |
24 |
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { |
| 53 |
27 |
super.doParse(element, parserContext, builder); |
| 54 |
|
|
| 55 |
27 |
String contextId = element.getAttribute("id"); |
| 56 |
|
|
| 57 |
27 |
Element routes = element.getOwnerDocument().createElement("routes"); |
| 58 |
|
|
| 59 |
27 |
NodeList list = element.getChildNodes(); |
| 60 |
121 |
for (int size = list.getLength(), i = 0; i < size; i++) { |
| 61 |
94 |
Node child = list.item(i); |
| 62 |
94 |
if (child instanceof Element) { |
| 63 |
35 |
Element childElement = (Element) child; |
| 64 |
35 |
if (child.getLocalName().equals("beanPostProcessor")) { |
| 65 |
4 |
String beanPostProcessorId = contextId + ":beanPostProcessor"; |
| 66 |
4 |
childElement.setAttribute("id", beanPostProcessorId); |
| 67 |
4 |
BeanDefinition definition = beanPostProcessorParser.parse(childElement, parserContext); |
| 68 |
4 |
definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId)); |
| 69 |
4 |
} |
| 70 |
|
else { |
| 71 |
31 |
element.removeChild(child); |
| 72 |
31 |
routes.appendChild(child); |
| 73 |
|
} |
| 74 |
|
} |
| 75 |
|
} |
| 76 |
27 |
String routeId = contextId + ":routes"; |
| 77 |
27 |
routes.setAttribute("id", routeId); |
| 78 |
|
|
| 79 |
27 |
BeanDefinition definition = routesParser.parse(routes, parserContext); |
| 80 |
27 |
definition.getPropertyValues().addPropertyValue("context", new RuntimeBeanReference(contextId)); |
| 81 |
27 |
parserContext.registerComponent(new BeanComponentDefinition(definition, routeId)); |
| 82 |
|
|
| 83 |
27 |
list = routes.getElementsByTagName("endpoint"); |
| 84 |
38 |
for (int size = list.getLength(), i = 0; i < size; i++) { |
| 85 |
11 |
Element node = (Element) list.item(i); |
| 86 |
11 |
definition = endpointParser.parse(node, parserContext); |
| 87 |
11 |
String id = node.getAttribute("id"); |
| 88 |
11 |
if (isNotNullOrBlank(id)) { |
| 89 |
11 |
definition.getPropertyValues().addPropertyValue("context", new RuntimeBeanReference(contextId)); |
| 90 |
|
|
| 91 |
11 |
parserContext.registerComponent(new BeanComponentDefinition(definition, id)); |
| 92 |
|
} |
| 93 |
|
} |
| 94 |
27 |
} |
| 95 |
|
}); |
| 96 |
|
|
| 97 |
24 |
registerParser("xpath", new BeanDefinitionParser(XPathBuilder.class) { |
| 98 |
|
@Override |
| 99 |
24 |
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { |
| 100 |
|
|
| 101 |
2 |
String xpath = DomUtils.getTextValue(element); |
| 102 |
2 |
builder.addConstructorArg(xpath); |
| 103 |
2 |
super.doParse(element, parserContext, builder); |
| 104 |
2 |
builder.addPropertyValue("namespacesFromDom", element); |
| 105 |
2 |
} |
| 106 |
|
}); |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
24 |
registerScriptParser("script", null); |
| 111 |
24 |
registerScriptParser("groovy", "groovy"); |
| 112 |
24 |
registerScriptParser("ruby", "jruby"); |
| 113 |
24 |
registerScriptParser("javaScript", "js"); |
| 114 |
24 |
registerScriptParser("python", "python"); |
| 115 |
24 |
registerScriptParser("php", "php"); |
| 116 |
24 |
} |
| 117 |
|
|
| 118 |
|
protected void registerScriptParser(String elementName, String engineName) { |
| 119 |
144 |
registerParser(elementName, new ScriptDefinitionParser(engineName)); |
| 120 |
144 |
} |
| 121 |
|
|
| 122 |
|
protected void registerParser(String name, org.springframework.beans.factory.xml.BeanDefinitionParser parser) { |
| 123 |
264 |
parserElementNames.add(name); |
| 124 |
264 |
registerBeanDefinitionParser(name, parser); |
| 125 |
264 |
} |
| 126 |
|
|
| 127 |
|
public Set<String> getParserElementNames() { |
| 128 |
2 |
return parserElementNames; |
| 129 |
|
} |
| 130 |
|
} |