Skip to content

Commit

Permalink
Switch Hibernate ORM/Reactive/Envers extensions to @ConfigMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Feb 12, 2024
1 parent 01a682b commit d44119e
Show file tree
Hide file tree
Showing 21 changed files with 520 additions and 582 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void disableHibernateEnversStaticInit(HibernateEnversRecorder recorder,
public void checkNoExplicitActiveTrue(HibernateEnversBuildTimeConfig buildTimeConfig) {
for (var entry : buildTimeConfig.getAllPersistenceUnitConfigsAsMap().entrySet()) {
var config = entry.getValue();
if (config.active.isPresent() && config.active.get()) {
if (config.active().isPresent() && config.active().get()) {
var puName = entry.getKey();
String enabledPropertyKey = HibernateEnversBuildTimeConfig.extensionPropertyKey("enabled");
String activePropertyKey = HibernateEnversBuildTimeConfig.persistenceUnitPropertyKey(puName, "active");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HibernateEnversEnabled implements BooleanSupplier {

@Override
public boolean getAsBoolean() {
return config.enabled;
return config.enabled();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void registerEnversReflections(BuildProducer<ReflectiveClassBuildItem> re
.methods().build());

for (HibernateEnversBuildTimeConfigPersistenceUnit pu : buildTimeConfig.getAllPersistenceUnitConfigsAsMap().values()) {
pu.revisionListener.ifPresent(
pu.revisionListener().ifPresent(
s -> reflectiveClass.produce(ReflectiveClassBuildItem.builder(s).methods().fields().build()));
pu.auditStrategy.ifPresent(
pu.auditStrategy().ifPresent(
s -> reflectiveClass.produce(ReflectiveClassBuildItem.builder(s).methods().fields().build()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil;
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigItem;
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;

@ConfigMapping(prefix = "quarkus.hibernate-envers")
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class HibernateEnversBuildTimeConfig {
public interface HibernateEnversBuildTimeConfig {
/**
* Whether Hibernate Envers is enabled <strong>during the build</strong>.
*
Expand All @@ -23,37 +26,35 @@ public class HibernateEnversBuildTimeConfig {
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;
@WithDefault("true")
boolean enabled();

/**
* Configuration for the default persistence unit.
*/
@ConfigItem(name = ConfigItem.PARENT)
public HibernateEnversBuildTimeConfigPersistenceUnit defaultPersistenceUnit;
@WithParentName
HibernateEnversBuildTimeConfigPersistenceUnit defaultPersistenceUnit();

/**
* Configuration for additional named persistence units.
*/
@ConfigDocSection
@WithParentName
@ConfigDocMapKey("persistence-unit-name")
@ConfigItem(name = ConfigItem.PARENT)
public Map<String, HibernateEnversBuildTimeConfigPersistenceUnit> persistenceUnits;
Map<String, HibernateEnversBuildTimeConfigPersistenceUnit> namedPersistenceUnits();

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

public static String extensionPropertyKey(String radical) {
static String extensionPropertyKey(String radical) {
return "quarkus.hibernate-envers." + radical;
}

public static String persistenceUnitPropertyKey(String persistenceUnitName, String radical) {
static String persistenceUnitPropertyKey(String persistenceUnitName, String radical) {
StringBuilder keyBuilder = new StringBuilder("quarkus.hibernate-envers.");
if (!PersistenceUnitUtil.isDefaultPersistenceUnit(persistenceUnitName)) {
keyBuilder.append("\"").append(persistenceUnitName).append("\".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigDocDefault;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.smallrye.config.WithDefault;

@ConfigGroup
public class HibernateEnversBuildTimeConfigPersistenceUnit {
public interface HibernateEnversBuildTimeConfigPersistenceUnit {

/**
* Whether Hibernate Envers should be active for this persistence unit at runtime.
Expand All @@ -24,164 +25,163 @@ public class HibernateEnversBuildTimeConfigPersistenceUnit {
*
* @asciidoclet
*/
@ConfigItem(defaultValueDocumentation = "'true' if Hibernate ORM is enabled; 'false' otherwise")
public Optional<Boolean> active = Optional.empty();
@ConfigDocDefault("'true' if Hibernate ORM is enabled; 'false' otherwise")
Optional<Boolean> active();

/**
* Enable store_data_at_delete feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#STORE_DATA_AT_DELETE}.
*/
@ConfigItem(defaultValue = "false")
public boolean storeDataAtDelete;
@WithDefault("false")
boolean storeDataAtDelete();

/**
* Defines a suffix for historical data table. Defaults to {@literal _AUD}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#AUDIT_TABLE_SUFFIX}.
*/
@ConfigItem(defaultValue = "_AUD")
public Optional<String> auditTableSuffix;
@WithDefault("_AUD")
Optional<String> auditTableSuffix();

/**
* Defines a prefix for historical data table. Default is the empty string.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#AUDIT_TABLE_PREFIX}.
*/
@ConfigItem(defaultValue = "")
public Optional<String> auditTablePrefix;
@WithDefault("")
Optional<String> auditTablePrefix();

/**
* Revision field name. Defaults to {@literal REV}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#REVISION_FIELD_NAME}.
*/
@ConfigItem(defaultValue = "REV")
public Optional<String> revisionFieldName;
@WithDefault("REV")
Optional<String> revisionFieldName();

/**
* Revision type field name. Defaults to {@literal REVTYPE}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#REVISION_TYPE_FIELD_NAME}.
*/
@ConfigItem(defaultValue = "REVTYPE")
public Optional<String> revisionTypeFieldName;
@WithDefault("REVTYPE")
Optional<String> revisionTypeFieldName();

/**
* Enable the revision_on_collection_change feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#REVISION_ON_COLLECTION_CHANGE}.
*/
@ConfigItem(defaultValue = "true")
public boolean revisionOnCollectionChange;
@WithDefault("true")
boolean revisionOnCollectionChange();

/**
* Enable the do_not_audit_optimistic_locking_field feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#DO_NOT_AUDIT_OPTIMISTIC_LOCKING_FIELD}.
*/
@ConfigItem(defaultValue = "true")
public boolean doNotAuditOptimisticLockingField;
@WithDefault("true")
boolean doNotAuditOptimisticLockingField();

/**
* Defines the default schema of where audit tables are to be created.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#DEFAULT_SCHEMA}.
*/
@ConfigItem(defaultValue = "")
public Optional<String> defaultSchema;
@WithDefault("")
Optional<String> defaultSchema();

/**
* Defines the default catalog of where audit tables are to be created.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#DEFAULT_CATALOG}.
*/
@ConfigItem(defaultValue = "")
public Optional<String> defaultCatalog;
@WithDefault("")
Optional<String> defaultCatalog();

/**
* Enables the track_entities_changed_in_revision feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#TRACK_ENTITIES_CHANGED_IN_REVISION}.
*/
@ConfigItem(defaultValue = "false")
public boolean trackEntitiesChangedInRevision;
@WithDefault("false")
boolean trackEntitiesChangedInRevision();

/**
* Enables the use_revision_entity_with_native_id feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#USE_REVISION_ENTITY_WITH_NATIVE_ID}.
*/
@ConfigItem(defaultValue = "true")
public boolean useRevisionEntityWithNativeId;
@WithDefault("true")
boolean useRevisionEntityWithNativeId();

/**
* Enables the global_with_modified_flag feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#GLOBAL_WITH_MODIFIED_FLAG}.
*/
@ConfigItem(defaultValue = "false")
public boolean globalWithModifiedFlag;
@WithDefault("false")
boolean globalWithModifiedFlag();

/**
* Defines the suffix to be used for modified flag columns. Defaults to {@literal _MOD}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#MODIFIED_FLAG_SUFFIX}
*/
@ConfigItem(defaultValue = "_MOD")
public Optional<String> modifiedFlagSuffix;
@WithDefault("_MOD")
Optional<String> modifiedFlagSuffix();

/**
* Defines the fully qualified class name of a user defined revision listener.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#REVISION_LISTENER}.
*/
@ConfigItem
public Optional<String> revisionListener;
Optional<String> revisionListener();

/**
* Defines the fully qualified class name of the audit strategy to be used.
*
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#AUDIT_STRATEGY}.
*/
@ConfigItem(defaultValue = "org.hibernate.envers.strategy.DefaultAuditStrategy")
public Optional<String> auditStrategy;
@WithDefault("org.hibernate.envers.strategy.DefaultAuditStrategy")
Optional<String> auditStrategy();

/**
* Defines the property name for the audit entity's composite primary key. Defaults to {@literal originalId}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#ORIGINAL_ID_PROP_NAME}.
*/
@ConfigItem(defaultValue = "originalId")
public Optional<String> originalIdPropName;
@WithDefault("originalId")
Optional<String> originalIdPropName();

/**
* Defines the column name that holds the end revision number in audit entities. Defaults to {@literal REVEND}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#AUDIT_STRATEGY_VALIDITY_END_REV_FIELD_NAME}.
*/
@ConfigItem(defaultValue = "REVEND")
public Optional<String> auditStrategyValidityEndRevFieldName;
@WithDefault("REVEND")
Optional<String> auditStrategyValidityEndRevFieldName();

/**
* Enables the audit_strategy_validity_store_revend_timestamp feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#AUDIT_STRATEGY_VALIDITY_STORE_REVEND_TIMESTAMP}.
*/
@ConfigItem(defaultValue = "false")
public boolean auditStrategyValidityStoreRevendTimestamp;
@WithDefault("false")
boolean auditStrategyValidityStoreRevendTimestamp();

/**
* Defines the column name of the revision end timestamp in the audit tables. Defaults to {@literal REVEND_TSTMP}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#AUDIT_STRATEGY_VALIDITY_REVEND_TIMESTAMP_FIELD_NAME}.
*/
@ConfigItem(defaultValue = "REVEND_TSTMP")
public Optional<String> auditStrategyValidityRevendTimestampFieldName;
@WithDefault("REVEND_TSTMP")
Optional<String> auditStrategyValidityRevendTimestampFieldName();

/**
* Defines the name of the column used for storing collection ordinal values for embeddable elements.
* Defaults to {@literal SETORDINAL}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#EMBEDDABLE_SET_ORDINAL_FIELD_NAME}.
*/
@ConfigItem(defaultValue = "SETORDINAL")
public Optional<String> embeddableSetOrdinalFieldName;
@WithDefault("SETORDINAL")
Optional<String> embeddableSetOrdinalFieldName();

/**
* Enables the allow_identifier_reuse feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#ALLOW_IDENTIFIER_REUSE}.
*/
@ConfigItem(defaultValue = "false")
public boolean allowIdentifierReuse;
@WithDefault("false")
boolean allowIdentifierReuse();

/**
* Defines the naming strategy to be used for modified columns.
* Defaults to {@literal org.hibernate.envers.boot.internal.LegacyModifiedColumnNamingStrategy}.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#MODIFIED_COLUMN_NAMING_STRATEGY}.
*/
@ConfigItem(defaultValue = "org.hibernate.envers.boot.internal.LegacyModifiedColumnNamingStrategy")
public Optional<String> modifiedColumnNamingStrategy;
@WithDefault("org.hibernate.envers.boot.internal.LegacyModifiedColumnNamingStrategy")
Optional<String> modifiedColumnNamingStrategy();

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,43 @@ public void contributeBootProperties(BiConsumer<String, Object> propertyCollecto
// Leave Envers unconfigured, but still activate it.
return;
}
if (puConfig.active.isPresent() && !puConfig.active.get()) {
if (puConfig.active().isPresent() && !puConfig.active().get()) {
propertyCollector.accept(EnversService.INTEGRATION_ENABLED, "false");
// Do not process other properties: Hibernate Envers is inactive anyway.
return;
}

addConfig(propertyCollector, EnversSettings.STORE_DATA_AT_DELETE, puConfig.storeDataAtDelete);
addConfig(propertyCollector, EnversSettings.AUDIT_TABLE_SUFFIX, puConfig.auditTableSuffix);
addConfig(propertyCollector, EnversSettings.AUDIT_TABLE_PREFIX, puConfig.auditTablePrefix);
addConfig(propertyCollector, EnversSettings.REVISION_FIELD_NAME, puConfig.revisionFieldName);
addConfig(propertyCollector, EnversSettings.REVISION_TYPE_FIELD_NAME, puConfig.revisionTypeFieldName);
addConfig(propertyCollector, EnversSettings.STORE_DATA_AT_DELETE, puConfig.storeDataAtDelete());
addConfig(propertyCollector, EnversSettings.AUDIT_TABLE_SUFFIX, puConfig.auditTableSuffix());
addConfig(propertyCollector, EnversSettings.AUDIT_TABLE_PREFIX, puConfig.auditTablePrefix());
addConfig(propertyCollector, EnversSettings.REVISION_FIELD_NAME, puConfig.revisionFieldName());
addConfig(propertyCollector, EnversSettings.REVISION_TYPE_FIELD_NAME, puConfig.revisionTypeFieldName());
addConfig(propertyCollector, EnversSettings.REVISION_ON_COLLECTION_CHANGE,
puConfig.revisionOnCollectionChange);
puConfig.revisionOnCollectionChange());
addConfig(propertyCollector, EnversSettings.DO_NOT_AUDIT_OPTIMISTIC_LOCKING_FIELD,
puConfig.doNotAuditOptimisticLockingField);
addConfig(propertyCollector, EnversSettings.DEFAULT_SCHEMA, puConfig.defaultSchema);
addConfig(propertyCollector, EnversSettings.DEFAULT_CATALOG, puConfig.defaultCatalog);
puConfig.doNotAuditOptimisticLockingField());
addConfig(propertyCollector, EnversSettings.DEFAULT_SCHEMA, puConfig.defaultSchema());
addConfig(propertyCollector, EnversSettings.DEFAULT_CATALOG, puConfig.defaultCatalog());
addConfig(propertyCollector, EnversSettings.TRACK_ENTITIES_CHANGED_IN_REVISION,
puConfig.trackEntitiesChangedInRevision);
puConfig.trackEntitiesChangedInRevision());
addConfig(propertyCollector, EnversSettings.USE_REVISION_ENTITY_WITH_NATIVE_ID,
puConfig.useRevisionEntityWithNativeId);
addConfig(propertyCollector, EnversSettings.GLOBAL_WITH_MODIFIED_FLAG, puConfig.globalWithModifiedFlag);
addConfig(propertyCollector, EnversSettings.MODIFIED_FLAG_SUFFIX, puConfig.modifiedFlagSuffix);
addConfigIfPresent(propertyCollector, EnversSettings.REVISION_LISTENER, puConfig.revisionListener);
addConfigIfPresent(propertyCollector, EnversSettings.AUDIT_STRATEGY, puConfig.auditStrategy);
addConfigIfPresent(propertyCollector, EnversSettings.ORIGINAL_ID_PROP_NAME, puConfig.originalIdPropName);
puConfig.useRevisionEntityWithNativeId());
addConfig(propertyCollector, EnversSettings.GLOBAL_WITH_MODIFIED_FLAG, puConfig.globalWithModifiedFlag());
addConfig(propertyCollector, EnversSettings.MODIFIED_FLAG_SUFFIX, puConfig.modifiedFlagSuffix());
addConfigIfPresent(propertyCollector, EnversSettings.REVISION_LISTENER, puConfig.revisionListener());
addConfigIfPresent(propertyCollector, EnversSettings.AUDIT_STRATEGY, puConfig.auditStrategy());
addConfigIfPresent(propertyCollector, EnversSettings.ORIGINAL_ID_PROP_NAME, puConfig.originalIdPropName());
addConfigIfPresent(propertyCollector, EnversSettings.AUDIT_STRATEGY_VALIDITY_END_REV_FIELD_NAME,
puConfig.auditStrategyValidityEndRevFieldName);
puConfig.auditStrategyValidityEndRevFieldName());
addConfig(propertyCollector, EnversSettings.AUDIT_STRATEGY_VALIDITY_STORE_REVEND_TIMESTAMP,
puConfig.auditStrategyValidityStoreRevendTimestamp);
puConfig.auditStrategyValidityStoreRevendTimestamp());
addConfigIfPresent(propertyCollector, EnversSettings.AUDIT_STRATEGY_VALIDITY_REVEND_TIMESTAMP_FIELD_NAME,
puConfig.auditStrategyValidityRevendTimestampFieldName);
puConfig.auditStrategyValidityRevendTimestampFieldName());
addConfigIfPresent(propertyCollector, EnversSettings.EMBEDDABLE_SET_ORDINAL_FIELD_NAME,
puConfig.embeddableSetOrdinalFieldName);
addConfig(propertyCollector, EnversSettings.ALLOW_IDENTIFIER_REUSE, puConfig.allowIdentifierReuse);
puConfig.embeddableSetOrdinalFieldName());
addConfig(propertyCollector, EnversSettings.ALLOW_IDENTIFIER_REUSE, puConfig.allowIdentifierReuse());
addConfigIfPresent(propertyCollector, EnversSettings.MODIFIED_COLUMN_NAMING_STRATEGY,
puConfig.modifiedColumnNamingStrategy);
puConfig.modifiedColumnNamingStrategy());
}

public static <T> void addConfig(BiConsumer<String, Object> propertyCollector, String configPath, T value) {
Expand Down
Loading

0 comments on commit d44119e

Please sign in to comment.