Search in sources :

Example 11 with FromDefinition

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

the class RouteBuilder method populateRests.

protected void populateRests() throws Exception {
    ModelCamelContext camelContext = getContext();
    if (camelContext == null) {
        throw new IllegalArgumentException("CamelContext has not been injected!");
    }
    getRestCollection().setCamelContext(camelContext);
    // setup rest configuration before adding the rests
    if (getRestConfigurations() != null) {
        for (Map.Entry<String, RestConfigurationDefinition> entry : getRestConfigurations().entrySet()) {
            RestConfiguration config = entry.getValue().asRestConfiguration(getContext());
            if ("".equals(entry.getKey())) {
                camelContext.setRestConfiguration(config);
            } else {
                camelContext.addRestConfiguration(config);
            }
        }
    }
    camelContext.addRestDefinitions(getRestCollection().getRests());
    // convert rests into routes so we they are routes for runtime
    List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
    for (RestDefinition rest : getRestCollection().getRests()) {
        List<RouteDefinition> list = rest.asRouteDefinition(getContext());
        routes.addAll(list);
    }
    // convert rests api-doc into routes so they are routes for runtime
    for (RestConfiguration config : camelContext.getRestConfigurations()) {
        if (config.getApiContextPath() != null) {
            // avoid adding rest-api multiple times, in case multiple RouteBuilder classes is added
            // to the CamelContext, as we only want to setup rest-api once
            // so we check all existing routes if they have rest-api route already added
            boolean hasRestApi = false;
            for (RouteDefinition route : camelContext.getRouteDefinitions()) {
                FromDefinition from = route.getInputs().get(0);
                if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
                    hasRestApi = true;
                }
            }
            if (!hasRestApi) {
                RouteDefinition route = RestDefinition.asRouteApiDefinition(camelContext, config);
                log.debug("Adding routeId: {} as rest-api route", route.getId());
                routes.add(route);
            }
        }
    }
    // add the rest routes
    for (RouteDefinition route : routes) {
        getRouteCollection().route(route);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) FromDefinition(org.apache.camel.model.FromDefinition) InterceptFromDefinition(org.apache.camel.model.InterceptFromDefinition) ArrayList(java.util.ArrayList) ModelCamelContext(org.apache.camel.model.ModelCamelContext) RestConfigurationDefinition(org.apache.camel.model.rest.RestConfigurationDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) RestConfiguration(org.apache.camel.spi.RestConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with FromDefinition

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

the class RouteIdFactory method extractId.

/**
     * Extract id from routes
     */
private Optional<String> extractId(RouteDefinition routeDefinition) {
    if (routeDefinition.getRestDefinition() != null) {
        return Optional.empty();
    }
    List<FromDefinition> inputs = routeDefinition.getInputs();
    if (inputs == null || inputs.isEmpty()) {
        return Optional.empty();
    }
    FromDefinition from = inputs.get(0);
    String uri = from.getUri();
    // we want to use the context-path of the route
    int colon = uri.indexOf(':');
    if (colon > 0) {
        String name = uri.substring(colon + 1);
        int questionMark = name.indexOf("?");
        if (questionMark > 0) {
            return Optional.of(name.substring(0, questionMark));
        } else {
            return Optional.of(name);
        }
    }
    return Optional.empty();
}
Also used : FromDefinition(org.apache.camel.model.FromDefinition)

Example 13 with FromDefinition

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

the class AbstractCamelContextFactoryBean method setupRoutes.

/**
     * Setup all the routes which must be done prior starting {@link CamelContext}.
     */
protected void setupRoutes() throws Exception {
    if (routesSetupDone.compareAndSet(false, true)) {
        LOG.debug("Setting up routes");
        // mark that we are setting up routes
        getContext().setupRoutes(false);
        // must init route refs before we prepare the routes below
        initRouteRefs();
        // must init rest refs before we add the rests
        initRestRefs();
        // and add the rests
        getContext().addRestDefinitions(getRests());
        // convert rests into routes so we reuse routes for runtime
        for (RestDefinition rest : getRests()) {
            List<RouteDefinition> routes = rest.asRouteDefinition(getContext());
            for (RouteDefinition route : routes) {
                getRoutes().add(route);
            }
        }
        // convert rests api-doc into routes so they are routes for runtime
        for (RestConfiguration config : getContext().getRestConfigurations()) {
            if (config.getApiContextPath() != null) {
                // avoid adding rest-api multiple times, in case multiple RouteBuilder classes is added
                // to the CamelContext, as we only want to setup rest-api once
                // so we check all existing routes if they have rest-api route already added
                boolean hasRestApi = false;
                for (RouteDefinition route : getContext().getRouteDefinitions()) {
                    FromDefinition from = route.getInputs().get(0);
                    if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
                        hasRestApi = true;
                    }
                }
                if (!hasRestApi) {
                    RouteDefinition route = RestDefinition.asRouteApiDefinition(getContext(), config);
                    LOG.debug("Adding routeId: {} as rest-api route", route.getId());
                    getRoutes().add(route);
                }
            }
        }
        // do special preparation for some concepts such as interceptors and policies
        // this is needed as JAXB does not build exactly the same model definition as Spring DSL would do
        // using route builders. So we have here a little custom code to fix the JAXB gaps
        prepareRoutes();
        // and add the routes
        getContext().addRouteDefinitions(getRoutes());
        LOG.debug("Found JAXB created routes: {}", getRoutes());
        findRouteBuilders();
        installRoutes();
        // and we are now finished setting up the routes
        getContext().setupRoutes(true);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) FromDefinition(org.apache.camel.model.FromDefinition) InterceptFromDefinition(org.apache.camel.model.InterceptFromDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 14 with FromDefinition

use of org.apache.camel.model.FromDefinition in project ddf by codice.

the class ContentDirectoryMonitorTest method verifyRoute.

private void verifyRoute(RouteDefinition routeDefinition, String monitoredDirectory, String processingMechanism) {
    List<FromDefinition> fromDefinitions = routeDefinition.getInputs();
    assertThat(fromDefinitions.size(), is(1));
    String uri = fromDefinitions.get(0).getUri();
    String expectedUri = "file:" + monitoredDirectory + "?recursive=true&moveFailed=.errors&readLockMinLength=1&readLock=changed&readLockTimeout=2000&readLockCheckInterval=1000";
    if (ContentDirectoryMonitor.DELETE.equals(processingMechanism)) {
        expectedUri += "&delete=true";
    } else if (ContentDirectoryMonitor.MOVE.equals(processingMechanism)) {
        expectedUri += "&move=.ingested";
    } else if (ContentDirectoryMonitor.IN_PLACE.equals(processingMechanism)) {
        expectedUri = "durable:" + monitoredDirectory;
    }
    assertThat(uri, equalTo(expectedUri));
    List<ProcessorDefinition<?>> processorDefinitions = routeDefinition.getOutputs();
    if (ContentDirectoryMonitor.IN_PLACE.equals(processingMechanism)) {
        assertThat(processorDefinitions.size(), is(2));
    } else {
        assertThat(processorDefinitions.size(), is(1));
    }
}
Also used : FromDefinition(org.apache.camel.model.FromDefinition) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 15 with FromDefinition

use of org.apache.camel.model.FromDefinition in project ddf by codice.

the class ContentDirectoryMonitor method dumpCamelContext.

private void dumpCamelContext(String msg) {
    LOGGER.debug("\n\n***************  START: {}  *****************", msg);
    List<RouteDefinition> routeDefinitions = camelContext.getRouteDefinitions();
    if (routeDefinitions != null) {
        LOGGER.debug("Number of routes = {}", routeDefinitions.size());
        for (RouteDefinition routeDef : routeDefinitions) {
            String routeId = routeDef.getId();
            LOGGER.debug("route ID = {}", routeId);
            List<FromDefinition> routeInputs = routeDef.getInputs();
            if (routeInputs.isEmpty()) {
                LOGGER.debug("routeInputs are EMPTY");
            } else {
                for (FromDefinition fromDef : routeInputs) {
                    LOGGER.debug("route input's URI = {}", fromDef.getUri());
                }
            }
            ServiceStatus routeStatus = camelContext.getRouteStatus(routeId);
            if (routeStatus != null) {
                LOGGER.debug("Route ID {} is started = {}", routeId, routeStatus.isStarted());
            } else {
                LOGGER.debug("routeStatus is NULL for routeId = {}", routeId);
            }
        }
    }
    LOGGER.debug("***************  END: {}  *****************\n\n", msg);
}
Also used : FromDefinition(org.apache.camel.model.FromDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) ServiceStatus(org.apache.camel.ServiceStatus)

Aggregations

FromDefinition (org.apache.camel.model.FromDefinition)15 RouteDefinition (org.apache.camel.model.RouteDefinition)9 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ChoiceDefinition (org.apache.camel.model.ChoiceDefinition)2 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)2 LogDefinition (org.apache.camel.model.LogDefinition)2 ModelCamelContext (org.apache.camel.model.ModelCamelContext)2 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)2 WhenDefinition (org.apache.camel.model.WhenDefinition)2 RestDefinition (org.apache.camel.model.rest.RestDefinition)2 RestConfiguration (org.apache.camel.spi.RestConfiguration)2 IOException (java.io.IOException)1 URI (java.net.URI)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)1