-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 #27134 from yrodiere/orm-enabled-active
Add .enabled/.active configuration properties to Hibernate ORM, Reactive and Envers extensions
- Loading branch information
Showing
55 changed files
with
1,317 additions
and
370 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...in/java/io/quarkus/hibernate/envers/deployment/HibernateEnversAlwaysEnabledProcessor.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.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")); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...rc/main/java/io/quarkus/hibernate/envers/deployment/HibernateEnversDisabledProcessor.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,60 @@ | ||
package io.quarkus.hibernate.envers.deployment; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
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.HibernateEnversBuildTimeConfig; | ||
import io.quarkus.hibernate.envers.HibernateEnversRecorder; | ||
import io.quarkus.hibernate.orm.deployment.PersistenceUnitDescriptorBuildItem; | ||
import io.quarkus.hibernate.orm.deployment.integration.HibernateOrmIntegrationStaticConfiguredBuildItem; | ||
import io.quarkus.runtime.configuration.ConfigurationException; | ||
|
||
@BuildSteps(onlyIfNot = HibernateEnversEnabled.class) | ||
public final class HibernateEnversDisabledProcessor { | ||
|
||
@BuildStep | ||
@Record(ExecutionTime.STATIC_INIT) | ||
public void disableHibernateEnversStaticInit(HibernateEnversRecorder recorder, | ||
HibernateEnversBuildTimeConfig buildTimeConfig, | ||
List<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptorBuildItems, | ||
BuildProducer<HibernateOrmIntegrationStaticConfiguredBuildItem> integrationProducer) { | ||
checkNoExplicitActiveTrue(buildTimeConfig); | ||
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)); | ||
} | ||
} | ||
|
||
// 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()) { | ||
var config = entry.getValue(); | ||
if (config.active.isPresent() && config.active.get()) { | ||
var puName = entry.getKey(); | ||
String enabledPropertyKey = HibernateEnversBuildTimeConfig.extensionPropertyKey("enabled"); | ||
String activePropertyKey = HibernateEnversBuildTimeConfig.persistenceUnitPropertyKey(puName, "active"); | ||
throw new ConfigurationException( | ||
"Hibernate Envers activated explicitly for persistence unit '" + puName | ||
+ "', but the Hibernate Envers extension was disabled at build time." | ||
+ " If you want Hibernate Envers to be active for this persistence unit, you must set '" | ||
+ enabledPropertyKey | ||
+ "' to 'true' at build time." | ||
+ " If you don't want Hibernate Envers to be active for this persistence unit, you must leave '" | ||
+ activePropertyKey | ||
+ "' unset or set it to 'false'.", | ||
Set.of(enabledPropertyKey, activePropertyKey)); | ||
} | ||
} | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...ployment/src/main/java/io/quarkus/hibernate/envers/deployment/HibernateEnversEnabled.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,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; | ||
} | ||
|
||
} |
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
42 changes: 42 additions & 0 deletions
42
...st/java/io/quarkus/hibernate/orm/envers/config/ConfigActiveFalseAndAuditedEntityTest.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,42 @@ | ||
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 ConfigActiveFalseAndAuditedEntityTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClass(MyAuditedEntity.class)) | ||
.withConfigurationResource("application.properties") | ||
.overrideConfigKey("quarkus.hibernate-envers.active", "false"); | ||
|
||
@Inject | ||
SessionFactory sessionFactory; | ||
|
||
@Test | ||
public void test() { | ||
assertThat(sessionFactory.getMetamodel().getEntities()) | ||
.extracting(Bindable::getBindableJavaType) | ||
// In particular this should not contain the revision entity | ||
.containsExactlyInAnyOrder((Class) MyAuditedEntity.class); | ||
|
||
try (Session session = sessionFactory.openSession()) { | ||
assertThatThrownBy(() -> AuditReaderFactory.get(session).isEntityClassAudited(MyAuditedEntity.class)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Service is not yet initialized"); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...test/java/io/quarkus/hibernate/orm/envers/config/ConfigEnabledFalseAndActiveTrueTest.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,50 @@ | ||
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.runtime.configuration.ConfigurationException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigEnabledFalseAndActiveTrueTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClass(MyAuditedEntity.class)) | ||
.withConfigurationResource("application.properties") | ||
.overrideConfigKey("quarkus.hibernate-envers.enabled", "false") | ||
.overrideConfigKey("quarkus.hibernate-envers.active", "true") | ||
.assertException(throwable -> assertThat(throwable) | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContaining( | ||
"Hibernate Envers activated explicitly for persistence unit '<default>', but the Hibernate Envers extension was disabled at build time", | ||
"If you want Hibernate Envers to be active for this persistence unit, you must set 'quarkus.hibernate-envers.enabled' to 'true' at build time", | ||
"If you don't want Hibernate Envers to be active for this persistence unit, you must leave 'quarkus.hibernate-envers.active' unset or set it to 'false'")); | ||
|
||
@Inject | ||
SessionFactory sessionFactory; | ||
|
||
@Test | ||
public void test() { | ||
assertThat(sessionFactory.getMetamodel().getEntities()) | ||
.extracting(Bindable::getBindableJavaType) | ||
// In particular this should not contain the revision entity | ||
.containsExactlyInAnyOrder((Class) MyAuditedEntity.class); | ||
|
||
try (Session session = sessionFactory.openSession()) { | ||
assertThatThrownBy(() -> AuditReaderFactory.get(session).isEntityClassAudited(MyAuditedEntity.class)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Service is not yet initialized"); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...t/java/io/quarkus/hibernate/orm/envers/config/ConfigEnabledFalseAndAuditedEntityTest.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,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 | ||
.containsExactlyInAnyOrder((Class) MyAuditedEntity.class); | ||
|
||
try (Session session = sessionFactory.openSession()) { | ||
assertThatThrownBy(() -> AuditReaderFactory.get(session).isEntityClassAudited(MyAuditedEntity.class)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Service is not yet initialized"); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.