Search in sources :

Example 11 with Node

use of org.w3c.dom.Node in project camel by apache.

the class CamelNamespaceHandler method injectNamespaces.

protected void injectNamespaces(Element element, Binder<Node> binder) {
    NodeList list = element.getChildNodes();
    Namespaces namespaces = null;
    int size = list.getLength();
    for (int i = 0; i < size; i++) {
        Node child = list.item(i);
        if (child instanceof Element) {
            Element childElement = (Element) child;
            Object object = binder.getJAXBNode(child);
            if (object instanceof NamespaceAware) {
                NamespaceAware namespaceAware = (NamespaceAware) object;
                if (namespaces == null) {
                    namespaces = new Namespaces(element);
                }
                namespaces.configure(namespaceAware);
            }
            injectNamespaces(childElement, binder);
        }
    }
}
Also used : Namespaces(org.apache.camel.builder.xml.Namespaces) NamespaceAware(org.apache.camel.spi.NamespaceAware) NodeList(org.w3c.dom.NodeList) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Endpoint(org.apache.camel.Endpoint)

Example 12 with Node

use of org.w3c.dom.Node in project camel by apache.

the class CamelNamespaceHandler method parseSSLContextParametersNode.

private Metadata parseSSLContextParametersNode(Element element, ParserContext context) {
    LOG.trace("Parsing SSLContextParameters {}", element);
    // now parse the key store parameters with JAXB
    Binder<Node> binder;
    try {
        binder = getJaxbContext().createBinder();
    } catch (JAXBException e) {
        throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
    }
    Object value = parseUsingJaxb(element, context, binder);
    if (!(value instanceof SSLContextParametersFactoryBean)) {
        throw new ComponentDefinitionException("Expected an instance of " + SSLContextParametersFactoryBean.class);
    }
    SSLContextParametersFactoryBean scpfb = (SSLContextParametersFactoryBean) value;
    String id = scpfb.getId();
    MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
    factory.setId(".camelBlueprint.passThrough." + id);
    factory.setObject(new PassThroughCallable<Object>(scpfb));
    MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
    factory2.setId(".camelBlueprint.factory." + id);
    factory2.setFactoryComponent(factory);
    factory2.setFactoryMethod("call");
    factory2.setInitMethod("afterPropertiesSet");
    factory2.setDestroyMethod("destroy");
    factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
    ctx.setId(id);
    ctx.setRuntimeClass(SSLContextParameters.class);
    ctx.setFactoryComponent(factory2);
    ctx.setFactoryMethod("getObject");
    // must be lazy as we want CamelContext to be activated first
    ctx.setActivation(ACTIVATION_LAZY);
    LOG.trace("Parsing SSLContextParameters done, returning {}", ctx);
    return ctx;
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) SSLContextParametersFactoryBean(org.apache.camel.util.blueprint.SSLContextParametersFactoryBean)

Example 13 with Node

use of org.w3c.dom.Node in project camel by apache.

the class CamelNamespaceHandler method parseEndpointNode.

private Metadata parseEndpointNode(Element element, ParserContext context) {
    LOG.trace("Parsing Endpoint {}", element);
    // now parse the rests with JAXB
    Binder<Node> binder;
    try {
        binder = getJaxbContext().createBinder();
    } catch (JAXBException e) {
        throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
    }
    Object value = parseUsingJaxb(element, context, binder);
    if (!(value instanceof CamelEndpointFactoryBean)) {
        throw new ComponentDefinitionException("Expected an instance of " + CamelEndpointFactoryBean.class);
    }
    CamelEndpointFactoryBean rcfb = (CamelEndpointFactoryBean) value;
    String id = rcfb.getId();
    MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
    factory.setId(".camelBlueprint.passThrough." + id);
    factory.setObject(new PassThroughCallable<Object>(rcfb));
    MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
    factory2.setId(".camelBlueprint.factory." + id);
    factory2.setFactoryComponent(factory);
    factory2.setFactoryMethod("call");
    factory2.setInitMethod("afterPropertiesSet");
    factory2.setDestroyMethod("destroy");
    factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
    ctx.setId(id);
    ctx.setRuntimeClass(Endpoint.class);
    ctx.setFactoryComponent(factory2);
    ctx.setFactoryMethod("getObject");
    // must be lazy as we want CamelContext to be activated first
    ctx.setActivation(ACTIVATION_LAZY);
    LOG.trace("Parsing endpoint done, returning {}", element, ctx);
    return ctx;
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) CamelEndpointFactoryBean(org.apache.camel.blueprint.CamelEndpointFactoryBean)

