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/09/04 09:59:56 UTC

(camel) branch sup created (now c131d4a817b)

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

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


      at c131d4a817b CAMEL-21168: Add startup configuration dev console and jbang command

This branch includes the following new commits:

     new c131d4a817b CAMEL-21168: Add startup configuration dev console and jbang command

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel) 01/01: CAMEL-21168: Add startup configuration dev console and jbang command

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit c131d4a817b147a3c7e745a3d65b6d1aef602b3f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Sep 4 11:59:38 2024 +0200

    CAMEL-21168: Add startup configuration dev console and jbang command
---
 .../apache/camel/catalog/dev-consoles.properties   |   1 +
 .../catalog/dev-consoles/main-configuration.json   |  15 ++
 .../camel/dev-console/main-configuration.json      |  15 ++
 .../apache/camel/dev-console/main-configuration    |   2 +
 .../org/apache/camel/dev-consoles.properties       |   7 +
 .../org/apache/camel/main/BaseMainSupport.java     |  23 ++-
 .../camel/main/MainConfigurationDevConsole.java    | 110 +++++++++++++
 .../java/org/apache/camel/main/MainHelper.java     |   8 +-
 .../camel/cli/connector/LocalCliConnector.java     |   7 +
 .../dsl/jbang/core/commands/CamelJBangMain.java    |   1 +
 .../commands/process/StartupConfiguration.java     | 177 +++++++++++++++++++++
 11 files changed, 360 insertions(+), 6 deletions(-)

diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles.properties b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles.properties
index 3c95c1b6ed1..d634e0271ec 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles.properties
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles.properties
@@ -19,6 +19,7 @@ java-security
 jvm
 kafka
 log
+main-configuration
 memory
 micrometer
 platform-http
diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles/main-configuration.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles/main-configuration.json
new file mode 100644
index 00000000000..cad6d7910c7
--- /dev/null
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles/main-configuration.json
@@ -0,0 +1,15 @@
+{
+  "console": {
+    "kind": "console",
+    "group": "camel",
+    "name": "main-configuration",
+    "title": "Main Configuration",
+    "description": "Display Camel startup configuration",
+    "deprecated": false,
+    "javaType": "org.apache.camel.main.MainConfigurationDevConsole",
+    "groupId": "org.apache.camel",
+    "artifactId": "camel-main",
+    "version": "4.8.0-SNAPSHOT"
+  }
+}
+
diff --git a/core/camel-main/src/generated/resources/META-INF/org/apache/camel/dev-console/main-configuration.json b/core/camel-main/src/generated/resources/META-INF/org/apache/camel/dev-console/main-configuration.json
new file mode 100644
index 00000000000..cad6d7910c7
--- /dev/null
+++ b/core/camel-main/src/generated/resources/META-INF/org/apache/camel/dev-console/main-configuration.json
@@ -0,0 +1,15 @@
+{
+  "console": {
+    "kind": "console",
+    "group": "camel",
+    "name": "main-configuration",
+    "title": "Main Configuration",
+    "description": "Display Camel startup configuration",
+    "deprecated": false,
+    "javaType": "org.apache.camel.main.MainConfigurationDevConsole",
+    "groupId": "org.apache.camel",
+    "artifactId": "camel-main",
+    "version": "4.8.0-SNAPSHOT"
+  }
+}
+
diff --git a/core/camel-main/src/generated/resources/META-INF/services/org/apache/camel/dev-console/main-configuration b/core/camel-main/src/generated/resources/META-INF/services/org/apache/camel/dev-console/main-configuration
new file mode 100644
index 00000000000..b7b011a85e5
--- /dev/null
+++ b/core/camel-main/src/generated/resources/META-INF/services/org/apache/camel/dev-console/main-configuration
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.main.MainConfigurationDevConsole
diff --git a/core/camel-main/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties b/core/camel-main/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
new file mode 100644
index 00000000000..ea5f8255a50
--- /dev/null
+++ b/core/camel-main/src/generated/resources/META-INF/services/org/apache/camel/dev-consoles.properties
@@ -0,0 +1,7 @@
+# Generated by camel build tools - do NOT edit this file!
+dev-consoles=main-configuration
+groupId=org.apache.camel
+artifactId=camel-main
+version=4.8.0-SNAPSHOT
+projectName=Camel :: Main
+projectDescription=Camel Main
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index ee56ba4864f..032a13d199e 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -552,6 +552,15 @@ public abstract class BaseMainSupport extends BaseService {
             camelContext.addService(new MainPropertiesReload(this));
         }
 
