forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#36281 from mkouba/static-init-config-val…
…idation Config: detect injected config value mismatch during static init
- Loading branch information
Showing
23 changed files
with
525 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
core/runtime/src/main/java/io/quarkus/runtime/ExecutionMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.runtime; | ||
|
||
/** | ||
* The runtime execution mode. | ||
*/ | ||
public enum ExecutionMode { | ||
|
||
/** | ||
* Static initializiation. | ||
*/ | ||
STATIC_INIT, | ||
|
||
/** | ||
* Runtime initialization. | ||
*/ | ||
RUNTIME_INIT, | ||
|
||
/** | ||
* The application is running. | ||
*/ | ||
RUNNING, | ||
|
||
UNSET, | ||
; | ||
|
||
public static ExecutionMode current() { | ||
return ExecutionModeManager.getExecutionMode(); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
core/runtime/src/main/java/io/quarkus/runtime/ExecutionModeManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.quarkus.runtime; | ||
|
||
public final class ExecutionModeManager { | ||
|
||
private static volatile ExecutionMode executionMode = ExecutionMode.UNSET; | ||
|
||
public static void staticInit() { | ||
executionMode = ExecutionMode.STATIC_INIT; | ||
} | ||
|
||
public static void runtimeInit() { | ||
executionMode = ExecutionMode.RUNTIME_INIT; | ||
} | ||
|
||
public static void running() { | ||
executionMode = ExecutionMode.RUNNING; | ||
} | ||
|
||
public static void unset() { | ||
executionMode = ExecutionMode.UNSET; | ||
} | ||
|
||
public static ExecutionMode getExecutionMode() { | ||
return executionMode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ns/arc/deployment/src/main/java/io/quarkus/arc/deployment/ConfigStaticInitBuildSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.arc.deployment; | ||
|
||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
import io.quarkus.arc.processor.DotNames; | ||
import io.quarkus.arc.runtime.ConfigStaticInitCheck; | ||
import io.quarkus.arc.runtime.ConfigStaticInitCheckInterceptor; | ||
import io.quarkus.arc.runtime.ConfigStaticInitValues; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
|
||
public class ConfigStaticInitBuildSteps { | ||
|
||
@BuildStep | ||
AdditionalBeanBuildItem registerBeans() { | ||
return AdditionalBeanBuildItem.builder() | ||
.addBeanClasses(ConfigStaticInitCheckInterceptor.class, ConfigStaticInitValues.class, | ||
ConfigStaticInitCheck.class) | ||
.build(); | ||
} | ||
|
||
@BuildStep | ||
AnnotationsTransformerBuildItem transformConfigProducer() { | ||
DotName configProducerName = DotName.createSimple("io.smallrye.config.inject.ConfigProducer"); | ||
|
||
return new AnnotationsTransformerBuildItem(AnnotationsTransformer.appliedToMethod().whenMethod(m -> { | ||
// Apply to all producer methods declared on io.smallrye.config.inject.ConfigProducer | ||
return m.declaringClass().name().equals(configProducerName) | ||
&& m.hasAnnotation(DotNames.PRODUCES) | ||
&& m.hasAnnotation(ConfigBuildStep.MP_CONFIG_PROPERTY_NAME); | ||
}).thenTransform(t -> { | ||
t.add(ConfigStaticInitCheck.class); | ||
})); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ns/arc/deployment/src/test/java/io/quarkus/arc/test/config/staticinit/StaticInitBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Initialized; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
@Singleton | ||
public class StaticInitBean { | ||
|
||
@ConfigProperty(name = "apfelstrudel") | ||
String value; | ||
|
||
// bean is instantiated during STATIC_INIT | ||
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) { | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...test/java/io/quarkus/arc/test/config/staticinit/StaticInitConfigInjectionFailureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class StaticInitConfigInjectionFailureTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(StaticInitBean.class, StaticInitEagerBean.class, UnsafeConfigSource.class) | ||
.addAsServiceProvider(ConfigSource.class, UnsafeConfigSource.class) | ||
// the value from application.properties should be injected during STATIC_INIT | ||
.addAsResource(new StringAsset("apfelstrudel=jandex"), "application.properties")) | ||
.assertException(t -> { | ||
assertThat(t).isInstanceOf(IllegalStateException.class) | ||
.hasMessageContainingAll( | ||
"A runtime config property value differs from the value that was injected during the static intialization phase", | ||
"the runtime value of 'apfelstrudel' is [gizmo] but the value [jandex] was injected into io.quarkus.arc.test.config.staticinit.StaticInitBean#value", | ||
"the runtime value of 'apfelstrudel' is [gizmo] but the value [jandex] was injected into io.quarkus.arc.test.config.staticinit.StaticInitEagerBean#value"); | ||
}); | ||
|
||
@Test | ||
public void test() { | ||
fail(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...c/deployment/src/test/java/io/quarkus/arc/test/config/staticinit/StaticInitEagerBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Initialized; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
@Singleton | ||
public class StaticInitEagerBean { | ||
|
||
@ConfigProperty(name = "apfelstrudel") | ||
Instance<String> value; | ||
|
||
// bean is instantiated during STATIC_INIT | ||
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) { | ||
// this should trigger the failure | ||
value.get(); | ||
} | ||
|
||
} |
Oops, something went wrong.