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 2022/04/28 07:36:36 UTC

[camel] branch main updated: camel-jbang - Package uber-jar should detect if rest-dsl was in use and uses 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


The following commit(s) were added to refs/heads/main by this push:
     new 0959faeb764 camel-jbang - Package uber-jar should detect if rest-dsl was in use and uses http server
0959faeb764 is described below

commit 0959faeb764b48790a3e4d75519765d0761de031
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Apr 28 09:36:20 2022 +0200

    camel-jbang - Package uber-jar should detect if rest-dsl was in use and uses http server
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 15 +++---
 .../camel/main/CamelJBangSettingsHelper.java       | 60 ++++++++++++++++++++++
 .../org/apache/camel/main/VertxHttpServer.java     |  3 ++
 3 files changed, 70 insertions(+), 8 deletions(-)

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 35fa604948f..cb14a3fc774 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
@@ -55,7 +55,6 @@ class Run implements Callable<Integer> {
     private static final String[] ACCEPTED_FILE_EXT
             = new String[] { "properties", "java", "groovy", "js", "jsh", "kts", "xml", "yaml" };
 
-    private FileOutputStream settings;
     private CamelContext context;
     private File lockFile;
     private ScheduledExecutorService executor;
@@ -169,8 +168,6 @@ class Run implements Callable<Integer> {
         FileUtil.removeDir(work);
         work.mkdirs();
 
-        settings = new FileOutputStream(WORK_DIR + "/" + RUN_SETTINGS_FILE, false);
-
         // configure logging first
         if (logging) {
             RuntimeUtil.configureLog(loggingLevel);
@@ -437,8 +434,6 @@ class Run implements Callable<Integer> {
 
         main.run();
 
-        IOHelper.close(settings);
-
         int code = main.getExitCode();
         return code;
     }
@@ -492,12 +487,16 @@ class Run implements Callable<Integer> {
     }
 
     private void writeSettings(String key, String value) {
-        String line = key + "=" + value;
+        FileOutputStream fos = null;
         try {
-            settings.write(line.getBytes(StandardCharsets.UTF_8));
-            settings.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
+            fos = new FileOutputStream(WORK_DIR + "/" + RUN_SETTINGS_FILE, true);
+            String line = key + "=" + value;
+            fos.write(line.getBytes(StandardCharsets.UTF_8));
+            fos.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
         } catch (Exception e) {
             // ignore
+        } finally {
+            IOHelper.close(fos);
         }
     }
 
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/CamelJBangSettingsHelper.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/CamelJBangSettingsHelper.java
new file mode 100644
index 00000000000..5972634270b
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/CamelJBangSettingsHelper.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.main;
+
+import org.apache.camel.util.IOHelper;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * When running we need to store
+ */
+public final class CamelJBangSettingsHelper {
+
+    private static final String WORK_DIR = ".camel-jbang";
+    private static final String RUN_SETTINGS_FILE = "camel-jbang-run.properties";
+    private static final File FILE = new File(WORK_DIR + "/" + RUN_SETTINGS_FILE);
+
+    private CamelJBangSettingsHelper() {
+    }
+
+    public static void writeSettings(String key, String value) {
+        if (FILE.exists()) {
+            try {
+                String line = key + "=" + value;
+
+                FileInputStream fis = new FileInputStream(FILE);
+                String context = IOHelper.loadText(new FileInputStream(FILE));
+                IOHelper.close(fis);
+
+                if (!context.contains(line)) {
+                    // append line as it was not already present
+                    FileOutputStream fos = new FileOutputStream(FILE, true);
+                    fos.write(line.getBytes(StandardCharsets.UTF_8));
+                    fos.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
+                    IOHelper.close(fos);
+                }
+            } catch (Exception e) {
+                // ignore
+            }
+        }
+    }
+
+}
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 63d71b4e6e9..7312e52e516 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
@@ -91,6 +91,9 @@ public final class VertxHttpServer {
                 phc = camelContext.getComponent("platform-http", PlatformHttpComponent.class);
             }
 
+            // need to capture we use http-server
+            CamelJBangSettingsHelper.writeSettings("camel.jbang.platform-http.port", "" + port);
+
             // after camel is started then add event notifier
             camelContext.addStartupListener(new StartupListener() {
                 @Override