From commits-return-65675-archive-asf-public=cust-asf.ponee.io@camel.apache.org Wed Oct 3 23:33:58 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 3DB9718065B for ; Wed, 3 Oct 2018 23:33:58 +0200 (CEST) Received: (qmail 70134 invoked by uid 500); 3 Oct 2018 21:33:57 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 70125 invoked by uid 99); 3 Oct 2018 21:33:57 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Oct 2018 21:33:57 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id AF9FB8584C; Wed, 3 Oct 2018 21:33:56 +0000 (UTC) Date: Wed, 03 Oct 2018 21:33:56 +0000 To: "commits@camel.apache.org" Subject: [camel-k] branch master updated: runtime: improve rest dsl for groovy and kotlin runtimes MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <153860243665.17803.290182073067664309@gitbox.apache.org> From: lburgazzoli@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: camel-k X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Oldrev: 2fe4d278fb133d2f98cb549aab61add5adb22c45 X-Git-Newrev: c7a376000b94867b11f7d8cb8ef3ccd126ac8dba X-Git-Rev: c7a376000b94867b11f7d8cb8ef3ccd126ac8dba X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated 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 c7a3760 runtime: improve rest dsl for groovy and kotlin runtimes c7a3760 is described below commit c7a376000b94867b11f7d8cb8ef3ccd126ac8dba Author: lburgazzoli AuthorDate: Wed Oct 3 15:48:40 2018 +0200 runtime: improve rest dsl for groovy and kotlin runtimes --- runtime/examples/camel-caffeine.groovy | 68 +++++++++++----------- .../k/groovy/dsl/IntegrationConfiguration.groovy | 29 +-------- .../camel/k/groovy/dsl/RestConfiguration.groovy | 45 ++++++++++++++ .../camel/k/groovy/dsl/IntegrationTest.groovy | 2 + .../src/test/resources/routes-with-rest.groovy | 20 ++++--- .../apache/camel/k/kotlin/KotlinRoutesLoader.kt | 14 +---- .../apache/camel/k/kotlin/dsl/RestConfiguration.kt | 39 +++++++++++++ .../apache/camel/k/kotlin/dsl/IntegrationTest.kt | 2 + .../kotlin/src/test/resources/routes-with-rest.kts | 19 +++--- 9 files changed, 153 insertions(+), 85 deletions(-) diff --git a/runtime/examples/camel-caffeine.groovy b/runtime/examples/camel-caffeine.groovy index d1aada7..84da8fc 100644 --- a/runtime/examples/camel-caffeine.groovy +++ b/runtime/examples/camel-caffeine.groovy @@ -6,38 +6,40 @@ import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; - Cache cache = Caffeine.newBuilder().recordStats().build(); +Cache cache = Caffeine.newBuilder().recordStats().build(); - registry { - bind 'caffeineCache', cache - } +context { + registry { + bind 'caffeineCache', cache + } +} - from('timer:tick') - .setBody().constant('Hello') - .process { - it.in.headers['CamelCaffeineAction'] = 'PUT' - it.in.headers['CamelCaffeineKey'] = 1 - } - .toF('caffeine-cache://%s?cache=#caffeineCache', 'test') - .log('Result of Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} is: ${body}') - .setBody().constant(null) - .process { - it.in.headers['CamelCaffeineAction'] = 'GET' - it.in.headers['CamelCaffeineKey'] = 1 - } - .toF('caffeine-cache://%s?cache=#caffeineCache', 'test') - .log('Result of Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} is: ${body}') - .setBody().constant(null) - .process { - it.in.headers['CamelCaffeineAction'] = 'INVALIDATE' - it.in.headers['CamelCaffeineKey'] = 1 - } - .toF('caffeine-cache://%s?cache=#caffeineCache', 'test') - .log('Invalidating entry with key ${header.CamelCaffeineKey}') - .setBody().constant(null) - .process { - it.in.headers['CamelCaffeineAction'] = 'GET' - it.in.headers['CamelCaffeineKey'] = 1 - } - .toF('caffeine-cache://%s?cache=#caffeineCache', 'test') - .log('The Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} has result? ${header.CamelCaffeineActionHasResult}'); +from('timer:tick') + .setBody().constant('Hello') + .process { + it.in.headers['CamelCaffeineAction'] = 'PUT' + it.in.headers['CamelCaffeineKey'] = 1 + } + .toF('caffeine-cache://%s?cache=#caffeineCache', 'test') + .log('Result of Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} is: ${body}') + .setBody().constant(null) + .process { + it.in.headers['CamelCaffeineAction'] = 'GET' + it.in.headers['CamelCaffeineKey'] = 1 + } + .toF('caffeine-cache://%s?cache=#caffeineCache', 'test') + .log('Result of Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} is: ${body}') + .setBody().constant(null) + .process { + it.in.headers['CamelCaffeineAction'] = 'INVALIDATE' + it.in.headers['CamelCaffeineKey'] = 1 + } + .toF('caffeine-cache://%s?cache=#caffeineCache', 'test') + .log('Invalidating entry with key ${header.CamelCaffeineKey}') + .setBody().constant(null) + .process { + it.in.headers['CamelCaffeineAction'] = 'GET' + it.in.headers['CamelCaffeineKey'] = 1 + } + .toF('caffeine-cache://%s?cache=#caffeineCache', 'test') + .log('The Action ${header.CamelCaffeineAction} with key ${header.CamelCaffeineKey} has result? ${header.CamelCaffeineActionHasResult}'); diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy index 5fccd30..5b07bd5 100644 --- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy +++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy @@ -24,8 +24,6 @@ import org.apache.camel.builder.RouteBuilder import org.apache.camel.k.jvm.RuntimeRegistry import org.apache.camel.k.jvm.dsl.Components import org.apache.camel.model.RouteDefinition -import org.apache.camel.model.rest.RestConfigurationDefinition -import org.apache.camel.model.rest.RestDefinition class IntegrationConfiguration { private final RuntimeRegistry registry @@ -47,34 +45,14 @@ class IntegrationConfiguration { callable.call() } - RouteDefinition from(String endpoint) { - return builder.from(endpoint) - } - - RestDefinition rest() { - return builder.rest() - } - def rest(Closure callable) { callable.resolveStrategy = Closure.DELEGATE_FIRST - callable.delegate = builder.rest() + callable.delegate = new RestConfiguration(builder) callable.call() } - RestConfigurationDefinition restConfiguration() { - return builder.restConfiguration() - } - - def restConfiguration(Closure callable) { - callable.resolveStrategy = Closure.DELEGATE_FIRST - callable.delegate = builder.restConfiguration() - callable.call() - } - - def restConfiguration(String component, Closure callable) { - callable.resolveStrategy = Closure.DELEGATE_FIRST - callable.delegate = builder.restConfiguration(component) - callable.call() + RouteDefinition from(String endpoint) { + return builder.from(endpoint) } def processor(Closure callable) { @@ -87,7 +65,6 @@ class IntegrationConfiguration { } } - def predicate(Closure callable) { return new Predicate() { @Override diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RestConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RestConfiguration.groovy new file mode 100644 index 0000000..d0b2fcf --- /dev/null +++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RestConfiguration.groovy @@ -0,0 +1,45 @@ +/** + * 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.builder.RouteBuilder + +class RestConfiguration { + private final RouteBuilder builder + + RestConfiguration(RouteBuilder builder) { + this.builder = builder + } + + def configuration(Closure callable) { + callable.resolveStrategy = Closure.DELEGATE_FIRST + callable.delegate = builder.restConfiguration() + callable.call() + } + + def configuration(String component, Closure callable) { + callable.resolveStrategy = Closure.DELEGATE_FIRST + callable.delegate = builder.restConfiguration(component) + callable.call() + } + + def path(String path, Closure callable) { + callable.resolveStrategy = Closure.DELEGATE_FIRST + callable.delegate = builder.rest(path) + callable.call() + } +} 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 3572e95..4e2fdfe 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 @@ -46,6 +46,8 @@ class IntegrationTest extends Specification { runtime.camelContext.restConfiguration.port == 9192 runtime.camelContext.getRestConfiguration('undertow', false).host == 'my-undertow-host' runtime.camelContext.getRestConfiguration('undertow', false).port == 9193 + runtime.camelContext.restDefinitions.size() == 1 + runtime.camelContext.restDefinitions[0].path == '/my/path' } def "load integration with bindings"() { diff --git a/runtime/groovy/src/test/resources/routes-with-rest.groovy b/runtime/groovy/src/test/resources/routes-with-rest.groovy index f0187e4..6df9367 100644 --- a/runtime/groovy/src/test/resources/routes-with-rest.groovy +++ b/runtime/groovy/src/test/resources/routes-with-rest.groovy @@ -1,12 +1,18 @@ -restConfiguration { - host 'my-host' - port '9192' -} +rest { + configuration { + host 'my-host' + port '9192' + } + + configuration('undertow') { + host 'my-undertow-host' + port '9193' + } + + path('/my/path') { -restConfiguration('undertow') { - host 'my-undertow-host' - port '9193' + } } from('timer:tick') diff --git a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt index 804664b..62e8369 100644 --- a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt +++ b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt @@ -49,18 +49,8 @@ class KotlinRoutesLoader : RoutesLoader { val pre = """ val builder = bindings["builder"] as org.apache.camel.builder.RouteBuilder - fun rest(block: org.apache.camel.model.rest.RestDefinition.() -> Unit) { - val delegate = builder.rest() - delegate.block() - } - - fun restConfiguration(block: org.apache.camel.model.rest.RestConfigurationDefinition.() -> Unit) { - val delegate = builder.restConfiguration() - delegate.block() - } - - fun restConfiguration(component: String, block: org.apache.camel.model.rest.RestConfigurationDefinition.() -> Unit) { - val delegate = builder.restConfiguration(component) + fun rest(block: org.apache.camel.k.kotlin.dsl.RestConfiguration.() -> Unit) { + val delegate = org.apache.camel.k.kotlin.dsl.RestConfiguration(builder) delegate.block() } diff --git a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RestConfiguration.kt b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RestConfiguration.kt new file mode 100644 index 0000000..9f9f490 --- /dev/null +++ b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RestConfiguration.kt @@ -0,0 +1,39 @@ +/** + * 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.kotlin.dsl + +import org.apache.camel.builder.RouteBuilder +import org.apache.camel.model.rest.RestConfigurationDefinition +import org.apache.camel.model.rest.RestDefinition + +class RestConfiguration(val builder: RouteBuilder) { + + fun configuration(block: RestConfigurationDefinition.() -> Unit) { + val delegate = builder.restConfiguration() + delegate.block() + } + + fun configuration(component: String, block: RestConfigurationDefinition.() -> Unit) { + val delegate = builder.restConfiguration(component) + delegate.block() + } + + fun path(path: String, block: RestDefinition.() -> Unit) { + val delegate = builder.rest(path) + delegate.block() + } +} \ No newline at end of file diff --git a/runtime/kotlin/src/test/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationTest.kt b/runtime/kotlin/src/test/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationTest.kt index 4e56d34..d35bc61 100644 --- a/runtime/kotlin/src/test/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationTest.kt +++ b/runtime/kotlin/src/test/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationTest.kt @@ -29,6 +29,8 @@ class IntegrationTest { assertThat(runtime.camelContext.restConfiguration.port).isEqualTo(9192) assertThat(runtime.camelContext.getRestConfiguration("undertow", false).host).isEqualTo("my-undertow-host") assertThat(runtime.camelContext.getRestConfiguration("undertow", false).port).isEqualTo(9193) + assertThat(runtime.camelContext.restDefinitions.size).isEqualTo(1) + assertThat(runtime.camelContext.restDefinitions[0].path).isEqualTo("/my/path") } @Test diff --git a/runtime/kotlin/src/test/resources/routes-with-rest.kts b/runtime/kotlin/src/test/resources/routes-with-rest.kts index 29a748f..c94ccb5 100644 --- a/runtime/kotlin/src/test/resources/routes-with-rest.kts +++ b/runtime/kotlin/src/test/resources/routes-with-rest.kts @@ -1,12 +1,17 @@ -restConfiguration { - host = "my-host" - port = "9192" -} +rest { + configuration { + host = "my-host" + port = "9192" + } -restConfiguration("undertow") { - host = "my-undertow-host" - port = "9193" + configuration("undertow") { + host = "my-undertow-host" + port = "9193" + } + + path("/my/path") { + } }