Skip to content

Commit 21aa7a0

Browse files
committed
Migrate extension Config to @ConfigMapping
Moving the config interface to runtime If the config is runtime, the call to the config should be done in the recorder
1 parent dbee2bb commit 21aa7a0

File tree

6 files changed

+42
-38
lines changed

6 files changed

+42
-38
lines changed

quarkus-workshop-super-heroes/docs/src/docs/asciidoc/optional-quarkus-extension/quarkus-extension.adoc

+19-16
Original file line numberDiff line numberDiff line change
@@ -135,44 +135,47 @@ For this, what do we need:
135135
The _runtime_ part of an extension contains only the classes and resources required at runtime.
136136
For the version extension, it would be a single class that prints the version.
137137

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

141-
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:
142+
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:
142143

143144
[source,java]
144145
----
145-
include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionRecorder.java[]
146+
include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java[]
146147
----
147148
--
149+
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.
148150

149-
Simple right?
150-
But how does it work?
151-
Look at the `@Recorder` annotation.
152-
It indicates that this class is a _recorder_ used to record actions executed later at runtime.
153-
Indeed, these actions are replayed at runtime.
154-
We will see how this recorder is used from the _deployment_ module.
151+
The `@ConfigRoot` annotation declares that the configuration is available during runtime.
155152

156-
== The deployment module
153+
The `@WithDefault("true")` is used to declare the default value returned when `enabled()` is called.
157154

158-
This module contains _build steps_, i.e., methods called during the augmentation phase and computing just enough bytecode to serve the services the application requires.
159-
The version extension consists of a single build step that extracts the version and uses the `VersionRecorder`.
155+
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`.
160156

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

165-
Under the `deployment` module, create new class `io.quarkus.workshop.superheroes.version.deployment.VersionConfig` with the following content:
160+
Still in the `runtime` module, create the `io.quarkus.workshop.superheroes.version.runtime.VersionRecorder` class with the following content:
166161

167162
[source,java]
168163
----
169-
include::{code-root}/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java[]
164+
include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionRecorder.java[]
170165
----
171166
--
172167

173-
The `@ConfigRoot` annotation declares `VersionConfig` as a configuration class which configuration properties are prefixed with `quarkus.` prefix.
168+
Simple right?
169+
But how does it work?
170+
Look at the `@Recorder` annotation.
171+
It indicates that this class is a _recorder_ used to record actions executed later at runtime.
172+
Indeed, these actions are replayed at runtime.
173+
We will see how this recorder is used from the _deployment_ module.
174+
175+
== The deployment module
174176

175-
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`.
177+
This module contains _build steps_, i.e., methods called during the augmentation phase and computing just enough bytecode to serve the services the application requires.
178+
The version extension consists of a single build step that extracts the version and uses the `VersionRecorder`.
176179

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

quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/pom.xml

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
<version>${quarkus.version}</version>
3838
</path>
3939
</annotationProcessorPaths>
40-
<compilerArgs>
41-
<arg>-AlegacyConfigRoot=true</arg>
42-
</compilerArgs>
4340
</configuration>
4441
</plugin>
4542
</plugins>

quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/ExtensionVersionProcessor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.quarkus.deployment.annotations.Record;
66
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
77
import io.quarkus.deployment.builditem.FeatureBuildItem;
8+
import io.quarkus.workshop.superheroes.version.runtime.VersionConfig;
89
import io.quarkus.workshop.superheroes.version.runtime.VersionRecorder;
910

1011
class ExtensionVersionProcessor {
@@ -19,8 +20,6 @@ FeatureBuildItem feature() {
1920
@BuildStep
2021
@Record(ExecutionTime.RUNTIME_INIT)
2122
void recordVersion(ApplicationInfoBuildItem app, VersionConfig versionConfig, VersionRecorder recorder) {
22-
if (versionConfig.enabled) {
23-
recorder.printVersion(app.getVersion());
24-
}
23+
recorder.printVersion(versionConfig, app.getVersion());
2524
}
2625
}

quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.quarkus.workshop.superheroes.version.runtime;
2+
3+
import io.quarkus.runtime.annotations.ConfigPhase;
4+
import io.quarkus.runtime.annotations.ConfigRoot;
5+
import io.smallrye.config.ConfigMapping;
6+
import io.smallrye.config.WithDefault;
7+
8+
@ConfigMapping(prefix = "quarkus.version")
9+
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
10+
public interface VersionConfig {
11+
12+
/**
13+
* Enables or disables the version printing at startup.
14+
*/
15+
@WithDefault("true")
16+
boolean enabled();
17+
}

quarkus-workshop-super-heroes/super-heroes/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionRecorder.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
@Recorder
77
public class VersionRecorder {
88

9-
public void printVersion(String version) {
10-
Logger.getLogger(VersionRecorder.class.getName()).infof("Version: %s", version);
9+
public void printVersion(VersionConfig versionConfig, String version) {
10+
if (versionConfig.enabled()) {
11+
Logger.getLogger(VersionRecorder.class.getName()).infof("Version: %s", version);
12+
}
1113
}
1214

1315
}

0 commit comments

Comments
 (0)