Skip to content

Commit

Permalink
Use @WithUnnamedKey in config of Hibernate ORM/Reactive/Envers
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Feb 12, 2024
1 parent d44119e commit db431ab
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void disableHibernateEnversStaticInit(HibernateEnversRecorder recorder,
// TODO move this to runtime init once we implement in Hibernate ORM a way
// to remove entity types from the metamodel on runtime init
public void checkNoExplicitActiveTrue(HibernateEnversBuildTimeConfig buildTimeConfig) {
for (var entry : buildTimeConfig.getAllPersistenceUnitConfigsAsMap().entrySet()) {
for (var entry : buildTimeConfig.persistenceUnits().entrySet()) {
var config = entry.getValue();
if (config.active().isPresent() && config.active().get()) {
var puName = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void registerEnversReflections(BuildProducer<ReflectiveClassBuildItem> re
.produce(ReflectiveClassBuildItem.builder("org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity")
.methods().build());

for (HibernateEnversBuildTimeConfigPersistenceUnit pu : buildTimeConfig.getAllPersistenceUnitConfigsAsMap().values()) {
for (HibernateEnversBuildTimeConfigPersistenceUnit pu : buildTimeConfig.persistenceUnits().values()) {
pu.revisionListener().ifPresent(
s -> reflectiveClass.produce(ReflectiveClassBuildItem.builder(s).methods().fields().build()));
pu.auditStrategy().ifPresent(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package io.quarkus.hibernate.envers;

import java.util.Map;
import java.util.TreeMap;

import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil;
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithParentName;
import io.smallrye.config.WithUnnamedKey;

@ConfigMapping(prefix = "quarkus.hibernate-envers")
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
Expand All @@ -30,25 +29,12 @@ public interface HibernateEnversBuildTimeConfig {
boolean enabled();

/**
* Configuration for the default persistence unit.
* Configuration for persistence units.
*/
@WithParentName
HibernateEnversBuildTimeConfigPersistenceUnit defaultPersistenceUnit();

/**
* Configuration for additional named persistence units.
*/
@ConfigDocSection
@WithParentName
@WithUnnamedKey(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME)
@ConfigDocMapKey("persistence-unit-name")
Map<String, HibernateEnversBuildTimeConfigPersistenceUnit> namedPersistenceUnits();

default Map<String, HibernateEnversBuildTimeConfigPersistenceUnit> getAllPersistenceUnitConfigsAsMap() {
Map<String, HibernateEnversBuildTimeConfigPersistenceUnit> map = new TreeMap<>();
map.put(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, defaultPersistenceUnit());
map.putAll(namedPersistenceUnits());
return map;
}
Map<String, HibernateEnversBuildTimeConfigPersistenceUnit> persistenceUnits();

static String extensionPropertyKey(String radical) {
return "quarkus.hibernate-envers." + radical;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private HibernateEnversIntegrationStaticInitListener(HibernateEnversBuildTimeCon

@Override
public void contributeBootProperties(BiConsumer<String, Object> propertyCollector) {
var puConfig = buildTimeConfig.getAllPersistenceUnitConfigsAsMap().get(puName);
var puConfig = buildTimeConfig.persistenceUnits().get(puName);
if (puConfig == null) {
// Leave Envers unconfigured, but still activate it.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.smallrye.config.WithDefaults;
import io.smallrye.config.WithName;
import io.smallrye.config.WithParentName;
import io.smallrye.config.WithUnnamedKey;

@ConfigMapping(prefix = "quarkus.hibernate-orm")
@ConfigRoot
Expand All @@ -40,19 +41,24 @@ public interface HibernateOrmConfig {
HibernateOrmConfigDatabase database();

/**
* Configuration for the default persistence unit.
* Configuration for persistence units.
*/
@WithParentName
HibernateOrmConfigPersistenceUnit defaultPersistenceUnit();

/**
* Additional named persistence units.
*/
@ConfigDocSection
@WithParentName
@WithUnnamedKey(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME)
@WithDefaults
@ConfigDocMapKey("persistence-unit-name")
Map<String, HibernateOrmConfigPersistenceUnit> namedPersistenceUnits();
Map<String, HibernateOrmConfigPersistenceUnit> persistenceUnits();

default HibernateOrmConfigPersistenceUnit defaultPersistenceUnit() {
return persistenceUnits().get(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME);
}

default Map<String, HibernateOrmConfigPersistenceUnit> namedPersistenceUnits() {
Map<String, HibernateOrmConfigPersistenceUnit> map = new TreeMap<>();
map.putAll(persistenceUnits());
map.remove(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME);
return map;
}

/**
* Configuration for the {@code persistence.xml} handling.
Expand Down Expand Up @@ -94,13 +100,6 @@ default boolean isAnyNonPersistenceXmlPropertySet() {
metrics().isAnyPropertySet();
}

default Map<String, HibernateOrmConfigPersistenceUnit> getAllPersistenceUnitConfigsAsMap() {
Map<String, HibernateOrmConfigPersistenceUnit> map = new TreeMap<>();
map.put(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, defaultPersistenceUnit());
map.putAll(namedPersistenceUnits());
return map;
}

@ConfigGroup
interface HibernateOrmConfigPersistenceXml {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void checkTransactionsSupport(Capabilities capabilities, BuildProducer<Validatio
@BuildStep
void includeArchivesHostingEntityPackagesInIndex(HibernateOrmConfig hibernateOrmConfig,
BuildProducer<AdditionalApplicationArchiveMarkerBuildItem> additionalApplicationArchiveMarkers) {
for (HibernateOrmConfigPersistenceUnit persistenceUnit : hibernateOrmConfig.getAllPersistenceUnitConfigsAsMap()
for (HibernateOrmConfigPersistenceUnit persistenceUnit : hibernateOrmConfig.persistenceUnits()
.values()) {
if (persistenceUnit.packages().isPresent()) {
for (String pakkage : persistenceUnit.packages().get()) {
Expand All @@ -217,7 +217,7 @@ void includeArchivesHostingEntityPackagesInIndex(HibernateOrmConfig hibernateOrm
@Consume(ServiceStartBuildItem.class)
@BuildStep(onlyIf = IsDevelopment.class)
void warnOfSchemaProblems(HibernateOrmConfig config, HibernateOrmRecorder recorder) {
for (var e : config.getAllPersistenceUnitConfigsAsMap().entrySet()) {
for (var e : config.persistenceUnits().entrySet()) {
if (e.getValue().validateInDevMode()) {
recorder.doValidation(e.getKey());
}
Expand Down Expand Up @@ -377,7 +377,7 @@ public void contributePersistenceXmlToJpaModel(
public void contributeQuarkusConfigToJpaModel(
BuildProducer<JpaModelPersistenceUnitContributionBuildItem> jpaModelPuContributions,
HibernateOrmConfig hibernateOrmConfig) {
for (Entry<String, HibernateOrmConfigPersistenceUnit> entry : hibernateOrmConfig.getAllPersistenceUnitConfigsAsMap()
for (Entry<String, HibernateOrmConfigPersistenceUnit> entry : hibernateOrmConfig.persistenceUnits()
.entrySet()) {
String name = entry.getKey();
HibernateOrmConfigPersistenceUnit config = entry.getValue();
Expand Down Expand Up @@ -1441,7 +1441,7 @@ private static String normalizePackage(String pakkage) {
}

private static boolean hasPackagesInQuarkusConfig(HibernateOrmConfig hibernateOrmConfig) {
for (HibernateOrmConfigPersistenceUnit persistenceUnitConfig : hibernateOrmConfig.getAllPersistenceUnitConfigsAsMap()
for (HibernateOrmConfigPersistenceUnit persistenceUnitConfig : hibernateOrmConfig.persistenceUnits()
.values()) {
if (persistenceUnitConfig.packages().isPresent()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void devServicesAutoGenerateByDefault(List<JdbcDataSourceSchemaReadyBuildItem> s
Set<String> managedSources = schemaReadyBuildItems.stream().map(JdbcDataSourceSchemaReadyBuildItem::getDatasourceNames)
.collect(HashSet::new, Collection::addAll, Collection::addAll);

for (Map.Entry<String, HibernateOrmConfigPersistenceUnit> entry : config.getAllPersistenceUnitConfigsAsMap()
for (Map.Entry<String, HibernateOrmConfigPersistenceUnit> entry : config.persistenceUnits()
.entrySet()) {
Optional<String> dataSourceName = entry.getValue().datasource();
List<String> propertyKeysIndicatingDataSourceConfigured = DataSourceUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String
"Attempting to boot a blocking Hibernate ORM instance on a reactive RecordedState");
}
final PrevalidatedQuarkusMetadata metadata = recordedState.getMetadata();
var puConfig = hibernateOrmRuntimeConfig.getPersistenceUnitOrDefault(persistenceUnit.getConfigurationName());
var puConfig = hibernateOrmRuntimeConfig.persistenceUnits().get(persistenceUnit.getConfigurationName());
if (puConfig.active().isPresent() && !puConfig.active().get()) {
throw new IllegalStateException(
"Attempting to boot a deactivated Hibernate ORM persistence unit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class HibernateOrmDisabledRecorder {

public void checkNoExplicitActiveTrue(HibernateOrmRuntimeConfig runtimeConfig) {
for (var entry : runtimeConfig.getAllPersistenceUnitConfigsAsMap().entrySet()) {
for (var entry : runtimeConfig.persistenceUnits().entrySet()) {
var config = entry.getValue();
if (config.active().isPresent() && config.active().get()) {
var puName = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,27 @@
package io.quarkus.hibernate.orm.runtime;

import java.util.Map;
import java.util.TreeMap;

import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefaults;
import io.smallrye.config.WithParentName;
import io.smallrye.config.WithUnnamedKey;

@ConfigMapping(prefix = "quarkus.hibernate-orm")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public interface HibernateOrmRuntimeConfig {

/**
* Configuration for the default persistence unit.
* Configuration for persistence units.
*/
@WithParentName
HibernateOrmRuntimeConfigPersistenceUnit defaultPersistenceUnit();

/**
* Additional named persistence units.
*/
@ConfigDocSection
@WithParentName
@WithUnnamedKey(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME)
@WithDefaults
@ConfigDocMapKey("persistence-unit-name")
Map<String, HibernateOrmRuntimeConfigPersistenceUnit> namedPersistenceUnits();

default Map<String, HibernateOrmRuntimeConfigPersistenceUnit> getAllPersistenceUnitConfigsAsMap() {
Map<String, HibernateOrmRuntimeConfigPersistenceUnit> map = new TreeMap<>();
map.put(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, defaultPersistenceUnit());
map.putAll(namedPersistenceUnits());
return map;
}

default HibernateOrmRuntimeConfigPersistenceUnit getPersistenceUnitOrDefault(String name) {
if (PersistenceUnitUtil.isDefaultPersistenceUnit(name)) {
// This is never null
return defaultPersistenceUnit();
} else {
// See @WithDefaults on namedPersistenceUnits(): this never returns null
return namedPersistenceUnits().get(name);
}
}
Map<String, HibernateOrmRuntimeConfigPersistenceUnit> persistenceUnits();

static String extensionPropertyKey(String radical) {
return "quarkus.hibernate-orm." + radical;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class JPAConfig {
public JPAConfig(HibernateOrmRuntimeConfig hibernateOrmRuntimeConfig) {
for (RuntimePersistenceUnitDescriptor descriptor : PersistenceUnitsHolder.getPersistenceUnitDescriptors()) {
String puName = descriptor.getName();
var puConfig = hibernateOrmRuntimeConfig.getPersistenceUnitOrDefault(descriptor.getConfigurationName());
var puConfig = hibernateOrmRuntimeConfig.persistenceUnits().get(descriptor.getConfigurationName());
if (puConfig.active().isPresent() && !puConfig.active().get()) {
LOGGER.infof("Hibernate ORM persistence unit '%s' was deactivated through configuration properties",
puName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String
RuntimeSettings.Builder runtimeSettingsBuilder = new RuntimeSettings.Builder(buildTimeSettings,
integrationSettings);

var puConfig = hibernateOrmRuntimeConfig.getPersistenceUnitOrDefault(persistenceUnit.getConfigurationName());
var puConfig = hibernateOrmRuntimeConfig.persistenceUnits().get(persistenceUnit.getConfigurationName());
if (puConfig.active().isPresent() && !puConfig.active().get()) {
throw new IllegalStateException(
"Attempting to boot a deactivated Hibernate Reactive persistence unit");
Expand Down

0 comments on commit db431ab

Please sign in to comment.