You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2018/10/04 17:14:16 UTC

[camel-k] branch master updated: runtime: refine groovy dsl

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

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new 6d2b84a  runtime: refine groovy dsl
6d2b84a is described below

commit 6d2b84afef85ca00162ae14a768fa8367207c0d5
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Thu Oct 4 19:00:33 2018 +0200

    runtime: refine groovy dsl
---
 runtime/examples/routes.groovy                     |  6 +-
 .../k/groovy/dsl/ComponentsConfiguration.groovy    | 64 ++++++++++++++++------
 .../camel/k/groovy/dsl/IntegrationTest.groovy      | 20 +++++--
 .../routes-with-component-configuration.groovy     | 17 ++++--
 4 files changed, 79 insertions(+), 28 deletions(-)

diff --git a/runtime/examples/routes.groovy b/runtime/examples/routes.groovy
index a04b251..091713f 100644
--- a/runtime/examples/routes.groovy
+++ b/runtime/examples/routes.groovy
@@ -5,6 +5,10 @@ import java.util.concurrent.ThreadLocalRandom
 //
 //     kamel run --runtime groovy runtime/examples/routes.groovy
 //
+// Or leveraging runtime detection
+//
+//     kamel run runtime/examples/routes.groovy
+//
 
 context {
 
@@ -12,7 +16,7 @@ context {
     // configure components
     //
     components {
-        'log' {
+        log {
             formatter {
                 'body: ' + it.in.body + ', random-value: ' + it.in.headers['RandomValue']
             }
diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ComponentsConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ComponentsConfiguration.groovy
index bb7862a..7bf0f93 100644
--- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ComponentsConfiguration.groovy
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ComponentsConfiguration.groovy
@@ -17,6 +17,7 @@
 package org.apache.camel.k.groovy.dsl
 
 import org.apache.camel.CamelContext
+import org.apache.camel.Component
 
 import java.lang.reflect.Array
 
@@ -27,28 +28,57 @@ class ComponentsConfiguration {
         this.context = context
     }
 
-    def methodMissing(String name, args) {
-        final Object value
-
-        if (args == null) {
-            value = null
-        } else if (!args.getClass().isArray()) {
-            value = args
-        } else if (Array.getLength(args) == 1) {
-            value = Array.get(args, 0)
-        } else {
-            throw new IllegalArgumentException("Unexpected argument type: " + args)
+    def component(String name, Closure<?> callable) {
+        def component = context.getComponent(name, true, false)
+
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
+        callable.delegate = new ComponentConfiguration(component)
+        callable.call()
+    }
+
+    def component(String name, Class<? extends Component> type, Closure <?> callable) {
+        def component = context.getComponent(name, true, false)
+
+        // if the component is not found, let's create a new one. This is
+        // equivalent to create a new named component, useful to create
+        // multiple instances of the same component but with different setup
+        if (component == null) {
+            component = context.injector.newInstance(type)
+
+            // let's the camel context be aware of the new component
+            context.addComponent(name, component)
         }
 
-        if (value instanceof Closure<?>) {
-            def component = context.getComponent(name, true, false)
+        if (type.isAssignableFrom(component.class)) {
+            callable.resolveStrategy = Closure.DELEGATE_FIRST
+            callable.delegate = new ComponentConfiguration(component)
+            callable.call()
+
+            return
+        }
+
+        throw new IllegalArgumentException("Type mismatch, expected: " + type + ", got: " + component.class)
+    }
+
+    def methodMissing(String name, args) {
+        if (args != null && args.getClass().isArray()) {
+            if (Array.getLength(args) == 1) {
+                def clos = Array.get(args, 0)
 
-            value.resolveStrategy = Closure.DELEGATE_FIRST
-            value.delegate = new ComponentConfiguration(component)
+                if (clos instanceof Closure) {
+                    return component(name, clos)
+                }
+            }
+            if (Array.getLength(args) == 2) {
+                def type = Array.get(args, 0)
+                def clos = Array.get(args, 1)
 
-            return value.call()
+                if (type instanceof Class && Component.class.isAssignableFrom(type) && clos instanceof Closure) {
+                    return component(name,type, clos)
+                }
+            }
         }
 
-        throw new MissingMethodException("Missing method \"" + name + "\"")
+        throw new MissingMethodException("Missing method: \"$name\", args: $args")
     }
 }
diff --git a/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/IntegrationTest.groovy b/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/IntegrationTest.groovy
index 4e2fdfe..785b2e4 100644
--- a/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/IntegrationTest.groovy
+++ b/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/IntegrationTest.groovy
@@ -71,8 +71,10 @@ class IntegrationTest extends Specification {
 
     def "load integration with component configuration"()  {
         given:
-        def size = new AtomicInteger()
-        def consumers = new AtomicInteger()
+        def sedaSize = new AtomicInteger()
+        def sedaConsumers = new AtomicInteger()
+        def mySedaSize = new AtomicInteger()
+        def mySedaConsumers = new AtomicInteger()
         def format = new AtomicReference()
 
         when:
@@ -83,13 +85,17 @@ class IntegrationTest extends Specification {
             @Override
             void afterStart(MainSupport main) {
                 def seda = runtime.camelContext.getComponent('seda', SedaComponent)
+                def mySeda = runtime.camelContext.getComponent('mySeda', SedaComponent)
                 def log = runtime.camelContext.getComponent('log', LogComponent)
 
                 assert seda != null
+                assert mySeda != null
                 assert log != null
 
-                size = seda.queueSize
-                consumers = seda.concurrentConsumers
+                sedaSize = seda.queueSize
+                sedaConsumers = seda.concurrentConsumers
+                mySedaSize = mySeda.queueSize
+                mySedaConsumers = mySeda.concurrentConsumers
                 format = log.exchangeFormatter
 
                 main.stop()
@@ -99,8 +105,10 @@ class IntegrationTest extends Specification {
         runtime.run()
 
         then:
-        size == 1234
-        consumers == 12
+        sedaSize == 1234
+        sedaConsumers == 12
+        mySedaSize == 4321
+        mySedaConsumers == 21
         format != null
     }
 }
diff --git a/runtime/groovy/src/test/resources/routes-with-component-configuration.groovy b/runtime/groovy/src/test/resources/routes-with-component-configuration.groovy
index 1d7b5a4..ee6eb24 100644
--- a/runtime/groovy/src/test/resources/routes-with-component-configuration.groovy
+++ b/runtime/groovy/src/test/resources/routes-with-component-configuration.groovy
@@ -1,7 +1,8 @@
+import org.apache.camel.component.seda.SedaComponent
 
 context {
     components {
-        'seda' {
+        seda {
             // set value as method
             queueSize 1234
 
@@ -9,10 +10,18 @@ context {
             concurrentConsumers = 12
         }
 
-        'log' {
-            exchangeFormatter = {
+        mySeda(SedaComponent) {
+            // set value as method
+            queueSize 4321
+
+            // set value as property
+            concurrentConsumers = 21
+        }
+
+        log {
+            formatter {
                 'body ==> ' + it.in.body
-            } as org.apache.camel.spi.ExchangeFormatter
+            }
         }
     }
 }