Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisit resource loading code #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/main/java/com/datastax/examples/MapperApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.datastax.oss.driver.api.core.cql.SimpleStatement;

import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -37,9 +38,9 @@
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -178,10 +179,17 @@ private static void maybeCreateSchema(CqlSession session) throws Exception {
}

private static List<String> getStatements(String fileName) throws Exception {
URI uri = Thread.currentThread().getContextClassLoader().getResource(fileName).toURI();
FileSystems.newFileSystem(uri, Map.of("create", "true"));
Path path = Paths.get(uri);

URI uri = MapperApp.class.getClassLoader().getResource(fileName).toURI();
Path path;
if (uri.toString().contains("!")) {
// This happens when the file is in a packaged JAR
String[] parts = uri.toString().split("!");
assert parts.length == 2;
FileSystem fs = FileSystems.newFileSystem(URI.create(parts[0]), Collections.emptyMap());
path = fs.getPath(parts[1]);
} else {
path = Paths.get(uri);
}
String contents = Files.readString(path);
return Arrays.stream(contents.split(";"))
.map(String::trim)
Expand Down