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/03 10:44:44 UTC
[GitHub] nicolaferraro closed pull request #150: runtime: improve groovy dsl
nicolaferraro closed pull request #150: runtime: improve groovy dsl
URL: https://github.com/apache/camel-k/pull/150
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 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/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy
new file mode 100644
index 0000000..405fe8c
--- /dev/null
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy
@@ -0,0 +1,42 @@
+/**
+ * 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 org.apache.camel.k.jvm.RuntimeRegistry
+
+class ContextConfiguration {
+ private final CamelContext context
+ private final RuntimeRegistry registry
+
+ ContextConfiguration(CamelContext context, RuntimeRegistry registry) {
+ this.context = context
+ this.registry = registry
+ }
+
+ def registry(Closure<?> callable) {
+ callable.resolveStrategy = Closure.DELEGATE_FIRST
+ callable.delegate = new RegistryConfiguration(registry)
+ callable.call()
+ }
+
+ 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/dsl/RegistryConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy
new file mode 100644
index 0000000..340c56b
--- /dev/null
+++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy
@@ -0,0 +1,31 @@
+/**
+ * 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.k.jvm.RuntimeRegistry
+
+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
+ }
+ }
}
}
----------------------------------------------------------------
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