diff --git a/components/camel-knative/camel-knative-component/src/main/java/org/apache/camel/component/knative/KnativeComponent.java b/components/camel-knative/camel-knative-component/src/main/java/org/apache/camel/component/knative/KnativeComponent.java index 862cfa6a18e13..cc81602bbf6bb 100644 --- a/components/camel-knative/camel-knative-component/src/main/java/org/apache/camel/component/knative/KnativeComponent.java +++ b/components/camel-knative/camel-knative-component/src/main/java/org/apache/camel/component/knative/KnativeComponent.java @@ -20,7 +20,6 @@ import java.util.Map; import org.apache.camel.CamelContext; -import org.apache.camel.CamelContextAware; import org.apache.camel.Endpoint; import org.apache.camel.component.knative.spi.Knative; import org.apache.camel.component.knative.spi.KnativeConsumerFactory; @@ -312,7 +311,7 @@ private void setUpProducerFactory() throws Exception { this.managedProducer = true; - CamelContextAware.trySetCamelContext(this.producerFactory, getCamelContext()); + getCamelContext().addService(this.producerFactory); } LOGGER.info("Using Knative producer factory: {} for protocol: {}", producerFactory, protocol.name()); @@ -339,7 +338,7 @@ private void setUpConsumerFactory() throws Exception { this.managedConsumer = true; - CamelContextAware.trySetCamelContext(this.consumerFactory, getCamelContext()); + getCamelContext().addService(this.consumerFactory); } LOGGER.info("Using Knative consumer factory: {} for protocol: {}", consumerFactory, protocol.name()); diff --git a/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumer.java b/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumer.java index d3b74233fd960..f7525322b161a 100644 --- a/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumer.java +++ b/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumer.java @@ -23,6 +23,7 @@ import java.util.Locale; import java.util.Map; import java.util.function.Predicate; +import java.util.function.Supplier; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.HttpMethod; @@ -57,7 +58,7 @@ public class KnativeHttpConsumer extends DefaultConsumer { private final KnativeTransportConfiguration configuration; private final Predicate filter; private final KnativeResource resource; - private final Router router; + private final Supplier router; private final HeaderFilterStrategy headerFilterStrategy; private String basePath; @@ -68,7 +69,7 @@ public class KnativeHttpConsumer extends DefaultConsumer { public KnativeHttpConsumer(KnativeTransportConfiguration configuration, Endpoint endpoint, KnativeResource resource, - Router router, + Supplier router, Processor processor) { super(endpoint, processor); this.configuration = configuration; @@ -116,7 +117,7 @@ protected void doStart() throws Exception { LOGGER.debug("Creating route for path: {}", path); - route = router.route( + route = router.get().route( HttpMethod.POST, path); diff --git a/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumerFactory.java b/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumerFactory.java index f734b206c67d7..e751ab3f06a4b 100644 --- a/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumerFactory.java +++ b/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumerFactory.java @@ -27,6 +27,7 @@ import org.apache.camel.component.knative.spi.KnativeConsumerFactory; import org.apache.camel.component.knative.spi.KnativeResource; import org.apache.camel.component.knative.spi.KnativeTransportConfiguration; +import org.apache.camel.support.service.ServiceHelper; import org.apache.camel.support.service.ServiceSupport; public class KnativeHttpConsumerFactory extends ServiceSupport implements CamelContextAware, KnativeConsumerFactory { @@ -38,6 +39,10 @@ public Router getRouter() { } public KnativeHttpConsumerFactory setRouter(Router router) { + if (ServiceHelper.isStarted(this)) { + throw new IllegalArgumentException("Can't set the Router instance after the service has been started"); + } + this.router = router; return this; } @@ -52,24 +57,31 @@ public CamelContext getCamelContext() { return camelContext; } - @Override - protected void doInit() throws Exception { - if (router == null) { - router = KnativeHttpSupport.lookupRouter(camelContext); - } - } - @Override public Consumer createConsumer( Endpoint endpoint, KnativeTransportConfiguration config, KnativeResource service, Processor processor) { - Objects.requireNonNull(this.router, "router"); - return new KnativeHttpConsumer( config, endpoint, service, - this.router, + this::lookupRouter, processor); } + /** + * Resolve router from given Camel context if not explicitly set. KnativeHttpConsumer implementation usually calls + * this method to retrieve the router during service startup phase. + * + * @return + */ + private Router lookupRouter() { + if (router == null) { + router = KnativeHttpSupport.lookupRouter(camelContext); + } + + Objects.requireNonNull(router, "router"); + + return router; + } + } diff --git a/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducerFactory.java b/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducerFactory.java index 7c6391b07b2d2..7baae79d83528 100644 --- a/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducerFactory.java +++ b/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducerFactory.java @@ -27,6 +27,7 @@ import org.apache.camel.component.knative.spi.KnativeProducerFactory; import org.apache.camel.component.knative.spi.KnativeResource; import org.apache.camel.component.knative.spi.KnativeTransportConfiguration; +import org.apache.camel.support.service.ServiceHelper; import org.apache.camel.support.service.ServiceSupport; public class KnativeHttpProducerFactory extends ServiceSupport implements CamelContextAware, KnativeProducerFactory { @@ -39,6 +40,10 @@ public Vertx getVertx() { } public KnativeHttpProducerFactory setVertx(Vertx vertx) { + if (ServiceHelper.isStarted(this)) { + throw new IllegalArgumentException("Can't set the Vertx instance after the service has been started"); + } + this.vertx = vertx; return this; } diff --git a/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpSupport.java b/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpSupport.java index a4d8c667843f6..3d0af7c4411b6 100644 --- a/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpSupport.java +++ b/components/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpSupport.java @@ -103,7 +103,7 @@ public static void remapCloudEventHeaders(CloudEvent ce, Message message) { } /** - * Retrieve router from given CamelContext or create new instance. + * Retrieve router from given CamelContext using the default platform http router name. * * @param camelContext the current context. * @return router @@ -114,15 +114,10 @@ public static Router lookupRouter(CamelContext camelContext) { return router; } - router = CamelContextHelper.lookup( + return CamelContextHelper.lookup( camelContext, PLATFORM_HTTP_ROUTER_NAME, Router.class); - if (router != null) { - return router; - } - - return Router.router(lookupVertxInstance(camelContext)); } /** diff --git a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java index 87baeee3ea6c8..31c4295d1f14f 100644 --- a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java +++ b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java @@ -254,11 +254,17 @@ public VertxPlatformHttpRouter getRouter() { } @Override - protected void doStart() throws Exception { + protected void doInit() throws Exception { ObjectHelper.notNull(camelContext, "CamelContext"); server = new VertxPlatformHttpServer(configuration); camelContext.addService(server); + } + + @Override + protected void doStart() throws Exception { + ObjectHelper.notNull(camelContext, "CamelContext"); + ServiceHelper.startService(server); router = VertxPlatformHttpRouter.lookup(camelContext); platformHttpComponent = camelContext.getComponent("platform-http", PlatformHttpComponent.class); diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java index 5e872cf65953e..8fd9e2536fb58 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java @@ -75,13 +75,14 @@ public Component resolveComponent(String name, CamelContext context) { sc.setShadow(true); sc.setShadowPattern(stubPattern); } - if (answer instanceof PlatformHttpComponent) { - // setup a default http server on port 8080 if not already done + if (answer instanceof PlatformHttpComponent || name.equals("knative")) { + // set up a default http server on configured port if not already done MainHttpServer server = camelContext.hasService(MainHttpServer.class); if (server == null) { - // need to capture we use http-server + // need to capture that we use a http-server HttpServerConfigurationProperties config = new HttpServerConfigurationProperties(null); - CamelJBangSettingsHelper.writeSettings("camel.jbang.platform-http.port", String.valueOf(config.getPort())); + CamelJBangSettingsHelper.writeSettingsIfNotExists("camel.jbang.platform-http.port", + String.valueOf(config.getPort())); if (!silent) { try { // enable http server if not silent diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/CamelJBangSettingsHelper.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/CamelJBangSettingsHelper.java index e92bacb6e10b1..725d84fda1b5b 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/CamelJBangSettingsHelper.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/CamelJBangSettingsHelper.java @@ -35,18 +35,18 @@ public final class CamelJBangSettingsHelper { private CamelJBangSettingsHelper() { } - public static void writeSettings(String key, String value) { + public static void writeSettingsIfNotExists(String key, String value) { if (FILE.exists()) { try { - String line = key + "=" + value; String context; try (FileInputStream fis = new FileInputStream(FILE)) { context = IOHelper.loadText(fis); } - if (!context.contains(line)) { - // append line as it was not already present + if (!context.contains(key + "=")) { + // append line as key has not been set before + String line = key + "=" + value; try (FileOutputStream fos = new FileOutputStream(FILE, true)) { fos.write(line.getBytes(StandardCharsets.UTF_8)); fos.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));