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 2019/08/02 10:34:27 UTC

[camel-k-runtime] branch master updated: chore(dsl): improbe bean 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-runtime.git


The following commit(s) were added to refs/heads/master by this push:
     new 945db35  chore(dsl): improbe bean dsl
945db35 is described below

commit 945db356eb193c74f37e69cd296d567a66dbc79b
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Fri Aug 2 12:34:15 2019 +0200

    chore(dsl): improbe bean dsl
---
 .../k/loader/groovy/dsl/BeansConfiguration.groovy  |  9 +++++---
 .../groovy/dsl/IntegrationConfiguration.groovy     | 26 ++--------------------
 .../camel/k/loader/groovy/dsl/Support.groovy       | 25 ++++++++++++---------
 .../k/loader/groovy/dsl/IntegrationTest.groovy     |  5 +++++
 .../src/test/resources/routes-with-beans.groovy    |  8 +++++++
 .../k/loader/kotlin/dsl/BeansConfiguration.kt      | 14 +++++++++++-
 .../loader/kotlin/dsl/IntegrationConfiguration.kt  | 12 +---------
 .../dsl/{BeansConfiguration.kt => Support.kt}      | 18 +++++++--------
 .../camel/k/loader/kotlin/dsl/IntegrationTest.kt   |  3 +++
 .../src/test/resources/routes-with-beans.kts       |  8 +++++++
 10 files changed, 68 insertions(+), 60 deletions(-)

diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/BeansConfiguration.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/BeansConfiguration.groovy
index e209971..0e47184 100644
--- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/BeansConfiguration.groovy
+++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/BeansConfiguration.groovy
@@ -17,14 +17,19 @@
 package org.apache.camel.k.loader.groovy.dsl
 
 import org.apache.camel.CamelContext
+import org.apache.camel.builder.endpoint.EndpointBuilderFactory
 
