Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve kotlin support #92

Merged
merged 2 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,26 @@ import org.slf4j.LoggerFactory
import java.io.InputStreamReader
import kotlin.script.experimental.api.*
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.jvm.BasicJvmScriptEvaluator
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.JvmScriptCompiler
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate

class KamelKtsConfigurator : ScriptCompilationConfiguration(
{
jvm {
//
// The Kotlin script compiler does not inherit
// the classpath by default
//
dependenciesFromClassloader(wholeClasspath = true)
}
ide {
acceptedLocations(ScriptAcceptedLocation.Everywhere)
}
})

class KotlinRoutesLoader : RoutesLoader {
companion object {
Expand All @@ -51,20 +66,12 @@ class KotlinRoutesLoader : RoutesLoader {
val compiler = JvmScriptCompiler()
val evaluator = BasicJvmScriptEvaluator()
val host = BasicJvmScriptingHost(compiler = compiler, evaluator = evaluator)
val config = createJvmCompilationConfigurationFromTemplate<IntegrationConfiguration>()

URIResolver.resolve(context, source).use { `is` ->
val result = host.eval(
InputStreamReader(`is`).readText().toScriptSource(),
ScriptCompilationConfiguration {
baseClass(IntegrationConfiguration::class)
jvm {
//
// The Kotlin script compiler does not inherit
// the classpath by default
//
dependenciesFromClassloader(wholeClasspath = true)
}
},
config,
ScriptEvaluationConfiguration {
//
// Arguments used to initialize the script base class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ 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.RouteDefinition
import org.apache.camel.k.kotlin.KamelKtsConfigurator
import org.apache.camel.model.*
import org.apache.camel.spi.Registry
import kotlin.script.experimental.annotations.KotlinScript

@KotlinScript(fileExtension = "kts", compilationConfiguration = KamelKtsConfigurator::class)
abstract class IntegrationConfiguration(
private val registry : Registry,
private val builder : RouteBuilder) {
private val builder : RouteBuilder) : org.apache.camel.builder.BuilderSupport(builder.context) {

fun rest(block: RestConfiguration.() -> Unit) {
val delegate = RestConfiguration(builder)
Expand All @@ -34,21 +37,46 @@ abstract class IntegrationConfiguration(

fun context(block: ContextConfiguration.() -> Unit) {
val delegate = ContextConfiguration(
context = builder.context,
context = context,
registry = registry
)

delegate.block()
}

fun from(uri: String): RouteDefinition {
return builder.from(uri)
}

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)
}


fun intercept() : InterceptDefinition {
return builder.intercept()
}

fun onException(exception: Class<out Throwable>) : OnExceptionDefinition {
return builder.onException(exception)
}

fun onCompletion() : OnCompletionDefinition {
return builder.onCompletion()
}

fun interceptFrom() : InterceptFromDefinition {
return builder.interceptFrom()
}

fun interceptFrom(uri: String) : InterceptFromDefinition{
return builder.interceptFrom(uri)
}

fun interceptSendToEndpoint(uri: String) : InterceptSendToEndpointDefinition {
return builder.interceptSendToEndpoint(uri)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.apache.camel.k.Runtime
import org.apache.camel.k.jvm.ApplicationRuntime
import org.apache.camel.k.listener.RoutesConfigurer
import org.apache.camel.model.ModelCamelContext
import org.apache.camel.processor.FatalFallbackErrorHandler
import org.apache.camel.spi.ExchangeFormatter
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -88,4 +89,25 @@ class IntegrationTest {
assertThat(mySedaConsumers.get()).isEqualTo(21)
assertThat(format.get()).isNotNull
}

@Test
fun `load integration with error handler`() {
var onExceptions = mutableListOf<Processor>()

var runtime = ApplicationRuntime()
runtime.addListener(RoutesConfigurer.forRoutes("classpath:routes-with-error-handler.kts"))
runtime.addListener(Runtime.Phase.Started) {
assertThat(it.camelContext.routes).hasSize(1)
assertThat(it.camelContext.routes[0].routeContext.getOnException("my-on-exception")).isNotNull

onExceptions.add(it.camelContext.routes[0].routeContext.getOnException("my-on-exception"))

runtime.stop()
}

runtime.run()

assertThat(onExceptions).hasSize(1)
assertThat(onExceptions).first().isInstanceOf(FatalFallbackErrorHandler::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

onException(IllegalArgumentException::class.java)
.id("my-on-exception")
.to("log:exception")

from("timer:tick")
.process().message {
m -> m.headers["MyHeader"] = "MyHeaderValue"
}
.to("log:info")
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rest {
}



from("timer:tick")
.process().message {
m -> m.headers["MyHeader"] = "MyHeaderValue"
Expand Down