You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2018/10/03 10:44:48 UTC

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

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

nferraro 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 ba5ac21  runtime: improve groovy dsl
ba5ac21 is described below

commit ba5ac21e1a72576783ed137f5054f10cb1f00a8e
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue Oct 2 18:10:28 2018 +0200

    runtime: improve groovy dsl
---
 runtime/examples/routes.groovy                     | 34 +++++++++++---
 .../camel/k/groovy/GroovyRoutesLoader.groovy       |  4 +-
 .../k/groovy/dsl/ComponentsConfiguration.groovy    | 54 ++++++++++++++++++++++
 .../k/groovy/dsl/ContextConfiguration.groovy}      | 39 ++++++++--------
 ...tion.groovy => IntegrationConfiguration.groovy} | 44 ++++++++++++------
 .../RegistryConfiguration.groovy}                  | 25 +++++-----
 .../groovy/extension/LogComponentExtension.groovy  |  5 +-
 .../camel/k/groovy/dsl/IntegrationTest.groovy      | 23 ---------
 .../k/groovy/dsl/extension/LogExtensionTest.groovy |  2 +-
 .../src/test/resources/routes-with-bindings.groovy |  8 ++--
 .../routes-with-component-configuration.groovy     | 22 +++++----
 11 files changed, 165 insertions(+), 95 deletions(-)

diff --git a/runtime/examples/routes.groovy b/runtime/examples/routes.groovy
index 7ce04bb..a04b251 100644
--- a/runtime/examples/routes.groovy
+++ b/runtime/examples/routes.groovy
@@ -1,16 +1,38 @@
+import java.util.concurrent.ThreadLocalRandom
+
 //
 // To run this integrations use:
 //
-//     kamel run -d camel:groovy runtime/examples/routes.groovy
+//     kamel run --runtime groovy runtime/examples/routes.groovy
 //
 
-rnd = new Random()
+context {
+
+    //
+    // configure components
+    //
+    components {
+        'log' {
+            formatter {
+                'body: ' + it.in.body + ', random-value: ' + it.in.headers['RandomValue']
+            }
+        }
+    }
+
+    //
+    // configure registry
+    //
+    registry {
+        bind 'myProcessor', processor {
+            it.in.headers['RandomValue'] = ThreadLocalRandom.current().nextInt()
+        }
+    }
+}
+
 
 from('timer:groovy?period=1s')
     .routeId('groovy')
     .setBody()
         .constant('Hello Camel K!')
-    .process {
-        it.in.headers['RandomValue'] = rnd.nextInt()
-    }
-    .to('log:info?showHeaders=true')
\ No newline at end of file
+    .process('myProcessor')
+    .to('log:info')
\ No newline at end of file
diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/GroovyRoutesLoader.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/GroovyRoutesLoader.groovy
index e3042aa..1f61b10 100644
--- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/GroovyRoutesLoader.groovy
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/GroovyRoutesLoader.groovy
@@ -18,7 +18,7 @@ package org.apache.camel.k.groovy
 
 
 import org.apache.camel.builder.RouteBuilder
-import org.apache.camel.k.groovy.dsl.Integration
+import org.apache.camel.k.groovy.dsl.IntegrationConfiguration
 import org.apache.camel.k.jvm.Language
 import org.apache.camel.k.jvm.RoutesLoader
 import org.apache.camel.k.jvm.RuntimeRegistry
