Java 类org.apache.camel.model.ModelHelper 实例源码

项目:Camel    文件:AbstractLocalCamelController.java   
@SuppressWarnings("deprecation")
public String getRestModelAsXml(String camelContextName) throws Exception {
    CamelContext context = this.getLocalCamelContext(camelContextName);
    if (context == null) {
        return null;
    }

    List<RestDefinition> rests = context.getRestDefinitions();
    if (rests == null || rests.isEmpty()) {
        return null;
    }
    // use a rests definition to dump the rests
    RestsDefinition def = new RestsDefinition();
    def.setRests(rests);
    return ModelHelper.dumpModelAsXml(null, def);
}
项目:Camel    文件:DumpModelAsXmlTransformRouteTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("simple");
    assertEquals(1, nodes.getLength());
    Element node = (Element)nodes.item(0);
    assertNotNull("Node <simple> expected to be instanceof Element", node);
    assertEquals("Hello ${body}", node.getTextContent());

    nodes = doc.getElementsByTagName("to");
    assertEquals(1, nodes.getLength());
    node = (Element)nodes.item(0);
    assertNotNull("Node <to> expected to be instanceof Element", node);
    assertEquals("mock:result", node.getAttribute("uri"));
    assertEquals("myMock", node.getAttribute("id"));
    assertEquals("true", node.getAttribute("customId"));
}
项目:Camel    文件:DumpModelAsXmlTransformRouteConstantTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("constant");
    assertEquals(1, nodes.getLength());
    Element node = (Element)nodes.item(0);
    assertNotNull("Node <simple> expected to be instanceof Element", node);
    assertEquals("Hello World", node.getTextContent());

    nodes = doc.getElementsByTagName("to");
    assertEquals(1, nodes.getLength());
    node = (Element)nodes.item(0);
    assertNotNull("Node <to> expected to be instanceof Element", node);
    assertEquals("mock:result", node.getAttribute("uri"));
    assertEquals("myMock", node.getAttribute("id"));
    assertEquals("true", node.getAttribute("customId"));
}
项目:Camel    文件:DumpModelAsXmlTransformRouteLanguageTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("language");
    assertEquals(1, nodes.getLength());
    Element node = (Element)nodes.item(0);
    assertNotNull("Node <simple> expected to be instanceof Element", node);
    assertEquals("constant", node.getAttribute("language"));
    assertEquals("Hello World", node.getTextContent());

    nodes = doc.getElementsByTagName("to");
    assertEquals(1, nodes.getLength());
    node = (Element)nodes.item(0);
    assertNotNull("Node <to> expected to be instanceof Element", node);
    assertEquals("mock:result", node.getAttribute("uri"));
    assertEquals("myMock", node.getAttribute("id"));
    assertEquals("true", node.getAttribute("customId"));
}
项目:Camel    文件:DumpModelAsXmlSplitBodyRouteTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("simple");
    assertEquals(1, nodes.getLength());
    Element node = (Element)nodes.item(0);
    assertNotNull("Node <simple> expected to be instanceof Element", node);
    assertEquals("body", node.getTextContent());

    nodes = doc.getElementsByTagName("split");
    assertEquals(1, nodes.getLength());

    nodes = doc.getElementsByTagName("to");
    assertEquals(1, nodes.getLength());
    node = (Element)nodes.item(0);
    assertNotNull("Node <to> expected to be instanceof Element", node);
    assertEquals("mock:sub", node.getAttribute("uri"));
    assertEquals("myMock", node.getAttribute("id"));
    assertEquals("true", node.getAttribute("customId"));
}
项目:wildfly-camel    文件:JAXBInitalizationTest.java   
@Test
public void testJaxbDumpModelAsXML() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .routeId("route-1")
            .to("log:test");
        }
    });

    camelctx.start();

    try {
        String xml = ModelHelper.dumpModelAsXml(camelctx, camelctx.getRouteDefinition("route-1"));
        Assert.assertTrue(xml.contains("log:test"));
    } finally {
        camelctx.stop();
    }
}
项目:Camel    文件:AbstractLocalCamelController.java   
@SuppressWarnings("deprecation")
public String getRouteModelAsXml(String routeId, String camelContextName) throws Exception {
    CamelContext context = this.getLocalCamelContext(camelContextName);
    if (context == null) {
        return null;
    }
    RouteDefinition route = context.getRouteDefinition(routeId);
    if (route == null) {
        return null;
    }

    return ModelHelper.dumpModelAsXml(null, route);
}
项目:Camel    文件:ManagedRoute.java   
@Override
public String dumpRouteAsXml(boolean resolvePlaceholders) throws Exception {
    String id = route.getId();
    RouteDefinition def = context.getRouteDefinition(id);
    if (def != null) {
        String xml = ModelHelper.dumpModelAsXml(context, def);

        // if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
        if (resolvePlaceholders) {
            final AtomicBoolean changed = new AtomicBoolean();
            InputStream is = new ByteArrayInputStream(xml.getBytes());
            Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {
                @Override
                public String transform(String text) {
                    try {
                        String after = getContext().resolvePropertyPlaceholders(text);
                        if (!changed.get()) {
                            changed.set(!text.equals(after));
                        }
                        return after;
                    } catch (Exception e) {
                        // ignore
                        return text;
                    }
                }
            });
            // okay there were some property placeholder replaced so re-create the model
            if (changed.get()) {
                xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
                RouteDefinition copy = ModelHelper.createModelFromXml(context, xml, RouteDefinition.class);
                xml = ModelHelper.dumpModelAsXml(context, copy);
            }
        }
        return xml;
    }
    return null;
}
项目:Camel    文件:ManagedRoute.java   
public void updateRouteFromXml(String xml) throws Exception {
    // convert to model from xml
    RouteDefinition def = ModelHelper.createModelFromXml(context, xml, RouteDefinition.class);
    if (def == null) {
        return;
    }

    // if the xml does not contain the route-id then we fix this by adding the actual route id
    // this may be needed if the route-id was auto-generated, as the intend is to update this route
    // and not add a new route, adding a new route, use the MBean operation on ManagedCamelContext instead.
    if (ObjectHelper.isEmpty(def.getId())) {
        def.setId(getRouteId());
    } else if (!def.getId().equals(getRouteId())) {
        throw new IllegalArgumentException("Cannot update route from XML as routeIds does not match. routeId: "
                + getRouteId() + ", routeId from XML: " + def.getId());
    }

    LOG.debug("Updating route: {} from xml: {}", def.getId(), xml);

    try {
        // add will remove existing route first
        context.addRouteDefinition(def);
    } catch (Exception e) {
        // log the error as warn as the management api may be invoked remotely over JMX which does not propagate such exception
        String msg = "Error updating route: " + def.getId() + " from xml: " + xml + " due: " + e.getMessage();
        LOG.warn(msg, e);
        throw e;
    }
}
项目:Camel    文件:DumpModelAsXmlAggregateRouteTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<correlationExpression>"));
    assertTrue(xml.contains("<header>userId</header>"));
    assertTrue(xml.contains("</correlationExpression>"));
}
项目:Camel    文件:DumpModelAsXmlPlaceholdersTest.java   
public void testDumpModelAsXml() throws Exception {
    assertEquals("Gouda", context.getRoutes().get(0).getId());
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("Gouda"));
    assertNotNull(xml);
    log.info(xml);
    assertTrue(xml.contains("<route customId=\"true\" id=\"Gouda\" xmlns=\"http://camel.apache.org/schema/spring\">"));
    assertTrue(xml.contains("<from uri=\"direct:start-{{cheese.type}}\"/>"));
    assertTrue(xml.contains("<to uri=\"direct:end-{{cheese.type}}\" customId=\"true\" id=\"log\"/>"));
}
项目:Camel    文件:DumpModelAsXmlSplitNestedChoiceEndRouteTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("split");
    assertEquals(1, nodes.getLength());
    Element node = (Element)nodes.item(0);
    // there is an empty text document as we pretty print the xml, so need to do 2 x next sibling
    Element last = (Element) node.getNextSibling().getNextSibling();
    assertEquals("mock:last", last.getAttribute("uri"));
}
项目:Camel    文件:DumpModelAsXmlRouteExpressionTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<simple>Hello ${body}</simple>"));
}
项目:Camel    文件:DumpModelAsXmlRouteExpressionTest.java   
public void testDumpModelAsXmlXPath() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myOtherRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<xpath>/foo</xpath>"));
}
项目:Camel    文件:DumpModelAsXmlRouteExpressionTest.java   
public void testDumpModelAsXmlHeader() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myFooRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<header>bar</header>"));
}
项目:Camel    文件:DumpModelAsXmlRouteExpressionTest.java   
public void testDumpModelAsXmlBean() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myBeanRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<setHeader headerName=\"foo\""));
    assertTrue(xml.contains("<method>myCoolBean</method>"));
}
项目:Camel    文件:DumpModelAsXmlChoiceFilterRouteTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<header>gold</header>"));
    assertTrue(xml.contains("<header>extra-gold</header>"));
    assertTrue(xml.contains("<simple>${body} contains Camel</simple>"));
}
项目:Camel    文件:DumpModelAsXmlChoiceFilterRouteTest.java   
public void testDumpModelAsXmAl() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("a"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<constant>bar</constant>"));
    assertTrue(xml.contains("<expressionDefinition>header{test} is not null</expressionDefinition>"));
}
项目:Camel    文件:DumpModelAsXmlRoutePredicateTest.java   
public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<simple>${body} &gt; 10</simple>"));
}
项目:Camel    文件:DumpModelAsXmlRoutePredicateTest.java   
public void testDumpModelAsXmlXPath() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myOtherRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<xpath>/foo</xpath>"));
}
项目:Camel    文件:DumpModelAsXmlRoutePredicateTest.java   
public void testDumpModelAsXmlHeader() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myFooRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<header>bar</header>"));
}
项目:Camel    文件:DumpModelAsXmlRoutePredicateTest.java   
public void testDumpModelAsXmlBean() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myBeanRoute"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<method>myCoolBean</method>"));
}
项目:Camel    文件:PropertiesRouteFromTest.java   
public void testPropertiesRouteFrom() throws Exception {
    ProcessorDefinition out = context.getRouteDefinition("foo").getOutputs().get(0);
    assertEquals("{{cool.end}}", ((SendDefinition) out).getUri());

    String uri = context.getRouteDefinition("foo").getInputs().get(0).getUri();
    assertEquals("{{cool.start}}", uri);

    // use a routes definition to dump the routes
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("foo"));
    assertTrue(xml.contains("<from uri=\"{{cool.start}}\"/>"));
    assertTrue(xml.contains("<to uri=\"{{cool.end}}\""));
}
项目:Camel    文件:DumpModelAsXmlPlaceholdersTest.java   
public void testDumpModelAsXml() throws Exception {
    assertEquals("Gouda", context.getRoutes().get(0).getId());
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("Gouda"));
    assertNotNull(xml);
    log.info(xml);

    assertTrue(xml.contains("<route customId=\"true\" id=\"Gouda\" xmlns=\"http://camel.apache.org/schema/spring\">"));
    assertTrue(xml.contains("<from uri=\"direct:start-{{cheese.type}}\"/>"));
    assertTrue(xml.contains("<to uri=\"direct:end-{{cheese.type}}\" customId=\"true\" id=\"log\"/>"));
}
项目:switchyard    文件:CamelExchangeBus.java   
@Override
public ExchangeDispatcher createDispatcher(final ServiceReference reference) {
    if (_logger.isDebugEnabled()) {
        _logger.debug("Creating Camel dispatcher for " + reference.getName());
    }

    String endpoint = "direct:" + reference.getName();
    RouteBuilder rb = new CamelExchangeBusRouteBuilder(endpoint, reference);

    try {
        // TODO - remove this logic once the test framework is able 
        // to hot-replace a service endpoint.
        if (_camelContext.getRoute(endpoint) != null) {
            _camelContext.removeRoute(endpoint);
            if (_logger.isInfoEnabled()) {
                BusLogger.ROOT_LOGGER.removeRoute(endpoint);
            }
        }

        _camelContext.addRoutes(rb);

        if (_logger.isDebugEnabled()) {
            _logger.debug("Created route for " + endpoint + ", definition is: " + ModelHelper.dumpModelAsXml(_camelContext, rb.getRouteCollection()));
        }
    } catch (Exception ex) {
        throw BusMessages.MESSAGES.failedToCreateRoute(reference.getName(), ex);
    }

    ExchangeDispatcher dispatcher = new ExchangeDispatcher(_camelContext, reference);
    _dispatchers.put(reference.getName(), dispatcher);
    return dispatcher;
}
项目:Camel    文件:ManagedProcessor.java   
@Override
public String dumpProcessorAsXml() throws Exception {
    return ModelHelper.dumpModelAsXml(context, definition);
}
项目:Camel    文件:ManagedCamelContext.java   
@Override
public String dumpRestsAsXml(boolean resolvePlaceholders) throws Exception {
    List<RestDefinition> rests = context.getRestDefinitions();
    if (rests.isEmpty()) {
        return null;
    }

    // use a routes definition to dump the rests
    RestsDefinition def = new RestsDefinition();
    def.setRests(rests);
    String xml = ModelHelper.dumpModelAsXml(context, def);

    // if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
    if (resolvePlaceholders) {
        final AtomicBoolean changed = new AtomicBoolean();
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {
            @Override
            public String transform(String text) {
                try {
                    String after = getContext().resolvePropertyPlaceholders(text);
                    if (!changed.get()) {
                        changed.set(!text.equals(after));
                    }
                    return after;
                } catch (Exception e) {
                    // ignore
                    return text;
                }
            }
        });
        // okay there were some property placeholder replaced so re-create the model
        if (changed.get()) {
            xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
            RestsDefinition copy = ModelHelper.createModelFromXml(context, xml, RestsDefinition.class);
            xml = ModelHelper.dumpModelAsXml(context, copy);
        }
    }

    return xml;
}
项目:Camel    文件:ManagedCamelContext.java   
@Override
public String dumpRoutesAsXml(boolean resolvePlaceholders) throws Exception {
    List<RouteDefinition> routes = context.getRouteDefinitions();
    if (routes.isEmpty()) {
        return null;
    }

    // use a routes definition to dump the routes
    RoutesDefinition def = new RoutesDefinition();
    def.setRoutes(routes);
    String xml = ModelHelper.dumpModelAsXml(context, def);

    // if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
    if (resolvePlaceholders) {
        final AtomicBoolean changed = new AtomicBoolean();
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {
            @Override
            public String transform(String text) {
                try {
                    String after = getContext().resolvePropertyPlaceholders(text);
                    if (!changed.get()) {
                        changed.set(!text.equals(after));
                    }
                    return after;
                } catch (Exception e) {
                    // ignore
                    return text;
                }
            }
        });
        // okay there were some property placeholder replaced so re-create the model
        if (changed.get()) {
            xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
            RoutesDefinition copy = ModelHelper.createModelFromXml(context, xml, RoutesDefinition.class);
            xml = ModelHelper.dumpModelAsXml(context, copy);
        }
    }

    return xml;
}
项目:Camel    文件:RoutesXml.java   
/**
 * Loads the routes from the given XML content
 */
