Exemplo n.º 1
0
 private boolean addRouteToContext(
     Bean<?> routeBean, Bean<?> contextBean, BeanManager manager, AfterDeploymentValidation adv) {
   try {
     CamelContext context = getReference(manager, CamelContext.class, contextBean);
     try {
       Object route = getReference(manager, Object.class, routeBean);
       if (route instanceof RoutesBuilder) {
         context.addRoutes((RoutesBuilder) route);
       } else if (route instanceof RouteContainer) {
         context.addRouteDefinitions(((RouteContainer) route).getRoutes());
       } else {
         throw new IllegalArgumentException(
             "Invalid routes type ["
                 + routeBean.getBeanClass().getName()
                 + "], "
                 + "must be either of type RoutesBuilder or RouteContainer!");
       }
       return true;
     } catch (Exception cause) {
       adv.addDeploymentProblem(
           new InjectionException(
               "Error adding routes of type ["
                   + routeBean.getBeanClass().getName()
                   + "] "
                   + "to Camel context ["
                   + context.getName()
                   + "]",
               cause));
     }
   } catch (Exception exception) {
     adv.addDeploymentProblem(exception);
   }
   return false;
 }
 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();
   }
 }
Exemplo n.º 3
0
  @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;
    }
  }