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/09/23 19:27:58 UTC

[camel] 01/02: camel-netty - Add console for services

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 2ac16ae71784447ba7a3d84ca17f00ef802a47ec
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Sep 23 21:22:20 2022 +0200

    camel-netty - Add console for services
---
 components/camel-netty/pom.xml                     |  4 +
 .../services/org/apache/camel/dev-console/netty    |  2 +
 .../apache/camel/component/netty/NettyConsole.java | 88 ++++++++++++++++++++++
 .../camel/cli/connector/LocalCliConnector.java     |  9 +++
 .../jbang/core/commands/process/ListService.java   | 34 ++++++---
 5 files changed, 128 insertions(+), 9 deletions(-)

diff --git a/components/camel-netty/pom.xml b/components/camel-netty/pom.xml
index b41aced6b56..bcb9ef38db4 100644
--- a/components/camel-netty/pom.xml
+++ b/components/camel-netty/pom.xml
@@ -41,6 +41,10 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-support</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-console</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>io.netty</groupId>
diff --git a/components/camel-netty/src/generated/resources/META-INF/services/org/apache/camel/dev-console/netty b/components/camel-netty/src/generated/resources/META-INF/services/org/apache/camel/dev-console/netty
new file mode 100644
index 00000000000..cba5e6691ad
--- /dev/null
+++ b/components/camel-netty/src/generated/resources/META-INF/services/org/apache/camel/dev-console/netty
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.netty.NettyConsole
diff --git a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyConsole.java b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyConsole.java
new file mode 100644
index 00000000000..d35c4a667b5
--- /dev/null
+++ b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyConsole.java
@@ -0,0 +1,88 @@
+/*
+ * 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.component.netty;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Route;
+import org.apache.camel.impl.console.AbstractDevConsole;
+import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.util.json.JsonObject;
+
+@DevConsole("netty")
+public class NettyConsole extends AbstractDevConsole {
+
+    public NettyConsole() {
+        super("camel", "netty", "Netty", "Embedded Netty Server");
+    }
+
+    @Override
+    protected String doCallText(Map<String, Object> options) {
+        StringBuilder sb = new StringBuilder();
+
+        List<Consumer> list = getCamelContext().getRoutes()
+                .stream().map(Route::getConsumer)
+                .filter(c -> NettyConsumer.class.getName().equals(c.getClass().getName()))
+                .collect(Collectors.toList());
+
+        for (Consumer c : list) {
+            NettyConsumer nc = (NettyConsumer) c;
+            NettyConfiguration conf = nc.getConfiguration();
+            // skip client mode as then it is not a local service
+            if (!conf.isClientMode()) {
+                sb.append(String.format("    %s:%s:%d\n", conf.getProtocol(), conf.getHost(), conf.getPort()));
+            }
+        }
+
+        return sb.toString();
+    }
+
+    @Override
+    protected JsonObject doCallJson(Map<String, Object> options) {
+        JsonObject root = new JsonObject();
+
+        List<Consumer> list = getCamelContext().getRoutes()
+                .stream().map(Route::getConsumer)
+                .filter(c -> NettyConsumer.class.getName().equals(c.getClass().getName()))
+                .collect(Collectors.toList());
+
+        List<JsonObject> arr = new ArrayList<>();
+        for (Consumer c : list) {
+            NettyConsumer nc = (NettyConsumer) c;
+            NettyConfiguration conf = nc.getConfiguration();
+
+            if (!conf.isClientMode()) {
+                // skip client mode as then it is not a local service
+                JsonObject jo = new JsonObject();
+                jo.put("protocol", conf.getProtocol());
+                jo.put("host", conf.getHost());
+                jo.put("port", conf.getPort());
+                arr.add(jo);
+            }
+        }
+        if (!arr.isEmpty()) {
+            root.put("endpoints", arr);
+        }
+
+        return root;
+    }
+
+}
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 f45fb5e6100..18962894163 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
@@ -497,6 +497,15 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
                 root.put("platform-http", json);
             }
         }
+        // netty is optional
+        dc = camelContext.adapt(ExtendedCamelContext.class)
+                .getDevConsoleResolver().lookupDevConsole("netty");
+        if (dc.isPresent()) {
+            JsonObject json = (JsonObject) dc.get().call(DevConsole.MediaType.JSON);
+            if (json != null) {
+                root.put("netty", json);
+            }
+        }
         return root;
     }
 
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListService.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListService.java
index 9d57443a98d..5995f3d0a73 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListService.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListService.java
@@ -73,17 +73,33 @@ public class ListService extends ProcessBaseCommand {
                         }
                         if (jo != null) {
                             JsonArray arr = (JsonArray) jo.get("endpoints");
-                            for (int i = 0; i < arr.size(); i++) {
-                                if (i > 0) {
-                                    // create a copy for 2+ secrets
+                            if (arr != null) {
+                                for (int i = 0; i < arr.size(); i++) {
                                     row = row.copy();
+                                    jo = (JsonObject) arr.get(i);
+                                    row.component = "platform-http";
+                                    row.protocol = "http";
+                                    row.service = jo.getString("url");
+                                    row.verbs = jo.getString("verbs");
+                                    rows.add(row);
+                                }
+                            }
+                        }
+                        jo = (JsonObject) root.get("services");
+                        if (jo != null) {
+                            jo = (JsonObject) jo.get("netty");
+                        }
+                        if (jo != null) {
+                            JsonArray arr = (JsonArray) jo.get("endpoints");
+                            if (arr != null) {
+                                for (int i = 0; i < arr.size(); i++) {
+                                    row = row.copy();
+                                    jo = (JsonObject) arr.get(i);
+                                    row.component = "netty";
+                                    row.protocol = jo.getString("protocol");
+                                    row.service = row.protocol + ":" + jo.getString("host") + ":" + jo.getInteger("port");
+                                    rows.add(row);
                                 }
-                                jo = (JsonObject) arr.get(i);
-                                row.component = "platform-http";
-                                row.protocol = "http";
-                                row.service = jo.getString("url");
-                                row.verbs = jo.getString("verbs");
-                                rows.add(row);
                             }
                         }
                     }