nicolaferraro closed pull request #298: chore(runtime): try to lookup routes
loaders from registry first, then from resources/services
URL: https://github.com/apache/camel-k/pull/298
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/camel-k-runtime-core/src/main/java/org/apache/camel/k/Language.java
b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Language.java
index c338bd60..1f7f9a35 100644
---
a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Language.java
+++
b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Language.java
@@ -85,7 +85,7 @@ public static Language fromLanguageName(String name) {
return Unknown;
}
- public static Language fromResource(String resource) {
+ public static Language fromLocation(String resource) {
for (Language language: values()) {
String path = StringUtils.substringAfter(resource, ":");
diff --git
a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeTrait.java
b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeTrait.java
index 275f53a3..2bc78689 100644
---
a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeTrait.java
+++
b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeTrait.java
@@ -1,7 +1,3 @@
-package org.apache.camel.k;
-
-import org.apache.camel.CamelContext;
-
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -18,6 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+package org.apache.camel.k;
+
+import org.apache.camel.CamelContext;
@FunctionalInterface
public interface RuntimeTrait {
diff --git
a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Source.java
b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Source.java
index 0726b072..12b2d1bf 100644
--- a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Source.java
+++ b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Source.java
@@ -70,7 +70,7 @@ public static Source create(String uri) throws Exception {
Language language = ObjectHelper.isNotEmpty(languageName)
? Language.fromLanguageName(languageName)
- : Language.fromResource(location);
+ : Language.fromLocation(location);
return new Source(location, language, compression);
}
diff --git
a/runtime/camel-k-runtime-groovy/src/test/groovy/org/apache/camel/k/groovy/LoaderTest.groovy
b/runtime/camel-k-runtime-groovy/src/test/groovy/org/apache/camel/k/groovy/LoaderTest.groovy
index 0bc194ff..87ce8590 100644
---
a/runtime/camel-k-runtime-groovy/src/test/groovy/org/apache/camel/k/groovy/LoaderTest.groovy
+++
b/runtime/camel-k-runtime-groovy/src/test/groovy/org/apache/camel/k/groovy/LoaderTest.groovy
@@ -17,8 +17,8 @@
package org.apache.camel.k.groovy
import org.apache.camel.impl.DefaultCamelContext
+import org.apache.camel.k.jvm.Runtime
import org.apache.camel.k.jvm.RuntimeSupport
-import org.apache.camel.k.jvm.SimpleRuntimeRegistry
import org.apache.camel.k.Source
import org.apache.camel.model.ToDefinition
import spock.lang.Specification
@@ -31,7 +31,7 @@ class LoaderTest extends Specification {
when:
def loader = RuntimeSupport.loaderFor(new DefaultCamelContext(),
source)
- def builder = loader.load(new SimpleRuntimeRegistry(), source)
+ def builder = loader.load(new Runtime.Registry(), source)
then:
loader instanceof GroovyRoutesLoader
diff --git
a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Runtime.java
b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Runtime.java
index 3923e85a..4d5adb5f 100644
---
a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Runtime.java
+++
b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Runtime.java
@@ -16,12 +16,16 @@
*/
package org.apache.camel.k.jvm;
+import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
+import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+import java.util.stream.Collectors;
import org.apache.camel.CamelContext;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.properties.PropertiesComponent;
@@ -38,10 +42,11 @@
private static final Logger LOGGER =
LoggerFactory.getLogger(Runtime.class);
private final ConcurrentMap<String, CamelContext> contextMap;
- private final RuntimeRegistry registry = new SimpleRuntimeRegistry();
+ private final RuntimeRegistry registry;
public Runtime() {
this.contextMap = new ConcurrentHashMap<>();
+ this.registry = new Registry();
}
public void load(String[] routes) throws Exception {
@@ -121,4 +126,66 @@ protected void doStop() throws Exception {
getCamelContexts().get(0).stop();
}
}
+
+ // ********************************
+ //
+ // Registry
+ //
+ // ********************************
+
+ public static final class Registry implements RuntimeRegistry {
+ private final ConcurrentMap<String, Object> registry;
+
+ public Registry() {
+ this.registry = new ConcurrentHashMap<>();
+ }
+
+ public void bind(String name, Object bean) {
+ this.registry.put(name, bean);
+ }
+
+ @Override
+ public Object lookupByName(String name) {
+ return registry.get(name);
+ }
+
+ @Override
+ public <T> T lookupByNameAndType(String name, Class<T> type) {
+ final Object answer = lookupByName(name);
+
+ if (answer != null) {
+ try {
+ return type.cast(answer);
+ } catch (Throwable t) {
+ throw new NoSuchBeanException(
+ name,
+ "Found bean: " + name + " in RuntimeRegistry: " + this
+ " of type: " + answer.getClass().getName() + " expected type was: " + type,
+ t
+ );
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public <T> Map<String, T> findByTypeWithName(Class<T> type) {
+ final Map<String, T> result = new HashMap<>();
+
+ registry.entrySet().stream()
+ .filter(entry -> type.isInstance(entry.getValue()))
+ .forEach(entry -> result.put(entry.getKey(),
type.cast(entry.getValue())));
+
+ return result;
+ }
+
+ @Override
+ public <T> Set<T> findByType(Class<T> type) {
+ return registry.values().stream()
+ .filter(type::isInstance)
+ .map(type::cast)
+ .collect(Collectors.toSet());
+ }
+ }
+
}
diff --git
a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java
b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java
index b3192b66..700d70d8 100644
---
a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java
+++
b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java
@@ -184,6 +184,13 @@ public static void bindProperties(Properties properties,
Object target, String p
}
public static RoutesLoader loaderFor(CamelContext context, Source source) {
+ return context.getRegistry().findByType(RoutesLoader.class).stream()
+ .filter(rl ->
rl.getSupportedLanguages().contains(source.getLanguage()))
+ .findFirst()
+ .orElseGet(() -> lookupLoaderFromResource(context, source));
+ }
+
+ public static RoutesLoader lookupLoaderFromResource(CamelContext context,
Source source) {
final FactoryFinder finder;
final RoutesLoader loader;
diff --git
a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/SimpleRuntimeRegistry.java
b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/SimpleRuntimeRegistry.java
deleted file mode 100644
index c62175fe..00000000
---
a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/SimpleRuntimeRegistry.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/**
- * 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.jvm;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.stream.Collectors;
-
-import org.apache.camel.NoSuchBeanException;
-import org.apache.camel.k.RuntimeRegistry;
-
-public class SimpleRuntimeRegistry implements RuntimeRegistry {
- private final ConcurrentMap<String, Object> registry;
-
- public SimpleRuntimeRegistry() {
- this.registry = new ConcurrentHashMap<>();
- }
-
- public void bind(String name, Object bean) {
- this.registry.put(name, bean);
- }
-
- @Override
- public Object lookupByName(String name) {
- return registry.get(name);
- }
-
- @Override
- public <T> T lookupByNameAndType(String name, Class<T> type) {
- final Object answer = lookupByName(name);
-
- if (answer != null) {
- try {
- return type.cast(answer);
- } catch (Throwable t) {
- throw new NoSuchBeanException(
- name,
- "Found bean: " + name + " in RuntimeRegistry: " + this + "
of type: " + answer.getClass().getName() + " expected type was: " + type,
- t
- );
- }
- }
-
- return null;
- }
-
- @Override
- public <T> Map<String, T> findByTypeWithName(Class<T> type) {
- final Map<String, T> result = new HashMap<>();
-
- registry.entrySet().stream()
- .filter(entry -> type.isInstance(entry.getValue()))
- .forEach(entry -> result.put(entry.getKey(),
type.cast(entry.getValue())));
-
- return result;
- }
-
- @Override
- public <T> Set<T> findByType(Class<T> type) {
- return registry.values().stream()
- .filter(type::isInstance)
- .map(type::cast)
- .collect(Collectors.toSet());
- }
-}
diff --git
a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
index cc3b7c3b..88c29f99 100644
---
a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
+++
b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
@@ -21,6 +21,7 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.RoutesLoader;
+import org.apache.camel.k.RuntimeRegistry;
import org.apache.camel.k.Source;
import org.apache.camel.k.jvm.loader.JavaClassLoader;
import org.apache.camel.k.jvm.loader.JavaScriptLoader;
@@ -37,11 +38,24 @@
public class RoutesLoadersTest {
+ @Test
+ public void testLoaderFromRegistry() throws Exception {
+ RoutesLoader myLoader = new JavaClassLoader();
+ RuntimeRegistry registry = new Runtime.Registry();
+ registry.bind("my-loader", myLoader);
+
+ Source source = Source.create("classpath:" + MyRoutes.class.getName()
+ ".class");
+ RoutesLoader loader = RuntimeSupport.loaderFor(new
DefaultCamelContext(registry), source);
+
+ assertThat(loader).isInstanceOf(JavaClassLoader.class);
+ assertThat(loader).isSameAs(myLoader);
+ }
+
@Test
public void testLoadClass() throws Exception {
Source source = Source.create("classpath:" + MyRoutes.class.getName()
+ ".class");
RoutesLoader loader = RuntimeSupport.loaderFor(new
DefaultCamelContext(), source);
- RouteBuilder builder = loader.load(new SimpleRuntimeRegistry(),
source);
+ RouteBuilder builder = loader.load(new Runtime.Registry(), source);
assertThat(loader).isInstanceOf(JavaClassLoader.class);
assertThat(builder).isNotNull();
@@ -58,7 +72,7 @@ public void testLoadClass() throws Exception {
public void testLoadJava() throws Exception {
Source source = Source.create("classpath:MyRoutes.java");
RoutesLoader loader = RuntimeSupport.loaderFor(new
DefaultCamelContext(), source);
- RouteBuilder builder = loader.load(new SimpleRuntimeRegistry(),
source);
+ RouteBuilder builder = loader.load(new Runtime.Registry(), source);
assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(builder).isNotNull();
@@ -75,7 +89,7 @@ public void testLoadJava() throws Exception {
public void testLoadJavaWithNestedClass() throws Exception {
Source source =
Source.create("classpath:MyRoutesWithNestedClass.java");
RoutesLoader loader = RuntimeSupport.loaderFor(new
DefaultCamelContext(), source);
- RouteBuilder builder = loader.load(new SimpleRuntimeRegistry(),
source);
+ RouteBuilder builder = loader.load(new Runtime.Registry(), source);
assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(builder).isNotNull();
@@ -94,7 +108,7 @@ public void testLoadJavaWithNestedClass() throws Exception {
public void testLoadJavaScript() throws Exception {
Source source = Source.create("classpath:routes.js");
RoutesLoader loader = RuntimeSupport.loaderFor(new
DefaultCamelContext(), source);
- RouteBuilder builder = loader.load(new SimpleRuntimeRegistry(),
source);
+ RouteBuilder builder = loader.load(new Runtime.Registry(), source);
assertThat(loader).isInstanceOf(JavaScriptLoader.class);
assertThat(builder).isNotNull();
@@ -111,7 +125,7 @@ public void testLoadJavaScript() throws Exception {
public void testLoadCompressedRoute() throws Exception {
Source source =
Source.create("classpath:routes-compressed.js.gz.b64?language=js&compression=true");
RoutesLoader loader = RuntimeSupport.loaderFor(new
DefaultCamelContext(), source);
- RouteBuilder builder = loader.load(new SimpleRuntimeRegistry(),
source);
+ RouteBuilder builder = loader.load(new Runtime.Registry(), source);
assertThat(loader).isInstanceOf(JavaScriptLoader.class);
assertThat(builder).isNotNull();
@@ -128,7 +142,7 @@ public void testLoadCompressedRoute() throws Exception {
public void testLoadJavaScriptWithCustomExtension() throws Exception {
Source source = Source.create("classpath:routes.mytype?language=js");
RoutesLoader loader = RuntimeSupport.loaderFor(new
DefaultCamelContext(), source);
- RouteBuilder builder = loader.load(new SimpleRuntimeRegistry(),
source);
+ RouteBuilder builder = loader.load(new Runtime.Registry(), source);
assertThat(loader).isInstanceOf(JavaScriptLoader.class);
assertThat(builder).isNotNull();
@@ -145,7 +159,7 @@ public void testLoadJavaScriptWithCustomExtension() throws
Exception {
public void testLoadXml() throws Exception {
Source source = Source.create("classpath:routes.xml");
RoutesLoader loader = RuntimeSupport.loaderFor(new
DefaultCamelContext(), source);
- RouteBuilder builder = loader.load(new SimpleRuntimeRegistry(),
source);
+ RouteBuilder builder = loader.load(new Runtime.Registry(), source);
assertThat(loader).isInstanceOf(XmlLoader.class);
assertThat(builder).isNotNull();
diff --git
a/runtime/camel-k-runtime-kotlin/src/test/kotlin/org/apache/camel/k/kotlin/LoaderTest.kt
b/runtime/camel-k-runtime-kotlin/src/test/kotlin/org/apache/camel/k/kotlin/LoaderTest.kt
index c0f86c6c..be4dced0 100644
---
a/runtime/camel-k-runtime-kotlin/src/test/kotlin/org/apache/camel/k/kotlin/LoaderTest.kt
+++
b/runtime/camel-k-runtime-kotlin/src/test/kotlin/org/apache/camel/k/kotlin/LoaderTest.kt
@@ -18,8 +18,8 @@ package org.apache.camel.k.kotlin
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.Source
+import org.apache.camel.k.jvm.Runtime
import org.apache.camel.k.jvm.RuntimeSupport
-import org.apache.camel.k.jvm.SimpleRuntimeRegistry
import org.apache.camel.model.ProcessDefinition
import org.apache.camel.model.ToDefinition
import org.assertj.core.api.Assertions.assertThat
@@ -31,10 +31,10 @@ class LoaderTest {
fun `load route from classpath`() {
var source = Source.create("classpath:routes.kts")
val loader = RuntimeSupport.loaderFor(DefaultCamelContext(), source)
- val builder = loader.load(SimpleRuntimeRegistry(), source)
+ val builder = loader.load(Runtime.Registry(), source)
assertThat(loader).isInstanceOf(KotlinRoutesLoader::class.java)
- assertThat(builder).isNotNull()
+ assertThat(builder).isNotNull
builder.configure()
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services