private void loadRoutesDefinition( InputStream resourceAsStream, CamelContext context, String commandName) throws Exception { SmtpServer smtpd = mailMock.getSmtp(); ImapServer imapd = mailMock.getImap(); Map<String, Object> values = MapBuilder.<String, Object>newMapBuilder() .put("imaphost", imapd.getBindTo()) .put("imapport", imapd.getPort()) .put("commandname", commandName) .put("command", "D1=true;A2=123") .put("smtphost", smtpd.getBindTo()) .put("smtpport", smtpd.getPort()) .build(); String xml = replacePlaceHolders(toString(resourceAsStream), values); InputStream is = new ByteArrayInputStream(xml.getBytes()); try { context.addRouteDefinitions(context.loadRoutesDefinition(is).getRoutes()); } finally { is.close(); } }
@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("kura.camel"); 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; } }