-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from DragonRex004/master
Dependency Loader
- Loading branch information
Showing
8 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,5 @@ dependency-reduced-pom.xml | |
|
||
### Testing ### | ||
config.json | ||
config.toml | ||
Dependencys/ |
8 changes: 7 additions & 1 deletion
8
smoothcloud-launcher/src/main/java/eu/smoothcloud/launcher/SmoothCloudLauncher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package eu.smoothcloud.launcher; | ||
|
||
import eu.smoothcloud.launcher.dependency.*; | ||
|
||
public class SmoothCloudLauncher { | ||
|
||
public static void main(String[] args) { | ||
|
||
DependencyLoader.loadExternalDependencys(); | ||
System.out.println("Finished Downloading Default Dependencys"); | ||
System.out.println("Begin Download for Internal Dependencys"); | ||
InternalDepLoader.loadInternalDependencys(); | ||
System.out.println("Finished Dependency Downloading!"); | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
smoothcloud-launcher/src/main/java/eu/smoothcloud/launcher/dependency/Dependency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
smoothcloud-launcher/src/main/java/eu/smoothcloud/launcher/dependency/DependencyLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package eu.smoothcloud.launcher.dependency; | ||
|
||
import java.io.File; | ||
|
||
public class DependencyLoader { | ||
private static final String MAIN_FOLDER = "Dependencys/"; | ||
private static final String JAR_EXT = ".jar"; | ||
|
||
public static void loadExternalDependencys() { | ||
for (Dependency dep : Dependency.values()) { | ||
String link = MavenBuilder.buildMavenLink(dep); | ||
String depDir = MAIN_FOLDER + dep.getGroupId() + "." + dep.getArtifactId(); | ||
String depFileName = dep.getVersion() + JAR_EXT; | ||
System.out.println("Try to Download " + depFileName); | ||
if(new File(depDir, depFileName).exists()) { | ||
System.out.println("Skip Dependency " + depFileName); | ||
continue; | ||
} | ||
Downloader.download( | ||
link, | ||
depDir, | ||
depFileName | ||
); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
smoothcloud-launcher/src/main/java/eu/smoothcloud/launcher/dependency/Downloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package eu.smoothcloud.launcher.dependency; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
public class Downloader { | ||
|
||
public static void download(String link, String targetDir, String targetFileName) { | ||
File downloadDir = new File(targetDir); | ||
if(!downloadDir.exists()) | ||
downloadDir.mkdirs(); | ||
if(!downloadDir.isDirectory()) | ||
return; | ||
System.out.println("Begin Download!"); | ||
try (BufferedInputStream input = new BufferedInputStream(new URL(link).openStream())) { | ||
FileOutputStream fileOutputStream = new FileOutputStream(new File(targetDir, targetFileName)); | ||
byte dataBuffer[] = new byte[1024]; | ||
int bytesRead; | ||
int totalBytes = 0; | ||
while ((bytesRead = input.read(dataBuffer, 0, 1024)) != -1) { | ||
fileOutputStream.write(dataBuffer, 0, bytesRead); | ||
totalBytes += bytesRead; | ||
} | ||
System.out.println("Download Finished! Bytes Written: " + totalBytes / 1024 + " KB"); | ||
} catch (IOException e) { | ||
System.out.println("Unavailable Link"); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
smoothcloud-launcher/src/main/java/eu/smoothcloud/launcher/dependency/InternalDepLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package eu.smoothcloud.launcher.dependency; | ||
|
||
import com.sun.tools.javac.Main; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
public class InternalDepLoader { | ||
private static final String MAIN_FOLDER = "Dependencys/"; | ||
private static final String JAR_EXT = ".jar"; | ||
private static final String MANIFEST_URL = "https://cdn.smoothcloud.eu/dependencies/manifest.json"; | ||
|
||
|
||
public static void loadInternalDependencys() { | ||
try { | ||
File jarFile = new File( | ||
MAIN_FOLDER + Dependency.JSON.getGroupId() + "." + Dependency.JSON.getArtifactId(), | ||
Dependency.JSON.getVersion() + JAR_EXT | ||
); | ||
URL jarUrl = jarFile.toURI().toURL(); | ||
|
||
StringBuilder jsonData = new StringBuilder(); | ||
HttpURLConnection connection = (HttpURLConnection) new URL(MANIFEST_URL).openConnection(); | ||
connection.setRequestMethod("GET"); | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
jsonData.append(line); | ||
} | ||
} | ||
|
||
try (URLClassLoader classLoader = new URLClassLoader(new URL[] { jarUrl }, Main.class.getClassLoader())) { | ||
Class<?> jsonArrayClass = classLoader.loadClass("org.json.JSONArray"); | ||
Class<?> jsonObjectClass = classLoader.loadClass("org.json.JSONObject"); | ||
|
||
Object jsonArray = jsonArrayClass.getDeclaredConstructor(String.class).newInstance(jsonData.toString()); | ||
|
||
int length = (int) jsonArrayClass.getMethod("length").invoke(jsonArray); | ||
for (int i = 0; i < length; i++) { | ||
Object jsonObject = jsonArrayClass.getMethod("get", int.class).invoke(jsonArray, i); | ||
|
||
String name = (String) jsonObjectClass.getMethod("getString", String.class).invoke(jsonObject, | ||
"name"); | ||
String version = (String) jsonObjectClass.getMethod("getString", String.class).invoke(jsonObject, | ||
"version"); | ||
String url = "https://cdn.smoothcloud.eu/dependencies/" + name + "-" + version + ".jar"; | ||
|
||
String inDepDir = MAIN_FOLDER + name; | ||
String inDepFileName = name + "-" + version + JAR_EXT; | ||
if(new File(inDepDir, inDepFileName).exists()) { | ||
System.out.println("Skip Dependency " + inDepFileName); | ||
continue; | ||
} | ||
File tempFile = new File(inDepDir); | ||
if(tempFile.exists()) { | ||
for (File file : tempFile.listFiles()) { | ||
file.delete(); | ||
} | ||
} | ||
Downloader.download( | ||
url, | ||
inDepDir, | ||
inDepFileName | ||
); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
smoothcloud-launcher/src/main/java/eu/smoothcloud/launcher/dependency/MavenBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package eu.smoothcloud.launcher.dependency; | ||
|
||
public class MavenBuilder { | ||
public static String buildMavenLink(Dependency dependency) { | ||
return String.format( | ||
Repository.MAVEN.getLink(), | ||
dependency.getGroupId().replace(".", "/"), | ||
dependency.getArtifactId(), | ||
dependency.getVersion(), | ||
dependency.getArtifactId(), | ||
dependency.getVersion() | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
smoothcloud-launcher/src/main/java/eu/smoothcloud/launcher/dependency/Repository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package eu.smoothcloud.launcher.dependency; | ||
|
||
public enum Repository { | ||
MAVEN("https://repo1.maven.org/maven2/%s/%s/%s/%s-%s.jar"); | ||
|
||
private final String link; | ||
|
||
Repository(String link) { | ||
this.link = link; | ||
} | ||
|
||
public String getLink() { | ||
return link; | ||
} | ||
} |