-class BeansConfiguration {
+class BeansConfiguration implements Support, EndpointBuilderFactory {
     private final CamelContext context
 
     BeansConfiguration(CamelContext context) {
         this.context = context
     }
 
+    def propertyMissing(String name, value) {
+        context.registry.bind(name, value)
+    }
+
     def methodMissing(String name, arguments) {
         final Object[] args = arguments as Object[]
 
@@ -43,8 +48,6 @@ class BeansConfiguration {
             clos.resolveStrategy = Closure.DELEGATE_ONLY
 
             context.registry.bind(name, clos.call())
-        } else {
-            throw new MissingMethodException(name, this, args)
         }
     }
 }
diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationConfiguration.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationConfiguration.groovy
index 4eff02d..d52e44d 100644
--- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationConfiguration.groovy
+++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationConfiguration.groovy
@@ -16,9 +16,7 @@
  */
 package org.apache.camel.k.loader.groovy.dsl
 
-import org.apache.camel.Exchange
-import org.apache.camel.Predicate
-import org.apache.camel.Processor
+
 import org.apache.camel.builder.BuilderSupport
 import org.apache.camel.builder.EndpointConsumerBuilder
 import org.apache.camel.builder.endpoint.EndpointBuilderFactory
@@ -31,7 +29,7 @@ import org.apache.camel.model.OnExceptionDefinition
 import org.apache.camel.model.RouteDefinition
 import org.apache.camel.spi.Registry
 
-class IntegrationConfiguration extends BuilderSupport implements EndpointBuilderFactory {
+class IntegrationConfiguration extends BuilderSupport implements Support, EndpointBuilderFactory {
     final Registry registry
     final Components components
     final EndpointRouteBuilder builder
@@ -62,26 +60,6 @@ class IntegrationConfiguration extends BuilderSupport implements EndpointBuilder
         callable.call()
     }
 
-    def processor(@DelegatesTo(Exchange) Closure<?> callable) {
-        return new Processor() {
-            @Override
-            void process(Exchange exchange) throws Exception {
-                callable.resolveStrategy = Closure.DELEGATE_FIRST
-                callable.call(exchange)
-            }
-        }
-    }
-
-    def predicate(@DelegatesTo(Exchange) Closure<?> callable) {
-        return new Predicate() {
-            @Override
-            boolean matches(Exchange exchange) throws Exception {
-                callable.resolveStrategy = Closure.DELEGATE_FIRST
-                return callable.call(exchange)
-            }
-        }
-    }
-
     RouteDefinition from(String endpoint) {
         return builder.from(endpoint)
     }
diff --git a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/Support.groovy
similarity index 60%
copy from camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
copy to camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/Support.groovy
index ffedf1d..5a6cce7 100644
--- a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
+++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/Support.groovy
@@ -1,4 +1,4 @@
-/**
+/*
  * 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.
@@ -14,19 +14,22 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.k.loader.kotlin.dsl
+package org.apache.camel.k.loader.groovy.dsl
 
-import org.apache.camel.CamelContext
+import org.apache.camel.Exchange
 
-class BeansConfiguration(val context: CamelContext) {
-    inline fun <reified T : Any> bean(name: String, block: T.() -> Unit) {
-        var bean = T::class.java.newInstance()
-        bean.block()
-
-        context.registry.bind(name, T::class.java, bean)
+trait Support {
+    def processor(@DelegatesTo(Exchange) Closure<?> callable) {
+        return {
+            callable.resolveStrategy = Closure.DELEGATE_FIRST
+            callable.call(it)
+        } as org.apache.camel.Processor
     }
 
-    inline fun bean(name: String, crossinline function: () -> Any ) {
-        context.registry.bind(name, function())
+    def predicate(@DelegatesTo(Exchange) Closure<?> callable) {
+        return {
+            callable.resolveStrategy = Closure.DELEGATE_FIRST
+            return callable.call(it)
+        } as org.apache.camel.Predicate
     }
 }
\ No newline at end of file
diff --git a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationTest.groovy b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationTest.groovy
index b58d1ec..695c952 100644
--- a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationTest.groovy
+++ b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationTest.groovy
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.k.loader.groovy.dsl
 
+import org.apache.camel.Predicate
 import org.apache.camel.Processor
 import org.apache.camel.component.log.LogComponent
 import org.apache.camel.component.seda.SedaComponent
@@ -62,6 +63,9 @@ class IntegrationTest extends Specification {
             runtime.camelContext.registry.lookupByName('dataSource') instanceof DataSource
             runtime.camelContext.registry.findByType(HeaderFilterStrategy).size() == 1
             runtime.camelContext.registry.lookupByName('filterStrategy') instanceof DefaultHeaderFilterStrategy
+
+            runtime.camelContext.registry.lookupByName('myProcessor') instanceof Processor
+            runtime.camelContext.registry.lookupByName('myPredicate') instanceof Predicate
     }
 
     def "load integration with bindings"()  {
@@ -140,3 +144,4 @@ class IntegrationTest extends Specification {
             ch.output instanceof SendProcessor
     }
 }
+
diff --git a/camel-k-loader-groovy/src/test/resources/routes-with-beans.groovy b/camel-k-loader-groovy/src/test/resources/routes-with-beans.groovy
index 608a19d..fe92d1a 100644
--- a/camel-k-loader-groovy/src/test/resources/routes-with-beans.groovy
+++ b/camel-k-loader-groovy/src/test/resources/routes-with-beans.groovy
@@ -24,4 +24,12 @@ beans {
     filterStrategy {
         new org.apache.camel.support.DefaultHeaderFilterStrategy()
     }
+
+    myProcessor = processor {
+        it.in.body = 'value'
+    }
+
+    myPredicate = predicate {
+        false
+    }
 }
\ No newline at end of file
diff --git a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
index ffedf1d..044280f 100644
--- a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
+++ b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
@@ -17,8 +17,12 @@
 package org.apache.camel.k.loader.kotlin.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.endpoint.EndpointBuilderFactory
 
-class BeansConfiguration(val context: CamelContext) {
+class BeansConfiguration(val context: CamelContext) : EndpointBuilderFactory {
     inline fun <reified T : Any> bean(name: String, block: T.() -> Unit) {
         var bean = T::class.java.newInstance()
         bean.block()
@@ -29,4 +33,12 @@ class BeansConfiguration(val context: CamelContext) {
     inline fun bean(name: String, crossinline function: () -> Any ) {
         context.registry.bind(name, function())
     }
+
+    fun processor(name: String, fn: (Exchange) -> Unit) {
+        context.registry.bind(name, Processor { exchange -> fn(exchange) } )
+    }
+
+    fun predicate(name: String, fn: (Exchange) -> Boolean) {
+        context.registry.bind(name, Predicate { exchange -> fn(exchange) } )
+    }
 }
\ No newline at end of file
diff --git a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationConfiguration.kt b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationConfiguration.kt
index a6b6b8e..6eb100f 100644
--- a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationConfiguration.kt
+++ b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationConfiguration.kt
@@ -16,9 +16,6 @@
  */
 package org.apache.camel.k.loader.kotlin.dsl
 
-import org.apache.camel.Exchange
-import org.apache.camel.Predicate
-import org.apache.camel.Processor
 import org.apache.camel.builder.BuilderSupport
 import org.apache.camel.builder.EndpointConsumerBuilder
 import org.apache.camel.builder.endpoint.EndpointBuilderFactory
@@ -31,7 +28,7 @@ import kotlin.script.experimental.annotations.KotlinScript
 @KotlinScript(fileExtension = "kts", compilationConfiguration = KotlinCompilationConfiguration::class)
 abstract class IntegrationConfiguration(
         private val registry : Registry,
-        private val builder : EndpointRouteBuilder) : BuilderSupport(builder.context), EndpointBuilderFactory {
+        private val builder : EndpointRouteBuilder) : BuilderSupport(builder.context), Support, EndpointBuilderFactory {
 
     fun rest(block: RestConfiguration.() -> Unit) {
         RestConfiguration(builder).block()
@@ -45,13 +42,6 @@ abstract class IntegrationConfiguration(
         ContextConfiguration(context = context, registry = registry).block()
     }
 
-    fun processor(fn: (Exchange) -> Unit) : Processor {
-        return Processor { exchange -> fn(exchange) }
-    }
-    fun predicate(fn: (Exchange) -> Boolean) : Predicate {
-        return Predicate { exchange -> fn(exchange) }
-    }
-
     fun from(uri: String): RouteDefinition {
         return builder.from(uri)
     }
diff --git a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/Support.kt
similarity index 67%
copy from camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
copy to camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/Support.kt
index ffedf1d..261709d 100644
--- a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
+++ b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/Support.kt
@@ -16,17 +16,15 @@
  */
 package org.apache.camel.k.loader.kotlin.dsl
 
-import org.apache.camel.CamelContext
+import org.apache.camel.Exchange
+import org.apache.camel.Predicate
+import org.apache.camel.Processor
 
-class BeansConfiguration(val context: CamelContext) {
-    inline fun <reified T : Any> bean(name: String, block: T.() -> Unit) {
-        var bean = T::class.java.newInstance()
-        bean.block()
-
-        context.registry.bind(name, T::class.java, bean)
+interface Support {
+    fun processor(fn: (Exchange) -> Unit) : Processor {
+        return Processor { exchange -> fn(exchange) }
     }
-
-    inline fun bean(name: String, crossinline function: () -> Any ) {
-        context.registry.bind(name, function())
+    fun predicate(fn: (Exchange) -> Boolean) : Predicate {
+        return Predicate { exchange -> fn(exchange) }
     }
 }
\ No newline at end of file
diff --git a/camel-k-loader-kotlin/src/test/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationTest.kt b/camel-k-loader-kotlin/src/test/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationTest.kt
index 9ad06cf..7336c7d 100644
--- a/camel-k-loader-kotlin/src/test/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationTest.kt
+++ b/camel-k-loader-kotlin/src/test/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationTest.kt
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.k.loader.kotlin.dsl
 
+import org.apache.camel.Predicate
 import org.apache.camel.Processor
 import org.apache.camel.component.log.LogComponent
 import org.apache.camel.component.seda.SedaComponent
@@ -59,6 +60,8 @@ class IntegrationTest {
         assertThat(runtime.camelContext.registry.lookupByName("dataSource")).isInstanceOf(DataSource::class.java)
         assertThat(runtime.camelContext.registry.findByType(DefaultHeaderFilterStrategy::class.java)).hasSize(1)
         assertThat(runtime.camelContext.registry.lookupByName("filterStrategy")).isInstanceOf(DefaultHeaderFilterStrategy::class.java)
+        assertThat(runtime.camelContext.registry.lookupByName("myProcessor")).isInstanceOf(Processor::class.java)
+        assertThat(runtime.camelContext.registry.lookupByName("myPredicate")).isInstanceOf(Predicate::class.java)
     }
 
     @Test
diff --git a/camel-k-loader-kotlin/src/test/resources/routes-with-beans.kts b/camel-k-loader-kotlin/src/test/resources/routes-with-beans.kts
index 8e07874..2356dc9 100644
--- a/camel-k-loader-kotlin/src/test/resources/routes-with-beans.kts
+++ b/camel-k-loader-kotlin/src/test/resources/routes-with-beans.kts
@@ -9,4 +9,12 @@ beans {
     bean("filterStrategy") {
         org.apache.camel.support.DefaultHeaderFilterStrategy()
     }
+
+    processor("myProcessor") {
+        it.getIn().body = "Hello"
+    }
+
+    predicate("myPredicate") {
+        false
+    }
 }