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 cc288b85c35 camel-jbang - Show source code line when using --source
cc288b85c35 is described below

commit cc288b85c3550930f64473fcfbc3eaff14b00ef0
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Sep 16 12:16:24 2022 +0200

    camel-jbang - Show source code line when using --source
---
 .../apache/camel/impl/console/ConsoleHelper.java   | 105 +++++++++++++++++++++
 .../apache/camel/impl/console/RouteDevConsole.java |  13 +++
 .../camel/impl/console/SourceDevConsole.java       |  32 +------
 .../camel/model/ProcessorDefinitionHelper.java     |   2 +-
 .../org/apache/camel/support/LoggerHelper.java     |  15 +++
 .../camel/cli/connector/LocalCliConnector.java     |   2 +-
 .../jbang/core/commands/action/CamelSourceTop.java |   8 +-
 .../commands/process/CamelProcessorStatus.java     |  38 +++++++-
 .../core/commands/process/CamelRouteStatus.java    |   2 +-
 9 files changed, 176 insertions(+), 41 deletions(-)

diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsoleHelper.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsoleHelper.java
new file mode 100644
index 00000000000..bf45f524bcc
--- /dev/null
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsoleHelper.java
@@ -0,0 +1,105 @@
+/*
+ * 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.impl.console;
+
+import java.io.LineNumberReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.support.LoggerHelper;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+
+import static 
org.apache.camel.support.LoggerHelper.extractSourceLocationLineNumber;
+
+public final class ConsoleHelper {
+
+    private ConsoleHelper() {
+    }
+
+    public static List<JsonObject> loadSourceAsJson(CamelContext camelContext, 
String location) {
+        if (location == null) {
+            return null;
+        }
+        Integer lineNumber = extractSourceLocationLineNumber(location);
+        List<JsonObject> code = new ArrayList<>();
+        try {
+            location = LoggerHelper.stripSourceLocationLineNumber(location);
+            Resource resource = 
camelContext.adapt(ExtendedCamelContext.class).getResourceLoader()
+                    .resolveResource(location);
+            if (resource != null) {
+                LineNumberReader reader = new 
LineNumberReader(resource.getReader());
+                int i = 0;
+                String t;
+                do {
+                    t = reader.readLine();
+                    if (t != null) {
+                        i++;
+                        JsonObject c = new JsonObject();
+                        c.put("line", i);
+                        c.put("code", Jsoner.escape(t));
+                        if (lineNumber != null && lineNumber == i) {
+                            c.put("match", true);
+                        }
+                        code.add(c);
+                    }
+                } while (t != null);
+                IOHelper.close(reader);
+            }
+        } catch (Exception e) {
+            // ignore
+        }
+
+        return code.isEmpty() ? null : code;
+    }
+
+    public static String loadSourceLine(CamelContext camelContext, String 
location, Integer lineNumber) {
+        if (location == null || lineNumber == null) {
+            return null;
+        }
+
+        try {
+            location = LoggerHelper.stripSourceLocationLineNumber(location);
+            Resource resource = 
camelContext.adapt(ExtendedCamelContext.class).getResourceLoader()
+                    .resolveResource(location);
+            if (resource != null) {
+                LineNumberReader reader = new 
LineNumberReader(resource.getReader());
+                int i = 0;
+                String t;
+                do {
+                    t = reader.readLine();
+                    if (t != null) {
+                        i++;
+                        if (i == lineNumber) {
+                            return t;
+                        }
+                    }
+                } while (t != null);
+                IOHelper.close(reader);
+            }
+        } catch (Exception e) {
+            // ignore
+        }
+
+        return null;
+    }
+
+}
diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteDevConsole.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteDevConsole.java
index 9e8c68de25a..621a68b4cec 100644
--- 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteDevConsole.java
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteDevConsole.java
@@ -35,6 +35,7 @@ import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.TimeUtils;
 import org.apache.camel.util.json.JsonArray;
 import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
 
 @DevConsole("route")
 public class RouteDevConsole extends AbstractDevConsole {
@@ -269,6 +270,18 @@ public class RouteDevConsole extends AbstractDevConsole {
                 }
                 jo.put("source", loc);
             }
+            String line = ConsoleHelper.loadSourceLine(getCamelContext(), 
mp.getSourceLocation(), mp.getSourceLineNumber());
+            if (line != null) {
+                JsonArray ca = new JsonArray();
+                jo.put("code", ca);
+                JsonObject c = new JsonObject();
+                if (mp.getSourceLineNumber() != null) {
+                    c.put("line", mp.getSourceLineNumber());
+                }
+                c.put("code", Jsoner.escape(line));
+                c.put("match", true);
+                ca.add(c);
+            }
             jo.put("processor", mp.getProcessorName());
             jo.put("level", mp.getLevel());
             JsonObject stats = new JsonObject();
diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/SourceDevConsole.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/SourceDevConsole.java
index c0c982a752c..18b35b39485 100644
--- 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/SourceDevConsole.java
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/SourceDevConsole.java
@@ -34,7 +34,6 @@ import org.apache.camel.support.PatternHelper;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.json.JsonObject;
-import org.apache.camel.util.json.Jsoner;
 
 @DevConsole("source")
 public class SourceDevConsole extends AbstractDevConsole {
@@ -115,34 +114,9 @@ public class SourceDevConsole extends AbstractDevConsole {
             }
 
             String loc = mrb.getSourceLocation();
-            if (loc != null) {
-                List<JsonObject> code = new ArrayList<>();
-                try {
-                    loc = LoggerHelper.stripSourceLocationLineNumber(loc);
-                    Resource resource = 
getCamelContext().adapt(ExtendedCamelContext.class).getResourceLoader()
-                            .resolveResource(loc);
-                    if (resource != null) {
-                        LineNumberReader reader = new 
LineNumberReader(resource.getReader());
-                        int i = 0;
-                        String t;
-                        do {
-                            t = reader.readLine();
-                            if (t != null) {
-                                i++;
-                                JsonObject c = new JsonObject();
-                                c.put("line", i);
-                                c.put("code", Jsoner.escape(t));
-                                code.add(c);
-                            }
-                        } while (t != null);
-                        IOHelper.close(reader);
-                    }
-                } catch (Exception e) {
-                    // ignore
-                }
-                if (!code.isEmpty()) {
-                    jo.put("code", code);
-                }
+            List<JsonObject> code = 
ConsoleHelper.loadSourceAsJson(getCamelContext(), loc);
+            if (code != null) {
+                jo.put("code", code);
             }
             return null;
         };
diff --git 
a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java
 
b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java
index ec9105817d7..bcb42711f2c 100644
--- 
a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java
+++ 
b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java
@@ -429,7 +429,7 @@ public final class ProcessorDefinitionHelper {
     public static int getNodeLevel(NamedNode node) {
         int level = 0;
         while (node != null && node.getParent() != null) {
-            boolean shallow = node instanceof OutputExpressionNode || node 
instanceof OtherwiseDefinition;
+            boolean shallow = node instanceof WhenDefinition || node 
instanceof OtherwiseDefinition;
             node = node.getParent();
             if (!shallow) {
                 level++;
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/LoggerHelper.java 
b/core/camel-support/src/main/java/org/apache/camel/support/LoggerHelper.java
index 357cb858ca1..c2c1060d006 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/LoggerHelper.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/LoggerHelper.java
@@ -98,4 +98,19 @@ public final class LoggerHelper {
         }
     }
 
+    public static Integer extractSourceLocationLineNumber(String location) {
+        int cnt = StringHelper.countChar(location, ':');
+        if (cnt > 1) {
+            int pos = location.lastIndexOf(':');
+            String num = location.substring(pos);
+            try {
+                return Integer.valueOf(num);
+            } catch (Exception e) {
+                return null;
+            }
+        } else {
+            return null;
+        }
+    }
+
 }
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 8ccbce5d778..59385195a1b 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
@@ -323,7 +323,7 @@ public class LocalCliConnector extends ServiceSupport 
implements CliConnector, C
                     .getDevConsoleResolver().resolveDevConsole("route");
             if (dc != null && dc2 != null) {
                 JsonObject json = (JsonObject) 
dc.call(DevConsole.MediaType.JSON);
-                JsonObject json2 = (JsonObject) 
dc2.call(DevConsole.MediaType.JSON, Map.of("processors", "true"));
+                JsonObject json2 = (JsonObject) 
dc2.call(DevConsole.MediaType.JSON, Map.of("processors", "true", "source", 
"true"));
                 if (json != null && json2 != null) {
                     root.put("context", json);
                     root.put("routes", json2.get("routes"));
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceTop.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceTop.java
index 83ce440e189..2a71f09b672 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceTop.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceTop.java
@@ -117,7 +117,7 @@ public class CamelSourceTop extends ActionBaseCommand {
                 }
 
                 boolean add = true;
-                if (mean > 0 && row.mean != null && Long.parseLong(row.mean) < 
mean) {
+                if (mean > 0 && (row.mean == null || Long.parseLong(row.mean) 
< mean)) {
                     add = false;
                 }
                 if (limit > 0 && rows.size() >= limit) {
@@ -207,9 +207,9 @@ public class CamelSourceTop extends ActionBaseCommand {
     }
 
     private static class Code {
-        private int line;
-        private String code;
-        private boolean match;
+        int line;
+        String code;
+        boolean match;
     }
 
 }
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelProcessorStatus.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelProcessorStatus.java
index 46a31caeb88..0063af85637 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelProcessorStatus.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelProcessorStatus.java
@@ -29,6 +29,7 @@ import 
org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.json.JsonArray;
 import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
 import picocli.CommandLine;
 import picocli.CommandLine.Command;
 
@@ -108,7 +109,7 @@ public class CamelProcessorStatus extends 
ProcessBaseCommand {
                             }
 
                             boolean add = true;
-                            if (mean > 0 && row.mean != null && 
Long.parseLong(row.mean) < mean) {
+                            if (mean > 0 && (row.mean == null || 
Long.parseLong(row.mean) < mean)) {
                                 add = false;
                             }
                             if (limit > 0 && rows.size() >= limit) {
@@ -164,18 +165,30 @@ public class CamelProcessorStatus extends 
ProcessBaseCommand {
                     row.sinceLastFailed = last.toString();
                 }
             }
+            if (source) {
+                List<JsonObject> lines = o.getCollection("code");
+                for (JsonObject line : lines) {
+                    Code code = new Code();
+                    code.line = line.getInteger("line");
+                    code.code = line.getString("code");
+                    if (line.getBooleanOrDefault("match", false)) {
+                        code.match = true;
+                    }
+                    row.code.add(code);
+                }
+            }
         }
     }
 
     protected void printTable(List<Row> rows) {
-        // need to flatten list
         System.out.println(AsciiTable.getTable(AsciiTable.NO_BORDERS, rows, 
Arrays.asList(
                 new 
Column().header("PID").headerAlign(HorizontalAlign.CENTER).with(this::getPid),
                 new 
Column().header("NAME").dataAlign(HorizontalAlign.LEFT).maxWidth(30, 
OverflowBehaviour.ELLIPSIS_RIGHT)
                         .with(this::getName),
                 new 
Column().header("ID").dataAlign(HorizontalAlign.LEFT).maxWidth(40, 
OverflowBehaviour.ELLIPSIS_RIGHT)
                         .with(this::getId),
-                new 
Column().header("PROCESSOR").dataAlign(HorizontalAlign.LEFT).maxWidth(40, 
OverflowBehaviour.ELLIPSIS_RIGHT)
+                new 
Column().header("PROCESSOR").dataAlign(HorizontalAlign.LEFT).minWidth(25)
+                        .maxWidth(45, OverflowBehaviour.ELLIPSIS_RIGHT)
                         .with(this::getProcessor),
                 new Column().header("TOTAL").with(r -> r.total),
                 new Column().header("FAIL").with(r -> r.failed),
@@ -228,8 +241,16 @@ public class CamelProcessorStatus extends 
ProcessBaseCommand {
     }
 
     protected String getProcessor(Row r) {
-        String pad = StringHelper.padString(r.level, 1);
-        return pad + r.processor;
+        String answer = r.processor;
+        if (source) {
+            answer = r.code.stream()
+                    .filter(l -> l.match)
+                    .findFirst()
+                    .map(l -> Jsoner.unescape(l.code).trim())
+                    .orElse(r.processor);
+        }
+        String pad = StringHelper.padString(r.level, 2);
+        return pad + answer;
     }
 
     static class Row {
@@ -251,6 +272,13 @@ public class CamelProcessorStatus extends 
ProcessBaseCommand {
         String sinceLastStarted;
         String sinceLastCompleted;
         String sinceLastFailed;
+        List<Code> code = new ArrayList<>();
+    }
+
+    private static class Code {
+        int line;
+        String code;
+        boolean match;
     }
 
 }
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteStatus.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteStatus.java
index 59424264b48..a0141a191d1 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteStatus.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteStatus.java
@@ -130,7 +130,7 @@ public class CamelRouteStatus extends ProcessBaseCommand {
                             }
 
                             boolean add = true;
-                            if (mean > 0 && row.mean != null && 
Long.parseLong(row.mean) < mean) {
+                            if (mean > 0 && (row.mean == null || 
Long.parseLong(row.mean) < mean)) {
                                 add = false;
                             }
                             if (limit > 0 && rows.size() >= limit) {

Reply via email to