Skip to content

Commit

Permalink
Migrate extension Config to @ConfigMapping
Browse files Browse the repository at this point in the history
Moving the config interface to runtime

If the config is runtime, the call to the config should be done in the recorder
  • Loading branch information
gastaldi committed Sep 18, 2024
1 parent dbee2bb commit af16fa3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,29 @@ For this, what do we need:
The _runtime_ part of an extension contains only the classes and resources required at runtime.
For the version extension, it would be a single class that prints the version.

First, let's create the configuration class that will allow users to dynamically configure whether the version should be printed:
[example, role="cta"]
--

Under the `runtime` module, create the `src/main/java` directory, and, in this directory, create a new interface named `io.quarkus.workshop.superheroes.version.runtime.VersionConfig` with the following content:

[source,java]
----
include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java[]
----
--
The `@ConfigMapping` annotation declares `VersionConfig` as a configuration interface which configuration properties are prefixed with `quarkus.version` prefix. It is also used in CDI aware environments to scan and register Config Mappings.

The `@ConfigRoot` annotation declares that the configuration is available during runtime.

The `@WithDefault("true")` is used to declare the default value returned when `enabled()` is called.

By default, the name of the property is derived from what's declared in the `prefix` attribute inside `@ConfigMapping` plus the name of the method. In our case, the user-configured property will be `quarkus.version.enabled`.

[example, role="cta"]
--

In the runtime module, create the `src/main/java` directory, and, in this directory, the `io.quarkus.workshop.superheroes.version.runtime.VersionRecorder` class with the following content:
Still in the `runtime` module, create the `io.quarkus.workshop.superheroes.version.runtime.VersionRecorder` class with the following content:

[source,java]
----
Expand All @@ -160,27 +179,14 @@ The version extension consists of a single build step that extracts the version

[example, role="cta"]
--
First, let's create the configuration class that will allow users to dynamically configure whether the version should be printed:

Under the `deployment` module, create new class `io.quarkus.workshop.superheroes.version.deployment.VersionConfig` with the following content:

[source,java]
----
include::{code-root}/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java[]
----
--

The `@ConfigRoot` annotation declares `VersionConfig` as a configuration class which configuration properties are prefixed with `quarkus.` prefix.

The `@ConfigItem` is used to declare individual configuration properties. By default, the name of the property is derived from the name of the field. In our case, the user-configured property will be `quarkus.version.enabled`.

Next, open the `ExtensionVersionProcessor` class, and update the content to be:

[source, java]
----
include::{code-root}/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/ExtensionVersionProcessor.java[]
----

--
This class is the core of the extension.
It contains a set of methods annotated with `@BuildStep`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.workshop.superheroes.version.runtime.VersionConfig;
import io.quarkus.workshop.superheroes.version.runtime.VersionRecorder;

class ExtensionVersionProcessor {
Expand All @@ -19,8 +20,6 @@ FeatureBuildItem feature() {
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void recordVersion(ApplicationInfoBuildItem app, VersionConfig versionConfig, VersionRecorder recorder) {
if (versionConfig.enabled) {
recorder.printVersion(app.getVersion());
}
recorder.printVersion(versionConfig, app.getVersion());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.workshop.superheroes.version.runtime;

import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.version")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public interface VersionConfig {

/**
* Enables or disables the version printing at startup.
*/
@WithDefault("true")
boolean enabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
@Recorder
public class VersionRecorder {

public void printVersion(String version) {
Logger.getLogger(VersionRecorder.class.getName()).infof("Version: %s", version);
public void printVersion(VersionConfig versionConfig, String version) {
if (versionConfig.enabled()) {
Logger.getLogger(VersionRecorder.class.getName()).infof("Version: %s", version);
}
}

}

0 comments on commit af16fa3

Please sign in to comment.