+        // capture startup configuration
+        DevConsoleRegistry dcr = camelContext.getCamelContextExtension().getContextPlugin(DevConsoleRegistry.class);
+        if (dcr != null) {
+            MainConfigurationDevConsole mc = (MainConfigurationDevConsole) dcr.resolveById("main-configuration");
+            if (mc != null) {
+                mc.addStartupConfiguration(autoConfiguredProperties);
+            }
+        }
+
         // log summary of configurations
         if (mainConfigurationProperties.isAutoConfigurationLogSummary() && !autoConfiguredProperties.isEmpty()) {
             logConfigurationSummary(autoConfiguredProperties);
@@ -2313,12 +2322,22 @@ public abstract class BaseMainSupport extends BaseService {
             }
             // log summary of configurations
             if (mainConfigurationProperties.isAutoConfigurationLogSummary() && !autoConfiguredProperties.isEmpty()) {
+                MainConfigurationDevConsole mc = null;
+                DevConsoleRegistry dcr = camelContext.getCamelContextExtension().getContextPlugin(DevConsoleRegistry.class);
+                if (dcr != null) {
+                    mc = (MainConfigurationDevConsole) dcr.resolveById("main-configuration");
+                }
                 boolean header = false;
                 for (var entry : autoConfiguredProperties.entrySet()) {
                     String k = entry.getKey().toString();
                     Object v = entry.getValue();
                     String loc = locationSummary(autoConfiguredProperties, k);
 
+                    if (mc != null) {
+                        // also capture autowired configuration
+                        mc.addStartupConfiguration(loc, k, v);
+                    }
+
                     // tone down logging noise for our own internal configurations
                     boolean debug = loc.contains("[camel-main]");
                     if (debug && !LOG.isDebugEnabled()) {
@@ -2441,9 +2460,9 @@ public abstract class BaseMainSupport extends BaseService {
                         }
                         String loc = locationSummary(propertyPlaceholders, k);
                         if (SensitiveUtils.containsSensitive(k)) {
-                            LOG.info("    {} {}=xxxxxx", loc, k);
+                            LOG.info("    {} {} = xxxxxx", loc, k);
                         } else {
-                            LOG.info("    {} {}={}", loc, k, v);
+                            LOG.info("    {} {} = {}", loc, k, v);
                         }
                     }
                 }
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationDevConsole.java b/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationDevConsole.java
new file mode 100644
index 00000000000..47863fc98a0
--- /dev/null
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationDevConsole.java
@@ -0,0 +1,110 @@
+/*
+ * 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 java.util.Map;
+
+import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.support.console.AbstractDevConsole;
+import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.OrderedLocationProperties;
+import org.apache.camel.util.SensitiveUtils;
+import org.apache.camel.util.StringHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+
+import static org.apache.camel.util.LocationHelper.locationSummary;
+
+@DevConsole(name = "main-configuration", displayName = "Main Configuration",
+            description = "Display Camel startup configuration")
+public class MainConfigurationDevConsole extends AbstractDevConsole {
+
+    private final OrderedLocationProperties startupConfiguration = new OrderedLocationProperties();
+
+    public MainConfigurationDevConsole() {
+        super("camel", "main-configuration", "Main Configuration", "Display Camel startup configuration");
+    }
+
+    public void addStartupConfiguration(OrderedLocationProperties startupConfiguration) {
+        this.startupConfiguration.putAll(startupConfiguration);
+    }
+
+    public void addStartupConfiguration(String loc, Object key, Object value) {
+        startupConfiguration.put(loc, key, value);
+    }
+
+    @Override
+    protected String doCallText(Map<String, Object> options) {
+        StringBuilder sb = new StringBuilder();
+
+        if (!startupConfiguration.isEmpty()) {
+            sb.append("Camel Main Configuration:\n");
+            for (var entry : startupConfiguration.entrySet()) {
+                String k = entry.getKey().toString();
+                Object v = entry.getValue();
+                String loc = locationSummary(startupConfiguration, k);
+                if (SensitiveUtils.containsSensitive(k)) {
+                    sb.append(String.format("    %s %s = xxxxxx%n", loc, k));
+                } else {
+                    sb.append(String.format("    %s %s = %s%n", loc, k, v));
+                }
+            }
+        }
+
+        return sb.toString();
+    }
+
+    @Override
+    protected Map<String, Object> doCallJson(Map<String, Object> options) {
+        JsonObject root = new JsonObject();
+        if (!startupConfiguration.isEmpty()) {
+            JsonArray arr = new JsonArray();
+            for (var entry : startupConfiguration.entrySet()) {
+                String k = entry.getKey().toString();
+                Object v = entry.getValue();
+                String loc = sanitizeLocation(startupConfiguration.getLocation(k));
+
+                JsonObject jo = new JsonObject();
+                jo.put("key", k);
+                jo.put("value", v);
+                if (loc != null) {
+                    jo.put("location", loc);
+                }
+                arr.add(jo);
+            }
+            root.put("configurations", arr);
+        }
+
+        return root;
+    }
+
+    private static String sanitizeLocation(String loc) {
+        if (loc == null) {
+            return null;
+        }
+        // remove scheme to make it shorter
+        if (loc.contains(":")) {
+            loc = StringHelper.after(loc, ":");
+        }
+        // strip paths so location is only the name
+        loc = FileUtil.stripPath(loc);
+        if ("initial".equals(loc) || "override".equals(loc)) {
+            loc = "camel-main";
+        }
+        return loc;
+    }
+}
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
index 2eef2577a6b..c4647e982c2 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
@@ -596,15 +596,15 @@ public final class MainHelper {
         }
         if (SensitiveUtils.containsSensitive(k)) {
             if (debug) {
-                log.debug("    {} {}=xxxxxx", loc, k);
+                log.debug("    {} {} = xxxxxx", loc, k);
             } else {
-                log.info("    {} {}=xxxxxx", loc, k);
+                log.info("    {} {} = xxxxxx", loc, k);
             }
         } else {
             if (debug) {
-                log.debug("    {} {}={}", loc, k, v);
+                log.debug("    {} {} = {}", loc, k, v);
             } else {
-                log.info("    {} {}={}", loc, k, v);
+                log.info("    {} {} = {}", loc, k, v);
             }
         }
     }
diff --git a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
index 31b30118cd8..5d7b415d558 100644
--- a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
+++ b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
@@ -1060,6 +1060,13 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
                         root.put("kafka", json);
                     }
                 }
+                DevConsole dc21 = dcr.resolveById("main-configuration");
+                if (dc21 != null) {
+                    JsonObject json = (JsonObject) dc21.call(DevConsole.MediaType.JSON);
+                    if (json != null && !json.isEmpty()) {
+                        root.put("main-configuration", json);
+                    }
+                }
             }
             // various details
             JsonObject mem = collectMemory();
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index 32346bf5032..6b49e4ad007 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -107,6 +107,7 @@ public class CamelJBangMain implements Callable<Integer> {
                         .addSubcommand("source", new CommandLine(new CamelSourceAction(main)))
                         .addSubcommand("route-dump", new CommandLine(new CamelRouteDumpAction(main)))
                         .addSubcommand("startup-recorder", new CommandLine(new CamelStartupRecorderAction(main)))
+                        .addSubcommand("startup-configuration", new CommandLine(new StartupConfiguration(main)))
                         .addSubcommand("vault", new CommandLine(new ListVault(main))))
                 .addSubcommand("top", new CommandLine(new CamelTop(main))
                         .addSubcommand("context", new CommandLine(new CamelContextTop(main)))
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StartupConfiguration.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StartupConfiguration.java
new file mode 100644
index 00000000000..c874d319770
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StartupConfiguration.java
@@ -0,0 +1,177 @@
+/*
+ * 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.dsl.jbang.core.commands.process;
+
+import com.github.freva.asciitable.AsciiTable;
+import com.github.freva.asciitable.Column;
+import com.github.freva.asciitable.HorizontalAlign;
+import com.github.freva.asciitable.OverflowBehaviour;
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
+import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.SensitiveUtils;
+import org.apache.camel.util.StringHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import picocli.CommandLine;
+import picocli.CommandLine.Command;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+@Command(name = "startup-configuration", description = "List startup configuration of Camel integrations", sortOptions = false)
+public class StartupConfiguration extends ProcessWatchCommand {
+
+    public static class PidNameKeyCompletionCandidates implements Iterable<String> {
+
+        public PidNameKeyCompletionCandidates() {
+        }
+
+        @Override
+        public Iterator<String> iterator() {
+            return List.of("pid", "name", "key").iterator();
+        }
+
+    }
+
+    @CommandLine.Parameters(description = "Name or pid of running Camel integration", arity = "0..1")
+    String name = "*";
+
+    @CommandLine.Option(names = { "--sort" }, completionCandidates = PidNameKeyCompletionCandidates.class,
+                        description = "Sort by pid, name or key", defaultValue = "pid")
+    String sort;
+
+    @CommandLine.Option(names = { "--sensitive" }, description = "Mask sensitive values such as passwords",
+                        defaultValue = "true")
+    boolean sensitive = true;
+
+    public StartupConfiguration(CamelJBangMain main) {
+        super(main);
+    }
+
+    @Override
+    public Integer doProcessWatchCall() throws Exception {
+        List<Row> rows = new ArrayList<>();
+
+        List<Long> pids = findPids(name);
+        ProcessHandle.allProcesses()
+                .filter(ph -> pids.contains(ph.pid()))
+                .forEach(ph -> {
+                    JsonObject root = loadStatus(ph.pid());
+                    // there must be a status file for the running Camel integration
+                    if (root != null) {
+                        Row row = new Row();
+                        JsonObject context = (JsonObject) root.get("context");
+                        if (context == null) {
+                            return;
+                        }
+                        row.name = context.getString("name");
+                        if ("CamelJBang".equals(row.name)) {
+                            row.name = ProcessHelper.extractName(root, ph);
+                        }
+                        row.pid = Long.toString(ph.pid());
+
+                        JsonObject jv = (JsonObject) root.get("main-configuration");
+                        JsonArray arr = jv.getCollection("configurations");
+                        for (int i = 0; i < arr.size(); i++) {
+                            row = row.copy();
+                            JsonObject jo = (JsonObject) arr.get(i);
+                            row.key = jo.getString("key");
+                            String value = jo.getString("value");
+                            if (sensitive && SensitiveUtils.containsSensitive(row.key)) {
+                                value = "xxxxxx";
+                            }
+                            row.value = value;
+                            row.loc = sanitizeLocation(jo.getString("location"));
+                            rows.add(row);
+                        }
+                    }
+                });
+
+        // sort rows
+        rows.sort(this::sortRow);
+
+        if (!rows.isEmpty()) {
+            printer().println(AsciiTable.getTable(AsciiTable.NO_BORDERS, rows, Arrays.asList(
+                    new Column().header("PID").headerAlign(HorizontalAlign.CENTER).with(r -> r.pid),
+                    new Column().header("NAME").dataAlign(HorizontalAlign.LEFT).maxWidth(30, OverflowBehaviour.ELLIPSIS_RIGHT)
+                            .with(r -> r.name),
+                    new Column().header("LOCATION").dataAlign(HorizontalAlign.LEFT).maxWidth(80, OverflowBehaviour.NEWLINE)
+                            .with(r -> r.loc),
+                    new Column().header("KEY").dataAlign(HorizontalAlign.LEFT).maxWidth(50, OverflowBehaviour.ELLIPSIS_RIGHT)
+                            .with(r -> r.key),
+                    new Column().header("VALUE").dataAlign(HorizontalAlign.LEFT).maxWidth(80, OverflowBehaviour.NEWLINE)
+                            .with(r -> "" + r.value))));
+        }
+
+        return 0;
+    }
+
+    protected int sortRow(Row o1, Row o2) {
+        String s = sort;
+        int negate = 1;
+        if (s.startsWith("-")) {
+            s = s.substring(1);
+            negate = -1;
+        }
+        switch (s) {
+            case "pid":
+                return Long.compare(Long.parseLong(o1.pid), Long.parseLong(o2.pid)) * negate;
+            case "name":
+                return o1.name.compareToIgnoreCase(o2.name) * negate;
+            case "key":
+                return o1.key.compareToIgnoreCase(o2.key) * negate;
+            default:
+                return 0;
+        }
+    }
+
+    private static class Row implements Cloneable {
+        String pid;
+        String name;
+        String key;
+        Object value;
+        String loc;
+
+        Row copy() {
+            try {
+                return (Row) clone();
+            } catch (CloneNotSupportedException e) {
+                return null;
+            }
+        }
+    }
+
+    private static String sanitizeLocation(String loc) {
+        if (loc == null) {
+            return "";
+        }
+        if ("initial".equals(loc) || "override".equals(loc)) {
+            loc = "camel-main";
+        } else if ("SYS".equals(loc)) {
+            loc = "JVM System Property";
+        } else if ("ENV".equals(loc)) {
+            loc = "OS Environment Variable";
+        } else if ("arguments".equals(loc) || "CLI".equals(loc)) {
+            loc = "Command Line";
+        }
+        return loc;
+    }
+
+}