Skip to content

Commit

Permalink
Allow endpoint DSL in Camel k #122
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Aug 2, 2019
1 parent 1dbfc6e commit 9558a71
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 27 deletions.
5 changes: 5 additions & 0 deletions camel-k-loader-groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<artifactId>camel-log</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-endpointdsl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-groovy</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.apache.camel.k.loader.groovy

import org.apache.camel.CamelContext
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.builder.endpoint.EndpointRouteBuilder
import org.apache.camel.k.RoutesLoader
import org.apache.camel.k.Source
import org.apache.camel.k.loader.groovy.dsl.IntegrationConfiguration
Expand All @@ -34,7 +35,7 @@ class GroovyRoutesLoader implements RoutesLoader {

@Override
RouteBuilder load(CamelContext camelContext, Source source) throws Exception {
return new RouteBuilder() {
return new EndpointRouteBuilder() {
@Override
void configure() throws Exception {
def ic = new ImportCustomizer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@ 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.RouteBuilder
import org.apache.camel.model.*
import org.apache.camel.builder.BuilderSupport
import org.apache.camel.builder.EndpointConsumerBuilder
import org.apache.camel.builder.endpoint.EndpointBuilderFactory
import org.apache.camel.builder.endpoint.EndpointRouteBuilder
import org.apache.camel.model.InterceptDefinition
import org.apache.camel.model.InterceptFromDefinition
import org.apache.camel.model.InterceptSendToEndpointDefinition
import org.apache.camel.model.OnCompletionDefinition
import org.apache.camel.model.OnExceptionDefinition
import org.apache.camel.model.RouteDefinition
import org.apache.camel.spi.Registry

class IntegrationConfiguration extends org.apache.camel.builder.BuilderSupport {
class IntegrationConfiguration extends BuilderSupport implements EndpointBuilderFactory {
final Registry registry
final Components components
final RouteBuilder builder
final EndpointRouteBuilder builder

IntegrationConfiguration(RouteBuilder builder) {
IntegrationConfiguration(EndpointRouteBuilder builder) {
super(builder.context)

this.registry = this.context.registry
Expand Down Expand Up @@ -74,7 +82,11 @@ class IntegrationConfiguration extends org.apache.camel.builder.BuilderSupport {
}
}

ProcessorDefinition from(String endpoint) {
RouteDefinition from(String endpoint) {
return builder.from(endpoint)
}

RouteDefinition from(EndpointConsumerBuilder endpoint) {
return builder.from(endpoint)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package org.apache.camel.k.loader.groovy
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.Source
import org.apache.camel.k.support.RuntimeSupport
import org.apache.camel.model.FromDefinition
import org.apache.camel.model.ToDefinition
import spock.lang.Specification

class LoaderTest extends Specification {

def "load route from classpath"() {
def "load routes"() {
given:
def context = new DefaultCamelContext()
def source = Source.create("classpath:routes.groovy")
Expand All @@ -46,4 +47,32 @@ class LoaderTest extends Specification {
routes[0].outputs[0] instanceof ToDefinition
routes[0].input.endpointUri == 'timer:tick'
}

def "load routes with endpoint dsl"() {
given:
def context = new DefaultCamelContext()
def source = Source.create("classpath:routes-with-endpoint-dsl.groovy")

when:
def loader = RuntimeSupport.loaderFor(context, source)
def builder = loader.load(context, source)

then:
loader instanceof GroovyRoutesLoader
builder != null

builder.setContext(context)
builder.configure()

def routes = builder.routeCollection.routes

routes.size() == 1

with(routes[0].input, FromDefinition) {
it.endpointUri == 'timer:tick?period=1s'
}
with(routes[0].outputs[0], ToDefinition) {
it.endpointUri == 'log:info'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/
def f = timer('tick').period('1s')
def t = log('info')

from(f)
.to(t)
5 changes: 5 additions & 0 deletions camel-k-loader-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>camel-core-engine</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-endpointdsl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>joor-java-8</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ static Stream<Arguments> parameters() {
Arguments.arguments("classpath:" + MyRoutes.class.getName() + ".class", JavaClassRoutesLoader.class),
Arguments.arguments("classpath:MyRoutes.java", JavaSourceRoutesLoader.class),
Arguments.arguments("classpath:MyRoutesWithNameOverride.java?name=MyRoutes.java", JavaSourceRoutesLoader.class),
Arguments.arguments("classpath:MyRoutesWithPackage.java", JavaSourceRoutesLoader.class)
Arguments.arguments("classpath:MyRoutesWithPackage.java", JavaSourceRoutesLoader.class),
Arguments.arguments("classpath:MyRoutesWithEndpointDsl.java", JavaSourceRoutesLoader.class)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/
public class MyRoutesWithEndpointDsl extends org.apache.camel.builder.endpoint.EndpointRouteBuilder {
@Override
public void configure() throws Exception {
from(timer("tick"))
.to(log("info"));
}
}
9 changes: 9 additions & 0 deletions camel-k-loader-js/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>camel-core-engine</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-endpointdsl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
Expand Down Expand Up @@ -127,6 +132,10 @@
<version>${log4j2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-endpointdsl</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
import org.apache.camel.k.RoutesLoader;
import org.apache.camel.k.Source;
import org.apache.camel.k.loader.js.dsl.IntegrationConfiguration;
Expand All @@ -42,7 +43,7 @@ public List<String> getSupportedLanguages() {

@Override
public RouteBuilder load(CamelContext camelContext, Source source) throws Exception {
return new RouteBuilder() {
return new EndpointRouteBuilder() {
@Override
public void configure() throws Exception {
final Context context = Context.newBuilder("js").allowAllAccess(true).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,38 @@
*/
package org.apache.camel.k.loader.js.dsl;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.builder.BuilderSupport;
import org.apache.camel.builder.EndpointConsumerBuilder;
import org.apache.camel.builder.endpoint.EndpointBuilderFactory;
import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
import org.apache.camel.model.InterceptDefinition;
import org.apache.camel.model.InterceptFromDefinition;
import org.apache.camel.model.InterceptSendToEndpointDefinition;
import org.apache.camel.model.OnCompletionDefinition;
import org.apache.camel.model.OnExceptionDefinition;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.rest.RestConfigurationDefinition;
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.spi.Registry;

public class IntegrationConfiguration extends org.apache.camel.builder.BuilderSupport {
public class IntegrationConfiguration extends BuilderSupport implements EndpointBuilderFactory {
public final Registry registry;
public final Components components;
public final RouteBuilder builder;
public final EndpointRouteBuilder builder;

public IntegrationConfiguration(RouteBuilder builder) {
public IntegrationConfiguration(EndpointRouteBuilder builder) {
super(builder.getContext());

this.registry = builder.getContext().getRegistry();
this.components = new Components(builder.getContext());
this.builder = builder;
}

public ProcessorDefinition from(String endpoint) {
public RouteDefinition from(String endpoint) {
return builder.from(endpoint);
}

public RouteDefinition from(EndpointConsumerBuilder endpoint) {
return builder.from(endpoint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void testLoaders(String location, Class<? extends RoutesLoader> type) thr
static Stream<Arguments> parameters() {
return Stream.of(
Arguments.arguments("classpath:routes.js", JavaScriptRoutesLoader.class),
Arguments.arguments("classpath:routes-with-endpoint-dsl.js", JavaScriptRoutesLoader.class),
Arguments.arguments("classpath:routes-compressed.js.gz.b64?language=js&compression=true", JavaScriptRoutesLoader.class),
Arguments.arguments("classpath:routes.mytype?language=js", JavaScriptRoutesLoader.class)
);
Expand Down
22 changes: 22 additions & 0 deletions camel-k-loader-js/src/test/resources/routes-with-endpoint-dsl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/

f = timer("tick");
t = log("info");

from(f)
.to(t);
7 changes: 6 additions & 1 deletion camel-k-loader-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
<artifactId>camel-endpointdsl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -102,6 +102,11 @@
<artifactId>camel-properties</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
<scope>test</scope>
</dependency>

<!-- ******************************* -->
<!-- test deps :: misc -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
package org.apache.camel.k.loader.kotlin

import org.apache.camel.CamelContext
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.builder.endpoint.EndpointRouteBuilder
import org.apache.camel.k.RoutesLoader
import org.apache.camel.k.Source
import org.apache.camel.k.loader.kotlin.dsl.IntegrationConfiguration
import org.apache.camel.k.support.URIResolver
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.InputStreamReader
import kotlin.script.experimental.api.*
import kotlin.script.experimental.api.ScriptDiagnostic
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
import kotlin.script.experimental.api.constructorArgs
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator
import kotlin.script.experimental.jvm.dependenciesFromClassloader
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.JvmScriptCompiler
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
Expand All @@ -44,8 +44,8 @@ class KotlinRoutesLoader : RoutesLoader {
}

@Throws(Exception::class)
override fun load(camelContext: CamelContext, source: Source): RouteBuilder? {
return object : RouteBuilder() {
override fun load(camelContext: CamelContext, source: Source): EndpointRouteBuilder? {
return object : EndpointRouteBuilder() {
@Throws(Exception::class)
override fun configure() {
val builder = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ 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.RouteBuilder
import org.apache.camel.builder.BuilderSupport
import org.apache.camel.builder.EndpointConsumerBuilder
import org.apache.camel.builder.endpoint.EndpointBuilderFactory
import org.apache.camel.builder.endpoint.EndpointRouteBuilder
import org.apache.camel.k.loader.kotlin.KotlinCompilationConfiguration
import org.apache.camel.model.*
import org.apache.camel.spi.Registry
Expand All @@ -28,7 +31,7 @@ import kotlin.script.experimental.annotations.KotlinScript
@KotlinScript(fileExtension = "kts", compilationConfiguration = KotlinCompilationConfiguration::class)
abstract class IntegrationConfiguration(
private val registry : Registry,
private val builder : RouteBuilder) : org.apache.camel.builder.BuilderSupport(builder.context) {
private val builder : EndpointRouteBuilder) : BuilderSupport(builder.context), EndpointBuilderFactory {

fun rest(block: RestConfiguration.() -> Unit) {
RestConfiguration(builder).block()
Expand All @@ -53,6 +56,9 @@ abstract class IntegrationConfiguration(
return builder.from(uri)
}

fun from(endpoint: EndpointConsumerBuilder): RouteDefinition {
return builder.from(endpoint)
}

fun intercept() : InterceptDefinition {
return builder.intercept()
Expand Down
Loading

0 comments on commit 9558a71

Please sign in to comment.