Skip to content

Commit

Permalink
Add build-time configuration property quarkus.hibernate-envers.enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and miador committed Sep 6, 2022
1 parent 84e459a commit 8b7d549
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.hibernate.envers.deployment;

import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.logging.LogCleanupFilterBuildItem;

// Executed even if the extension is disabled, see https://github.com/quarkusio/quarkus/pull/26966/
public final class HibernateEnversAlwaysEnabledProcessor {

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(Feature.HIBERNATE_ENVERS);
}

@BuildStep
void setupLogFilters(BuildProducer<LogCleanupFilterBuildItem> filters) {
filters.produce(new LogCleanupFilterBuildItem("org.hibernate.envers.boot.internal.EnversServiceImpl",
"Envers integration enabled"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.hibernate.envers.deployment;

import java.util.List;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.hibernate.envers.HibernateEnversRecorder;
import io.quarkus.hibernate.orm.deployment.PersistenceUnitDescriptorBuildItem;
import io.quarkus.hibernate.orm.deployment.integration.HibernateOrmIntegrationStaticConfiguredBuildItem;

@BuildSteps(onlyIfNot = HibernateEnversEnabled.class)
public final class HibernateEnversDisabledProcessor {

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
public void disableHibernateEnvers(HibernateEnversRecorder recorder,
List<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptorBuildItems,
BuildProducer<HibernateOrmIntegrationStaticConfiguredBuildItem> integrationProducer) {
for (PersistenceUnitDescriptorBuildItem puDescriptor : persistenceUnitDescriptorBuildItems) {
integrationProducer.produce(
new HibernateOrmIntegrationStaticConfiguredBuildItem(HibernateEnversProcessor.HIBERNATE_ENVERS,
puDescriptor.getPersistenceUnitName())
.setInitListener(recorder.createStaticInitInactiveListener())
// We don't need XML mapping if Envers is disabled
.setXmlMappingRequired(false));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.envers.deployment;

import java.util.function.BooleanSupplier;

import io.quarkus.hibernate.envers.HibernateEnversBuildTimeConfig;

/**
* Supplier that can be used to only run build steps
* if the Hibernate Envers extension is enabled.
*/
public class HibernateEnversEnabled implements BooleanSupplier {

private final HibernateEnversBuildTimeConfig config;

HibernateEnversEnabled(HibernateEnversBuildTimeConfig config) {
this.config = config;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@
import java.util.Arrays;
import java.util.List;

import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.logging.LogCleanupFilterBuildItem;
import io.quarkus.hibernate.envers.HibernateEnversBuildTimeConfig;
import io.quarkus.hibernate.envers.HibernateEnversRecorder;
import io.quarkus.hibernate.orm.deployment.AdditionalJpaModelBuildItem;
import io.quarkus.hibernate.orm.deployment.PersistenceUnitDescriptorBuildItem;
import io.quarkus.hibernate.orm.deployment.integration.HibernateOrmIntegrationStaticConfiguredBuildItem;

@BuildSteps(onlyIf = HibernateEnversEnabled.class)
public final class HibernateEnversProcessor {

private static final String HIBERNATE_ENVERS = "Hibernate Envers";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(Feature.HIBERNATE_ENVERS);
}
static final String HIBERNATE_ENVERS = "Hibernate Envers";

@BuildStep
List<AdditionalJpaModelBuildItem> addJpaModelClasses() {
Expand Down Expand Up @@ -61,10 +55,4 @@ public void applyConfig(HibernateEnversRecorder recorder, HibernateEnversBuildTi
.setXmlMappingRequired(true));
}
}

@BuildStep
void setupLogFilters(BuildProducer<LogCleanupFilterBuildItem> filters) {
filters.produce(new LogCleanupFilterBuildItem("org.hibernate.envers.boot.internal.EnversServiceImpl",
"Envers integration enabled"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.hibernate.orm.envers.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import javax.inject.Inject;
import javax.persistence.metamodel.Bindable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.envers.AuditReaderFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.hibernate.orm.envers.MyAuditedEntity;
import io.quarkus.test.QuarkusUnitTest;

public class ConfigEnabledFalseAndAuditedEntityTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClass(MyAuditedEntity.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-envers.enabled", "false");

@Inject
SessionFactory sessionFactory;

@Test
@SuppressWarnings("rawtypes")
public void test() {
assertThat(sessionFactory.getMetamodel().getEntities())
.extracting(Bindable::getBindableJavaType)
// In particular this should not contain the revision entity
.containsExactly((Class) MyAuditedEntity.class);

try (Session session = sessionFactory.openSession()) {
assertThatThrownBy(() -> AuditReaderFactory.get(session).isEntityClassAudited(MyAuditedEntity.class))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Service is not yet initialized");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@

@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class HibernateEnversBuildTimeConfig {
/**
* Whether Hibernate Envers is enabled <strong>during the build</strong>.
*
* If Hibernate Envers is disabled during the build, all processing related to Hibernate Envers will be skipped,
* and the audit entities will not be added to the Hibernate ORM metamodel
* nor to the database schema that Hibernate ORM generates,
* but it will not be possible to use Hibernate Envers at runtime.
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;

/**
* Enable store_data_at_delete feature.
* Maps to {@link org.hibernate.envers.configuration.EnversSettings#STORE_DATA_AT_DELETE}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.hibernate.boot.Metadata;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.configuration.EnversSettings;

import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationStaticInitListener;
Expand All @@ -14,13 +15,14 @@
public class HibernateEnversRecorder {

public HibernateOrmIntegrationStaticInitListener createStaticInitListener(HibernateEnversBuildTimeConfig buildTimeConfig) {
return new HibernateEnversIntegrationListener(buildTimeConfig);
return new HibernateEnversIntegrationStaticInitListener(buildTimeConfig);
}

private static final class HibernateEnversIntegrationListener implements HibernateOrmIntegrationStaticInitListener {
private static final class HibernateEnversIntegrationStaticInitListener
implements HibernateOrmIntegrationStaticInitListener {
private HibernateEnversBuildTimeConfig buildTimeConfig;

private HibernateEnversIntegrationListener(HibernateEnversBuildTimeConfig buildTimeConfig) {
private HibernateEnversIntegrationStaticInitListener(HibernateEnversBuildTimeConfig buildTimeConfig) {
this.buildTimeConfig = buildTimeConfig;
}

Expand Down Expand Up @@ -81,4 +83,24 @@ public void onMetadataInitialized(Metadata metadata, BootstrapContext bootstrapC
BiConsumer<String, Object> propertyCollector) {
}
}

public HibernateOrmIntegrationStaticInitListener createStaticInitInactiveListener() {
return new HibernateEnversIntegrationStaticInitInactiveListener();
}

private static final class HibernateEnversIntegrationStaticInitInactiveListener
implements HibernateOrmIntegrationStaticInitListener {
private HibernateEnversIntegrationStaticInitInactiveListener() {
}

@Override
public void contributeBootProperties(BiConsumer<String, Object> propertyCollector) {
propertyCollector.accept(EnversService.INTEGRATION_ENABLED, "false");
}

@Override
public void onMetadataInitialized(Metadata metadata, BootstrapContext bootstrapContext,
BiConsumer<String, Object> propertyCollector) {
}
}
}

0 comments on commit 8b7d549

Please sign in to comment.