diff --git a/extensions/hibernate-search-orm-coordination-outbox-polling/deployment/src/main/java/io/quarkus/hibernate/search/orm/coordination/outboxpolling/deployment/HibernateSearchOutboxPollingProcessor.java b/extensions/hibernate-search-orm-coordination-outbox-polling/deployment/src/main/java/io/quarkus/hibernate/search/orm/coordination/outboxpolling/deployment/HibernateSearchOutboxPollingProcessor.java index a7bd284e1f49af..8fcea1ff147212 100644 --- a/extensions/hibernate-search-orm-coordination-outbox-polling/deployment/src/main/java/io/quarkus/hibernate/search/orm/coordination/outboxpolling/deployment/HibernateSearchOutboxPollingProcessor.java +++ b/extensions/hibernate-search-orm-coordination-outbox-polling/deployment/src/main/java/io/quarkus/hibernate/search/orm/coordination/outboxpolling/deployment/HibernateSearchOutboxPollingProcessor.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.Optional; +import io.quarkus.hibernate.search.orm.elasticsearch.deployment.HibernateSearchEnabled; import org.hibernate.search.mapper.orm.coordination.outboxpolling.cfg.HibernateOrmMapperOutboxPollingSettings; import org.hibernate.search.mapper.orm.coordination.outboxpolling.mapping.spi.HibernateOrmMapperOutboxPollingClasses; @@ -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 additionalIndexedClasses, BuildProducer reflectiveClasses, BuildProducer additionalJpaModel) { @@ -41,7 +38,7 @@ void registerInternalModel(BuildProducer addi } } - @BuildStep + @BuildStep(onlyIf = HibernateSearchEnabled.class) @Record(ExecutionTime.STATIC_INIT) void setStaticConfig(HibernateSearchOutboxPollingRecorder recorder, List configuredPersistenceUnits, @@ -59,7 +56,7 @@ void setStaticConfig(HibernateSearchOutboxPollingRecorder recorder, } } - @BuildStep + @BuildStep(onlyIf = HibernateSearchEnabled.class) @Record(ExecutionTime.RUNTIME_INIT) void setRuntimeConfig(HibernateSearchOutboxPollingRecorder recorder, HibernateSearchOutboxPollingRuntimeConfig runtimeConfig, diff --git a/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchElasticsearchCdiProcessor.java b/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchElasticsearchCdiProcessor.java index 5e952a91301f63..ae798228c096e7 100644 --- a/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchElasticsearchCdiProcessor.java +++ b/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchElasticsearchCdiProcessor.java @@ -25,7 +25,7 @@ public class HibernateSearchElasticsearchCdiProcessor { @Record(ExecutionTime.RUNTIME_INIT) - @BuildStep + @BuildStep(onlyIf = HibernateSearchEnabled.class) void generateSearchBeans(HibernateSearchElasticsearchRecorder recorder, HibernateSearchElasticsearchRuntimeConfig runtimeConfig, List configuredPersistenceUnits, @@ -67,7 +67,7 @@ private static SyntheticBeanBuildItem createSyntheticBean(String persistence return configurator.done(); } - @BuildStep + @BuildStep(onlyIf = HibernateSearchEnabled.class) void registerAnnotations(BuildProducer additionalBeans, BuildProducer beanDefiningAnnotations) { // add the @SearchExtension class diff --git a/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchElasticsearchProcessor.java b/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchElasticsearchProcessor.java index b364c4b8c149a2..1970a170db6177 100644 --- a/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchElasticsearchProcessor.java +++ b/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchElasticsearchProcessor.java @@ -74,11 +74,13 @@ 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 filters) { // if the category changes, please also update DisableLoggingFeature in the runtime module @@ -86,7 +88,33 @@ void setupLogFilters(BuildProducer filters) { "org.hibernate.search.mapper.orm.bootstrap.impl.HibernateSearchPreIntegrationService", "HSEARCH000034")); } - @BuildStep + @BuildStep(onlyIfNot = HibernateSearchEnabled.class) + @Record(ExecutionTime.STATIC_INIT) + public void disableHibernateSearchStaticInit(HibernateSearchElasticsearchRecorder recorder, + List persistenceUnitDescriptorBuildItems, + BuildProducer staticIntegrations) { + for (PersistenceUnitDescriptorBuildItem puDescriptor : persistenceUnitDescriptorBuildItems) { + String puName = puDescriptor.getPersistenceUnitName(); + staticIntegrations.produce(new HibernateOrmIntegrationStaticConfiguredBuildItem(HIBERNATE_SEARCH_ELASTICSEARCH, + puName).setInitListener(recorder.createStartupDisabledStaticInitListener())); + } + } + + @BuildStep(onlyIfNot = HibernateSearchEnabled.class) + @Record(ExecutionTime.RUNTIME_INIT) + public void disableHibernateSearchRuntimeInit(HibernateSearchElasticsearchRecorder recorder, + HibernateSearchElasticsearchRuntimeConfig runtimeConfig, + List persistenceUnitDescriptorBuildItems, + BuildProducer runtimeIntegrations) { + recorder.checkNoExplicitStartTrue(runtimeConfig); + for (PersistenceUnitDescriptorBuildItem puDescriptor : persistenceUnitDescriptorBuildItems) { + String puName = puDescriptor.getPersistenceUnitName(); + runtimeIntegrations.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem(HIBERNATE_SEARCH_ELASTICSEARCH, + puName).setInitListener(recorder.createStartupDisabledRuntimeInitListener())); + } + } + + @BuildStep(onlyIf = HibernateSearchEnabled.class) @Record(ExecutionTime.STATIC_INIT) public void build(HibernateSearchElasticsearchRecorder recorder, CombinedIndexBuildItem combinedIndexBuildItem, @@ -160,7 +188,8 @@ private void buildForPersistenceUnit(HibernateSearchElasticsearchRecorder record // 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.createStartupDisabledRuntimeInitListener())); + persistenceUnitName) + .setInitListener(recorder.createStartupDisabledRuntimeInitListener())); return; } @@ -176,7 +205,7 @@ private void buildForPersistenceUnit(HibernateSearchElasticsearchRecorder record backendNamesForIndexedEntities, backendAndIndexNamesForSearchExtensions)); } - @BuildStep + @BuildStep(onlyIf = HibernateSearchEnabled.class) void registerBeans(List searchEnabledPUs, BuildProducer unremovableBean) { if (searchEnabledPUs.isEmpty()) { @@ -189,7 +218,7 @@ void registerBeans(List integrationStaticConfigBuildItems, @@ -224,7 +253,7 @@ void setStaticConfig(RecorderContext recorderContext, HibernateSearchElasticsear } } - @BuildStep + @BuildStep(onlyIf = HibernateSearchEnabled.class) @Record(ExecutionTime.RUNTIME_INIT) void setRuntimeConfig(HibernateSearchElasticsearchRecorder recorder, HibernateSearchElasticsearchRuntimeConfig runtimeConfig, @@ -251,7 +280,7 @@ void setRuntimeConfig(HibernateSearchElasticsearchRecorder recorder, } } - @BuildStep + @BuildStep(onlyIf = HibernateSearchEnabled.class) public void processPersistenceUnitBuildTimeConfig( List configuredPersistenceUnits, ApplicationArchivesBuildItem applicationArchivesBuildItem, @@ -358,7 +387,7 @@ private void registerReflectionForGson(BuildProducer 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. @@ -376,7 +405,7 @@ DevservicesElasticsearchBuildItem devServices(HibernateSearchElasticsearchBuildT } } - @BuildStep(onlyIfNot = IsNormal.class) + @BuildStep(onlyIf = HibernateSearchEnabled.class, onlyIfNot = IsNormal.class) void devServicesDropAndCreateAndDropByDefault( List configuredPersistenceUnits, BuildProducer devServicesAdditionalConfigProducer) { diff --git a/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchEnabled.java b/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchEnabled.java new file mode 100644 index 00000000000000..a979855b48bc40 --- /dev/null +++ b/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/HibernateSearchEnabled.java @@ -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; + } + +} diff --git a/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/devconsole/HibernateSearchElasticsearchDevConsoleProcessor.java b/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/devconsole/HibernateSearchElasticsearchDevConsoleProcessor.java index 5540c3fb62e6d9..fd6292d15b67f6 100644 --- a/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/devconsole/HibernateSearchElasticsearchDevConsoleProcessor.java +++ b/extensions/hibernate-search-orm-elasticsearch/deployment/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/deployment/devconsole/HibernateSearchElasticsearchDevConsoleProcessor.java @@ -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, @@ -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()); diff --git a/extensions/hibernate-search-orm-elasticsearch/deployment/src/test/java/io/quarkus/hibernate/search/orm/elasticsearch/test/configuration/ConfigEnabledFalseAndIndexedEntityTest.java b/extensions/hibernate-search-orm-elasticsearch/deployment/src/test/java/io/quarkus/hibernate/search/orm/elasticsearch/test/configuration/ConfigEnabledFalseAndIndexedEntityTest.java new file mode 100644 index 00000000000000..2cabf59fb13a99 --- /dev/null +++ b/extensions/hibernate-search-orm-elasticsearch/deployment/src/test/java/io/quarkus/hibernate/search/orm/elasticsearch/test/configuration/ConfigEnabledFalseAndIndexedEntityTest.java @@ -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 enabledFalse() { + 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."); + } + } +} diff --git a/extensions/hibernate-search-orm-elasticsearch/deployment/src/test/java/io/quarkus/hibernate/search/orm/elasticsearch/test/configuration/ConfigEnabledFalseAndStartedTrueTest.java b/extensions/hibernate-search-orm-elasticsearch/deployment/src/test/java/io/quarkus/hibernate/search/orm/elasticsearch/test/configuration/ConfigEnabledFalseAndStartedTrueTest.java new file mode 100644 index 00000000000000..72a3a2a61e476e --- /dev/null +++ b/extensions/hibernate-search-orm-elasticsearch/deployment/src/test/java/io/quarkus/hibernate/search/orm/elasticsearch/test/configuration/ConfigEnabledFalseAndStartedTrueTest.java @@ -0,0 +1,62 @@ +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 ConfigEnabledFalseAndStartedTrueTest { + + @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.started", "true") + .assertException(throwable -> assertThat(throwable) + .isInstanceOf(ConfigurationException.class) + .hasMessageContaining("Hibernate Search startup requested, but Hibernate Search was disabled at build time", + "If you want to start Hibernate Search, you must set 'quarkus.hibernate-search-orm.enabled' to 'true' at build time", + "If you don't want to start Hibernate Search, you must leave 'quarkus.hibernate-search-orm.started' unset or set it to 'false'")); + + @Inject + SessionFactory sessionFactory; + + @Test + public void enabledFalseAndStartedTrue() { + 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."); + } + } +} diff --git a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchBuildTimeConfig.java b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchBuildTimeConfig.java index 621d075ff2b4a7..371ee397968979 100644 --- a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchBuildTimeConfig.java +++ b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchBuildTimeConfig.java @@ -13,6 +13,18 @@ @ConfigRoot(name = "hibernate-search-orm", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED) public class HibernateSearchElasticsearchBuildTimeConfig { + /** + * Whether Hibernate Search is enabled during the build. + * + * If Hibernate Search is disabled during the build, all processing related to Hibernate Search will be skipped, + * but it will not be possible to start Hibernate Search at runtime: + * `quarkus.hibernate-search-orm.started` 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. */ diff --git a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRecorder.java b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRecorder.java index 8d2e72eb4424c9..0d6204745f4aa3 100644 --- a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRecorder.java +++ b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRecorder.java @@ -48,6 +48,7 @@ import io.quarkus.hibernate.search.orm.elasticsearch.runtime.HibernateSearchElasticsearchRuntimeConfigPersistenceUnit.ElasticsearchIndexRuntimeConfig; import io.quarkus.hibernate.search.orm.elasticsearch.runtime.bean.HibernateSearchBeanUtil; import io.quarkus.runtime.annotations.Recorder; +import io.quarkus.runtime.configuration.ConfigurationException; @Recorder public class HibernateSearchElasticsearchRecorder { @@ -74,6 +75,24 @@ public HibernateOrmIntegrationRuntimeInitListener createRuntimeInitListener( backendAndIndexNamesForSearchExtensions, integrationRuntimeInitListeners); } + public void checkNoExplicitStartTrue(HibernateSearchElasticsearchRuntimeConfig runtimeConfig) { + for (var entry : runtimeConfig.getAllPersistenceUnitConfigsAsMap().entrySet()) { + var config = entry.getValue(); + if (config.started.orElse(false)) { + var puName = entry.getKey(); + String enabledPropertyKey = HibernateSearchElasticsearchRuntimeConfig.extensionPropertyKey("enabled"); + String startedPropertyKey = HibernateSearchElasticsearchRuntimeConfig.mapperPropertyKey(puName, "started"); + throw new ConfigurationException( + "Hibernate Search startup requested, but Hibernate Search was disabled at build time." + + " If you want to start Hibernate Search, you must set '" + enabledPropertyKey + + "' to 'true' at build time." + + " If you don't want to start Hibernate Search, you must leave '" + startedPropertyKey + + "' unset or set it to 'false'.", + Set.of(enabledPropertyKey, startedPropertyKey)); + } + } + } + public HibernateOrmIntegrationRuntimeInitListener createStartupDisabledRuntimeInitListener() { return new HibernateSearchIntegrationRuntimeInitStartupDisabledListener(); } @@ -83,9 +102,9 @@ public Supplier searchMappingSupplier(HibernateSearchElasticsearc return new Supplier() { @Override public SearchMapping get() { - HibernateSearchElasticsearchRuntimeConfigPersistenceUnit config = runtimeConfig + HibernateSearchElasticsearchRuntimeConfigPersistenceUnit puRuntimeConfig = runtimeConfig .getAllPersistenceUnitConfigsAsMap().get(persistenceUnitName); - if (config != null && !config.started) { + if (puRuntimeConfig != null && !puRuntimeConfig.started.orElse(true)) { throw new IllegalStateException( "Cannot retrieve the SearchMapping: Hibernate Search startup was disabled through configuration properties"); } @@ -106,9 +125,9 @@ public Supplier searchSessionSupplier(HibernateSearchElasticsearc return new Supplier() { @Override public SearchSession get() { - HibernateSearchElasticsearchRuntimeConfigPersistenceUnit config = runtimeConfig + HibernateSearchElasticsearchRuntimeConfigPersistenceUnit puRuntimeConfig = runtimeConfig .getAllPersistenceUnitConfigsAsMap().get(persistenceUnitName); - if (config != null && !config.started) { + if (puRuntimeConfig != null && !puRuntimeConfig.started.orElse(true)) { throw new IllegalStateException( "Cannot retrieve the SearchSession: Hibernate Search startup was disabled through configuration properties"); } @@ -303,7 +322,7 @@ private HibernateSearchIntegrationRuntimeInitListener(String persistenceUnitName @Override public void contributeRuntimeProperties(BiConsumer propertyCollector) { if (runtimeConfig != null) { - if (!runtimeConfig.started) { + if (!runtimeConfig.started.orElse(true)) { addConfig(propertyCollector, HibernateOrmMapperSettings.ENABLED, false); // Do not process other properties: Hibernate Search is disabled anyway. return; diff --git a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRuntimeConfig.java b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRuntimeConfig.java index 83f3a9f5c8988e..361c6eb7092f59 100644 --- a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRuntimeConfig.java +++ b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRuntimeConfig.java @@ -41,6 +41,12 @@ public static String elasticsearchVersionPropertyKey(String persistenceUnitName, return backendPropertyKey(persistenceUnitName, backendName, null, "version"); } + public static String extensionPropertyKey(String radical) { + StringBuilder keyBuilder = new StringBuilder("quarkus.hibernate-search-orm."); + keyBuilder.append(radical); + return keyBuilder.toString(); + } + public static String mapperPropertyKey(String persistenceUnitName, String radical) { StringBuilder keyBuilder = new StringBuilder("quarkus.hibernate-search-orm."); if (!PersistenceUnitUtil.isDefaultPersistenceUnit(persistenceUnitName)) { diff --git a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRuntimeConfigPersistenceUnit.java b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRuntimeConfigPersistenceUnit.java index d6a4ecaeae16fa..4c9eee61ae352b 100644 --- a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRuntimeConfigPersistenceUnit.java +++ b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/HibernateSearchElasticsearchRuntimeConfigPersistenceUnit.java @@ -24,12 +24,17 @@ public class HibernateSearchElasticsearchRuntimeConfigPersistenceUnit { /** * Whether Hibernate Search should be started along with the application. - *

+ * * If Hibernate Search is not started, it won't index Hibernate ORM entities, * and accessing the SearchMapping/SearchSession for search or other operation will not be possible. + * + * Note that if Hibernate Search is disabled (i.e. `quarkus.hibernate-search-orm.enabled` is set to `false`), + * it won't be started, and setting this property to `true` will fail. + * + * @asciidoclet */ - @ConfigItem(defaultValue = "true") - public boolean started; + @ConfigItem(defaultValueDocumentation = "`true` if Hibernate Search is enabled; `false` otherwise") + public Optional started; /** * Default backend diff --git a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/devconsole/HibernateSearchSupplier.java b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/devconsole/HibernateSearchSupplier.java index 33c2dace12b8e1..b663384fef6ec8 100644 --- a/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/devconsole/HibernateSearchSupplier.java +++ b/extensions/hibernate-search-orm-elasticsearch/runtime/src/main/java/io/quarkus/hibernate/search/orm/elasticsearch/runtime/devconsole/HibernateSearchSupplier.java @@ -53,7 +53,7 @@ public IndexedPersistenceUnits get() { } private boolean shouldStart() { - return runtimeConfig.defaultPersistenceUnit.started; + return runtimeConfig.defaultPersistenceUnit.started.orElse(true); } public static Map searchMapping(Set persistenceUnitNames) {