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.
Use a Quarkus-specific clock provider that is reinitialized at runtime
- so that the actual runtime system timezone is picked up and clock-based (date/time) constraints are correctly evaluated
- Loading branch information
1 parent
646c96b
commit 01bb56c
Showing
7 changed files
with
129 additions
and
0 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
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
27 changes: 27 additions & 0 deletions
27
...te/validator/runtime/clockprovider/HibernateValidatorClockProviderSystemZoneIdHolder.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,27 @@ | ||
package io.quarkus.hibernate.validator.runtime.clockprovider; | ||
|
||
import java.time.ZoneId; | ||
|
||
/** | ||
* A helper class holding a system timezone. | ||
* <p> | ||
* It is reloaded at runtime to provide the runtime-system time zone | ||
* to the constraints based on a {@link jakarta.validation.ClockProvider}. | ||
* <p> | ||
* Note, that we do not hold the timezone constant in the clock provider itself as we need to "reinitialize" this class, | ||
* so that the timezone is set to the actual runtime-system-timezone. | ||
* Having a constant in the clock provider and asking to reload the provider class leads to native build failure: | ||
* <p> | ||
* <em> | ||
* Error: An object of type 'io.quarkus.hibernate.validator.runtime.clockprovider.HibernateValidatorClockProvider' was found in | ||
* the image heap. This type, however, is marked for initialization at image run time for the following reason: classes are | ||
* initialized at run time by default. | ||
* This is not allowed for correctness reasons: All objects that are stored in the image heap must be initialized at build time. | ||
* </em> | ||
* <p> | ||
* And we do have instances of the clock provider/clock in the Hibernate Validator metadata as we eagerly initialize | ||
* constraints. | ||
*/ | ||
class HibernateValidatorClockProviderSystemZoneIdHolder { | ||
static final ZoneId SYSTEM_ZONE_ID = ZoneId.systemDefault(); | ||
} |
45 changes: 45 additions & 0 deletions
45
...s/hibernate/validator/runtime/clockprovider/RuntimeReinitializedDefaultClockProvider.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,45 @@ | ||
package io.quarkus.hibernate.validator.runtime.clockprovider; | ||
|
||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
|
||
import jakarta.validation.ClockProvider; | ||
|
||
/** | ||
* A Quarkus-specific clock provider that can provide a clock based on a runtime system time zone. | ||
*/ | ||
public class RuntimeReinitializedDefaultClockProvider implements ClockProvider { | ||
|
||
public static final RuntimeReinitializedDefaultClockProvider INSTANCE = new RuntimeReinitializedDefaultClockProvider(); | ||
|
||
private static final RuntimeReinitializedDefaultClock clock = new RuntimeReinitializedDefaultClock(); | ||
|
||
private RuntimeReinitializedDefaultClockProvider() { | ||
} | ||
|
||
@Override | ||
public Clock getClock() { | ||
return clock; | ||
} | ||
|
||
private static class RuntimeReinitializedDefaultClock extends Clock { | ||
|
||
@Override | ||
public ZoneId getZone() { | ||
// we delegate getting the zone id value to a helper class that is reinitialized at runtime | ||
// allowing to pick up an actual runtime timezone. | ||
return HibernateValidatorClockProviderSystemZoneIdHolder.SYSTEM_ZONE_ID; | ||
} | ||
|
||
@Override | ||
public Clock withZone(ZoneId zone) { | ||
return Clock.system(zone); | ||
} | ||
|
||
@Override | ||
public Instant instant() { | ||
return Instant.now(); | ||
} | ||
} | ||
} |
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