diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/eu/m724/jarupdater/download/Downloader.java b/src/main/java/eu/m724/jarupdater/download/Downloader.java index d573f0f..d07437d 100644 --- a/src/main/java/eu/m724/jarupdater/download/Downloader.java +++ b/src/main/java/eu/m724/jarupdater/download/Downloader.java @@ -6,16 +6,16 @@ import java.util.concurrent.CompletableFuture; public interface Downloader { /** * downloads a file and verifies it - * @param url - * @param sha256hex + * @param url the file url to download from + * @param sha256hex the sha256 hash to verify * @return a future which can throw ioexception or signatureexception */ CompletableFuture downloadAndVerify(String url, String sha256hex); /** * moves source into destination - * @param source - * @param destination + * @param source source file + * @param destination destination file (path) * @return a future which can throw ioexception */ CompletableFuture install(File source, File destination); diff --git a/src/main/java/eu/m724/jarupdater/environment/ConstantEnvironment.java b/src/main/java/eu/m724/jarupdater/environment/ConstantEnvironment.java index d32a3a8..b52cb0b 100644 --- a/src/main/java/eu/m724/jarupdater/environment/ConstantEnvironment.java +++ b/src/main/java/eu/m724/jarupdater/environment/ConstantEnvironment.java @@ -3,9 +3,9 @@ package eu.m724.jarupdater.environment; import java.nio.file.Path; public class ConstantEnvironment implements Environment { - private String runningVersion; - private String channel; - private Path runningJarFilePath; + private final String runningVersion; + private final String channel; + private final Path runningJarFilePath; public ConstantEnvironment(String runningVersion, String channel, Path runningJarFilePath) { this.runningVersion = runningVersion; @@ -13,19 +13,8 @@ public class ConstantEnvironment implements Environment { this.runningJarFilePath = runningJarFilePath; } - @Override - public String getRunningVersion() { - return runningVersion; - } - - @Override - public String getChannel() { - return channel; - } - - @Override - public Path getRunningJarFilePath() { - return runningJarFilePath; - } + @Override public String getRunningVersion() { return runningVersion; } + @Override public String getChannel() { return channel; } + @Override public Path getRunningJarFilePath() { return runningJarFilePath; } } diff --git a/src/main/java/eu/m724/jarupdater/environment/Environment.java b/src/main/java/eu/m724/jarupdater/environment/Environment.java index 9bfafcb..04b057a 100644 --- a/src/main/java/eu/m724/jarupdater/environment/Environment.java +++ b/src/main/java/eu/m724/jarupdater/environment/Environment.java @@ -3,7 +3,7 @@ package eu.m724.jarupdater.environment; import java.nio.file.Path; public interface Environment { - public String getRunningVersion(); - public String getChannel(); - public Path getRunningJarFilePath(); + String getRunningVersion(); + String getChannel(); + Path getRunningJarFilePath(); } diff --git a/src/main/java/eu/m724/jarupdater/live/GiteaMetadataDAO.java b/src/main/java/eu/m724/jarupdater/live/GiteaMetadataDAO.java index f688975..c0325a4 100644 --- a/src/main/java/eu/m724/jarupdater/live/GiteaMetadataDAO.java +++ b/src/main/java/eu/m724/jarupdater/live/GiteaMetadataDAO.java @@ -17,8 +17,8 @@ import com.google.gson.Gson; import eu.m724.jarupdater.object.Version; public class GiteaMetadataDAO implements MetadataDAO { - private String url; - private String branch; + private final String url; + private final String branch; public GiteaMetadataDAO(String url, String branch) { this.url = url; @@ -29,50 +29,44 @@ public class GiteaMetadataDAO implements MetadataDAO { @Override public CompletableFuture> getChannels() { String url = getFileUrl("channels.txt"); - - CompletableFuture> channelsFuture = - makeRequest(url).thenApply(response -> { - if (response.statusCode() != 200) - throw new CompletionException(new IOException("Server returned status code %d".formatted(response.statusCode()))); - - return response.body().lines().toList(); - }); - - return channelsFuture; + + return makeRequest(url).thenApply(response -> { + if (response.statusCode() != 200) + throw new CompletionException(new IOException("Server returned status code %d".formatted(response.statusCode()))); + + return response.body().lines().toList(); + }); } @Override public CompletableFuture getMetadata(String channel, String versionLabel) { String url = getFileUrl(channel, versionLabel, "meta-v1.json"); - - CompletableFuture metadataFuture = - makeRequest(url).thenApply(response -> { - if (response.statusCode() != 200) - throw new CompletionException(new IOException("Server returned status code %d".formatted(response.statusCode()))); - // not throwing nosuchversionexecpion because it's not possible to tell if the server is broken or it really doesn't exist - - Version version = new Gson().fromJson(response.body(), Version.class); - return version; - }); - - return metadataFuture; + + // not throwing nosuchversionexecpion because it's not possible to tell if the server is broken or it really doesn't exist + + return makeRequest(url).thenApply(response -> { + if (response.statusCode() != 200) + throw new CompletionException(new IOException("Server returned status code %d".formatted(response.statusCode()))); + // not throwing nosuchversionexecpion because it's not possible to tell if the server is broken or it really doesn't exist + + return new Gson().fromJson(response.body(), Version.class); + }); } private CompletableFuture> makeRequest(String url) { - System.out.println("Request: " + url); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url)) .header("User-Agent", "ju/1") // jar updater v1 .build(); - - CompletableFuture> responseFuture = - HttpClient.newBuilder() + + HttpClient.Builder httpClientBuilder = HttpClient.newBuilder() .followRedirects(Redirect.NORMAL) - .proxy(ProxySelector.getDefault()).build(). - sendAsync(request, BodyHandlers.ofString()); - - return responseFuture; + .proxy(ProxySelector.getDefault()); + + try (HttpClient httpClient = httpClientBuilder.build()) { + return httpClient.sendAsync(request, BodyHandlers.ofString()); + } } private String getFileUrl(String... paths) { diff --git a/src/main/java/eu/m724/jarupdater/live/MetadataDAO.java b/src/main/java/eu/m724/jarupdater/live/MetadataDAO.java index 1c6afc2..e34f31d 100644 --- a/src/main/java/eu/m724/jarupdater/live/MetadataDAO.java +++ b/src/main/java/eu/m724/jarupdater/live/MetadataDAO.java @@ -14,8 +14,8 @@ public interface MetadataDAO { /** * get metadata of a version in channel - * @param channel - * @param versionLabel + * @param channel the channel + * @param versionLabel basically version number * @return a future which can throw ioexcpeitons or nosuchfilexxception */ CompletableFuture getMetadata(String channel, String versionLabel); diff --git a/src/main/java/eu/m724/jarupdater/live/MetadataFacade.java b/src/main/java/eu/m724/jarupdater/live/MetadataFacade.java index f212601..403dccf 100644 --- a/src/main/java/eu/m724/jarupdater/live/MetadataFacade.java +++ b/src/main/java/eu/m724/jarupdater/live/MetadataFacade.java @@ -8,10 +8,10 @@ import eu.m724.jarupdater.environment.Environment; import eu.m724.jarupdater.object.Version; public class MetadataFacade { - private Environment environment; - private MetadataDAO metadataDao; + private final Environment environment; + private final MetadataDAO metadataDao; - private HashMap> cache = new HashMap<>(); + private final HashMap> cache = new HashMap<>(); private CompletableFuture> channels = null; public MetadataFacade(Environment environment, MetadataDAO metadataDao) { diff --git a/src/main/java/eu/m724/jarupdater/object/NoSuchVersionException.java b/src/main/java/eu/m724/jarupdater/object/NoSuchVersionException.java index 4ead06e..fe466d6 100644 --- a/src/main/java/eu/m724/jarupdater/object/NoSuchVersionException.java +++ b/src/main/java/eu/m724/jarupdater/object/NoSuchVersionException.java @@ -1,9 +1,11 @@ package eu.m724.jarupdater.object; import java.io.IOException; +import java.io.Serial; public class NoSuchVersionException extends IOException { + @Serial private static final long serialVersionUID = 8435964987348892266L; public NoSuchVersionException(String version) { diff --git a/src/main/java/eu/m724/jarupdater/updater/Updater.java b/src/main/java/eu/m724/jarupdater/updater/Updater.java index 462ca3d..497e97a 100644 --- a/src/main/java/eu/m724/jarupdater/updater/Updater.java +++ b/src/main/java/eu/m724/jarupdater/updater/Updater.java @@ -73,9 +73,9 @@ public class Updater { downloaded = latestVersionFuture.thenApply(latestVersion -> { String url = latestVersion.getFileUrl(); String hash = latestVersion.getSha256(); - - File file = downloader.downloadAndVerify(url, hash).join(); - return file; // TODO better way of catching exception? + + // TODO better way of catching exception? + return downloader.downloadAndVerify(url, hash).join(); }); return downloaded;