From 685b2aff7ddea44a5c005af09ee08b9d3dfc1874 Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Wed, 8 May 2024 12:18:38 +0200 Subject: [PATCH] feat: add data-import file from class-path (#162) --- .../includes/tkit-quarkus-data-import.adoc | 17 ++++++++ .../quarkus/dataimport/DataImportInvoker.java | 42 ++++++++++++++++--- .../dataimport/DataImportRuntimeConfig.java | 6 +++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-data-import.adoc b/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-data-import.adoc index c6e86c82..d55109ea 100644 --- a/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-data-import.adoc +++ b/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-data-import.adoc @@ -78,6 +78,23 @@ endif::add-copy-button-to-env-var[] |required icon:exclamation-circle[title=Configuration property is required] +a| [[tkit-quarkus-data-import_tkit-dataimport-configurations-configurations-class-path]]`link:#tkit-quarkus-data-import_tkit-dataimport-configurations-configurations-class-path[tkit.dataimport.configurations."configurations".class-path]` + + +[.description] +-- +Set to true if the file is a classpath file. + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++TKIT_DATAIMPORT_CONFIGURATIONS__CONFIGURATIONS__CLASS_PATH+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++TKIT_DATAIMPORT_CONFIGURATIONS__CONFIGURATIONS__CLASS_PATH+++` +endif::add-copy-button-to-env-var[] +--|boolean +|`false` + + a| [[tkit-quarkus-data-import_tkit-dataimport-configurations-configurations-metadata-metadata]]`link:#tkit-quarkus-data-import_tkit-dataimport-configurations-configurations-metadata-metadata[tkit.dataimport.configurations."configurations".metadata]` diff --git a/extensions/data-import/runtime/src/main/java/org/tkit/quarkus/dataimport/DataImportInvoker.java b/extensions/data-import/runtime/src/main/java/org/tkit/quarkus/dataimport/DataImportInvoker.java index 03887fda..b910209d 100644 --- a/extensions/data-import/runtime/src/main/java/org/tkit/quarkus/dataimport/DataImportInvoker.java +++ b/extensions/data-import/runtime/src/main/java/org/tkit/quarkus/dataimport/DataImportInvoker.java @@ -3,6 +3,7 @@ import static jakarta.persistence.LockModeType.PESSIMISTIC_WRITE; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -24,6 +25,7 @@ import io.quarkus.arc.InjectableBean; import io.quarkus.arc.InstanceHandle; import io.quarkus.arc.Unremovable; +import io.quarkus.runtime.util.ClassPathUtils; @Unremovable @ApplicationScoped @@ -49,10 +51,16 @@ public void processItem(String key, String bean, DataImportRuntimeConfig.DataImp return; } - // check if the file exists - Path file = Paths.get(config.file); - if (!Files.exists(file)) { - LOGGER.info("Data import file does not exists. Key: " + key + " file: " + config.file); + // check and load data from file + byte[] data; + if (config.classpath) { + data = loadClassPathData(config.file); + } else { + data = loadData(Paths.get(config.file)); + } + if (data == null) { + LOGGER.info("Data import file does not exists. Key: " + key + " class-path: " + config.classpath + " file: " + + config.file); return; } @@ -60,8 +68,8 @@ public void processItem(String key, String bean, DataImportRuntimeConfig.DataImp DataImportConfig param = new DataImportConfig(); param.key = key; param.metadata = config.metadata; - param.file = file; - param.data = loadData(param.file); + param.file = Path.of(config.file); + param.data = data; param.md5 = createChecksum(param.data); // find import entry and lock it @@ -118,7 +126,29 @@ private static String createChecksum(byte[] data) throws RuntimeException { } } + private static byte[] loadClassPathData(String file) throws RuntimeException { + try { + var url = Thread.currentThread().getContextClassLoader().getResource(file); + if (url == null) { + return null; + } + + return ClassPathUtils.readStream(url, inputStream -> { + try { + return inputStream.readAllBytes(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } catch (Exception ex) { + throw new RuntimeException("Error loading class-path data from " + file, ex); + } + } + private static byte[] loadData(Path path) throws RuntimeException { + if (!Files.exists(path)) { + return null; + } try { return Files.readAllBytes(path); } catch (IOException e) { diff --git a/extensions/data-import/runtime/src/main/java/org/tkit/quarkus/dataimport/DataImportRuntimeConfig.java b/extensions/data-import/runtime/src/main/java/org/tkit/quarkus/dataimport/DataImportRuntimeConfig.java index 127a9ebb..2ff6daad 100644 --- a/extensions/data-import/runtime/src/main/java/org/tkit/quarkus/dataimport/DataImportRuntimeConfig.java +++ b/extensions/data-import/runtime/src/main/java/org/tkit/quarkus/dataimport/DataImportRuntimeConfig.java @@ -44,6 +44,12 @@ public static class DataImportConfiguration { @ConfigItem(name = "file") String file; + /** + * Set to true if the file is a classpath file. + */ + @ConfigItem(name = "class-path", defaultValue = "false") + boolean classpath; + /** * The metadata for the execution */