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

Create a CompositeClassloader and set is as Camel's ApplicationClassloader #368

Merged
merged 1 commit into from
Jun 20, 2020
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 @@ -22,6 +22,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.camel.k.CompositeClassloader;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.Source;
import org.apache.camel.k.SourceLoader;
Expand All @@ -47,6 +48,14 @@ public Result load(Runtime runtime, Source source) throws Exception {
final Reflect compiled = Reflect.compile(name, content);
final Object instance = compiled.create().get();

// The given source may contains additional nested classes which are unknown to Camel
// as they are associated to the ClassLoader used to compile the source thus we need
// to add it to the ApplicationContextClassLoader.
final ClassLoader loader = runtime.getCamelContext().getApplicationContextClassLoader();
if (loader instanceof CompositeClassloader) {
((CompositeClassloader) loader).addClassLoader(instance.getClass().getClassLoader());
}

return Result.on(instance);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.CompositeClassloader;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.Source;
import org.apache.camel.k.SourceLoader;
Expand Down Expand Up @@ -109,6 +110,20 @@ public void testLoadJavaWithModel() throws Exception {
});
}

@Test
public void testLoadJavaWithNestedType() throws Exception {
TestRuntime runtime = new TestRuntime();
Source source = Sources.fromURI("classpath:MyRoutesWithNestedTypes.java");
SourceLoader loader = RoutesConfigurer.load(runtime, source);

assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(runtime.builders).hasSize(1);
assertThat(runtime.builders).first().isInstanceOf(RouteBuilder.class);

runtime.getCamelContext().addRoutes(runtime.builders.get(0));
runtime.getCamelContext().getApplicationContextClassLoader().loadClass("MyRoutesWithNestedTypes$MyModel");
}

@ParameterizedTest
@MethodSource("parameters")
public void testLoaders(String location, Class<? extends SourceLoader> type) throws Exception {
Expand Down Expand Up @@ -148,6 +163,7 @@ static class TestRuntime implements Runtime {

public TestRuntime() {
this.camelContext = new DefaultCamelContext();
this.camelContext.setApplicationContextClassLoader(new CompositeClassloader());
this.builders = new ArrayList<>();
this.configurations = new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

public class MyRoutesWithNestedTypes extends RouteBuilder {
@Override
public void configure() throws Exception {
}

public static class MyModel {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.camel.RoutesBuilder;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.CompositeClassloader;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.support.PropertiesSupport;
import org.apache.camel.main.BaseMainSupport;
Expand All @@ -52,6 +53,7 @@ public ApplicationRuntime() {

this.context = new DefaultCamelContext();
this.context.setName("camel-k");
this.context.setApplicationContextClassLoader(new CompositeClassloader());

this.main = new MainAdapter();
this.main.configure().setXmlRoutes("false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.camel.k.Constants;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.core.quarkus.RuntimeRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelMainListenerBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelServicePatternBuildItem;
import org.apache.camel.spi.HasId;
Expand Down Expand Up @@ -133,4 +134,10 @@ CamelMainListenerBuildItem registerListener(RuntimeRecorder recorder) {

return new CamelMainListenerBuildItem(recorder.createMainListener(listeners));
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
void customizeContext(RuntimeRecorder recorder, BuildProducer<CamelContextCustomizerBuildItem> customizers) {
customizers.produce(new CamelContextCustomizerBuildItem(recorder.registerCompositeClassLoader()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,27 @@

import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import org.apache.camel.CamelContext;
import org.apache.camel.k.CompositeClassloader;
import org.apache.camel.k.Runtime;
import org.apache.camel.main.MainListener;
import org.apache.camel.quarkus.core.CamelContextCustomizer;

@Recorder
public class RuntimeRecorder {
public RuntimeValue<MainListener> createMainListener(List<Runtime.Listener> listeners) {
return new RuntimeValue<>(new RuntimeListenerAdapter(listeners));
}

public RuntimeValue<CamelContextCustomizer> registerCompositeClassLoader() {
return new RuntimeValue<>(new CamelContextCustomizer() {
@Override
public void customize(CamelContext context) {
final ClassLoader oldLoader = context.getApplicationContextClassLoader();
final ClassLoader newLoader = CompositeClassloader.wrap(oldLoader);

context.setApplicationContextClassLoader(newLoader);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ServiceLoader;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
Expand All @@ -27,11 +28,15 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.camel.CamelContext;
import org.apache.camel.k.Runtime;

@Path("/test")
@ApplicationScoped
public class Application {
@Inject
CamelContext camelContext;

@GET
@Path("/services")
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -46,4 +51,11 @@ public JsonObject getServices() {
.add("services", builder)
.build();
}

@GET
@Path("/application-classloader")
@Produces(MediaType.TEXT_PLAIN)
public String getApplicationClassloader() {
return camelContext.getApplicationContextClassLoader().getClass().getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@

import javax.ws.rs.core.MediaType;

import io.quarkus.test.junit.DisabledOnNativeImage;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import org.apache.camel.k.CompositeClassloader;
import org.apache.camel.k.listener.ContextConfigurer;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;

@QuarkusTest
public class ExtensionTest {
Expand All @@ -45,4 +48,15 @@ public void testServices() {
RoutesConfigurer.class.getName()
);
}

@DisabledOnNativeImage
@Test
public void testClassLoader() {
RestAssured.given()
.accept(MediaType.TEXT_PLAIN)
.get("/test/application-classloader")
.then()
.statusCode(200)
.body(is(CompositeClassloader.class.getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class CompositeClassloader extends ClassLoader {
private final List<ClassLoader> loaders = new CopyOnWriteArrayList<>();

public CompositeClassloader() {
}

public CompositeClassloader(ClassLoader parent) {
super(parent);
}

public void addClassLoader(ClassLoader loader) {
loaders.add(loader);
}

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
for (ClassLoader loader: loaders) {
try {
return loader.loadClass(name);
} catch (ClassNotFoundException e) {
// ignore
}
}

return super.loadClass(name);
}

public static CompositeClassloader wrap(ClassLoader parent) {
return parent != null ? new CompositeClassloader(parent) : new CompositeClassloader();
}
}