From 6e5a5bba82b8c8e0d8354cae5fc2616d901661e7 Mon Sep 17 00:00:00 2001 From: Candicey <13133556-Candicey@users.noreply.gitlab.com> Date: Sun, 28 Jul 2024 02:13:58 +0700 Subject: [PATCH] Roughly finished stage 0 and stage 1 --- .../oneconfig/loader/LoaderBase.java | 4 +- .../oneconfig/loader/utils/ErrorHandler.java | 4 +- .../oneconfig/loader/utils/IOUtils.java | 13 ++ .../oneconfig/loader/utils}/Lazy.java | 2 +- .../oneconfig/loader/utils/RequestHelper.java | 4 +- .../oneconfig/loader/utils}/XDG.java | 2 +- .../stage0/LaunchWrapperCapabilities.java | 6 +- .../loader/stage0/LaunchWrapperTweaker.java | 2 +- .../oneconfig/loader/stage0/Stage0Loader.java | 146 ++++++------------ .../oneconfig/loader/stage1/Stage1Loader.java | 112 +++++++++++++- .../stage1/dependency/DependencyManager.java | 6 +- .../maven/MavenArtifactDeclaration.java | 9 +- .../maven/MavenArtifactDependency.java | 16 ++ .../maven/MavenDependencyExclusion.java | 25 +++ .../maven/MavenDependencyManager.java | 107 +++++++++++-- .../maven/cache/MavenCachingSolution.java | 2 +- .../loader/stage1/util/SystemProperties.java | 2 + 17 files changed, 330 insertions(+), 132 deletions(-) rename {stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util => common/src/main/java/org/polyfrost/oneconfig/loader/utils}/Lazy.java (91%) rename {stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util => common/src/main/java/org/polyfrost/oneconfig/loader/utils}/XDG.java (98%) create mode 100644 stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenArtifactDependency.java create mode 100644 stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenDependencyExclusion.java diff --git a/common/src/main/java/org/polyfrost/oneconfig/loader/LoaderBase.java b/common/src/main/java/org/polyfrost/oneconfig/loader/LoaderBase.java index 8f8df04..274bc58 100644 --- a/common/src/main/java/org/polyfrost/oneconfig/loader/LoaderBase.java +++ b/common/src/main/java/org/polyfrost/oneconfig/loader/LoaderBase.java @@ -18,8 +18,8 @@ @Getter public abstract class LoaderBase implements ILoader { protected static final String UNKNOWN_VERSION = "0.0.0+unknown"; - private final @NotNull String name; - private final @NotNull String version; + private final @NotNull @Getter String name; + private final @NotNull @Getter String version; protected final @NotNull Capabilities capabilities; protected final org.apache.logging.log4j.Logger logger; diff --git a/common/src/main/java/org/polyfrost/oneconfig/loader/utils/ErrorHandler.java b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/ErrorHandler.java index b80b82e..e980ff8 100644 --- a/common/src/main/java/org/polyfrost/oneconfig/loader/utils/ErrorHandler.java +++ b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/ErrorHandler.java @@ -1,10 +1,10 @@ package org.polyfrost.oneconfig.loader.utils; +import lombok.extern.log4j.Log4j2; import org.apache.logging.log4j.LogManager; +import org.jetbrains.annotations.NotNull; import org.polyfrost.oneconfig.loader.ILoader; import org.polyfrost.oneconfig.loader.IMetaHolder; -import lombok.extern.log4j.Log4j2; -import org.jetbrains.annotations.NotNull; import javax.swing.*; import java.awt.*; diff --git a/common/src/main/java/org/polyfrost/oneconfig/loader/utils/IOUtils.java b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/IOUtils.java index 9a06557..daa6c4d 100644 --- a/common/src/main/java/org/polyfrost/oneconfig/loader/utils/IOUtils.java +++ b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/IOUtils.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -30,6 +31,18 @@ private IOUtils() { throw new IllegalStateException("This class cannot be instantiated."); } + public static void readInto(InputStream input, OutputStream output) throws IOException { + readInto(input, output, 1024); + } + + public static void readInto(InputStream input, OutputStream output, int bufferSize) throws IOException { + byte[] buffer = new byte[bufferSize]; + int bytesRead; + while ((bytesRead = input.read(buffer)) != -1) { + output.write(buffer, 0, bytesRead); + } + } + public static byte[] readAllBytes(InputStream inputStream) throws IOException { return readNBytes(inputStream, Integer.MAX_VALUE); } diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/Lazy.java b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/Lazy.java similarity index 91% rename from stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/Lazy.java rename to common/src/main/java/org/polyfrost/oneconfig/loader/utils/Lazy.java index 5b754c4..b8945de 100644 --- a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/Lazy.java +++ b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/Lazy.java @@ -1,4 +1,4 @@ -package org.polyfrost.oneconfig.loader.stage1.util; +package org.polyfrost.oneconfig.loader.utils; import java.util.function.Supplier; diff --git a/common/src/main/java/org/polyfrost/oneconfig/loader/utils/RequestHelper.java b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/RequestHelper.java index c290bdf..3a05f40 100644 --- a/common/src/main/java/org/polyfrost/oneconfig/loader/utils/RequestHelper.java +++ b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/RequestHelper.java @@ -86,11 +86,11 @@ public class RequestHelper { private static IMetaHolder metaHolder; private static SSLSocketFactory sslSocketFactory; - protected URLConnection establishConnection(URL url) throws IOException { + public URLConnection establishConnection(URL url) throws IOException { return establishConnection(url, "application/json"); } - protected URLConnection establishConnection(URL url, String requestedType) throws IOException { + public URLConnection establishConnection(URL url, String requestedType) throws IOException { URLConnection connection = url.openConnection(); if (connection instanceof HttpsURLConnection) { ((HttpsURLConnection) connection).setSSLSocketFactory( diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/XDG.java b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/XDG.java similarity index 98% rename from stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/XDG.java rename to common/src/main/java/org/polyfrost/oneconfig/loader/utils/XDG.java index 8582436..c71da88 100644 --- a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/XDG.java +++ b/common/src/main/java/org/polyfrost/oneconfig/loader/utils/XDG.java @@ -1,4 +1,4 @@ -package org.polyfrost.oneconfig.loader.stage1.util; +package org.polyfrost.oneconfig.loader.utils; import lombok.Data; import org.jetbrains.annotations.Nullable; diff --git a/stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperCapabilities.java b/stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperCapabilities.java index b63c75b..e911439 100644 --- a/stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperCapabilities.java +++ b/stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperCapabilities.java @@ -1,11 +1,11 @@ package org.polyfrost.oneconfig.loader.stage0; -import net.minecraft.launchwrapper.Launch; -import org.polyfrost.oneconfig.loader.ILoader; -import org.polyfrost.oneconfig.loader.utils.EnumEntrypoint; import lombok.Data; +import net.minecraft.launchwrapper.Launch; import net.minecraft.launchwrapper.LaunchClassLoader; import org.jetbrains.annotations.NotNull; +import org.polyfrost.oneconfig.loader.ILoader; +import org.polyfrost.oneconfig.loader.utils.EnumEntrypoint; import java.net.URL; import java.nio.file.Path; diff --git a/stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperTweaker.java b/stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperTweaker.java index 44e9173..52135b5 100644 --- a/stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperTweaker.java +++ b/stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperTweaker.java @@ -1,8 +1,8 @@ package org.polyfrost.oneconfig.loader.stage0; -import org.polyfrost.oneconfig.loader.ILoader; import net.minecraft.launchwrapper.ITweaker; import net.minecraft.launchwrapper.LaunchClassLoader; +import org.polyfrost.oneconfig.loader.ILoader; import java.io.File; import java.util.List; diff --git a/stage0/src/main/java/org/polyfrost/oneconfig/loader/stage0/Stage0Loader.java b/stage0/src/main/java/org/polyfrost/oneconfig/loader/stage0/Stage0Loader.java index 10b49a4..da06db5 100644 --- a/stage0/src/main/java/org/polyfrost/oneconfig/loader/stage0/Stage0Loader.java +++ b/stage0/src/main/java/org/polyfrost/oneconfig/loader/stage0/Stage0Loader.java @@ -1,19 +1,23 @@ package org.polyfrost.oneconfig.loader.stage0; import lombok.SneakyThrows; +import org.polyfrost.oneconfig.loader.IMetaHolder; import org.polyfrost.oneconfig.loader.LoaderBase; import org.polyfrost.oneconfig.loader.utils.IOUtils; +import org.polyfrost.oneconfig.loader.utils.RequestHelper; +import org.polyfrost.oneconfig.loader.utils.XDG; import javax.swing.*; -import java.io.*; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; import java.net.URL; -import java.nio.charset.StandardCharsets; +import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.Arrays; +import java.util.function.Supplier; +import java.util.jar.Attributes; +import java.util.jar.Manifest; /** * The first stage of the OneConfig Loader. @@ -31,6 +35,8 @@ public class Stage0Loader extends LoaderBase { private static final String STAGE1_CLASS_NAME = "org.polyfrost.oneconfig.loader.stage1.Stage1Loader"; + private final Attributes manifestAttributes; + Stage0Loader(Capabilities capabilities) { super( "stage0", @@ -39,20 +45,17 @@ public class Stage0Loader extends LoaderBase { ), capabilities ); - } - /*private static final String TEST_FILE_PATH = - "/home/x/Work/Polyfrost/test/build/libs/test-1.0-SNAPSHOT.jar";*/ + try { + this.manifestAttributes = new Manifest(this.getClass().getResourceAsStream("/META-INF/MANIFEST.MF")).getMainAttributes(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } @SneakyThrows @Override public void load() { - /*capabilities.appendToClassPath( - false, - new File(TEST_FILE_PATH).toURI().toURL() - );*/ -// Class testingClass = capabilities.getClassLoader().loadClass("me.xtrm.test.Testing2"); -// testingClass.getMethod("hi").invoke(null); JOptionPane.showMessageDialog(null, "Loading hook"); // fetch settings @@ -61,12 +64,11 @@ public void load() { // Fetch stage1 version info logger.info("Fetching stage1 version info"); - URL stage1JarUrl = buildMavenJarArtifactUrlPath(stage1Version, ".jar", "org", "polyfrost", "oneconfig", "loader", "stage1"); -// JsonObject stage1MavenJson = (JsonObject) new JsonParser().parse(getString(stage1JarUrl.toString(), DEF_AGENT, 5000, true)); + Supplier stage1JarUrl = () -> MAVEN_URL + MAVEN_REPO + "/org/polyfrost/oneconfig/loader/stage1/" + stage1Version + "Stage1.jar"; // Lookup stage1 in cache, handle downloading logger.info("Getting stage1 from cache"); - File stage1Jar = lookupStage1CacheOrDownload(stage1Version, stage1JarUrl.toString()); + File stage1Jar = lookupStage1CacheOrDownload(stage1Version, stage1JarUrl); // Load in classloader as a library logger.info("Loading stage1 as a library"); @@ -74,93 +76,45 @@ public void load() { // Delegate loading to stage1 logger.info("GO"); - - Class stage1Class = capabilities.getClassLoader().loadClass(STAGE1_CLASS_NAME); + Class stage1Class = capabilities.getClassLoader().loadClass(manifestAttributes.getValue("Stage1-Class")); Object stage1Instance = stage1Class.getDeclaredConstructor(Capabilities.class).newInstance(capabilities); stage1Class.getDeclaredMethod("load").invoke(stage1Instance); } private String fetchStage1Version() { - // TODO: Change the way to fetch version later - String version = System.getProperty("oneconfig.stage0.version"); - return version; - } - - private File lookupStage1CacheOrDownload(String version, String downloadUrl) { - File cache = new File("./OneConfig/cache/OneConfigLoader/stage1/OneConfigLoader-stage1-" + version + ".jar"); - if (!cache.exists()) { - cache.getParentFile().mkdirs(); - downloadFile(downloadUrl, cache.toPath()); - } - - return cache; + String value = manifestAttributes.getValue("OneConfig-Stage1-Version"); + return value != null ? value : System.getProperty("oneconfig.stage0.version"); } - public URL buildMavenJarArtifactUrlPath(String version, String extension, String... path) { - StringBuilder stringBuilder = new StringBuilder(MAVEN_URL); - stringBuilder.append(MAVEN_REPO); - - for (String s : path) { - stringBuilder.append('/'); - stringBuilder.append(s); - } - - stringBuilder.append(version); - - stringBuilder.append('/'); - stringBuilder.append(path[path.length - 1]); - stringBuilder.append('-'); - stringBuilder.append(version); - stringBuilder.append(extension); - - try { - return new URL(stringBuilder.toString()); - } catch (MalformedURLException e) { - logger.error("Failed to build maven artifact url from {}", String.join(", ", Arrays.asList(path))); - return null; - } - } - - public String getString(String url, String userAgent, int timeout, boolean useCaches) { - StringBuilder sb = new StringBuilder(); - try (BufferedReader input = new BufferedReader(new InputStreamReader(setupConnection(url, userAgent, timeout, useCaches), StandardCharsets.UTF_8))) { - String line; - while ((line = input.readLine()) != null) { - sb.append(line).append('\n'); + private File lookupStage1CacheOrDownload(String version, Supplier downloadUrl) { + Path cache = XDG.provideCacheDir("OneConfig").resolve("loader"); + File cacheFile = cache.toFile(); + + if (!cacheFile.exists()) { + cacheFile.getParentFile().mkdirs(); + + LoaderBase instance = this; + RequestHelper.tryInitialize(new IMetaHolder() { + @Override + public String getName() { + return instance.getName(); + } + + @Override + public String getVersion() { + return instance.getVersion(); + } + }); + + try { + URLConnection urlConnection = new RequestHelper().establishConnection(new URL(downloadUrl.get())); + OutputStream outputStream = Files.newOutputStream(cache); + IOUtils.readInto(urlConnection.getInputStream(), outputStream); + } catch (IOException e) { + throw new RuntimeException("Failed to download Stage1 jar", e); } - } catch (Exception e) { - logger.error("Failed to getString from {}", url, e); - return null; } - return sb.toString(); - } - - public InputStream setupConnection(String url) throws IOException { - return setupConnection(url, DEF_AGENT, 5000, false); - } - - public InputStream setupConnection(String url, String userAgent, int timeout, boolean useCaches) throws IOException { - HttpURLConnection connection = ((HttpURLConnection) new URL(url).openConnection()); - connection.setRequestMethod("GET"); - connection.setUseCaches(useCaches); - connection.addRequestProperty("User-Agent", userAgent); - connection.setReadTimeout(timeout); - connection.setConnectTimeout(timeout); - connection.setDoOutput(true); - return connection.getInputStream(); - } - - public boolean downloadFile(String url, Path path, String userAgent, int timeout, boolean useCaches) { - try (BufferedInputStream in = new BufferedInputStream(setupConnection(url, userAgent, timeout, useCaches))) { - Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING); - } catch (Exception e) { - logger.error("Failed to download file from {}", url, e); - return false; - } - return true; - } - public boolean downloadFile(String url, Path path) { - return downloadFile(url, path, DEF_AGENT, 5000, false); + return cacheFile; } } diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/Stage1Loader.java b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/Stage1Loader.java index befe8db..695d2a6 100644 --- a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/Stage1Loader.java +++ b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/Stage1Loader.java @@ -3,18 +3,32 @@ import lombok.SneakyThrows; import org.polyfrost.oneconfig.loader.LoaderBase; import org.polyfrost.oneconfig.loader.stage1.dependency.DependencyManager; +import org.polyfrost.oneconfig.loader.stage1.dependency.maven.MavenArtifact; +import org.polyfrost.oneconfig.loader.stage1.dependency.maven.MavenArtifactDeclaration; import org.polyfrost.oneconfig.loader.stage1.dependency.maven.MavenDependencyManager; -import org.polyfrost.oneconfig.loader.stage1.dependency.model.Artifact; import org.polyfrost.oneconfig.loader.stage1.util.SystemProperties; -import org.polyfrost.oneconfig.loader.stage1.util.XDG; +import org.polyfrost.oneconfig.loader.utils.IOUtils; +import org.polyfrost.oneconfig.loader.utils.XDG; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.MalformedURLException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.jar.Attributes; +import java.util.jar.JarFile; /** * @author xtrm * @since 1.1.0 */ public class Stage1Loader extends LoaderBase { + private final Attributes manifestAttributes; + private final XDG.ApplicationStore applicationStore; - private final DependencyManager dependencyManager; + private final DependencyManager dependencyManager; + private final String oneConfigVersion; @SneakyThrows public Stage1Loader(Capabilities capabilities) { @@ -23,19 +37,103 @@ public Stage1Loader(Capabilities capabilities) { Stage1Loader.class.getPackage().getImplementationVersion(), capabilities ); + this.applicationStore = XDG.provideApplicationStore("Polyfrost/OneConfig/loader"); + this.dependencyManager = new MavenDependencyManager( applicationStore, SystemProperties.REPOSITORY_URL.get() ); + + try (JarFile jarFile = new JarFile(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath())) { + this.manifestAttributes = jarFile.getManifest().getMainAttributes(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + String oneConfigVersion = manifestAttributes.getValue("OneConfig-Version"); + if (oneConfigVersion == null) { + throw new RuntimeException("OneConfig-Version option is not found in MANIFEST.MF"); + } + this.oneConfigVersion = oneConfigVersion; } @Override public void load() { - // Fetch oneconfig version info - Artifact oneconfigArtifact = dependencyManager.fetchArtifact(); - // Lookup dependencies metadata + // Fetch oneConfig version info + MavenArtifact oneConfigArtifact = dependencyManager.buildArtifact("org.polyfrost", "oneconfig", oneConfigVersion); + // Download to cache - // Delegate everything to OneConfig + checkAndAppendArtifactToClasspath(oneConfigArtifact); + + MavenArtifactDeclaration mavenArtifactDeclaration = dependencyManager.resolveArtifact(oneConfigArtifact); + mavenArtifactDeclaration.resolveDependencies(dependencyManager); + + mavenArtifactDeclaration + .getDependencies() + .stream() + .flatMap((dependency) -> dependency.getDeclaration().getDependencies().stream()) + .forEach((dependency) -> checkAndAppendArtifactToClasspath(dependency.getDeclaration().getArtifact())); + + try { + this.capabilities + .getClassLoader() + .loadClass("org.spongepowered.asm.mixin.Mixins") + .getDeclaredMethod("addConfigurations", String[].class) + .invoke(null, (Object) new String[] { "mixins.oneconfig.json" }); + + String oneConfigMainClass = this.manifestAttributes.getValue("OneConfig-Main-Class"); + if (oneConfigMainClass == null) { + throw new RuntimeException("OneConfig-Main-Class option is not found in MANIFEST.MF"); + } + this.capabilities + .getClassLoader() + .loadClass(manifestAttributes.getValue(oneConfigMainClass)) + // TODO: CHANGE PARAMETER + .getDeclaredMethod("init") + .invoke(null); + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + private Path provideLocalArtifactPath(MavenArtifact mavenArtifact) { + return XDG + .provideCacheDir("OneConfig") + .resolve("loader") + .resolve(XDG + .provideCacheDir("OneConfig") + .resolve("loader") + .resolve(mavenArtifact.getRelativePath()) + ); + } + + private void checkAndAppendArtifactToClasspath(MavenArtifact mavenArtifact) { + File artifactFile = provideLocalArtifactPath(mavenArtifact).toFile(); + + if (!artifactFile.exists()) { + downloadArtifact(mavenArtifact); + } + + this.appendToClasspath(artifactFile); + } + + private void downloadArtifact(MavenArtifact mavenArtifact) { + try { + IOUtils.readInto( + dependencyManager.createArtifactInputStream(mavenArtifact), + Files.newOutputStream(provideLocalArtifactPath(mavenArtifact)) + ); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private void appendToClasspath(File jar) { + try { + this.capabilities.appendToClassPath(false, jar.toURI().toURL()); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } } } diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/DependencyManager.java b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/DependencyManager.java index 4d7eac3..39b215c 100644 --- a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/DependencyManager.java +++ b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/DependencyManager.java @@ -3,6 +3,8 @@ import org.polyfrost.oneconfig.loader.stage1.dependency.model.Artifact; import org.polyfrost.oneconfig.loader.stage1.dependency.model.ArtifactDeclaration; +import java.io.InputStream; + /** * * @param @@ -11,6 +13,8 @@ * @since 1.1.0 */ public interface DependencyManager { - A buildArtifact(String groupId, String artifactId, String version); + A buildArtifact(String groupId, String artifactId, String version, String classifier, String extension); + default A buildArtifact(String groupId, String artifactId, String version) { return buildArtifact(groupId, artifactId, version, null, "jar"); } D resolveArtifact(A artifact); + InputStream createArtifactInputStream(A artifact); } diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenArtifactDeclaration.java b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenArtifactDeclaration.java index babfce7..dbfcb31 100644 --- a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenArtifactDeclaration.java +++ b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenArtifactDeclaration.java @@ -2,11 +2,8 @@ import lombok.Data; import org.polyfrost.oneconfig.loader.stage1.dependency.DependencyManager; -import org.polyfrost.oneconfig.loader.stage1.dependency.model.Artifact; import org.polyfrost.oneconfig.loader.stage1.dependency.model.ArtifactDeclaration; -import org.polyfrost.oneconfig.loader.stage1.dependency.model.ArtifactDependency; -import java.util.ArrayList; import java.util.List; /** @@ -15,13 +12,13 @@ */ public @Data class MavenArtifactDeclaration implements ArtifactDeclaration { private final MavenArtifact artifact; - private final List dependencies = new ArrayList<>(); + private final List dependencies; - void resolveDependencies(DependencyManager dependencyManager) { + public void resolveDependencies(DependencyManager dependencyManager) { for (MavenArtifactDependency dependency : dependencies) { MavenArtifactDeclaration declaration = dependency.getDeclaration(); if (declaration == null) { - declaration = (MavenArtifactDeclaration) dependencyManager.resolveArtifact(dependency.getDeclaration().getArtifact()); + declaration = dependencyManager.resolveArtifact(dependency.getDeclaration().getArtifact()); dependency.setDeclaration(declaration); declaration.resolveDependencies(dependencyManager); } diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenArtifactDependency.java b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenArtifactDependency.java new file mode 100644 index 0000000..e59975e --- /dev/null +++ b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenArtifactDependency.java @@ -0,0 +1,16 @@ +package org.polyfrost.oneconfig.loader.stage1.dependency.maven; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.polyfrost.oneconfig.loader.stage1.dependency.model.ArtifactDependency; +import org.polyfrost.oneconfig.loader.stage1.dependency.model.DependencyExclusion; +import org.polyfrost.oneconfig.loader.stage1.dependency.model.Scope; + +@Getter +@RequiredArgsConstructor +public class MavenArtifactDependency implements ArtifactDependency { + @Setter private MavenArtifactDeclaration declaration; + private Scope scope; + private DependencyExclusion exclusions; +} diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenDependencyExclusion.java b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenDependencyExclusion.java new file mode 100644 index 0000000..12c5b65 --- /dev/null +++ b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenDependencyExclusion.java @@ -0,0 +1,25 @@ +package org.polyfrost.oneconfig.loader.stage1.dependency.maven; + +import lombok.Getter; +import org.polyfrost.oneconfig.loader.stage1.dependency.model.Artifact; +import org.polyfrost.oneconfig.loader.stage1.dependency.model.DependencyExclusion; + +@Getter +public class MavenDependencyExclusion implements DependencyExclusion { + private final String groupId; + private final String artifactId; + + public MavenDependencyExclusion(String groupId, String artifactId) { + this.groupId = groupId; + this.artifactId = artifactId; + } + + public MavenDependencyExclusion(Artifact artifact) { + this(artifact.getGroupId(), artifact.getArtifactId()); + } + + @Override + public boolean matches(Artifact artifact) { + return this.groupId.equals(artifact.getGroupId()) && this.artifactId.equals(artifact.getArtifactId()); + } +} diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenDependencyManager.java b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenDependencyManager.java index 04650ad..7a1a304 100644 --- a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenDependencyManager.java +++ b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/MavenDependencyManager.java @@ -4,10 +4,27 @@ import org.polyfrost.oneconfig.loader.stage1.dependency.DependencyManager; import org.polyfrost.oneconfig.loader.stage1.dependency.cache.CachingSolution; import org.polyfrost.oneconfig.loader.stage1.dependency.maven.cache.MavenCachingSolution; -import org.polyfrost.oneconfig.loader.stage1.util.XDG; +import org.polyfrost.oneconfig.loader.utils.IOUtils; +import org.polyfrost.oneconfig.loader.utils.RequestHelper; +import org.polyfrost.oneconfig.loader.utils.XDG; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URI; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; /** * @author xtrm @@ -19,15 +36,21 @@ public class MavenDependencyManager implements DependencyManager dependencyList = this.getDependency(Files.newInputStream(localPomPath)) + .stream() + .map((mavenArtifact) -> { + MavenArtifactDependency mavenArtifactDependency = new MavenArtifactDependency(); + mavenArtifactDependency.setDeclaration(new MavenArtifactDeclaration(mavenArtifact, new ArrayList<>())); + return mavenArtifactDependency; + }).collect(Collectors.toList()); + return new MavenArtifactDeclaration(artifact, dependencyList); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public InputStream createArtifactInputStream(MavenArtifact mavenArtifact) { + URI resolved = repository.resolve(mavenArtifact.getDeclaration()); + + try { + // assume resolved is URL + HttpURLConnection httpURLConnection = (HttpURLConnection) requestHelper.establishConnection(resolved.toURL()); + return httpURLConnection.getInputStream(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private List getDependency(InputStream pom) { + try { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.parse(pom); + Element documentElement = document.getDocumentElement(); + Element dependencies = (Element) documentElement.getElementsByTagName("dependencies").item(0); + NodeList dependencyList = dependencies.getElementsByTagName("dependency"); + + List list = new ArrayList<>(); + + for (int i = 0; i < dependencyList.getLength(); i++) { + Element dependency = (Element) dependencyList.item(i); + + String groupId = dependency.getElementsByTagName("groupId").item(0).getTextContent(); + String artifactId = dependency.getElementsByTagName("artifactId").item(0).getTextContent(); + String version = dependency.getElementsByTagName("version").item(0).getTextContent(); + + list.add( + new MavenArtifact( + groupId, + artifactId, + version, + null, + ".jar" + ) + ); + } + + return list; + } catch (ParserConfigurationException | IOException | SAXException e) { + throw new RuntimeException(e); } - return null; } } diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/cache/MavenCachingSolution.java b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/cache/MavenCachingSolution.java index 1d01e8f..881e2e2 100644 --- a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/cache/MavenCachingSolution.java +++ b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/dependency/maven/cache/MavenCachingSolution.java @@ -2,8 +2,8 @@ import lombok.RequiredArgsConstructor; import org.polyfrost.oneconfig.loader.stage1.dependency.cache.CachingSolution; -import org.polyfrost.oneconfig.loader.stage1.util.XDG; import org.polyfrost.oneconfig.loader.utils.RequestHelper; +import org.polyfrost.oneconfig.loader.utils.XDG; import java.net.HttpURLConnection; import java.net.URI; diff --git a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/SystemProperties.java b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/SystemProperties.java index b52d552..e810859 100644 --- a/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/SystemProperties.java +++ b/stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/util/SystemProperties.java @@ -1,6 +1,8 @@ package org.polyfrost.oneconfig.loader.stage1.util; import lombok.Getter; +import org.polyfrost.oneconfig.loader.utils.Lazy; +import org.polyfrost.oneconfig.loader.utils.XDG; import java.io.File; import java.net.URI;