Search in sources :

Example 1 with Source

use of org.apache.camel.k.Source in project camel-k-runtime by apache.

the class Sources method fromDefinition.

public static Source fromDefinition(SourceDefinition definition) {
    if (definition.getLocation() == null && definition.getContent() == null) {
        throw new IllegalArgumentException("Either the source location or the source content should be set");
    }
    return new Source() {

        @Override
        public String getLocation() {
            return definition.getLocation();
        }

        @Override
        public String getId() {
            return ObjectHelper.supplyIfEmpty(definition.getId(), this::getName);
        }

        @Override
        public String getName() {
            String answer = definition.getName();
            if (ObjectHelper.isEmpty(answer) && ObjectHelper.isNotEmpty(definition.getLocation())) {
                answer = StringSupport.substringAfter(definition.getLocation(), ":");
                answer = StringSupport.substringBeforeLast(answer, ".");
                if (answer.contains("/")) {
                    answer = StringSupport.substringAfterLast(answer, "/");
                }
            }
            return answer;
        }

        @Override
        public String getLanguage() {
            String answer = definition.getLanguage();
            if (ObjectHelper.isEmpty(answer) && ObjectHelper.isNotEmpty(definition.getLocation())) {
                answer = StringSupport.substringAfterLast(definition.getLocation(), ":");
                answer = StringSupport.substringAfterLast(answer, ".");
            }
            return answer;
        }

        @Override
        public SourceType getType() {
            return ObjectHelper.supplyIfEmpty(definition.getType(), () -> SourceType.source);
        }

        @Override
        public Optional<String> getLoader() {
            return Optional.ofNullable(definition.getLoader());
        }

        @Override
        public List<String> getInterceptors() {
            return ObjectHelper.supplyIfEmpty(definition.getInterceptors(), Collections::emptyList);
        }

        @Override
        public List<String> getPropertyNames() {
            return ObjectHelper.supplyIfEmpty(definition.getPropertyNames(), Collections::emptyList);
        }

        /**
         * Read the content of the source as {@link InputStream}.
         *
         * @param ctx the {@link CamelContext}
         * @return the {@link InputStream} representing the source content
         */
        @Override
        public InputStream resolveAsInputStream(CamelContext ctx) {
            try {
                InputStream is;
                if (definition.getContent() != null) {
                    is = new ByteArrayInputStream(definition.getContent());
                } else {
                    is = ResourceHelper.resolveMandatoryResourceAsInputStream(ctx, definition.getLocation());
                }
                return definition.isCompressed() ? new GZIPInputStream(Base64.getDecoder().wrap(is)) : is;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
}
Also used : CamelContext(org.apache.camel.CamelContext) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Collections(java.util.Collections) Source(org.apache.camel.k.Source) IOException(java.io.IOException)

Example 2 with Source

use of org.apache.camel.k.Source in project camel-k-runtime by apache.

the class SourcesSupport method load.

public static void load(Runtime runtime, Source source) {
    final List<RouteBuilderLifecycleStrategy> interceptors;
    switch(source.getType()) {
        case source:
            interceptors = RuntimeSupport.loadInterceptors(runtime.getCamelContext(), source);
            interceptors.forEach(interceptor -> {
                if (interceptor instanceof RuntimeAware) {
                    ((RuntimeAware) interceptor).setRuntime(runtime);
                }
            });
            break;
        case template:
            if (!source.getInterceptors().isEmpty()) {
                LOGGER.warn("Interceptors associated to the route template {} will be ignored", source.getName());
            }
            interceptors = List.of(new RouteBuilderLifecycleStrategy() {

                @Override
                public void afterConfigure(RouteBuilder builder) {
                    List<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
                    List<RouteTemplateDefinition> templates = builder.getRouteTemplateCollection().getRouteTemplates();
                    if (routes.size() != 1) {
                        throw new IllegalArgumentException("There should be a single route definition, got " + routes.size());
                    }
                    if (!templates.isEmpty()) {
                        throw new IllegalArgumentException("There should not be any template, got " + templates.size());
                    }
                    // create a new template from the source
                    RouteTemplateDefinition templatesDefinition = builder.getRouteTemplateCollection().routeTemplate(source.getId());
                    templatesDefinition.setRoute(routes.get(0));
                    source.getPropertyNames().forEach(templatesDefinition::templateParameter);
                    // remove all routes definitions as they have been translated
                    // in the related route template
                    routes.clear();
                }
            });
            break;
        case errorHandler:
            if (!source.getInterceptors().isEmpty()) {
                LOGGER.warn("Interceptors associated to the route template {} will be ignored", source.getName());
            }
            interceptors = List.of(new RouteBuilderLifecycleStrategy() {

                @Override
                public void afterConfigure(RouteBuilder builder) {
                    List<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
                    List<RouteTemplateDefinition> templates = builder.getRouteTemplateCollection().getRouteTemplates();
                    if (!routes.isEmpty()) {
                        throw new IllegalArgumentException("There should be no route definition, got " + routes.size());
                    }
                    if (!templates.isEmpty()) {
                        throw new IllegalArgumentException("There should not be any template, got " + templates.size());
                    }
                    if (hasErrorHandlerBuilder(builder)) {
                        LOGGER.debug("Setting default error handler builder factory as {}", builder.getErrorHandlerBuilder());
                        runtime.getCamelContext().adapt(ExtendedCamelContext.class).setErrorHandlerFactory(builder.getErrorHandlerBuilder());
                    }
                }
            });
            break;
        default:
            throw new IllegalArgumentException("Unknown source type: " + source.getType());
    }
    try {
        final Resource resource = Sources.asResource(runtime.getCamelContext(), source);
        final ExtendedCamelContext ecc = runtime.getCamelContext(ExtendedCamelContext.class);
        final Collection<RoutesBuilder> builders = ecc.getRoutesLoader().findRoutesBuilders(resource);
        builders.stream().map(RouteBuilder.class::cast).peek(b -> interceptors.forEach(b::addLifecycleInterceptor)).forEach(runtime::addRoutes);
    } catch (Exception e) {
        throw RuntimeCamelException.wrapRuntimeCamelException(e);
    }
}
Also used : RuntimeCamelException(org.apache.camel.RuntimeCamelException) RouteDefinition(org.apache.camel.model.RouteDefinition) RoutesBuilder(org.apache.camel.RoutesBuilder) RouteTemplateDefinition(org.apache.camel.model.RouteTemplateDefinition) SourcesConfigurer(org.apache.camel.k.listener.SourcesConfigurer) Logger(org.slf4j.Logger) Resource(org.apache.camel.spi.Resource) AbstractPhaseListener(org.apache.camel.k.listener.AbstractPhaseListener) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) List(java.util.List) SourceDefinition(org.apache.camel.k.SourceDefinition) RouteBuilder(org.apache.camel.builder.RouteBuilder) Source(org.apache.camel.k.Source) RuntimeAware(org.apache.camel.k.RuntimeAware) Runtime(org.apache.camel.k.Runtime) ObjectHelper(org.apache.camel.util.ObjectHelper) RouteBuilderLifecycleStrategy(org.apache.camel.builder.RouteBuilderLifecycleStrategy) RuntimeAware(org.apache.camel.k.RuntimeAware) RouteBuilder(org.apache.camel.builder.RouteBuilder) Resource(org.apache.camel.spi.Resource) RouteTemplateDefinition(org.apache.camel.model.RouteTemplateDefinition) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RouteDefinition(org.apache.camel.model.RouteDefinition) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) RouteBuilderLifecycleStrategy(org.apache.camel.builder.RouteBuilderLifecycleStrategy) RoutesBuilder(org.apache.camel.RoutesBuilder)

Aggregations

Source (org.apache.camel.k.Source)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 CamelContext (org.apache.camel.CamelContext)1 ExtendedCamelContext (org.apache.camel.ExtendedCamelContext)1 RoutesBuilder (org.apache.camel.RoutesBuilder)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 RouteBuilderLifecycleStrategy (org.apache.camel.builder.RouteBuilderLifecycleStrategy)1 Runtime (org.apache.camel.k.Runtime)1 RuntimeAware (org.apache.camel.k.RuntimeAware)1 SourceDefinition (org.apache.camel.k.SourceDefinition)1 AbstractPhaseListener (org.apache.camel.k.listener.AbstractPhaseListener)1 SourcesConfigurer (org.apache.camel.k.listener.SourcesConfigurer)1 RouteDefinition (org.apache.camel.model.RouteDefinition)1