Example 14 with Node

use of org.w3c.dom.Node in project camel by apache.

the class CamelNamespaceHandler method parseCamelContextNode.

private Metadata parseCamelContextNode(Element element, ParserContext context) {
    LOG.trace("Parsing CamelContext {}", element);
    // Find the id, generate one if needed
    String contextId = element.getAttribute("id");
    boolean implicitId = false;
    // let's avoid folks having to explicitly give an ID to a camel context
    if (ObjectHelper.isEmpty(contextId)) {
        // if no explicit id was set then use a default auto generated name
        CamelContextNameStrategy strategy = new DefaultCamelContextNameStrategy();
        contextId = strategy.getName();
        element.setAttributeNS(null, "id", contextId);
        implicitId = true;
    }
    // now let's parse the routes with JAXB
    Binder<Node> binder;
    try {
        binder = getJaxbContext().createBinder();
    } catch (JAXBException e) {
        throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
    }
    Object value = parseUsingJaxb(element, context, binder);
    if (!(value instanceof CamelContextFactoryBean)) {
        throw new ComponentDefinitionException("Expected an instance of " + CamelContextFactoryBean.class);
    }
    CamelContextFactoryBean ccfb = (CamelContextFactoryBean) value;
    ccfb.setImplicitId(implicitId);
    // The properties component is always used / created by the CamelContextFactoryBean
    // so we need to ensure that the resolver is ready to use
    ComponentMetadata propertiesComponentResolver = getComponentResolverReference(context, "properties");
    MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
    factory.setId(".camelBlueprint.passThrough." + contextId);
    factory.setObject(new PassThroughCallable<Object>(value));
    MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
    factory2.setId(".camelBlueprint.factory." + contextId);
    factory2.setFactoryComponent(factory);
    factory2.setFactoryMethod("call");
    factory2.setInitMethod("afterPropertiesSet");
    factory2.setDestroyMethod("destroy");
    factory2.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    factory2.addProperty("bundleContext", createRef(context, "blueprintBundleContext"));
    factory2.addDependsOn(propertiesComponentResolver.getId());
    // We need to add other components which the camel context dependsOn
    if (ObjectHelper.isNotEmpty(ccfb.getDependsOn())) {
        factory2.setDependsOn(Arrays.asList(ccfb.getDependsOn().split(" |,")));
    }
    context.getComponentDefinitionRegistry().registerComponentDefinition(factory2);
    MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
    ctx.setId(contextId);
    ctx.setRuntimeClass(BlueprintCamelContext.class);
    ctx.setFactoryComponent(factory2);
    ctx.setFactoryMethod("getContext");
    ctx.setInitMethod("init");
    ctx.setDestroyMethod("destroy");
    // Register factory beans
    registerBeans(context, contextId, ccfb.getThreadPools());
    registerBeans(context, contextId, ccfb.getEndpoints());
    registerBeans(context, contextId, ccfb.getRedeliveryPolicies());
    registerBeans(context, contextId, ccfb.getBeansFactory());
    // Register processors
    MutablePassThroughMetadata beanProcessorFactory = context.createMetadata(MutablePassThroughMetadata.class);
    beanProcessorFactory.setId(".camelBlueprint.processor.bean.passThrough." + contextId);
    beanProcessorFactory.setObject(new PassThroughCallable<Object>(new CamelInjector(contextId)));
    MutableBeanMetadata beanProcessor = context.createMetadata(MutableBeanMetadata.class);
    beanProcessor.setId(".camelBlueprint.processor.bean." + contextId);
    beanProcessor.setRuntimeClass(CamelInjector.class);
    beanProcessor.setFactoryComponent(beanProcessorFactory);
    beanProcessor.setFactoryMethod("call");
    beanProcessor.setProcessor(true);
    beanProcessor.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    context.getComponentDefinitionRegistry().registerComponentDefinition(beanProcessor);
    MutablePassThroughMetadata regProcessorFactory = context.createMetadata(MutablePassThroughMetadata.class);
    regProcessorFactory.setId(".camelBlueprint.processor.registry.passThrough." + contextId);
    regProcessorFactory.setObject(new PassThroughCallable<Object>(new CamelDependenciesFinder(contextId, context)));
    MutableBeanMetadata regProcessor = context.createMetadata(MutableBeanMetadata.class);
    regProcessor.setId(".camelBlueprint.processor.registry." + contextId);
    regProcessor.setRuntimeClass(CamelDependenciesFinder.class);
    regProcessor.setFactoryComponent(regProcessorFactory);
    regProcessor.setFactoryMethod("call");
    regProcessor.setProcessor(true);
    regProcessor.addDependsOn(".camelBlueprint.processor.bean." + contextId);
    regProcessor.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
    context.getComponentDefinitionRegistry().registerComponentDefinition(regProcessor);
    // lets inject the namespaces into any namespace aware POJOs
    injectNamespaces(element, binder);
    LOG.trace("Parsing CamelContext done, returning {}", ctx);
    return ctx;
}
Also used : MutablePassThroughMetadata(org.apache.aries.blueprint.mutable.MutablePassThroughMetadata) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) DefaultCamelContextNameStrategy(org.apache.camel.impl.DefaultCamelContextNameStrategy) CamelContextNameStrategy(org.apache.camel.spi.CamelContextNameStrategy) DefaultCamelContextNameStrategy(org.apache.camel.impl.DefaultCamelContextNameStrategy) CamelContextFactoryBean(org.apache.camel.blueprint.CamelContextFactoryBean)

