Skip to content

Commit

Permalink
Fix apache#136 @ConfigProperty and @Inject do not work in RouteBuilders
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Nov 11, 2019
1 parent 396e008 commit cdbfd19
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 6 deletions.
Expand Up @@ -16,11 +16,15 @@
*/
package org.apache.camel.quarkus.core.deployment;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -46,6 +50,8 @@
import org.apache.camel.quarkus.core.UploadAttacher;
import org.apache.camel.quarkus.support.common.CamelCapabilities;
import org.apache.camel.spi.Registry;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -191,9 +197,17 @@ public static class Main {
public List<CamelRoutesBuilderBuildItem> collectRoutes(
CombinedIndexBuildItem combinedIndex,
CamelMainRecorder recorder,
BeanRegistrationPhaseBuildItem arcBeans,
RecorderContext recorderContext) {

final Set<DotName> arcBeanClasses = arcBeans.getBeanProcessor().getBeanDeployment().getBeans().stream()
.map(BeanInfo::getImplClazz)
.map(ClassInfo::name)
.collect(Collectors.toSet());

return CamelSupport.getRouteBuilderClasses(combinedIndex.getIndex())
.filter(ci -> !arcBeanClasses.contains(ci.name()))
.map(ClassInfo::toString)
.map(recorderContext::<RoutesBuilder> newInstance)
.map(CamelRoutesBuilderBuildItem::new)
.collect(Collectors.toList());
Expand Down
Expand Up @@ -71,7 +71,7 @@ public static Stream<Path> resources(ApplicationArchivesBuildItem archives, Stri
.filter(Files::isRegularFile);
}

public static Stream<String> getRouteBuilderClasses(IndexView view) {
public static Stream<ClassInfo> getRouteBuilderClasses(IndexView view) {
Set<ClassInfo> allKnownImplementors = new HashSet<>();
allKnownImplementors.addAll(
view.getAllKnownImplementors(DotName.createSimple(RoutesBuilder.class.getName())));
Expand All @@ -84,7 +84,8 @@ public static Stream<String> getRouteBuilderClasses(IndexView view) {
.stream()
.filter(CamelSupport::isConcrete)
.filter(CamelSupport::isPublic)
.map(ClassInfo::toString);
// .map(ClassInfo::toString)
;
}

public static Stream<CamelServiceInfo> services(ApplicationArchivesBuildItem applicationArchivesBuildItem) {
Expand Down
Expand Up @@ -185,9 +185,11 @@ void process(
// Register routes as reflection aware as camel-main main use reflection
// to bind beans to the registry
//
CamelSupport.getRouteBuilderClasses(view).forEach(name -> {
reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, name));
});
CamelSupport.getRouteBuilderClasses(view)
.map(ClassInfo::toString)
.forEach(name -> {
reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, name));
});

reflectiveClass.produce(new ReflectiveClassBuildItem(
true,
Expand Down
@@ -0,0 +1,73 @@
/*
* 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.quarkus.component.bean;

import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class AppScopedRouteBuilder extends RouteBuilder {

static final AtomicInteger INSTANCE_COUNTER = new AtomicInteger(0);
static final AtomicInteger CONFIGURE_COUNTER = new AtomicInteger(0);

@Inject
Counter counter;

@ConfigProperty(name = "my.foo.property", defaultValue = "not found")
String myFooValue;

@PostConstruct
public void postConstruct() {
INSTANCE_COUNTER.incrementAndGet();
}

@Override
public void addRoutesToCamelContext(CamelContext context) throws Exception {
CONFIGURE_COUNTER.incrementAndGet();
super.addRoutesToCamelContext(context);
}

@Override
public void configure() {

/*
* counter and config-property should actually work without the bean extension. Doing it here because we have
* quarkus.camel.enable-main=true in the core itest
*/
from("direct:counter")
.id("counter")
.setBody(() -> counter.increment())
.to("log:counter");
from("direct:config-property")
.id("config-property")
.setBody(() -> "myFooValue = " + myFooValue)
.to("log:config-property");
}

public Counter getCounter() {
return counter;
}

}
Expand Up @@ -19,6 +19,7 @@
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -32,11 +33,60 @@ public class CamelResource {
@Inject
ProducerTemplate template;

@Inject
Counter counter;

@Inject
AppScopedRouteBuilder routeBuilder;

@Path("/process-order")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String processOrder(String statement) {
return template.requestBody("direct:process-order", statement, String.class);
}

@Path("/increment")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String increment() {
return template.requestBody("direct:counter", null, String.class);
}

@Path("/counter")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int counter() {
return counter.getValue();
}

@Path("/config-property")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String configProperty() {
return template.requestBody("direct:config-property", null, String.class);
}

@Path("/route-builder-instance-counter")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int routeBuilderInstanceCounter() {
return AppScopedRouteBuilder.INSTANCE_COUNTER.get();
}

@Path("/route-builder-configure-counter")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int routeBuilderConfigureCounter() {
return AppScopedRouteBuilder.CONFIGURE_COUNTER.get();
}

@Path("/route-builder-injected-count")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int routeBuilderInjectedCount() {
return routeBuilder.getCounter().getValue();
}

}
@@ -0,0 +1,34 @@
/*
* 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.quarkus.component.bean;

import java.util.concurrent.atomic.AtomicInteger;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class Counter {
private final AtomicInteger value = new AtomicInteger(0);

public int increment() {
return value.incrementAndGet();
}

public int getValue() {
return value.get();
}
}
Expand Up @@ -27,4 +27,7 @@ quarkus.camel.dump-routes=true
#
# Camel
#
camel.context.name = quarkus-camel-example
camel.context.name = quarkus-camel-example

# A test value
my.foo.property = foo
Expand Up @@ -31,4 +31,39 @@ public void testRoutes() {
.body(equalTo("{success=true, lines=[(id=1,item=nuts), (id=2,item=bolts)]}"));
}

@Test
public void inject() {

/* Ensure that @Inject works */
RestAssured.when().get("/bean/counter").then().body(equalTo("0"));
RestAssured.when().get("/bean/route-builder-injected-count").then().body(equalTo("0"));
RestAssured.when().get("/bean/increment").then().body(equalTo("1"));
RestAssured.when().get("/bean/counter").then().body(equalTo("1"));
RestAssured.when().get("/bean/route-builder-injected-count").then().body(equalTo("1"));
RestAssured.when().get("/bean/increment").then().body(equalTo("2"));
RestAssured.when().get("/bean/counter").then().body(equalTo("2"));
RestAssured.when().get("/bean/route-builder-injected-count").then().body(equalTo("2"));

/* Ensure that @ConfigProperty works */
RestAssured.when()
.get("/bean/config-property")
.then()
.statusCode(200)
.body(equalTo("myFooValue = foo"));

/* Ensure that the bean was not instantiated multiple times */
RestAssured.when()
.get("/bean/route-builder-instance-counter")
.then()
.statusCode(200)
.body(equalTo("1"));

/* Ensure that the RoutesBuilder.configure() was not called multiple times */
RestAssured.when()
.get("/bean/route-builder-configure-counter")
.then()
.statusCode(200)
.body(equalTo("1"));
}

}

0 comments on commit cdbfd19

Please sign in to comment.