Search in sources :

Example 11 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project camel by apache.

the class KuraRouter method start.

// Lifecycle
@Override
public void start(BundleContext bundleContext) throws Exception {
    try {
        this.bundleContext = bundleContext;
        log.debug("Initializing bundle {}.", bundleContext.getBundle().getBundleId());
        camelContext = createCamelContext();
        camelContext.addRoutes(this);
        ConfigurationAdmin configurationAdmin = requiredService(ConfigurationAdmin.class);
        Configuration camelKuraConfig = configurationAdmin.getConfiguration(camelXmlRoutesPid());
        if (camelKuraConfig != null && camelKuraConfig.getProperties() != null) {
            Object routePropertyValue = camelKuraConfig.getProperties().get(camelXmlRoutesProperty());
            if (routePropertyValue != null) {
                InputStream routesXml = new ByteArrayInputStream(routePropertyValue.toString().getBytes());
                RoutesDefinition loadedRoutes = camelContext.loadRoutesDefinition(routesXml);
                camelContext.addRouteDefinitions(loadedRoutes.getRoutes());
            }
        }
        beforeStart(camelContext);
        log.debug("About to start Camel Kura router: {}", getClass().getName());
        camelContext.start();
        producerTemplate = camelContext.createProducerTemplate();
        consumerTemplate = camelContext.createConsumerTemplate();
        log.debug("Bundle {} started.", bundleContext.getBundle().getBundleId());
    } catch (Throwable e) {
        String errorMessage = "Problem when starting Kura module " + getClass().getName() + ":";
        log.warn(errorMessage, e);
        // Print error to the Kura console.
        System.err.println(errorMessage);
        e.printStackTrace();
        throw e;
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RoutesDefinition(org.apache.camel.model.RoutesDefinition) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 12 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project camel by apache.

the class ReloadStrategySupport method onReloadXml.

@Override
public void onReloadXml(CamelContext camelContext, String name, InputStream resource) {
    log.debug("Reloading routes from XML resource: {}", name);
    Document dom;
    String xml;
    try {
        xml = camelContext.getTypeConverter().mandatoryConvertTo(String.class, resource);
        // the JAXB model expects the spring namespace (even for blueprint)
        dom = XmlLineNumberParser.parseXml(new ByteArrayInputStream(xml.getBytes()), null, "camelContext,routes", "http://camel.apache.org/schema/spring");
    } catch (Exception e) {
        failed++;
        log.warn("Cannot load the resource " + name + " as XML");
        return;
    }
    ResourceState state = cache.get(name);
    if (state == null) {
        state = new ResourceState(name, dom, xml);
        cache.put(name, state);
    }
    String oldXml = state.getXml();
    List<Integer> changed = StringHelper.changedLines(oldXml, xml);
    // find all <route> which are the routes
    NodeList list = dom.getElementsByTagName("route");
    // collect which routes are updated/skipped
    List<RouteDefinition> routes = new ArrayList<>();
    if (list != null && list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            // what line number are this within
            String lineNumber = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER);
            String lineNumberEnd = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
            if (lineNumber != null && lineNumberEnd != null && !changed.isEmpty()) {
                int start = Integer.valueOf(lineNumber);
                int end = Integer.valueOf(lineNumberEnd);
                boolean within = withinChanged(start, end, changed);
                if (within) {
                    log.debug("Updating route in lines: {}-{}", start, end);
                } else {
                    log.debug("No changes to route in lines: {}-{}", start, end);
                    continue;
                }
            }
            try {
                // load from XML -> JAXB model and store as routes to be updated
                RoutesDefinition loaded = ModelHelper.loadRoutesDefinition(camelContext, node);
                if (!loaded.getRoutes().isEmpty()) {
                    routes.addAll(loaded.getRoutes());
                }
            } catch (Exception e) {
                failed++;
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
        }
    }
    if (!routes.isEmpty()) {
        try {
            boolean unassignedRouteIds = false;
            CollectionStringBuffer csb = new CollectionStringBuffer(",");
            // collect route ids and force assign ids if not in use
            for (RouteDefinition route : routes) {
                unassignedRouteIds |= route.hasCustomIdAssigned();
                String id = route.idOrCreate(camelContext.getNodeIdFactory());
                csb.append(id);
            }
            log.debug("Reloading routes: [{}] from XML resource: {}", csb, name);
            if (unassignedRouteIds) {
                log.warn("Routes with no id's detected. Its recommended to assign id's to your routes so Camel can reload the routes correctly.");
            }
            // update the routes (add will remove and shutdown first)
            camelContext.addRouteDefinitions(routes);
            log.info("Reloaded routes: [{}] from XML resource: {}", csb, name);
        } catch (Exception e) {
            failed++;
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    // update cache
    state = new ResourceState(name, dom, xml);
    cache.put(name, state);
    succeeded++;
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) RouteDefinition(org.apache.camel.model.RouteDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) RoutesDefinition(org.apache.camel.model.RoutesDefinition)

Example 13 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project camel by apache.

the class CreateModelFromXmlTest method testCreateModelFromXmlForInputStreamWithAdditionalNamespaces.

@Test
public void testCreateModelFromXmlForInputStreamWithAdditionalNamespaces() throws Exception {
    RoutesDefinition routesDefinition = createModelFromXml("simpleRouteWithNamespaces.xml", false);
    assertNotNull(routesDefinition);
    Map<String, String> expectedNamespaces = new LinkedHashMap<>();
    expectedNamespaces.put("xmlns", NS_CAMEL);
    expectedNamespaces.put("foo", NS_FOO);
    expectedNamespaces.put("bar", NS_BAR);
    assertNamespacesPresent(routesDefinition, expectedNamespaces);
}
Also used : RoutesDefinition(org.apache.camel.model.RoutesDefinition) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 14 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project camel by apache.

the class CreateModelFromXmlTest method testCreateModelFromXmlForStringWithDefaultNamespace.

@Test
public void testCreateModelFromXmlForStringWithDefaultNamespace() throws Exception {
    RoutesDefinition routesDefinition = createModelFromXml("simpleRoute.xml", true);
    assertNotNull(routesDefinition);
    Map<String, String> expectedNamespaces = new LinkedHashMap<>();
    expectedNamespaces.put("xmlns", NS_CAMEL);
    assertNamespacesPresent(routesDefinition, expectedNamespaces);
}
Also used : RoutesDefinition(org.apache.camel.model.RoutesDefinition) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 15 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project camel by apache.

the class CreateModelFromXmlTest method testCreateModelFromXmlForStringWithAdditionalNamespaces.

@Test
public void testCreateModelFromXmlForStringWithAdditionalNamespaces() throws Exception {
    RoutesDefinition routesDefinition = createModelFromXml("simpleRouteWithNamespaces.xml", true);
    assertNotNull(routesDefinition);
    Map<String, String> expectedNamespaces = new LinkedHashMap<>();
    expectedNamespaces.put("xmlns", NS_CAMEL);
    expectedNamespaces.put("foo", NS_FOO);
    expectedNamespaces.put("bar", NS_BAR);
    assertNamespacesPresent(routesDefinition, expectedNamespaces);
}
Also used : RoutesDefinition(org.apache.camel.model.RoutesDefinition) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

RoutesDefinition (org.apache.camel.model.RoutesDefinition)20 Test (org.junit.Test)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 RouteDefinition (org.apache.camel.model.RouteDefinition)5 IOException (java.io.IOException)4 LinkedHashMap (java.util.LinkedHashMap)4 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 CamelContext (org.apache.camel.CamelContext)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 Element (de.pdark.decentxml.Element)1 Node (de.pdark.decentxml.Node)1 StepAction (io.syndesis.common.model.action.StepAction)1 FileNotFoundException (java.io.FileNotFoundException)1 StringReader (java.io.StringReader)1 Collections.emptySet (java.util.Collections.emptySet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1