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 2021/12/30 12:04:40 UTC

[camel] 24/30: CAMEL-17384: Developer Console SPI

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

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

commit d6ba6bf8b5e64af6b929fabea9cadd1f51a9d1de
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Dec 30 07:33:44 2021 +0100

    CAMEL-17384: Developer Console SPI
---
 .../impl/console/MemoryDevConsoleConfigurer.java   | 49 ++++++++++++++++++
 .../org.apache.camel.impl.console.MemoryDevConsole |  2 +
 .../services/org/apache/camel/dev-console/memory   |  2 +
 .../apache/camel/impl/console/JvmDevConsole.java   |  2 +-
 .../camel/impl/console/MemoryDevConsole.java       | 58 ++++++++++++++++++++++
 .../main/java/org/apache/camel/util/UnitUtils.java | 45 +++++++++++++++++
 .../java/org/apache/camel/util/UnitUtilsTest.java  | 19 ++++++-
 7 files changed, 175 insertions(+), 2 deletions(-)

diff --git a/core/camel-console/src/generated/java/org/apache/camel/impl/console/MemoryDevConsoleConfigurer.java b/core/camel-console/src/generated/java/org/apache/camel/impl/console/MemoryDevConsoleConfigurer.java
new file mode 100644
index 0000000..b5f9088
--- /dev/null
+++ b/core/camel-console/src/generated/java/org/apache/camel/impl/console/MemoryDevConsoleConfigurer.java
@@ -0,0 +1,49 @@
+/* Generated by camel build tools - do NOT edit this file! */
+package org.apache.camel.impl.console;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.ExtendedPropertyConfigurerGetter;
+import org.apache.camel.spi.PropertyConfigurerGetter;
+import org.apache.camel.spi.ConfigurerStrategy;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
+import org.apache.camel.util.CaseInsensitiveMap;
+import org.apache.camel.impl.console.MemoryDevConsole;
+
+/**
+ * Generated by camel build tools - do NOT edit this file!
+ */
+@SuppressWarnings("unchecked")
+public class MemoryDevConsoleConfigurer extends org.apache.camel.support.component.PropertyConfigurerSupport implements GeneratedPropertyConfigurer, PropertyConfigurerGetter {
+
+    @Override
+    public boolean configure(CamelContext camelContext, Object obj, String name, Object value, boolean ignoreCase) {
+        org.apache.camel.impl.console.MemoryDevConsole target = (org.apache.camel.impl.console.MemoryDevConsole) obj;
+        switch (ignoreCase ? name.toLowerCase() : name) {
+        case "camelcontext":
+        case "CamelContext": target.setCamelContext(property(camelContext, org.apache.camel.CamelContext.class, value)); return true;
+        default: return false;
+        }
+    }
+
+    @Override
+    public Class<?> getOptionType(String name, boolean ignoreCase) {
+        switch (ignoreCase ? name.toLowerCase() : name) {
+        case "camelcontext":
+        case "CamelContext": return org.apache.camel.CamelContext.class;
+        default: return null;
+        }
+    }
+
+    @Override
+    public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
+        org.apache.camel.impl.console.MemoryDevConsole target = (org.apache.camel.impl.console.MemoryDevConsole) obj;
+        switch (ignoreCase ? name.toLowerCase() : name) {
+        case "camelcontext":
+        case "CamelContext": return target.getCamelContext();
+        default: return null;
+        }
+    }
+}
+
diff --git a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/configurer/org.apache.camel.impl.console.MemoryDevConsole b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/configurer/org.apache.camel.impl.console.MemoryDevConsole
new file mode 100644
index 0000000..8f1eacc
--- /dev/null
+++ b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/configurer/org.apache.camel.impl.console.MemoryDevConsole
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.impl.console.MemoryDevConsoleConfigurer
diff --git a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/memory b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/memory
new file mode 100644
index 0000000..d38602e
--- /dev/null
+++ b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/memory
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.impl.console.MemoryDevConsole
diff --git a/core/camel-console/src/main/java/org/apache/camel/impl/console/JvmDevConsole.java b/core/camel-console/src/main/java/org/apache/camel/impl/console/JvmDevConsole.java
index 481dfdc..5534fcd 100644
--- a/core/camel-console/src/main/java/org/apache/camel/impl/console/JvmDevConsole.java
+++ b/core/camel-console/src/main/java/org/apache/camel/impl/console/JvmDevConsole.java
@@ -33,7 +33,7 @@ public class JvmDevConsole extends AbstractDevConsole {
     private boolean showClasspath = true;
 
     public JvmDevConsole() {
-        super("camel", "jvm", "JVM", "Displays JVM information");
+        super("jvm", "jvm", "JVM", "Displays JVM information");
     }
 
     public boolean isShowClasspath() {
diff --git a/core/camel-console/src/main/java/org/apache/camel/impl/console/MemoryDevConsole.java b/core/camel-console/src/main/java/org/apache/camel/impl/console/MemoryDevConsole.java
new file mode 100644
index 0000000..1fa9c0c
--- /dev/null
+++ b/core/camel-console/src/main/java/org/apache/camel/impl/console/MemoryDevConsole.java
@@ -0,0 +1,58 @@
+/*
+ * 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.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
+import java.util.Map;
+
+import org.apache.camel.spi.Configurer;
+import org.apache.camel.spi.annotations.DevConsole;
+
+import static org.apache.camel.util.UnitUtils.printUnitFromBytesDot;
+
+@DevConsole("memory")
+@Configurer(bootstrap = true)
+public class MemoryDevConsole extends AbstractDevConsole {
+
+    public MemoryDevConsole() {
+        super("jvm", "memory", "JVM Memory", "Displays JVM memory information");
+    }
+
+    @Override
+    protected Object doCall(MediaType mediaType, Map<String, Object> options) {
+        // only text is supported
+        StringBuilder sb = new StringBuilder();
+
+        MemoryMXBean mb = ManagementFactory.getMemoryMXBean();
+        if (mb != null) {
+            sb.append(String.format("Heap Init: %s\n", printUnitFromBytesDot(mb.getHeapMemoryUsage().getInit())));
+            sb.append(String.format("Heap Max: %s\n", printUnitFromBytesDot(mb.getHeapMemoryUsage().getMax())));
+            sb.append(String.format("Heap Used: %s\n", printUnitFromBytesDot(mb.getHeapMemoryUsage().getUsed())));
+            sb.append(String.format("Heap Committed: %s\n", printUnitFromBytesDot(mb.getHeapMemoryUsage().getCommitted())));
+            sb.append("\n");
+            sb.append(String.format("Non-Heap Init: %s\n", printUnitFromBytesDot(mb.getNonHeapMemoryUsage().getInit())));
+            sb.append(String.format("Non-Heap Max: %s\n", printUnitFromBytesDot(mb.getNonHeapMemoryUsage().getMax())));
+            sb.append(String.format("Non-Heap Used: %s\n", printUnitFromBytesDot(mb.getNonHeapMemoryUsage().getUsed())));
+            sb.append(String.format("Non-Heap Committed: %s\n",
+                    printUnitFromBytesDot(mb.getNonHeapMemoryUsage().getCommitted())));
+        }
+
+        return sb.toString();
+    }
+
+}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/UnitUtils.java b/core/camel-util/src/main/java/org/apache/camel/util/UnitUtils.java
index 528e53c..5265d71 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/UnitUtils.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/UnitUtils.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.util;
 
+import java.text.DecimalFormatSymbols;
+
 /**
  * Unit utils.
  */
@@ -31,6 +33,10 @@ public final class UnitUtils {
      * @param bytes the value in bytes
      */
     public static String printUnitFromBytes(long bytes) {
+        if (bytes < 0) {
+            return "";
+        }
+
         // http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
         int unit = 1000;
         if (bytes < unit) {
@@ -40,4 +46,43 @@ public final class UnitUtils {
         String pre = "" + "kMGTPE".charAt(exp - 1);
         return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
     }
+
+    /**
+     * If having a size in bytes and wanting to print this in human friendly\ format with xx kB, xx MB, xx GB instead of
+     * a large byte number, using dot as decimal separator.
+     *
+     * @param bytes the value in bytes
+     */
+    public static String printUnitFromBytesDot(long bytes) {
+        return printUnitFromBytes(bytes, '.');
+    }
+
+    /**
+     * If having a size in bytes and wanting to print this in human friendly\ format with xx kB, xx MB, xx GB instead of
+     * a large byte number.
+     *
+     * @param bytes   the value in bytes
+     * @param decimal the decimal separator char to use
+     */
+    public static String printUnitFromBytes(long bytes, char decimal) {
+        if (bytes < 0) {
+            return "";
+        }
+
+        // http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
+        int unit = 1000;
+        if (bytes < unit) {
+            return bytes + " B";
+        }
+        int exp = (int) (Math.log(bytes) / Math.log(unit));
+        String pre = "" + "kMGTPE".charAt(exp - 1);
+        String answer = String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
+
+        char sep = DecimalFormatSymbols.getInstance().getDecimalSeparator();
+        if (decimal != sep) {
+            answer = answer.replace(sep, decimal);
+        }
+        return answer;
+    }
+
 }
diff --git a/core/camel-util/src/test/java/org/apache/camel/util/UnitUtilsTest.java b/core/camel-util/src/test/java/org/apache/camel/util/UnitUtilsTest.java
index 9a73840..a932321 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/UnitUtilsTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/UnitUtilsTest.java
@@ -21,19 +21,20 @@ import java.text.DecimalFormatSymbols;
 import org.junit.jupiter.api.Test;
 
 import static org.apache.camel.util.UnitUtils.printUnitFromBytes;
+import static org.apache.camel.util.UnitUtils.printUnitFromBytesDot;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 public class UnitUtilsTest {
 
     @Test
     public void testPrintUnitFromBytes() throws Exception {
-
         // needed for the locales that have a decimal separator other than comma
         char decimalSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
 
         assertEquals("999 B", printUnitFromBytes(999));
         assertEquals("1" + decimalSeparator + "0 kB", printUnitFromBytes(1000));
         assertEquals("1" + decimalSeparator + "0 kB", printUnitFromBytes(1001));
+        assertEquals("1" + decimalSeparator + "2 kB", printUnitFromBytes(1201));
 
         assertEquals("1000" + decimalSeparator + "0 kB", printUnitFromBytes(999999));
         assertEquals("1" + decimalSeparator + "0 MB", printUnitFromBytes(1000000));
@@ -41,4 +42,20 @@ public class UnitUtilsTest {
 
         assertEquals("1" + decimalSeparator + "5 MB", printUnitFromBytes(1500001));
     }
+
+    @Test
+    public void testPrintUnitFromBytesDot() throws Exception {
+        char decimalSeparator = '.';
+
+        assertEquals("999 B", printUnitFromBytes(999));
+        assertEquals("1" + decimalSeparator + "0 kB", printUnitFromBytesDot(1000));
+        assertEquals("1" + decimalSeparator + "0 kB", printUnitFromBytesDot(1001));
+        assertEquals("1" + decimalSeparator + "2 kB", printUnitFromBytesDot(1201));
+
+        assertEquals("1000" + decimalSeparator + "0 kB", printUnitFromBytesDot(999999));
+        assertEquals("1" + decimalSeparator + "0 MB", printUnitFromBytesDot(1000000));
+        assertEquals("1" + decimalSeparator + "0 MB", printUnitFromBytesDot(1000001));
+
+        assertEquals("1" + decimalSeparator + "5 MB", printUnitFromBytesDot(1500001));
+    }
 }