From 24917c3e4b97fda2e0cf72f8a22a7ad1a9cc0f76 Mon Sep 17 00:00:00 2001 From: Chris Lenderman Date: Mon, 4 Mar 2024 22:58:33 -0500 Subject: [PATCH] Robustify --- .../lenderman/nabu/adapter/loader/Loader.java | 3 +- .../nabu/adapter/loader/LocalLoader.java | 22 ++++++++-- .../nabu/adapter/loader/WebLoader.java | 40 +++++++++++-------- .../nabu/adapter/server/NabuServer.java | 33 ++++++++++++--- src/main/resources/NabuNetwork.xml | 29 ++++++++------ 5 files changed, 87 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/lenderman/nabu/adapter/loader/Loader.java b/src/main/java/com/lenderman/nabu/adapter/loader/Loader.java index 4c77785..31aa8c4 100644 --- a/src/main/java/com/lenderman/nabu/adapter/loader/Loader.java +++ b/src/main/java/com/lenderman/nabu/adapter/loader/Loader.java @@ -35,8 +35,7 @@ public interface Loader * desire to preserve data) * @return Optional */ - public Optional tryGetData(String path, String preserveDataPath) - throws Exception; + public Optional tryGetData(String path, String preserveDataPath); /** * Try to get the containing directory of the specified file diff --git a/src/main/java/com/lenderman/nabu/adapter/loader/LocalLoader.java b/src/main/java/com/lenderman/nabu/adapter/loader/LocalLoader.java index c83a8dd..021368d 100644 --- a/src/main/java/com/lenderman/nabu/adapter/loader/LocalLoader.java +++ b/src/main/java/com/lenderman/nabu/adapter/loader/LocalLoader.java @@ -37,12 +37,19 @@ public class LocalLoader implements Loader */ @Override public Optional tryGetData(String path, String preserveDataPath) - throws Exception { if (path.equalsIgnoreCase(Settings.HeadlessBootLoader)) { - return Optional.of(IOUtils.toByteArray(getClass().getClassLoader() - .getResourceAsStream(Settings.HeadlessBootResource))); + try + { + return Optional.of(IOUtils.toByteArray( + getClass().getClassLoader().getResourceAsStream( + Settings.HeadlessBootResource))); + } + catch (Exception ex) + { + return Optional.empty(); + } } try @@ -65,7 +72,14 @@ public Optional tryGetDirectory(String path) throws Exception { try { - return Optional.of(Paths.get(path).getParent().toString()); + if (Files.isDirectory(Paths.get(path))) + { + return Optional.of(path); + } + else + { + return Optional.of(Paths.get(path).getParent().toString()); + } } catch (Exception ex) { diff --git a/src/main/java/com/lenderman/nabu/adapter/loader/WebLoader.java b/src/main/java/com/lenderman/nabu/adapter/loader/WebLoader.java index 0eb8b73..386e56f 100644 --- a/src/main/java/com/lenderman/nabu/adapter/loader/WebLoader.java +++ b/src/main/java/com/lenderman/nabu/adapter/loader/WebLoader.java @@ -47,28 +47,34 @@ public class WebLoader implements Loader */ @Override public Optional tryGetData(String path, String preserveDataPath) - throws Exception { - URLConnection connection = WebUtils.openWebClient(path); - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - byte[] bytes = new byte[8192]; - int len; - while ((len = connection.getInputStream().read(bytes)) > 0) + try { - buffer.write(bytes, 0, len); - } + URLConnection connection = WebUtils.openWebClient(path); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + byte[] bytes = new byte[8192]; + int len; + while ((len = connection.getInputStream().read(bytes)) > 0) + { + buffer.write(bytes, 0, len); + } - if (preserveDataPath != null) + if (preserveDataPath != null) + { + logger.debug("Preserving {}", path); + Path outputFile = Paths.get(preserveDataPath, + getPathSeparator(), new URI(path).getPath()); + Files.createDirectories(outputFile.getParent()); + Files.write(outputFile, buffer.toByteArray(), + StandardOpenOption.CREATE); + } + buffer.close(); + return (Optional.of(buffer.toByteArray())); + } + catch (Exception ex) { - logger.debug("Preserving {}", path); - Path outputFile = Paths.get(preserveDataPath, getPathSeparator(), - new URI(path).getPath()); - Files.createDirectories(outputFile.getParent()); - Files.write(outputFile, buffer.toByteArray(), - StandardOpenOption.CREATE); + return Optional.empty(); } - buffer.close(); - return (Optional.of(buffer.toByteArray())); } /** diff --git a/src/main/java/com/lenderman/nabu/adapter/server/NabuServer.java b/src/main/java/com/lenderman/nabu/adapter/server/NabuServer.java index b4885d4..3fbe15f 100644 --- a/src/main/java/com/lenderman/nabu/adapter/server/NabuServer.java +++ b/src/main/java/com/lenderman/nabu/adapter/server/NabuServer.java @@ -326,6 +326,14 @@ else if (this.settings.getPath().toLowerCase().endsWith(".pak") + ".nabu"; data = loader.tryGetData(segmentFullPath, this.settings.getPreservedPath()); + if (!data.isPresent()) + { + segmentFullPath = directory.get() + + loader.getPathSeparator() + segmentName + + ".NABU"; + data = loader.tryGetData(segmentFullPath, + this.settings.getPreservedPath()); + } if (data.isPresent()) { logger.debug("Creating NABU segment {} from {}", @@ -339,11 +347,26 @@ else if (this.settings.getPath().toLowerCase().endsWith(".pak") String pakFullPath = directory.get() + loader.getPathSeparator() + segmentName + ".pak"; - logger.debug("Loading NABU segment {} from {}", - String.format("%06x", segmentNumber), - pakFullPath); - segment = Optional.of(SegmentManager - .loadPackets(segmentNumber, data.get())); + data = loader.tryGetData(pakFullPath, + this.settings.getPreservedPath()); + if (!data.isPresent()) + { + pakFullPath = directory.get() + + loader.getPathSeparator() + + segmentName + ".PAK"; + data = loader.tryGetData(pakFullPath, + this.settings.getPreservedPath()); + } + + if (data.isPresent()) + { + logger.debug("Loading NABU segment {} from {}", + String.format("%06x", segmentNumber), + pakFullPath); + segment = Optional + .of(SegmentManager.loadPackets( + segmentNumber, data.get())); + } } } } diff --git a/src/main/resources/NabuNetwork.xml b/src/main/resources/NabuNetwork.xml index e0e7eae..69320a1 100644 --- a/src/main/resources/NabuNetwork.xml +++ b/src/main/resources/NabuNetwork.xml @@ -5,21 +5,26 @@ NABU 1984 Cycle (need to verify) https://adaptor.thenabunetwork.com/cycle - - - + NABU Network 1996 Cycle v3 + https://adaptor.thenabunetwork.com/cycle3 + + + NabuNetwork + NABU Network DJ Cycle + https://adaptor.thenabunetwork.com/cycledj + NabuNetwork CP/M 2.2 (ISHKUR)