You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/03/25 13:10:30 UTC
[camel-k-runtime] branch master updated: Cleanup kotlin loader
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-runtime.git
The following commit(s) were added to refs/heads/master by this push:
new c808cf2 Cleanup kotlin loader
c808cf2 is described below
commit c808cf2027ba7b4b4515a45d344bac6cf6647065
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Wed Mar 25 12:59:05 2020 +0100
Cleanup kotlin loader
---
camel-k-loader-kotlin/pom.xml | 5 --
.../camel/k/loader/kotlin/KotlinSourceLoader.kt | 84 ++++++++++++++--------
.../k/loader/kotlin/dsl/BeansConfiguration.kt | 3 +-
.../k/loader/kotlin/dsl/ComponentsConfiguration.kt | 10 ++-
.../camel/k/loader/kotlin/dsl/IntegrationTest.kt | 17 +++++
.../routes-with-components-configuration-error.kts | 27 +++++++
6 files changed, 106 insertions(+), 40 deletions(-)
diff --git a/camel-k-loader-kotlin/pom.xml b/camel-k-loader-kotlin/pom.xml
index 3dde92a..2063906 100644
--- a/camel-k-loader-kotlin/pom.xml
+++ b/camel-k-loader-kotlin/pom.xml
@@ -52,11 +52,6 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
- <artifactId>kotlin-reflect</artifactId>
- <version>${kotlin.version}</version>
- </dependency>
- <dependency>
- <groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-script-util</artifactId>
<version>${kotlin.version}</version>
</dependency>
diff --git a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/KotlinSourceLoader.kt b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/KotlinSourceLoader.kt
index f245876..afe6d42 100644
--- a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/KotlinSourceLoader.kt
+++ b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/KotlinSourceLoader.kt
@@ -16,6 +16,7 @@
*/
package org.apache.camel.k.loader.kotlin
+import org.apache.camel.RuntimeCamelException
import org.apache.camel.builder.endpoint.EndpointRouteBuilder
import org.apache.camel.k.Runtime
import org.apache.camel.k.Source
@@ -23,10 +24,13 @@ import org.apache.camel.k.SourceLoader
import org.apache.camel.k.loader.kotlin.dsl.IntegrationConfiguration
import org.slf4j.Logger
import org.slf4j.LoggerFactory
+import java.io.InputStream
import java.io.InputStreamReader
+import kotlin.script.experimental.api.ResultValue
import kotlin.script.experimental.api.ScriptDiagnostic
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
import kotlin.script.experimental.api.constructorArgs
+import kotlin.script.experimental.api.valueOrNull
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
@@ -35,7 +39,51 @@ import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromT
class KotlinSourceLoader : SourceLoader {
companion object {
- val LOGGER : Logger = LoggerFactory.getLogger(KotlinSourceLoader::class.java)
+ private val LOGGER : Logger = LoggerFactory.getLogger(KotlinSourceLoader::class.java)
+
+ @JvmStatic
+ fun load(inputStream: InputStream): EndpointRouteBuilder {
+ return object : EndpointRouteBuilder() {
+ @Throws(Exception::class)
+ override fun configure() {
+ load(inputStream, this)
+ }
+ }
+ }
+
+ @JvmStatic
+ fun load(inputStream: InputStream, builder: EndpointRouteBuilder) {
+ val compiler = JvmScriptCompiler()
+ val evaluator = BasicJvmScriptEvaluator()
+ val host = BasicJvmScriptingHost(compiler = compiler, evaluator = evaluator)
+ val config = createJvmCompilationConfigurationFromTemplate<IntegrationConfiguration>()
+
+ val result = host.eval(
+ InputStreamReader(inputStream).readText().toScriptSource(),
+ config,
+ ScriptEvaluationConfiguration {
+ //
+ // Arguments used to initialize the script base class
+ //
+ constructorArgs(builder)
+ }
+ )
+
+ // ensure that evaluation errors propagation
+ when(val rv = result.valueOrNull()?.returnValue) {
+ is ResultValue.Error -> throw RuntimeCamelException(rv.error)
+ }
+
+ for (report in result.reports) {
+ when (report.severity) {
+ ScriptDiagnostic.Severity.FATAL -> LOGGER.error("{}", report.message, report.exception)
+ ScriptDiagnostic.Severity.ERROR -> LOGGER.error("{}", report.message, report.exception)
+ ScriptDiagnostic.Severity.WARNING -> LOGGER.warn("{}", report.message, report.exception)
+ ScriptDiagnostic.Severity.INFO -> LOGGER.info("{}", report.message)
+ ScriptDiagnostic.Severity.DEBUG -> LOGGER.debug("{}", report.message)
+ }
+ }
+ }
}
override fun getSupportedLanguages(): List<String> {
@@ -44,39 +92,13 @@ class KotlinSourceLoader : SourceLoader {
@Throws(Exception::class)
override fun load(runtime: Runtime, source: Source): SourceLoader.Result {
- var builder = object : EndpointRouteBuilder() {
+ return SourceLoader.Result.on(object : EndpointRouteBuilder() {
@Throws(Exception::class)
override fun configure() {
- val builder = this
- val compiler = JvmScriptCompiler()
- val evaluator = BasicJvmScriptEvaluator()
- val host = BasicJvmScriptingHost(compiler = compiler, evaluator = evaluator)
- val config = createJvmCompilationConfigurationFromTemplate<IntegrationConfiguration>()
- val camelContext = runtime.camelContext
-
- source.resolveAsInputStream(camelContext).use { `is` ->
- val result = host.eval(
- InputStreamReader(`is`).readText().toScriptSource(),
- config,
- ScriptEvaluationConfiguration {
- //
- // Arguments used to initialize the script base class
- //
- constructorArgs(builder)
- }
- )
-
- for (report in result.reports) {
- when (report.severity) {
- ScriptDiagnostic.Severity.ERROR -> LOGGER.error("{}", report.message, report.exception)
- ScriptDiagnostic.Severity.WARNING -> LOGGER.warn("{}", report.message, report.exception)
- else -> LOGGER.info("{}", report.message)
- }
- }
+ source.resolveAsInputStream(runtime.camelContext).use {
+ load(it, this)
}
}
- }
-
- return SourceLoader.Result.on(builder)
+ })
}
}
diff --git a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
index 9baf638..d6c23fd 100644
--- a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
+++ b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/BeansConfiguration.kt
@@ -21,13 +21,12 @@ import org.apache.camel.Exchange
import org.apache.camel.Predicate
import org.apache.camel.Processor
import org.apache.camel.builder.endpoint.EndpointBuilderFactory
-import kotlin.reflect.full.createInstance
class BeansConfiguration(
val context: CamelContext) : EndpointBuilderFactory {
inline fun <reified T : Any> bean(name: String, block: T.() -> Unit) {
- var bean = T::class.createInstance()
+ val bean = context.injector.newInstance(T::class.java)
bean.block()
context.registry.bind(name, T::class.java, bean)
diff --git a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/ComponentsConfiguration.kt b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/ComponentsConfiguration.kt
index 2820839..5aa5b97 100644
--- a/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/ComponentsConfiguration.kt
+++ b/camel-k-loader-kotlin/src/main/kotlin/org/apache/camel/k/loader/kotlin/dsl/ComponentsConfiguration.kt
@@ -18,14 +18,20 @@ package org.apache.camel.k.loader.kotlin.dsl
import org.apache.camel.CamelContext
import org.apache.camel.Component
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
class ComponentsConfiguration(val context: CamelContext) {
+ companion object {
+ val LOGGER: Logger = LoggerFactory.getLogger(ComponentsConfiguration::class.java)
+ }
+
inline fun <reified T : Component> component(name: String, block: T.() -> Unit) : T {
var target = context.getComponent(name, true, false)
var bind = false
- if (target != null && target !is T) {
- throw IllegalArgumentException("Type mismatch, expected: " + T::class.java + ", got: " + target.javaClass)
+ if (target != null && !T::class.java.isInstance(target)) {
+ throw IllegalArgumentException("Type mismatch, expected: ${T::class.java}, got: ${target.javaClass}")
}
// if the component is not found, let's create a new one. This is
diff --git a/camel-k-loader-kotlin/src/test/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationTest.kt b/camel-k-loader-kotlin/src/test/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationTest.kt
index 1115ea6..71c8299 100644
--- a/camel-k-loader-kotlin/src/test/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationTest.kt
+++ b/camel-k-loader-kotlin/src/test/kotlin/org/apache/camel/k/loader/kotlin/dsl/IntegrationTest.kt
@@ -18,6 +18,7 @@ package org.apache.camel.k.loader.kotlin.dsl
import org.apache.camel.Predicate
import org.apache.camel.Processor
+import org.apache.camel.RuntimeCamelException
import org.apache.camel.component.jackson.JacksonDataFormat
import org.apache.camel.component.log.LogComponent
import org.apache.camel.component.seda.SedaComponent
@@ -31,7 +32,12 @@ import org.apache.camel.model.rest.PostVerbDefinition
import org.apache.camel.processor.FatalFallbackErrorHandler
import org.apache.camel.support.DefaultHeaderFilterStrategy
import org.assertj.core.api.Assertions.assertThat
+import org.assertj.core.api.Assertions.assertThatExceptionOfType
+import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertThrows
+import java.lang.IllegalArgumentException
+import java.lang.RuntimeException
import javax.sql.DataSource
class IntegrationTest {
@@ -105,6 +111,17 @@ class IntegrationTest {
}
@Test
+ fun `load integration with components configuration error`() {
+ val context = DefaultCamelContext()
+ val runtime = Runtime.on(context)
+
+ assertThatExceptionOfType(RuntimeCamelException::class.java)
+ .isThrownBy { forRoutes("classpath:routes-with-components-configuration-error.kts").accept(Runtime.Phase.ConfigureRoutes, runtime) }
+ .withCauseInstanceOf(IllegalArgumentException::class.java)
+ .withMessageContaining("Type mismatch, expected: class org.apache.camel.component.log.LogComponent, got: class org.apache.camel.component.seda.SedaComponent");
+ }
+
+ @Test
fun `load integration with languages configuration`() {
val context = DefaultCamelContext()
val runtime = Runtime.on(context)
diff --git a/camel-k-loader-kotlin/src/test/resources/routes-with-components-configuration-error.kts b/camel-k-loader-kotlin/src/test/resources/routes-with-components-configuration-error.kts
new file mode 100644
index 0000000..3c209b5
--- /dev/null
+++ b/camel-k-loader-kotlin/src/test/resources/routes-with-components-configuration-error.kts
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+import org.apache.camel.Exchange
+
+camel {
+ components {
+ component<org.apache.camel.component.log.LogComponent>("seda") {
+ setExchangeFormatter {
+ e: Exchange -> "" + e.getIn().body
+ }
+ }
+ }
+}
\ No newline at end of file