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 2021/12/08 09:46:44 UTC

[camel] 02/02: CAMEL-17299: camel-jbang - Add --port option to enable embedded http server

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

commit 24dd1a926319dfa68fe83a3acbc0ceb09a836bed
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 8 10:43:30 2021 +0100

    CAMEL-17299: camel-jbang - Add --port option to enable embedded http server
---
 .../VertxPlatformHttpServerConfiguration.java      | 19 ++++++++++++++++++
 dsl/camel-jbang/camel-jbang-core/pom.xml           |  4 ++++
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  6 ++++++
 .../DependencyDownloaderComponentResolver.java     |  6 +++---
 .../java/org/apache/camel/main/KameletMain.java    |  6 ++++++
 .../org/apache/camel/main/VertxHttpServer.java     | 23 ++++++++++++++++++++--
 6 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServerConfiguration.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServerConfiguration.java
index a17e594..9bb4806 100644
--- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServerConfiguration.java
+++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServerConfiguration.java
@@ -22,6 +22,9 @@ import java.util.List;
 
 import org.apache.camel.support.jsse.SSLContextParameters;
 
+/**
+ * HTTP server configuration
+ */
 public class VertxPlatformHttpServerConfiguration {
     public static final String DEFAULT_BIND_HOST = "0.0.0.0";
     public static final int DEFAULT_BIND_PORT = 8080;
@@ -38,6 +41,22 @@ public class VertxPlatformHttpServerConfiguration {
     private BodyHandler bodyHandler = new BodyHandler();
     private Cors cors = new Cors();
 
+    public int getPort() {
+        return getBindPort();
+    }
+
+    public void setPort(int port) {
+        setBindPort(port);
+    }
+
+    public void setHost(String host) {
+        setBindHost(host);
+    }
+
+    public String getHost() {
+        return getBindHost();
+    }
+
     public String getBindHost() {
         return bindHost;
     }
diff --git a/dsl/camel-jbang/camel-jbang-core/pom.xml b/dsl/camel-jbang/camel-jbang-core/pom.xml
index a33b6eb..b6bc866 100644
--- a/dsl/camel-jbang/camel-jbang-core/pom.xml
+++ b/dsl/camel-jbang/camel-jbang-core/pom.xml
@@ -68,6 +68,10 @@
             <artifactId>camel-file-watch</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-platform-http-vertx</artifactId>
+        </dependency>
+        <dependency>
             <groupId>commons-io</groupId>
             <artifactId>commons-io</artifactId>
         </dependency>
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 0c808ee..42d037e 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -82,6 +82,9 @@ class Run implements Callable<Integer> {
             description = "Local directory to load Kamelets from (take precedence))")
     private String localKameletDir;
 
+    @Option(names = { "--port" }, description = "Embeds a local HTTP server on this port")
+    private int port;
+
     @Override
     public Integer call() throws Exception {
         if (stopRequested) {
@@ -135,6 +138,9 @@ class Run implements Callable<Integer> {
         if (maxIdleSeconds > 0) {
             main.addInitialProperty("camel.main.durationMaxIdleSeconds", String.valueOf(maxIdleSeconds));
         }
+        if (port > 0) {
+            main.addInitialProperty("camel.jbang.platform-http.port", String.valueOf(port));
+        }
 
         if (fileLock) {
             lockFile = createLockFile();
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DependencyDownloaderComponentResolver.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DependencyDownloaderComponentResolver.java
index af6f6bd..ec05475 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DependencyDownloaderComponentResolver.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DependencyDownloaderComponentResolver.java
@@ -57,11 +57,11 @@ final class DependencyDownloaderComponentResolver extends DefaultComponentResolv
         if ("platform-http".equals(name) && !DownloaderHelper.alreadyOnClasspath(camelContext, "camel-platform-http-vertx")) {
             DownloaderHelper.downloadDependency(camelContext, model.getGroupId(), "camel-platform-http-vertx",
                     model.getVersion());
-
-            // setup a default http server on port 8080
-            VertxHttpServer.registerServer(camelContext);
         }
 
+        // setup a default http server on port 8080 if not already done when using platform-http
+        VertxHttpServer.registerServer(camelContext);
+
         return super.resolveComponent(name, context);
     }
 
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index 356fad4..9ade32a 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -161,6 +161,12 @@ public class KameletMain extends MainCommandLineSupport {
         answer.setApplicationContextClassLoader(kameletClassLoader);
         answer.setRegistry(registry);
 
+        // embed HTTP server if port is specified
+        Object port = getInitialProperties().get("camel.jbang.platform-http.port");
+        if (port != null) {
+            VertxHttpServer.registerServer(answer, Integer.parseInt(port.toString()));
+        }
+
         if (download) {
             try {
                 // use resolver that can auto downloaded
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/VertxHttpServer.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/VertxHttpServer.java
index b443a7c..2014be2 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/VertxHttpServer.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/VertxHttpServer.java
@@ -16,27 +16,46 @@
  */
 package org.apache.camel.main;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.support.IntrospectionSupport;
 
 /**
  * To setup vertx http server in the running Camel application
  */
 public final class VertxHttpServer {
 
+    private static final AtomicBoolean REGISTERED = new AtomicBoolean();
+
     private VertxHttpServer() {
     }
 
     public static void registerServer(CamelContext camelContext) {
+        if (REGISTERED.compareAndSet(false, true)) {
+            doRegisterServer(camelContext, 8080);
+        }
+    }
+
+    public static void registerServer(CamelContext camelContext, int port) {
+        if (REGISTERED.compareAndSet(false, true)) {
+            doRegisterServer(camelContext, port);
+        }
+    }
+
+    private static void doRegisterServer(CamelContext camelContext, int port) {
         try {
             // must load via the classloader set on camel context that will have the classes on its classpath
-            Class clazz = camelContext.getClassResolver()
+            Class<?> clazz = camelContext.getClassResolver()
                     .resolveMandatoryClass(
                             "org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServerConfiguration");
             Object config = clazz.getConstructors()[0].newInstance();
+            IntrospectionSupport.setProperty(camelContext, config, "port", port);
 
             clazz = camelContext.getClassResolver()
-                    .resolveMandatoryClass("org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServer");
+                    .resolveMandatoryClass(
+                            "org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServer");
             Object server = clazz.getConstructors()[0].newInstance(config);
 
             camelContext.addService(server);