public static RoutesDefinition loadRoutesFromXML(ModelCamelContext camelContext, String xml) throws JAXBException {
    return ModelHelper.createModelFromXml(camelContext, xml, RoutesDefinition.class);
}
项目:Camel    文件:RoutesXml.java   
/**
 * Loads the routes from the classpath
 */
public static RoutesDefinition loadRoutesFromClasspath(ModelCamelContext camelContext, String uri) throws JAXBException {
    InputStream stream = ObjectHelper.loadResourceAsStream(uri);
    ObjectHelper.notNull(stream, "Could not find resource '" + uri + "' on the ClassLoader");
    return ModelHelper.createModelFromXml(camelContext, stream, RoutesDefinition.class);
}
项目:Camel    文件:RoutesXml.java   
/**
 * Loads the routes from a {@link URL}
 */
public static RoutesDefinition loadRoutesFromURL(ModelCamelContext camelContext, URL url) throws JAXBException, IOException {
    ObjectHelper.notNull(url, "url");
    return ModelHelper.createModelFromXml(camelContext, url.openStream(), RoutesDefinition.class);
}
项目:Camel    文件:RoutesXml.java   
/**
 * Loads the routes from a {@link File}
 */
public static RoutesDefinition loadRoutesFromFile(ModelCamelContext camelContext, File file) throws JAXBException, FileNotFoundException {
    ObjectHelper.notNull(file, "file");
    return ModelHelper.createModelFromXml(camelContext, new FileInputStream(file), RoutesDefinition.class);
}
项目:Camel    文件:RouteDefinitionsFromXmlTest.java   
@Produces
private RoutesDefinition routes(CamelContext context) throws Exception {
    try (InputStream routes = getClass().getResourceAsStream("/camel-context-routes.xml")) {
        return ModelHelper.createModelFromXml(context, routes, RoutesDefinition.class);
    }
}
项目:Camel    文件:RestSwaggerSupport.java   
public List<RestDefinition> getRestDefinitions(String camelId) throws Exception {
    ObjectName found = null;
    boolean supportResolvePlaceholder = false;

    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.camel:type=context,*"), null);
    for (ObjectName on : names) {
        String id = on.getKeyProperty("name");
        if (id.startsWith("\"") && id.endsWith("\"")) {
            id = id.substring(1, id.length() - 1);
        }
        if (camelId == null || camelId.equals(id)) {
            // filter out older Camel versions as this requires Camel 2.15 or better (rest-dsl)
            String version = (String) server.getAttribute(on, "CamelVersion");
            if (CamelVersionHelper.isGE("2.15.0", version)) {
                found = on;
            }
            if (CamelVersionHelper.isGE("2.15.3", version)) {
                supportResolvePlaceholder = true;
            }
        }
    }

    if (found != null) {
        String xml;
        if (supportResolvePlaceholder) {
            xml = (String) server.invoke(found, "dumpRestsAsXml", new Object[]{true}, new String[]{"boolean"});
        } else {
            xml = (String) server.invoke(found, "dumpRestsAsXml", null, null);
        }
        if (xml != null) {
            LOG.debug("DumpRestAsXml:\n{}", xml);
            RestsDefinition rests = ModelHelper.createModelFromXml(null, xml, RestsDefinition.class);
            if (rests != null) {
                return rests.getRests();
            }
        }
    }

    return null;
}
项目:camel-cdi    文件:RouteDefinitionsFromXmlTest.java   
@Produces
private RoutesDefinition routes(CamelContext context) throws Exception {
    try (InputStream routes = getClass().getResourceAsStream("/camel-context-routes.xml")) {
        return ModelHelper.createModelFromXml(context, routes, RoutesDefinition.class);
    }
}