Skip to content

Commit

Permalink
Merge pull request #26840 from yrodiere/hsearch-enabled-started
Browse files Browse the repository at this point in the history
Rename quarkus.hibernate-search-orm.enabled to quarkus.hibernate-search-orm.active and repurpose quarkus.hibernate-search-orm.enabled to disable Hibernate Search at build time
  • Loading branch information
gsmet authored Jul 26, 2022
2 parents be06973 + c5be9a4 commit 7fac557
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.quarkus.hibernate.search.orm.coordination.outboxpolling.runtime.HibernateSearchOutboxPollingRecorder;
import io.quarkus.hibernate.search.orm.coordination.outboxpolling.runtime.HibernateSearchOutboxPollingRuntimeConfig;
import io.quarkus.hibernate.search.orm.elasticsearch.deployment.HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem;
import io.quarkus.hibernate.search.orm.elasticsearch.deployment.HibernateSearchEnabled;
import io.quarkus.hibernate.search.orm.elasticsearch.deployment.HibernateSearchIntegrationRuntimeConfiguredBuildItem;
import io.quarkus.hibernate.search.orm.elasticsearch.deployment.HibernateSearchIntegrationStaticConfiguredBuildItem;
import io.quarkus.hibernate.search.orm.elasticsearch.runtime.HibernateSearchElasticsearchBuildTimeConfigPersistenceUnit;
Expand All @@ -24,11 +25,7 @@ class HibernateSearchOutboxPollingProcessor {

private static final String HIBERNATE_SEARCH_ORM_COORDINATION_OUTBOX_POLLING = "Hibernate Search ORM - Coordination - Outbox polling";

@BuildStep
void registerIndexedClasses() {
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
void registerInternalModel(BuildProducer<AdditionalIndexedClassesBuildItem> additionalIndexedClasses,
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses,
BuildProducer<AdditionalJpaModelBuildItem> additionalJpaModel) {
Expand All @@ -41,7 +38,7 @@ void registerInternalModel(BuildProducer<AdditionalIndexedClassesBuildItem> addi
}
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
@Record(ExecutionTime.STATIC_INIT)
void setStaticConfig(HibernateSearchOutboxPollingRecorder recorder,
List<HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem> configuredPersistenceUnits,
Expand All @@ -59,7 +56,7 @@ void setStaticConfig(HibernateSearchOutboxPollingRecorder recorder,
}
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
@Record(ExecutionTime.RUNTIME_INIT)
void setRuntimeConfig(HibernateSearchOutboxPollingRecorder recorder,
HibernateSearchOutboxPollingRuntimeConfig runtimeConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class HibernateSearchElasticsearchCdiProcessor {

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
void generateSearchBeans(HibernateSearchElasticsearchRecorder recorder,
HibernateSearchElasticsearchRuntimeConfig runtimeConfig,
List<HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem> configuredPersistenceUnits,
Expand Down Expand Up @@ -67,7 +67,7 @@ private static <T> SyntheticBeanBuildItem createSyntheticBean(String persistence
return configurator.done();
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
void registerAnnotations(BuildProducer<AdditionalBeanBuildItem> additionalBeans,
BuildProducer<BeanDefiningAnnotationBuildItem> beanDefiningAnnotations) {
// add the @SearchExtension class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,47 @@ class HibernateSearchElasticsearchProcessor {

HibernateSearchElasticsearchBuildTimeConfig buildTimeConfig;

// Note this is necessary even if Hibernate Search is disabled
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
NativeImageFeatureBuildItem nativeImageFeature() {
return new NativeImageFeatureBuildItem(DisableLoggingFeature.class);
}

// Note this is necessary even if Hibernate Search is disabled
@BuildStep
void setupLogFilters(BuildProducer<LogCleanupFilterBuildItem> filters) {
// if the category changes, please also update DisableLoggingFeature in the runtime module
filters.produce(new LogCleanupFilterBuildItem(
"org.hibernate.search.mapper.orm.bootstrap.impl.HibernateSearchPreIntegrationService", "HSEARCH000034"));
}

@BuildStep
@BuildStep(onlyIfNot = HibernateSearchEnabled.class)
@Record(ExecutionTime.STATIC_INIT)
public void disableHibernateSearchStaticInit(HibernateSearchElasticsearchRecorder recorder,
List<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptorBuildItems,
BuildProducer<HibernateOrmIntegrationStaticConfiguredBuildItem> staticIntegrations) {
for (PersistenceUnitDescriptorBuildItem puDescriptor : persistenceUnitDescriptorBuildItems) {
String puName = puDescriptor.getPersistenceUnitName();
staticIntegrations.produce(new HibernateOrmIntegrationStaticConfiguredBuildItem(HIBERNATE_SEARCH_ELASTICSEARCH,
puName).setInitListener(recorder.createStaticInitInactiveListener()));
}
}

@BuildStep(onlyIfNot = HibernateSearchEnabled.class)
@Record(ExecutionTime.RUNTIME_INIT)
public void disableHibernateSearchRuntimeInit(HibernateSearchElasticsearchRecorder recorder,
HibernateSearchElasticsearchRuntimeConfig runtimeConfig,
List<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptorBuildItems,
BuildProducer<HibernateOrmIntegrationRuntimeConfiguredBuildItem> runtimeIntegrations) {
recorder.checkNoExplicitActiveTrue(runtimeConfig);
for (PersistenceUnitDescriptorBuildItem puDescriptor : persistenceUnitDescriptorBuildItems) {
String puName = puDescriptor.getPersistenceUnitName();
runtimeIntegrations.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem(HIBERNATE_SEARCH_ELASTICSEARCH,
puName).setInitListener(recorder.createRuntimeInitInactiveListener()));
}
}

@BuildStep(onlyIf = HibernateSearchEnabled.class)
@Record(ExecutionTime.STATIC_INIT)
public void build(HibernateSearchElasticsearchRecorder recorder,
CombinedIndexBuildItem combinedIndexBuildItem,
Expand Down Expand Up @@ -156,11 +184,12 @@ private void buildForPersistenceUnit(HibernateSearchElasticsearchRecorder record
if (indexedAnnotationsForPU.isEmpty()) {
// we don't have any indexed entity, we can disable Hibernate Search
staticIntegrations.produce(new HibernateOrmIntegrationStaticConfiguredBuildItem(HIBERNATE_SEARCH_ELASTICSEARCH,
persistenceUnitName).setInitListener(recorder.createDisabledStaticInitListener()));
persistenceUnitName).setInitListener(recorder.createStaticInitInactiveListener()));
// we need a runtime listener even when Hibernate Search is disabled,
// just to let Hibernate Search boot up until the point where it checks whether it's enabled or not
runtimeIntegrations.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem(HIBERNATE_SEARCH_ELASTICSEARCH,
persistenceUnitName).setInitListener(recorder.createDisabledRuntimeInitListener()));
persistenceUnitName)
.setInitListener(recorder.createRuntimeInitInactiveListener()));
return;
}

Expand All @@ -176,7 +205,7 @@ private void buildForPersistenceUnit(HibernateSearchElasticsearchRecorder record
backendNamesForIndexedEntities, backendAndIndexNamesForSearchExtensions));
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
void registerBeans(List<HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem> searchEnabledPUs,
BuildProducer<UnremovableBeanBuildItem> unremovableBean) {
if (searchEnabledPUs.isEmpty()) {
Expand All @@ -189,7 +218,7 @@ void registerBeans(List<HibernateSearchElasticsearchPersistenceUnitConfiguredBui
ElasticsearchAnalysisConfigurer.class, IndexLayoutStrategy.class));
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
@Record(ExecutionTime.STATIC_INIT)
void setStaticConfig(RecorderContext recorderContext, HibernateSearchElasticsearchRecorder recorder,
List<HibernateSearchIntegrationStaticConfiguredBuildItem> integrationStaticConfigBuildItems,
Expand Down Expand Up @@ -224,7 +253,7 @@ void setStaticConfig(RecorderContext recorderContext, HibernateSearchElasticsear
}
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
@Record(ExecutionTime.RUNTIME_INIT)
void setRuntimeConfig(HibernateSearchElasticsearchRecorder recorder,
HibernateSearchElasticsearchRuntimeConfig runtimeConfig,
Expand All @@ -251,7 +280,7 @@ void setRuntimeConfig(HibernateSearchElasticsearchRecorder recorder,
}
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
public void processPersistenceUnitBuildTimeConfig(
List<HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem> configuredPersistenceUnits,
ApplicationArchivesBuildItem applicationArchivesBuildItem,
Expand Down Expand Up @@ -358,7 +387,7 @@ private void registerReflectionForGson(BuildProducer<ReflectiveClassBuildItem> r
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, reflectiveClasses));
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
DevservicesElasticsearchBuildItem devServices(HibernateSearchElasticsearchBuildTimeConfig buildTimeConfig) {
if (buildTimeConfig.defaultPersistenceUnit != null && buildTimeConfig.defaultPersistenceUnit.defaultBackend != null
// If the version is not set, the default backend is not in use.
Expand All @@ -376,7 +405,7 @@ DevservicesElasticsearchBuildItem devServices(HibernateSearchElasticsearchBuildT
}
}

@BuildStep(onlyIfNot = IsNormal.class)
@BuildStep(onlyIf = HibernateSearchEnabled.class, onlyIfNot = IsNormal.class)
void devServicesDropAndCreateAndDropByDefault(
List<HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem> configuredPersistenceUnits,
BuildProducer<DevServicesAdditionalConfigBuildItem> devServicesAdditionalConfigProducer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.search.orm.elasticsearch.deployment;

import java.util.function.BooleanSupplier;

import io.quarkus.hibernate.search.orm.elasticsearch.runtime.HibernateSearchElasticsearchBuildTimeConfig;

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

private final HibernateSearchElasticsearchBuildTimeConfig config;

HibernateSearchEnabled(HibernateSearchElasticsearchBuildTimeConfig config) {
this.config = config;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
import io.quarkus.devconsole.spi.DevConsoleRouteBuildItem;
import io.quarkus.devconsole.spi.DevConsoleRuntimeTemplateInfoBuildItem;
import io.quarkus.hibernate.search.orm.elasticsearch.deployment.HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem;
import io.quarkus.hibernate.search.orm.elasticsearch.deployment.HibernateSearchEnabled;
import io.quarkus.hibernate.search.orm.elasticsearch.runtime.HibernateSearchElasticsearchRuntimeConfig;
import io.quarkus.hibernate.search.orm.elasticsearch.runtime.devconsole.HibernateSearchDevConsoleRecorder;

public class HibernateSearchElasticsearchDevConsoleProcessor {

@BuildStep(onlyIf = IsDevelopment.class)
@BuildStep(onlyIf = { HibernateSearchEnabled.class, IsDevelopment.class })
@Record(RUNTIME_INIT)
public DevConsoleRuntimeTemplateInfoBuildItem collectBeanInfo(HibernateSearchDevConsoleRecorder recorder,
HibernateSearchElasticsearchRuntimeConfig runtimeConfig,
Expand All @@ -32,7 +33,7 @@ public DevConsoleRuntimeTemplateInfoBuildItem collectBeanInfo(HibernateSearchDev
recorder.infoSupplier(runtimeConfig, persistenceUnitNames), this.getClass(), curateOutcomeBuildItem);
}

@BuildStep
@BuildStep(onlyIf = HibernateSearchEnabled.class)
@Record(value = STATIC_INIT, optional = true)
DevConsoleRouteBuildItem invokeEndpoint(HibernateSearchDevConsoleRecorder recorder) {
return new DevConsoleRouteBuildItem("entity-types", "POST", recorder.indexEntity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
import io.quarkus.arc.Arc;
import io.quarkus.test.QuarkusUnitTest;

public class ConfigDisabledAndIndexedEntityTest {
public class ConfigActiveFalseAndIndexedEntityTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-search-orm.enabled", "false");
.overrideConfigKey("quarkus.hibernate-search-orm.active", "false");

@Inject
SessionFactory sessionFactory;

@Test
public void testDisabled() {
public void test() {
assertThatThrownBy(() -> Arc.container().instance(SearchMapping.class).get())
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining(
"Cannot retrieve the SearchMapping: Hibernate Search was disabled through configuration properties");
"Cannot retrieve the SearchMapping: Hibernate Search was deactivated through configuration properties");

assertThatThrownBy(() -> Search.mapping(sessionFactory))
.isInstanceOf(SearchException.class)
Expand All @@ -43,7 +43,7 @@ public void testDisabled() {
assertThatThrownBy(() -> Arc.container().instance(SearchSession.class).get())
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining(
"Cannot retrieve the SearchSession: Hibernate Search was disabled through configuration properties");
"Cannot retrieve the SearchSession: Hibernate Search was deactivated through configuration properties");

try (Session session = sessionFactory.openSession()) {
assertThatThrownBy(() -> Search.session(session).search(IndexedEntity.class))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.hibernate.search.orm.elasticsearch.test.configuration;

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

import javax.inject.Inject;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.hibernate.search.util.common.SearchException;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.test.QuarkusUnitTest;

public class ConfigEnabledFalseAndActiveTrueTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-search-orm.enabled", "false")
.overrideConfigKey("quarkus.hibernate-search-orm.active", "true")
.assertException(throwable -> assertThat(throwable)
.isInstanceOf(ConfigurationException.class)
.hasMessageContaining(
"Hibernate Search activated explicitly, but Hibernate Search was disabled at build time",
"If you want Hibernate Search to be active at runtime, you must set 'quarkus.hibernate-search-orm.enabled' to 'true' at build time",
"If you don't want Hibernate Search to be active at runtime, you must leave 'quarkus.hibernate-search-orm.active' unset or set it to 'false'"));

@Inject
SessionFactory sessionFactory;

@Test
public void test() {
assertThatThrownBy(() -> Arc.container().instance(SearchMapping.class).get())
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining(
"Cannot retrieve the SearchMapping: Hibernate Search was disabled through configuration properties");

assertThatThrownBy(() -> Search.mapping(sessionFactory))
.isInstanceOf(SearchException.class)
.hasMessageContaining("Hibernate Search was not initialized.");

assertThatThrownBy(() -> Arc.container().instance(SearchSession.class).get())
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining(
"Cannot retrieve the SearchSession: Hibernate Search was disabled through configuration properties");

try (Session session = sessionFactory.openSession()) {
assertThatThrownBy(() -> Search.session(session).search(IndexedEntity.class))
.isInstanceOf(SearchException.class)
.hasMessageContaining("Hibernate Search was not initialized.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.quarkus.hibernate.search.orm.elasticsearch.test.configuration;

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

import javax.inject.Inject;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.hibernate.search.util.common.SearchException;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.test.QuarkusUnitTest;

public class ConfigEnabledFalseAndIndexedEntityTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-search-orm.enabled", "false");

@Inject
SessionFactory sessionFactory;

@Test
public void test() {
assertThat(Arc.container().instance(SearchMapping.class).get())
.isNull();

assertThatThrownBy(() -> Search.mapping(sessionFactory))
.isInstanceOf(SearchException.class)
.hasMessageContaining("Hibernate Search was not initialized.");

assertThat(Arc.container().instance(SearchSession.class).get())
.isNull();

try (Session session = sessionFactory.openSession()) {
assertThatThrownBy(() -> Search.session(session).search(IndexedEntity.class))
.isInstanceOf(SearchException.class)
.hasMessageContaining("Hibernate Search was not initialized.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
@ConfigRoot(name = "hibernate-search-orm", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class HibernateSearchElasticsearchBuildTimeConfig {

/**
* Whether Hibernate Search is enabled <strong>during the build</strong>.
*
* If Hibernate Search is disabled during the build, all processing related to Hibernate Search will be skipped,
* but it will not be possible to activate Hibernate Search at runtime:
* `quarkus.hibernate-search-orm.active` will default to `false` and setting it to `true` will lead to an error.
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;

/**
* Configuration for the default persistence unit.
*/
Expand Down
Loading

0 comments on commit 7fac557

Please sign in to comment.