Skip to content

Commit

Permalink
feat: add data-import file from class-path (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored May 8, 2024
1 parent d9ec2a1 commit 685b2af
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]`


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -49,19 +51,25 @@ 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;
}

// create parameter
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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 685b2af

Please sign in to comment.