|
| 1 | +package io.quarkus.hibernate.search.backend.elasticsearch.common.deployment; |
| 2 | + |
| 3 | +import java.nio.file.Files; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.util.LinkedHashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map.Entry; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +import org.hibernate.search.backend.elasticsearch.analysis.ElasticsearchAnalysisConfigurer; |
| 12 | +import org.hibernate.search.backend.elasticsearch.gson.spi.GsonClasses; |
| 13 | +import org.hibernate.search.backend.elasticsearch.index.layout.IndexLayoutStrategy; |
| 14 | + |
| 15 | +import io.quarkus.arc.deployment.UnremovableBeanBuildItem; |
| 16 | +import io.quarkus.deployment.annotations.BuildProducer; |
| 17 | +import io.quarkus.deployment.annotations.BuildStep; |
| 18 | +import io.quarkus.deployment.annotations.BuildSteps; |
| 19 | +import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem; |
| 20 | +import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem; |
| 21 | +import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem; |
| 22 | +import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; |
| 23 | +import io.quarkus.hibernate.search.backend.elasticsearch.common.runtime.HibernateSearchBackendElasticsearchBuildTimeConfig; |
| 24 | +import io.quarkus.hibernate.search.backend.elasticsearch.common.runtime.MapperContext; |
| 25 | +import io.quarkus.runtime.configuration.ConfigurationException; |
| 26 | + |
| 27 | +@BuildSteps |
| 28 | +class HibernateSearchBackendElasticsearchProcessor { |
| 29 | + |
| 30 | + @BuildStep |
| 31 | + void registerBeans(List<HibernateSearchBackendElasticsearchEnabledBuildItem> enabled, |
| 32 | + BuildProducer<UnremovableBeanBuildItem> unremovableBean) { |
| 33 | + if (enabled.isEmpty()) { |
| 34 | + return; |
| 35 | + } |
| 36 | + // Some user-injectable beans are retrieved programmatically and shouldn't be removed |
| 37 | + unremovableBean.produce(UnremovableBeanBuildItem.beanTypes(ElasticsearchAnalysisConfigurer.class, |
| 38 | + IndexLayoutStrategy.class)); |
| 39 | + } |
| 40 | + |
| 41 | + @BuildStep |
| 42 | + void registerReflectionForGson(List<HibernateSearchBackendElasticsearchEnabledBuildItem> enabled, |
| 43 | + BuildProducer<ReflectiveClassBuildItem> reflectiveClass) { |
| 44 | + if (enabled.isEmpty()) { |
| 45 | + return; |
| 46 | + } |
| 47 | + String[] reflectiveClasses = GsonClasses.typesRequiringReflection().toArray(String[]::new); |
| 48 | + reflectiveClass.produce(ReflectiveClassBuildItem.builder(reflectiveClasses) |
| 49 | + .reason(getClass().getName()) |
| 50 | + .methods().fields().build()); |
| 51 | + } |
| 52 | + |
| 53 | + @BuildStep |
| 54 | + void processBuildTimeConfig(List<HibernateSearchBackendElasticsearchEnabledBuildItem> enabled, |
| 55 | + ApplicationArchivesBuildItem applicationArchivesBuildItem, |
| 56 | + BuildProducer<NativeImageResourceBuildItem> nativeImageResources, |
| 57 | + BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) { |
| 58 | + if (enabled.isEmpty()) { |
| 59 | + return; |
| 60 | + } |
| 61 | + for (HibernateSearchBackendElasticsearchEnabledBuildItem enabledItem : enabled) { |
| 62 | + processBuildTimeConfig(enabledItem, applicationArchivesBuildItem, nativeImageResources, |
| 63 | + hotDeploymentWatchedFiles); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void processBuildTimeConfig(HibernateSearchBackendElasticsearchEnabledBuildItem enabled, |
| 68 | + ApplicationArchivesBuildItem applicationArchivesBuildItem, |
| 69 | + BuildProducer<NativeImageResourceBuildItem> nativeImageResources, |
| 70 | + BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) { |
| 71 | + Set<String> propertyKeysWithNoVersion = new LinkedHashSet<>(); |
| 72 | + var buildTimeConfig = enabled.getBuildTimeConfig(); |
| 73 | + |
| 74 | + var mapperContext = enabled.getMapperContext(); |
| 75 | + Set<String> allBackendNames = new LinkedHashSet<>(mapperContext.getBackendNamesForIndexedEntities()); |
| 76 | + allBackendNames.addAll(buildTimeConfig.keySet()); |
| 77 | + // For all backends referenced either through @Indexed(backend = ...) or configuration... |
| 78 | + for (String backendName : allBackendNames) { |
| 79 | + HibernateSearchBackendElasticsearchBuildTimeConfig backendConfig = buildTimeConfig.get(backendName); |
| 80 | + // ... we validate that the backend is configured and the version is present |
| 81 | + if (backendConfig == null || backendConfig.version().isEmpty()) { |
| 82 | + propertyKeysWithNoVersion.add(mapperContext.backendPropertyKey(backendName, null, "version")); |
| 83 | + } |
| 84 | + if (backendConfig == null) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + // ... we register files referenced from backends configuration |
| 88 | + registerClasspathFilesFromBackendConfig(mapperContext, backendName, backendConfig, |
| 89 | + applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles); |
| 90 | + } |
| 91 | + if (!propertyKeysWithNoVersion.isEmpty()) { |
| 92 | + throw new ConfigurationException( |
| 93 | + "The Elasticsearch version needs to be defined via properties: " |
| 94 | + + String.join(", ", propertyKeysWithNoVersion) + ".", |
| 95 | + propertyKeysWithNoVersion); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static void registerClasspathFilesFromBackendConfig(MapperContext mapperContext, String backendName, |
| 100 | + HibernateSearchBackendElasticsearchBuildTimeConfig backendConfig, |
| 101 | + ApplicationArchivesBuildItem applicationArchivesBuildItem, |
| 102 | + BuildProducer<NativeImageResourceBuildItem> nativeImageResources, |
| 103 | + BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) { |
| 104 | + registerClasspathFilesFromIndexConfig(mapperContext, backendName, null, backendConfig.indexDefaults(), |
| 105 | + applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles); |
| 106 | + for (Entry<String, HibernateSearchBackendElasticsearchBuildTimeConfig.IndexConfig> entry : backendConfig.indexes() |
| 107 | + .entrySet()) { |
| 108 | + String indexName = entry.getKey(); |
| 109 | + HibernateSearchBackendElasticsearchBuildTimeConfig.IndexConfig indexConfig = entry.getValue(); |
| 110 | + registerClasspathFilesFromIndexConfig(mapperContext, backendName, indexName, indexConfig, |
| 111 | + applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static void registerClasspathFilesFromIndexConfig(MapperContext mapperContext, String backendName, String indexName, |
| 116 | + HibernateSearchBackendElasticsearchBuildTimeConfig.IndexConfig indexConfig, |
| 117 | + ApplicationArchivesBuildItem applicationArchivesBuildItem, |
| 118 | + BuildProducer<NativeImageResourceBuildItem> nativeImageResources, |
| 119 | + BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) { |
| 120 | + registerClasspathFileFromConfig(mapperContext, backendName, indexName, "schema-management.settings-file", |
| 121 | + indexConfig.schemaManagement().settingsFile(), |
| 122 | + applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles); |
| 123 | + registerClasspathFileFromConfig(mapperContext, backendName, indexName, "schema-management.mapping-file", |
| 124 | + indexConfig.schemaManagement().mappingFile(), |
| 125 | + applicationArchivesBuildItem, nativeImageResources, hotDeploymentWatchedFiles); |
| 126 | + } |
| 127 | + |
| 128 | + private static void registerClasspathFileFromConfig(MapperContext mapperContext, String backendName, String indexName, |
| 129 | + String propertyKeyRadical, |
| 130 | + Optional<String> classpathFileOptional, |
| 131 | + ApplicationArchivesBuildItem applicationArchivesBuildItem, |
| 132 | + BuildProducer<NativeImageResourceBuildItem> nativeImageResources, |
| 133 | + BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles) { |
| 134 | + if (!classpathFileOptional.isPresent()) { |
| 135 | + return; |
| 136 | + } |
| 137 | + String classpathFile = classpathFileOptional.get(); |
| 138 | + |
| 139 | + Path existingPath = applicationArchivesBuildItem.getRootArchive().getChildPath(classpathFile); |
| 140 | + |
| 141 | + if (existingPath == null || Files.isDirectory(existingPath)) { |
| 142 | + //raise exception if explicit file is not present (i.e. not the default) |
| 143 | + throw new ConfigurationException( |
| 144 | + "Unable to find file referenced in '" |
| 145 | + + mapperContext.backendPropertyKey(backendName, indexName, propertyKeyRadical) + "=" |
| 146 | + + classpathFile |
| 147 | + + "'. Remove property or add file to your path."); |
| 148 | + } |
| 149 | + nativeImageResources.produce(new NativeImageResourceBuildItem(classpathFile)); |
| 150 | + hotDeploymentWatchedFiles.produce(new HotDeploymentWatchedFileBuildItem(classpathFile)); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments