Skip to content

Commit

Permalink
Allow to use any class as configuration source
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Nov 25, 2019
1 parent 755df81 commit 89d6bcf
Show file tree
Hide file tree
Showing 50 changed files with 755 additions and 516 deletions.
11 changes: 11 additions & 0 deletions camel-k-loader-groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-apt</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-annotations</artifactId>
<optional>true</optional>
</dependency>

<!-- ******************************* -->
<!-- test deps :: camel -->
<!-- ******************************* -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,23 @@
*/
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.Runtime
import org.apache.camel.k.Source
import org.apache.camel.k.SourceLoader
import org.apache.camel.k.loader.groovy.dsl.IntegrationConfiguration
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.ImportCustomizer

class GroovyRoutesLoader implements RoutesLoader {
class GroovySourceLoader implements SourceLoader {
@Override
List<String> getSupportedLanguages() {
return Collections.singletonList("groovy")
return Collections.singletonList('groovy')
}

@Override
RouteBuilder load(CamelContext camelContext, Source source) throws Exception {
return new EndpointRouteBuilder() {
void load(Runtime runtime, Source source) throws Exception {
def builder = new EndpointRouteBuilder() {
@Override
void configure() throws Exception {
def ic = new ImportCustomizer()
Expand All @@ -58,5 +57,7 @@ class GroovyRoutesLoader implements RoutesLoader {
}
}
}

runtime.addRoutes(builder)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#

class=org.apache.camel.k.loader.groovy.GroovyRoutesLoader
class=org.apache.camel.k.loader.groovy.GroovySourceLoader
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
*/
package org.apache.camel.k.loader.groovy

import org.apache.camel.CamelContext
import org.apache.camel.RoutesBuilder
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.Runtime
import org.apache.camel.k.Sources
import org.apache.camel.k.support.RuntimeSupport
import org.apache.camel.model.FromDefinition
Expand All @@ -27,52 +31,76 @@ class LoaderTest extends Specification {

def "load routes"() {
given:
def context = new DefaultCamelContext()
def runtime = new TestRuntime()
def source = Sources.fromURI("classpath:routes.groovy")

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

then:
loader instanceof GroovyRoutesLoader
builder != null
loader instanceof GroovySourceLoader
runtime.builders.size() == 1
runtime.builders[0] instanceof RouteBuilder

builder.setContext(context)
builder.configure()
with(runtime.builders[0], RouteBuilder) {
it.setContext(runtime.camelContext)
it.configure()

def routes = builder.routeCollection.routes
def routes = it.routeCollection.routes

routes.size() == 1
routes[0].outputs[0] instanceof ToDefinition
routes[0].input.endpointUri == 'timer:tick'
routes.size() == 1
routes[0].outputs[0] instanceof ToDefinition
routes[0].input.endpointUri == 'timer:tick'
}
}

def "load routes with endpoint dsl"() {
given:
def context = new DefaultCamelContext()
def runtime = new TestRuntime()
def source = Sources.fromURI("classpath:routes-with-endpoint-dsl.groovy")

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

then:
loader instanceof GroovyRoutesLoader
builder != null

builder.setContext(context)
builder.configure()
loader instanceof GroovySourceLoader
runtime.builders.size() == 1

def routes = builder.routeCollection.routes
with(runtime.builders[0], RouteBuilder) {
it.setContext(runtime.camelContext)
it.configure()

routes.size() == 1
def routes = it.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'
with(routes[0].input, FromDefinition) {
it.endpointUri == 'timer:tick?period=1s'
}
with(routes[0].outputs[0], ToDefinition) {
it.endpointUri == 'log:info'
}
}
}

static class TestRuntime implements Runtime {
private final CamelContext camelContext
private final List<RoutesBuilder> builders

TestRuntime() {
this.camelContext = new DefaultCamelContext()
this.builders = new ArrayList<>()
}

@Override
CamelContext getCamelContext() {
return this.camelContext
}

@Override
void addRoutes(RoutesBuilder builder) {
this.builders.add(builder)
}
}
}
11 changes: 11 additions & 0 deletions camel-k-loader-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
<version>${joor.version}</version>
</dependency>

<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-apt</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-annotations</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,39 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.k.RoutesLoader;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.Source;
import org.apache.camel.k.SourceLoader;
import org.apache.camel.k.annotation.Loader;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.joor.Reflect;

public class JavaSourceRoutesLoader implements RoutesLoader {
@Loader("java")
public class JavaSourceLoader implements SourceLoader {
@Override
public List<String> getSupportedLanguages() {
return Collections.singletonList("java");
}

@Override
public RouteBuilder load(CamelContext camelContext, Source source) throws Exception {
try (InputStream is = source.resolveAsInputStream(camelContext)) {
public void load(Runtime runtime, Source source) throws Exception {
try (InputStream is = source.resolveAsInputStream(runtime.getCamelContext())) {
final String content = IOUtils.toString(is, StandardCharsets.UTF_8);
final String name = determineQualifiedName(source, content);
final Reflect compiled = Reflect.compile(name, content);
final Object instance = compiled.create().get();

RouteBuilder rb = compiled.create().get();
return rb;
if (instance instanceof RoutesBuilder) {
runtime.addRoutes((RoutesBuilder)instance);
} else {
runtime.addConfiguration(instance);
}
}
}

private static String determineQualifiedName(Source source, String content) throws Exception {
private static String determineQualifiedName(Source source, String content) {
String name = source.getName();
name = StringUtils.removeEnd(name, ".java");

Expand Down

This file was deleted.

Loading

0 comments on commit 89d6bcf

Please sign in to comment.