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:34 UTC

[camel] 18/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 50434265db24d332c968bcbc668e89f7d8f01281
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 29 11:13:48 2021 +0100

    CAMEL-17384: Developer Console SPI
---
 .../apache/camel/console/DevConsoleRegistry.java   |  2 +-
 .../camel/impl/console/EventConsoleConfigurer.java | 55 ++++++++++++++++++++++
 .../org.apache.camel.impl.console.EventConsole     |  2 +
 .../impl/console/DefaultDevConsoleRegistry.java    |  3 ++
 .../apache/camel/impl/console/EventConsole.java    | 25 ++++++++--
 .../org/apache/camel/main/BaseMainSupport.java     | 39 ++++++++++++++-
 6 files changed, 121 insertions(+), 5 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/console/DevConsoleRegistry.java b/core/camel-api/src/main/java/org/apache/camel/console/DevConsoleRegistry.java
index 4a160b9..f3051f5 100644
--- a/core/camel-api/src/main/java/org/apache/camel/console/DevConsoleRegistry.java
+++ b/core/camel-api/src/main/java/org/apache/camel/console/DevConsoleRegistry.java
@@ -60,7 +60,7 @@ public interface DevConsoleRegistry extends CamelContextAware, StaticService, Id
      *
      * @return either {@link DevConsole}, or <tt>null</tt> if none found.
      */
-    Object resolveById(String id);
+    DevConsole resolveById(String id);
 
     /**
      * Registers a {@link DevConsole}.
diff --git a/core/camel-console/src/generated/java/org/apache/camel/impl/console/EventConsoleConfigurer.java b/core/camel-console/src/generated/java/org/apache/camel/impl/console/EventConsoleConfigurer.java
new file mode 100644
index 0000000..4390bad
--- /dev/null
+++ b/core/camel-console/src/generated/java/org/apache/camel/impl/console/EventConsoleConfigurer.java
@@ -0,0 +1,55 @@
+/* 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.EventConsole;
+
+/**
+ * Generated by camel build tools - do NOT edit this file!
+ */
+@SuppressWarnings("unchecked")
+public class EventConsoleConfigurer 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.EventConsole target = (org.apache.camel.impl.console.EventConsole) obj;
+        switch (ignoreCase ? name.toLowerCase() : name) {
+        case "camelcontext":
+        case "CamelContext": target.setCamelContext(property(camelContext, org.apache.camel.CamelContext.class, value)); return true;
+        case "capacity":
+        case "Capacity": target.setCapacity(property(camelContext, int.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;
+        case "capacity":
+        case "Capacity": return int.class;
+        default: return null;
+        }
+    }
+
+    @Override
+    public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
+        org.apache.camel.impl.console.EventConsole target = (org.apache.camel.impl.console.EventConsole) obj;
+        switch (ignoreCase ? name.toLowerCase() : name) {
+        case "camelcontext":
+        case "CamelContext": return target.getCamelContext();
+        case "capacity":
+        case "Capacity": return target.getCapacity();
+        default: return null;
+        }
+    }
+}
+
diff --git a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/configurer/org.apache.camel.impl.console.EventConsole b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/configurer/org.apache.camel.impl.console.EventConsole
new file mode 100644
index 0000000..4d40f2f
--- /dev/null
+++ b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/configurer/org.apache.camel.impl.console.EventConsole
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.impl.console.EventConsoleConfigurer
diff --git a/core/camel-console/src/main/java/org/apache/camel/impl/console/DefaultDevConsoleRegistry.java b/core/camel-console/src/main/java/org/apache/camel/impl/console/DefaultDevConsoleRegistry.java
index 6071798..7089201 100644
--- a/core/camel-console/src/main/java/org/apache/camel/impl/console/DefaultDevConsoleRegistry.java
+++ b/core/camel-console/src/main/java/org/apache/camel/impl/console/DefaultDevConsoleRegistry.java
@@ -120,6 +120,9 @@ public class DefaultDevConsoleRegistry extends ServiceSupport implements DevCons
         if (answer == null) {
             DevConsoleResolver resolver = camelContext.adapt(ExtendedCamelContext.class).getDevConsoleResolver();
             answer = resolver.resolveDevConsole(id);
+            if (answer != null) {
+                register(answer);
+            }
         }
 
         return answer;
diff --git a/core/camel-console/src/main/java/org/apache/camel/impl/console/EventConsole.java b/core/camel-console/src/main/java/org/apache/camel/impl/console/EventConsole.java
index bbd2393..68ab032 100644
--- a/core/camel-console/src/main/java/org/apache/camel/impl/console/EventConsole.java
+++ b/core/camel-console/src/main/java/org/apache/camel/impl/console/EventConsole.java
@@ -21,29 +21,48 @@ import java.util.Map;
 import java.util.Queue;
 
 import org.apache.camel.spi.CamelEvent;
+import org.apache.camel.spi.Configurer;
 import org.apache.camel.spi.annotations.DevConsole;
 import org.apache.camel.support.EventNotifierSupport;
 
 @DevConsole("event")
