You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2018/10/04 17:14:08 UTC

[GitHub] lburgazzoli closed pull request #157: runtime: refine groovy dsl

lburgazzoli closed pull request #157: runtime: refine groovy dsl
URL: https://github.com/apache/camel-k/pull/157
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/runtime/examples/routes.groovy b/runtime/examples/routes.groovy
index a04b2518..091713f6 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 bb7862a8..7bf0f935 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 4e2fdfe8..785b2e49 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 1d7b5a4e..ee6eb245 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
+            }
         }
     }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services