@@ -48,7 +48,7 @@ class GroovyRoutesLoader implements RoutesLoader {
                     def script = (DelegatingScript) sh.parse(reader)
 
                     // set the delegate target
-                    script.setDelegate(new Integration(registry, this))
+                    script.setDelegate(new IntegrationConfiguration(registry, this))
                     script.run()
                 }
             }
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
new file mode 100644
index 0000000..bb7862a
--- /dev/null
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ComponentsConfiguration.groovy
@@ -0,0 +1,54 @@
+/**
+ * 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.k.groovy.dsl
+
+import org.apache.camel.CamelContext
+
+import java.lang.reflect.Array
+
+class ComponentsConfiguration {
+    private final CamelContext context
+
+    ComponentsConfiguration(CamelContext context) {
+        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)
+        }
+
+        if (value instanceof Closure<?>) {
+            def component = context.getComponent(name, true, false)
+
+            value.resolveStrategy = Closure.DELEGATE_FIRST
+            value.delegate = new ComponentConfiguration(component)
+
+            return value.call()
+        }
+
+        throw new MissingMethodException("Missing method \"" + name + "\"")
+    }
+}
diff --git a/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/extension/LogExtensionTest.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy
similarity index 51%
copy from runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/extension/LogExtensionTest.groovy
copy to runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy
index e53433a..405fe8c 100644
--- a/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/extension/LogExtensionTest.groovy
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy
@@ -14,30 +14,29 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.k.groovy.dsl.extension
+package org.apache.camel.k.groovy.dsl
 
-import org.apache.camel.component.log.LogComponent
-import org.apache.camel.impl.DefaultCamelContext
-import org.apache.camel.impl.DefaultExchange
-import spock.lang.Specification
+import org.apache.camel.CamelContext
+import org.apache.camel.k.jvm.RuntimeRegistry
 
-class LogExtensionTest extends Specification {
+class ContextConfiguration {
+    private final CamelContext context
+    private final RuntimeRegistry registry
 
-    def "invoke extension method - formatter"()  {
-        given:
-        def ctx = new DefaultCamelContext()
-
-        when:
-        def log = new LogComponent()
-        log.formatter {
-            "body: " + in.body
-        }
+    ContextConfiguration(CamelContext context, RuntimeRegistry registry) {
+        this.context = context
+        this.registry = registry
+    }
 
-        def ex = new DefaultExchange(ctx)
-        ex.in.body = 'hello'
-        def result = log.exchangeFormatter.format(ex)
+    def registry(Closure<?> callable) {
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
+        callable.delegate = new RegistryConfiguration(registry)
+        callable.call()
+    }
 
-        then:
-        result == 'body: hello'
+    def components(Closure<?> callable) {
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
+        callable.delegate = new ComponentsConfiguration(context)
+        callable.call()
     }
 }
diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/Integration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy
similarity index 65%
rename from runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/Integration.groovy
rename to runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy
index f3127f6..5fccd30 100644
--- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/Integration.groovy
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy
@@ -17,6 +17,9 @@
 package org.apache.camel.k.groovy.dsl
 
 import org.apache.camel.CamelContext
+import org.apache.camel.Exchange
+import org.apache.camel.Predicate
+import org.apache.camel.Processor
 import org.apache.camel.builder.RouteBuilder
 import org.apache.camel.k.jvm.RuntimeRegistry
 import org.apache.camel.k.jvm.dsl.Components
@@ -24,25 +27,23 @@ import org.apache.camel.model.RouteDefinition
 import org.apache.camel.model.rest.RestConfigurationDefinition
 import org.apache.camel.model.rest.RestDefinition
 
-class Integration {
+class IntegrationConfiguration {
     private final RuntimeRegistry registry
 
     final CamelContext context
     final Components components
     final RouteBuilder builder
 
-    Integration(RuntimeRegistry registry, RouteBuilder builder) {
+    IntegrationConfiguration(RuntimeRegistry registry, RouteBuilder builder) {
         this.registry = registry
         this.context = builder.getContext()
         this.components = new Components(this.context)
         this.builder = builder
     }
 
-    def component(String name, Closure<?> callable) {
-        def component = context.getComponent(name, true, false)
-
-        callable.resolveStrategy = Closure.DELEGATE_ONLY
-        callable.delegate = new ComponentConfiguration(component)
+    def context(Closure<?> callable) {
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
+        callable.delegate = new ContextConfiguration(context, registry)
         callable.call()
     }
 
@@ -55,7 +56,7 @@ class Integration {
     }
 
     def rest(Closure<?> callable) {
-        callable.resolveStrategy = Closure.DELEGATE_ONLY
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
         callable.delegate = builder.rest()
         callable.call()
     }
@@ -65,20 +66,35 @@ class Integration {
     }
 
     def restConfiguration(Closure<?> callable) {
-        callable.resolveStrategy = Closure.DELEGATE_ONLY
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
         callable.delegate = builder.restConfiguration()
         callable.call()
     }
 
     def restConfiguration(String component, Closure<?> callable) {
-        callable.resolveStrategy = Closure.DELEGATE_ONLY
+        callable.resolveStrategy = Closure.DELEGATE_FIRST
         callable.delegate = builder.restConfiguration(component)
         callable.call()
     }
 
-    def registry(Closure<?> callable) {
-        callable.resolveStrategy = Closure.DELEGATE_ONLY
-        callable.delegate = registry
-        callable.call()
+    def processor(Closure<?> callable) {
+        return new Processor() {
+            @Override
+            void process(Exchange exchange) throws Exception {
+                callable.resolveStrategy = Closure.DELEGATE_FIRST
+                callable.call(exchange)
+            }
+        }
+    }
+
+
+    def predicate(Closure<?> callable) {
+        return new Predicate() {
+            @Override
+            boolean matches(Exchange exchange) throws Exception {
+                callable.resolveStrategy = Closure.DELEGATE_FIRST
+                return callable.call(exchange)
+            }
+        }
     }
 }
diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/extension/LogComponentExtension.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy
similarity index 59%
copy from runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/extension/LogComponentExtension.groovy
copy to runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy
index cb60da0..340c56b 100644
--- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/extension/LogComponentExtension.groovy
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy
@@ -14,21 +14,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.k.groovy.extension
+package org.apache.camel.k.groovy.dsl
 
-import org.apache.camel.Exchange
-import org.apache.camel.component.log.LogComponent
-import org.apache.camel.spi.ExchangeFormatter
+import org.apache.camel.k.jvm.RuntimeRegistry
 
-class LogComponentExtension {
-    static void formatter(LogComponent self, Closure callable) {
-        self.exchangeFormatter = new ExchangeFormatter() {
-            @Override
-            String format(Exchange exchange) {
-                callable.resolveStrategy = Closure.DELEGATE_ONLY
-                callable.delegate = exchange
-                callable.call()
-            }
-        }
+class RegistryConfiguration {
+    private final RuntimeRegistry registry
+
+    RegistryConfiguration(RuntimeRegistry registry) {
+        this.registry = registry
+    }
+
+    def bind(String name, value) {
+        registry.bind(name, value)
     }
 }
diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/extension/LogComponentExtension.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/extension/LogComponentExtension.groovy
index cb60da0..dac629f 100644
--- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/extension/LogComponentExtension.groovy
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/extension/LogComponentExtension.groovy
@@ -25,9 +25,8 @@ class LogComponentExtension {
         self.exchangeFormatter = new ExchangeFormatter() {
             @Override
             String format(Exchange exchange) {
-                callable.resolveStrategy = Closure.DELEGATE_ONLY
-                callable.delegate = exchange
-                callable.call()
+                callable.resolveStrategy = Closure.DELEGATE_FIRST
+                callable.call(exchange)
             }
         }
     }
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 a3ce2ce..3572e95 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
@@ -18,8 +18,6 @@ package org.apache.camel.k.groovy.dsl
 
 import org.apache.camel.component.log.LogComponent
 import org.apache.camel.component.seda.SedaComponent
-import org.apache.camel.impl.DefaultCamelContext
-import org.apache.camel.impl.DefaultExchange
 import org.apache.camel.k.jvm.Runtime
 import org.apache.camel.main.MainListenerSupport
 import org.apache.camel.main.MainSupport
@@ -103,25 +101,4 @@ class IntegrationTest extends Specification {
         consumers == 12
         format != null
     }
-
-
-
-    def "xyz"()  {
-        given:
-        def ctx = new DefaultCamelContext()
-
-        def log = new LogComponent()
-        log.formatter {
-            "body: " + in.body
-        }
-
-        def ex = new DefaultExchange(ctx)
-        ex.in.body = 'hello'
-
-        when:
-        def result = log.exchangeFormatter.format(ex)
-
-        then:
-        result == 'body: hello'
-    }
 }
diff --git a/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/extension/LogExtensionTest.groovy b/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/extension/LogExtensionTest.groovy
index e53433a..68bad92 100644
--- a/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/extension/LogExtensionTest.groovy
+++ b/runtime/groovy/src/test/groovy/org/apache/camel/k/groovy/dsl/extension/LogExtensionTest.groovy
@@ -30,7 +30,7 @@ class LogExtensionTest extends Specification {
         when:
         def log = new LogComponent()
         log.formatter {
-            "body: " + in.body
+            "body: " + it.in.body
         }
 
         def ex = new DefaultExchange(ctx)
diff --git a/runtime/groovy/src/test/resources/routes-with-bindings.groovy b/runtime/groovy/src/test/resources/routes-with-bindings.groovy
index 30df96a..cf589e1 100644
--- a/runtime/groovy/src/test/resources/routes-with-bindings.groovy
+++ b/runtime/groovy/src/test/resources/routes-with-bindings.groovy
@@ -1,7 +1,9 @@
 
-registry {
-    bind 'myEntry1', 'myRegistryEntry1'
-    bind 'myEntry2', 'myRegistryEntry2'
+context {
+    registry {
+        bind 'myEntry1', 'myRegistryEntry1'
+        bind 'myEntry2', 'myRegistryEntry2'
+    }
 }
 
 from('timer:tick')
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 932e9da..b4ed14d 100644
--- a/runtime/groovy/src/test/resources/routes-with-component-configuration.groovy
+++ b/runtime/groovy/src/test/resources/routes-with-component-configuration.groovy
@@ -1,15 +1,19 @@
 
-component('seda') {
-    // set value as method
-    queueSize 1234
+context {
+    components {
+        'seda' {
+            // set value as method
+            queueSize 1234
 
-    // set value as property
-    concurrentConsumers = 12
-}
+            // set value as property
+            concurrentConsumers = 12
+        }
 
-component('log') {
-    formatter {
-        'body ==> ' + in.body
+        'log' {
+            formatter {
+                'body ==> ' + it.in.body
+            }
+        }
     }
 }