+@Configurer(bootstrap = true)
 public class EventConsole extends AbstractDevConsole {
 
     private int capacity = 25;
 
-    private final Queue<CamelEvent> events = new ArrayDeque<>(capacity);
-    private final Queue<CamelEvent.ExchangeEvent> exchangeEvents = new ArrayDeque<>(capacity);
+    private Queue<CamelEvent> events;
+    private Queue<CamelEvent.ExchangeEvent> exchangeEvents;
     private final ConsoleEventNotifier listener = new ConsoleEventNotifier();
 
     public EventConsole() {
         super("camel", "event");
     }
 
+    public int getCapacity() {
+        return capacity;
+    }
+
+    /**
+     * Shows last number of events.
+     */
+    public void setCapacity(int capacity) {
+        this.capacity = capacity;
+    }
+
     @Override
     protected void doInit() throws Exception {
+        this.events = new ArrayDeque<>(capacity);
+        this.exchangeEvents = new ArrayDeque<>(capacity);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
         getCamelContext().getManagementStrategy().addEventNotifier(listener);
     }
 
     @Override
-    protected void doShutdown() throws Exception {
+    protected void doStop() throws Exception {
         getCamelContext().getManagementStrategy().removeEventNotifier(listener);
         events.clear();
     }
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 e98ebd5..7568ccd 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
@@ -41,6 +41,8 @@ import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.NoSuchLanguageException;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.StartupStep;
+import org.apache.camel.console.DevConsole;
+import org.apache.camel.console.DevConsoleRegistry;
 import org.apache.camel.health.HealthCheck;
 import org.apache.camel.health.HealthCheckConfiguration;
 import org.apache.camel.health.HealthCheckRegistry;
@@ -721,6 +723,7 @@ public abstract class BaseMainSupport extends BaseService {
         Map<String, Object> lraProperties = new LinkedHashMap<>();
         Map<String, Object> routeTemplateProperties = new LinkedHashMap<>();
         Map<String, Object> beansProperties = new LinkedHashMap<>();
+        Map<String, Object> devConsoleProperties = new LinkedHashMap<>();
         Map<String, String> globalOptions = new LinkedHashMap<>();
         for (String key : prop.stringPropertyNames()) {
             if (key.startsWith("camel.context.")) {
@@ -777,6 +780,12 @@ public abstract class BaseMainSupport extends BaseService {
                 String option = key.substring(20);
                 validateOptionAndValue(key, option, value);
                 routeTemplateProperties.put(optionKey(option), value);
+            } else if (key.startsWith("camel.dev-console.")) {
+                // grab the value
+                String value = prop.getProperty(key);
+                String option = key.substring(18);
+                validateOptionAndValue(key, option, value);
+                devConsoleProperties.put(optionKey(option), value);
             } else if (key.startsWith("camel.beans.")) {
                 // grab the value
                 String value = prop.getProperty(key);
@@ -786,7 +795,7 @@ public abstract class BaseMainSupport extends BaseService {
             } else if (key.startsWith("camel.global-options.")) {
                 // grab the value
                 String value = prop.getProperty(key);
-                String option = key.substring(12);
+                String option = key.substring(21);
                 validateOptionAndValue(key, option, value);
                 globalOptions.put(optionKey(option), value);
             }
@@ -837,6 +846,12 @@ public abstract class BaseMainSupport extends BaseService {
             setLraCheckProperties(camelContext, lraProperties, mainConfigurationProperties.isAutoConfigurationFailFast(),
                     autoConfiguredProperties);
         }
+        if (!devConsoleProperties.isEmpty()) {
+            LOG.debug("Auto-configuring Dev Console from loaded properties: {}", devConsoleProperties.size());
+            setDevConsoleProperties(camelContext, devConsoleProperties,
+                    mainConfigurationProperties.isAutoConfigurationFailFast(),
+                    autoConfiguredProperties);
+        }
 
         // configure which requires access to the model
         MainSupportModelConfigurer.configureModelCamelContext(camelContext, mainConfigurationProperties,
@@ -1044,6 +1059,28 @@ public abstract class BaseMainSupport extends BaseService {
         }
     }
 
+    private void setDevConsoleProperties(
+            CamelContext camelContext, Map<String, Object> properties,
+            boolean failIfNotSet, Map<String, String> autoConfiguredProperties)
+            throws Exception {
+
+        // make defensive copy as we mutate the map
+        Set<String> keys = new LinkedHashSet<>(properties.keySet());
+        // set properties per console
+        for (String key : keys) {
+            String name = StringHelper.before(key, ".");
+            DevConsole console = camelContext.getExtension(DevConsoleRegistry.class).resolveById(name);
+            if (console == null) {
+                throw new IllegalArgumentException(
+                        "Cannot resolve DevConsole with id: " + name);
+            }
+            // configure all the properties on the console at once (to ensure they are configured in right order)
+            Map<String, Object> config = PropertiesHelper.extractProperties(properties, name + ".");
+            setPropertiesOnTarget(camelContext, console, config, "camel.dev-console." + name + ".", failIfNotSet, true,
+                    autoConfiguredProperties);
+        }
+    }
+
     private void bindBeansToRegistry(
             CamelContext camelContext, Map<String, Object> properties,
             String optionPrefix, boolean failIfNotSet, boolean logSummary, boolean ignoreCase,