You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/02/12 05:18:19 UTC

(camel) branch main updated: camel-knative-http: Avoid Nullpointer on KnativeHttpProducer/Consumer factory (#13087)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 605ac288bdf camel-knative-http: Avoid Nullpointer on KnativeHttpProducer/Consumer factory (#13087)
605ac288bdf is described below

commit 605ac288bdf6b65f77725443d11d87fed4eb1926
Author: Christoph Deppisch <cd...@redhat.com>
AuthorDate: Mon Feb 12 06:18:12 2024 +0100

    camel-knative-http: Avoid Nullpointer on KnativeHttpProducer/Consumer factory (#13087)
    
    - Lookup router and vertx instance when not explicitly set on the factory
    - Avoids Nullpointer exception when for instance running the Knative component with Camel JBang
---
 .../knative/http/KnativeHttpConsumerFactory.java   |  7 ++++
 .../knative/http/KnativeHttpProducerFactory.java   |  7 ++++
 .../component/knative/http/KnativeHttpSupport.java | 49 ++++++++++++++++++++++
 3 files changed, 63 insertions(+)

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 7797c21b3b3..f734b206c67 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
@@ -52,6 +52,13 @@ public class KnativeHttpConsumerFactory extends ServiceSupport implements CamelC
         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) {
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 ec5e9071ade..7c6391b07b2 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
@@ -61,6 +61,13 @@ public class KnativeHttpProducerFactory extends ServiceSupport implements CamelC
         return camelContext;
     }
 
+    @Override
+    protected void doInit() throws Exception {
+        if (vertx == null) {
+            vertx = KnativeHttpSupport.lookupVertxInstance(camelContext);
+        }
+    }
+
     @Override
     public Producer createProducer(Endpoint endpoint, KnativeTransportConfiguration config, KnativeResource service) {
         Objects.requireNonNull(this.vertx, "vertx");
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 58976f71a3e..a4d8c667843 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
@@ -22,12 +22,19 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.function.Predicate;
 
+import io.vertx.core.Vertx;
+import io.vertx.core.VertxOptions;
 import io.vertx.core.http.HttpServerRequest;
+import io.vertx.ext.web.Router;
+import org.apache.camel.CamelContext;
 import org.apache.camel.Message;
 import org.apache.camel.component.cloudevents.CloudEvent;
 import org.apache.camel.component.knative.spi.KnativeResource;
+import org.apache.camel.support.CamelContextHelper;
 
 public final class KnativeHttpSupport {
+    private static final String PLATFORM_HTTP_ROUTER_NAME = "platform-http-router";
+
     private KnativeHttpSupport() {
     }
 
@@ -95,4 +102,46 @@ public final class KnativeHttpSupport {
         }
     }
 
+    /**
+     * Retrieve router from given CamelContext or create new instance.
+     *
+     * @param  camelContext the current context.
+     * @return              router
+     */
+    public static Router lookupRouter(CamelContext camelContext) {
+        Router router = CamelContextHelper.findSingleByType(camelContext, Router.class);
+        if (router != null) {
+            return router;
+        }
+
+        router = CamelContextHelper.lookup(
+                camelContext,
+                PLATFORM_HTTP_ROUTER_NAME,
+                Router.class);
+        if (router != null) {
+            return router;
+        }
+
+        return Router.router(lookupVertxInstance(camelContext));
+    }
+
+    /**
+     * Retrieve Vert.x instance from given CamelContext or create new instance.
+     *
+     * @param  camelContext the current context.
+     * @return              vertx instance
+     */
+    public static Vertx lookupVertxInstance(CamelContext camelContext) {
+        Vertx vertx = CamelContextHelper.findSingleByType(camelContext, Vertx.class);
+        if (vertx != null) {
+            return vertx;
+        }
+
+        VertxOptions options = CamelContextHelper.findSingleByType(camelContext, VertxOptions.class);
+        if (options == null) {
+            options = new VertxOptions();
+        }
+
+        return Vertx.vertx(options);
+    }
 }