Simplify service binding with Camel*BeanBuildItem for trivial services
diff --git a/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/BuildProcessor.java b/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/BuildProcessor.java
index 8fd37d3..17161e8 100644
--- a/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/BuildProcessor.java
+++ b/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/BuildProcessor.java
@@ -128,6 +128,7 @@
/*
* Discover {@link TypeConverterLoader}.
*/
+ @SuppressWarnings("unchecked")
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
CamelTypeConverterRegistryBuildItem typeConverterRegistry(
@@ -158,7 +159,7 @@
.map(String::trim)
.filter(l -> !l.isEmpty())
.filter(l -> !l.startsWith("#"))
- .map(l -> recorderContext.<TypeConverterLoader> newInstance(l))
+ .map(l -> (Class<? extends TypeConverterLoader>) recorderContext.classProxy(l))
.forEach(loader -> recorder.addTypeConverterLoader(typeConverterRegistry, loader));
} catch (IOException e) {
throw new RuntimeException("Error discovering TypeConverterLoader", e);
@@ -217,11 +218,20 @@
.filter(item -> !containerBeans.getBeans().contains(item))
.forEach(item -> {
LOGGER.debug("Binding bean with name: {}, type {}", item.getName(), item.getType());
- recorder.bind(
- registry,
- item.getName(),
- recorderContext.classProxy(item.getType()),
- item.getValue());
+ if (item.getValue() != null) {
+ recorder.bind(
+ registry,
+ item.getName(),
+ recorderContext.classProxy(item.getType()),
+ item.getValue());
+ } else {
+ // the instance of the service will be instantiated by the recorder, this avoid
+ // creating a recorder for trivial services.
+ recorder.bind(
+ registry,
+ item.getName(),
+ recorderContext.classProxy(item.getType()));
+ }
});
return new CamelRegistryBuildItem(registry);
@@ -284,11 +294,20 @@
.forEach(item -> {
LOGGER.debug("Binding runtime bean with name: {}, type {}", item.getName(), item.getType());
- recorder.bind(
- registry.getRegistry(),
- item.getName(),
- recorderContext.classProxy(item.getType()),
- item.getValue());
+ if (item.getValue() != null) {
+ recorder.bind(
+ registry.getRegistry(),
+ item.getName(),
+ recorderContext.classProxy(item.getType()),
+ item.getValue());
+ } else {
+ // the instance of the service will be instantiated by the recorder, this avoid
+ // creating a recorder for trivial services.
+ recorder.bind(
+ registry.getRegistry(),
+ item.getName(),
+ recorderContext.classProxy(item.getType()));
+ }
});
return new CamelRuntimeRegistryBuildItem(registry.getRegistry());
diff --git a/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelBeanBuildItem.java b/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelBeanBuildItem.java
index 64ba7de..51c2fd4 100644
--- a/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelBeanBuildItem.java
+++ b/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelBeanBuildItem.java
@@ -18,6 +18,8 @@
import java.util.Objects;
+import javax.annotation.Nullable;
+
import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.runtime.RuntimeValue;
@@ -37,10 +39,24 @@
private final String type;
private final RuntimeValue<?> value;
- public CamelBeanBuildItem(String name, String type, RuntimeValue<?> value) {
+ /**
+ * @param name the name of the bean
+ * @param type the Java type of the bean
+ */
+ public CamelBeanBuildItem(String name, String type) {
+ this(name, type, null);
+ }
+
+ /**
+ * @param name the name of the bean
+ * @param type the Java type of the bean
+ * @param value the value to be bound to the registry, if <code>null</code> a new instance will be create
+ * by the {@link org.apache.camel.quarkus.core.CamelMainRecorder}
+ */
+ public CamelBeanBuildItem(String name, String type, @Nullable RuntimeValue<?> value) {
this.name = Objects.requireNonNull(name);
this.type = Objects.requireNonNull(type);
- this.value = Objects.requireNonNull(value);
+ this.value = value;
}
public String getName() {
@@ -51,6 +67,7 @@
return type;
}
+ @Nullable
public RuntimeValue<?> getValue() {
return value;
}
diff --git a/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelRuntimeBeanBuildItem.java b/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelRuntimeBeanBuildItem.java
index ab8b11a..af9386d 100644
--- a/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelRuntimeBeanBuildItem.java
+++ b/extensions/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelRuntimeBeanBuildItem.java
@@ -18,6 +18,8 @@
import java.util.Objects;
+import javax.annotation.Nullable;
+
import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.runtime.RuntimeValue;
@@ -37,10 +39,24 @@
private final String type;
private final RuntimeValue<?> value;
- public CamelRuntimeBeanBuildItem(String name, String type, RuntimeValue<?> value) {
+ /**
+ * @param name the name of the bean
+ * @param type the Java type of the bean
+ */
+ public CamelRuntimeBeanBuildItem(String name, String type) {
+ this(name, type, null);
+ }
+
+ /**
+ * @param name the name of the bean
+ * @param type the Java type of the bean
+ * @param value the value to be bound to the registry, if <code>null</code> a new instance will be create
+ * by the {@link org.apache.camel.quarkus.core.CamelMainRecorder}
+ */
+ public CamelRuntimeBeanBuildItem(String name, String type, @Nullable RuntimeValue<?> value) {
this.name = Objects.requireNonNull(name);
this.type = Objects.requireNonNull(type);
- this.value = Objects.requireNonNull(value);
+ this.value = value;
}
public String getName() {
@@ -51,6 +67,7 @@
return type;
}
+ @Nullable
public RuntimeValue<?> getValue() {
return value;
}
diff --git a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelRecorder.java b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelRecorder.java
index 56e6304..7865f03 100644
--- a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelRecorder.java
+++ b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelRecorder.java
@@ -44,6 +44,15 @@
loader.getValue().load(registry.getValue());
}
+ public void addTypeConverterLoader(RuntimeValue<TypeConverterRegistry> registry,
+ Class<? extends TypeConverterLoader> loader) {
+ try {
+ loader.newInstance().load(registry.getValue());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
@SuppressWarnings("unchecked")
public RuntimeValue<CamelContext> createContext(
RuntimeValue<Registry> registry,
diff --git a/extensions/microprofile-health/deployment/src/main/java/org/apache/camel/quarkus/component/microprofile/health/deployment/MicroProfileHealthProcessor.java b/extensions/microprofile-health/deployment/src/main/java/org/apache/camel/quarkus/component/microprofile/health/deployment/MicroProfileHealthProcessor.java
index 4f8220e..8b04405 100644
--- a/extensions/microprofile-health/deployment/src/main/java/org/apache/camel/quarkus/component/microprofile/health/deployment/MicroProfileHealthProcessor.java
+++ b/extensions/microprofile-health/deployment/src/main/java/org/apache/camel/quarkus/component/microprofile/health/deployment/MicroProfileHealthProcessor.java
@@ -25,18 +25,13 @@
import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
-import io.quarkus.deployment.annotations.ExecutionTime;
-import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
-import io.quarkus.deployment.recording.RecorderContext;
-
import org.apache.camel.health.HealthCheck;
import org.apache.camel.health.HealthCheckRepository;
import org.apache.camel.impl.health.DefaultHealthCheckRegistry;
import org.apache.camel.microprofile.health.AbstractCamelMicroProfileHealthCheck;
import org.apache.camel.quarkus.component.microprofile.health.runtime.CamelMicroProfileHealthConfig;
-import org.apache.camel.quarkus.component.microprofile.health.runtime.CamelMicroProfileHealthRecorder;
import org.apache.camel.quarkus.core.deployment.CamelBeanBuildItem;
import org.apache.camel.quarkus.core.deployment.CamelSupport;
import org.eclipse.microprofile.health.Liveness;
@@ -63,12 +58,9 @@
return new FeatureBuildItem(FEATURE);
}
- @Record(ExecutionTime.STATIC_INIT)
@BuildStep
List<CamelBeanBuildItem> camelHealthDiscovery(
CombinedIndexBuildItem combinedIndex,
- RecorderContext recorderContext,
- CamelMicroProfileHealthRecorder recorder,
CamelMicroProfileHealthConfig config) {
List<CamelBeanBuildItem> buildItems = new ArrayList<>();
@@ -83,10 +75,7 @@
.filter(CamelSupport::isConcrete)
.filter(CamelSupport::isPublic)
.filter(ClassInfo::hasNoArgsConstructor)
- .map(classInfo -> new CamelBeanBuildItem(
- classInfo.simpleName(),
- CAMEL_HEALTH_CHECK_DOTNAME.toString(),
- recorderContext.newInstance(classInfo.toString())))
+ .map(classInfo -> new CamelBeanBuildItem(classInfo.simpleName(), classInfo.name().toString()))
.forEach(buildItems::add);
// Create CamelBeanBuildItem to bind instances of HealthCheckRepository to the camel registry
@@ -95,10 +84,7 @@
.filter(CamelSupport::isPublic)
.filter(ClassInfo::hasNoArgsConstructor)
.filter(classInfo -> !classInfo.simpleName().equals(DefaultHealthCheckRegistry.class.getSimpleName()))
- .map(classInfo -> new CamelBeanBuildItem(
- classInfo.simpleName(),
- CAMEL_HEALTH_CHECK_REPOSITORY_DOTNAME.toString(),
- recorderContext.newInstance(classInfo.toString())))
+ .map(classInfo -> new CamelBeanBuildItem(classInfo.simpleName(), classInfo.name().toString()))
.forEach(buildItems::add);
}
diff --git a/extensions/microprofile-health/runtime/src/main/java/org/apache/camel/quarkus/component/microprofile/health/runtime/CamelMicroProfileHealthRecorder.java b/extensions/microprofile-health/runtime/src/main/java/org/apache/camel/quarkus/component/microprofile/health/runtime/CamelMicroProfileHealthRecorder.java
deleted file mode 100644
index 8f5f079..0000000
--- a/extensions/microprofile-health/runtime/src/main/java/org/apache/camel/quarkus/component/microprofile/health/runtime/CamelMicroProfileHealthRecorder.java
+++ /dev/null
@@ -1,27 +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.quarkus.component.microprofile.health.runtime;
-
-import io.quarkus.runtime.RuntimeValue;
-import io.quarkus.runtime.annotations.Recorder;
-
-import org.apache.camel.health.HealthCheck;
-import org.apache.camel.health.HealthCheckRepository;
-
-@Recorder
-public class CamelMicroProfileHealthRecorder {
-}