Example 15 with Node

use of org.w3c.dom.Node in project camel by apache.

the class CamelNamespaceHandler method doBeforeParse.

/**
     * Prepares the nodes before parsing.
     */
public static void doBeforeParse(Node node, String fromNamespace, String toNamespace) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Document doc = node.getOwnerDocument();
        if (node.getNamespaceURI().equals(fromNamespace)) {
            doc.renameNode(node, toNamespace, node.getLocalName());
        }
        // remove whitespace noise from uri, xxxUri attributes, eg new lines, and tabs etc, which allows end users to format
        // their Camel routes in more human readable format, but at runtime those attributes must be trimmed
        // the parser removes most of the noise, but keeps double spaces in the attribute values
        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Node att = map.item(i);
            if (att.getNodeName().equals("uri") || att.getNodeName().endsWith("Uri")) {
                final String value = att.getNodeValue();
                String before = ObjectHelper.before(value, "?");
                String after = ObjectHelper.after(value, "?");
                if (before != null && after != null) {
                    // remove all double spaces in the uri parameters
                    String changed = after.replaceAll("\\s{2,}", "");
                    if (!after.equals(changed)) {
                        String newAtr = before.trim() + "?" + changed.trim();
                        LOG.debug("Removed whitespace noise from attribute {} -> {}", value, newAtr);
                        att.setNodeValue(newAtr);
                    }
                }
            }
        }
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        doBeforeParse(list.item(i), fromNamespace, toNamespace);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Endpoint(org.apache.camel.Endpoint)

Aggregations

Node (org.w3c.dom.Node)2347 NodeList (org.w3c.dom.NodeList)1062 Element (org.w3c.dom.Element)720 Document (org.w3c.dom.Document)545 NamedNodeMap (org.w3c.dom.NamedNodeMap)333 ArrayList (java.util.ArrayList)318 DocumentBuilder (javax.xml.parsers.DocumentBuilder)202 IOException (java.io.IOException)176 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)147 Test (org.junit.Test)132 HashMap (java.util.HashMap)127 Attr (org.w3c.dom.Attr)126 SAXException (org.xml.sax.SAXException)107 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)98 HashSet (java.util.HashSet)86 InputSource (org.xml.sax.InputSource)75 XPath (javax.xml.xpath.XPath)70 List (java.util.List)67 File (java.io.File)62 ByteArrayInputStream (java.io.ByteArrayInputStream)57