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 2023/12/30 15:15:14 UTC

(camel) 17/25: CAMEL-19749: Add variables as concept to Camel

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

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

commit 98e0b91b64de72db5a1f58cfe0ce3ffb853ec083
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Dec 30 10:00:10 2023 +0100

    CAMEL-19749: Add variables as concept to Camel
---
 .../org/apache/camel/dev-console/variables         |  2 +
 .../camel/impl/console/VariablesDevConsole.java    | 81 ++++++++++++++++++++++
 2 files changed, 83 insertions(+)

diff --git a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/variables b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/variables
new file mode 100644
index 00000000000..09e0f310219
--- /dev/null
+++ b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/variables
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.impl.console.VariablesDevConsole
diff --git a/core/camel-console/src/main/java/org/apache/camel/impl/console/VariablesDevConsole.java b/core/camel-console/src/main/java/org/apache/camel/impl/console/VariablesDevConsole.java
new file mode 100644
index 00000000000..a25e0a103eb
--- /dev/null
+++ b/core/camel-console/src/main/java/org/apache/camel/impl/console/VariablesDevConsole.java
@@ -0,0 +1,81 @@
+/*
+ * 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.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.camel.spi.BrowsableVariableRepository;
+import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.support.console.AbstractDevConsole;
+import org.apache.camel.util.json.JsonObject;
+
+@DevConsole("variables")
+public class VariablesDevConsole extends AbstractDevConsole {
+
+    public VariablesDevConsole() {
+        super("camel", "variables", "Variables", "Displays variables");
+    }
+
+    @Override
+    protected String doCallText(Map<String, Object> options) {
+        StringBuilder sb = new StringBuilder();
+
+        Set<BrowsableVariableRepository> repos = getCamelContext().getRegistry().findByType(BrowsableVariableRepository.class);
+        for (BrowsableVariableRepository repo : repos) {
+            sb.append(String.format("Variable repository: %s (size: %n)", repo.getId(), repo.size()));
+            sb.append("\n");
+            for (Map.Entry<String, Object> entry : repo.getVariables().entrySet()) {
+                String k = entry.getKey();
+                Object v = entry.getValue();
+                sb.append(String.format("\n    %s = %s", k, v));
+            }
+            sb.append("\n");
+        }
+
+        return sb.toString();
+    }
+
+    @Override
+    protected JsonObject doCallJson(Map<String, Object> options) {
+        JsonObject root = new JsonObject();
+
+        Set<BrowsableVariableRepository> repos = getCamelContext().getRegistry().findByType(BrowsableVariableRepository.class);
+        for (BrowsableVariableRepository repo : repos) {
+            List<JsonObject> arr = new ArrayList<>();
+            JsonObject jo = new JsonObject();
+            root.put(repo.getId(), jo);
+            jo.put("id", repo.getId());
+            jo.put("size", repo.size());
+            for (Map.Entry<String, Object> entry : repo.getVariables().entrySet()) {
+                String k = entry.getKey();
+                Object v = entry.getValue();
+                JsonObject e = new JsonObject();
+                e.put("key", k);
+                e.put("value", v);
+                arr.add(e);
+            }
+            if (!arr.isEmpty()) {
+                jo.put("variables", arr);
+            }
+        }
+
+        return root;
+    }
+}