Skip to content

Commit 5f67fac

Browse files
committed
Some cleanup...
1 parent ee36074 commit 5f67fac

File tree

2 files changed

+52
-50
lines changed

2 files changed

+52
-50
lines changed

src/main/java/org/mangorage/bootstrap/Bootstrap.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public static void main(String[] args) throws IOException {
2121

2222
cfg.handleJars();
2323

24-
final var cl = cfg.constructClassloaders();
25-
final var moduleLayer = cfg.constructModuleLayer(cl);
24+
final var moduleCfg = cfg.constructModuleConfiguration();
2625

26+
final var cl = cfg.constructClassloaders();
2727
Thread.currentThread().setContextClassLoader(cl);
28+
29+
final var moduleLayer = ModuleLayer.boot().defineModulesWithOneLoader(moduleCfg, cl);
2830
callMain(cfg.getMainClass(), args, cl, moduleLayer);
2931
}
3032
}

src/main/java/org/mangorage/bootstrap/internal/Config.java

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.io.UncheckedIOException;
99
import java.lang.module.Configuration;
1010
import java.lang.module.ModuleFinder;
11-
import java.net.MalformedURLException;
1211
import java.net.URL;
1312
import java.net.URLClassLoader;
1413
import java.nio.file.Files;
@@ -56,16 +55,20 @@ public static Config readFromInputStream(InputStream stream) {
5655
* @throws IOException
5756
*/
5857
public void handleJars() throws IOException {
59-
String[] folders = cfg.getOrDefault("Sorted-Path", "").split(":");
58+
if (cfg.containsKey("Sorted-Path")) {
59+
String[] folders = cfg.get("Sorted-Path").split(":");
6060

61-
for (final var folder : folders) {
62-
final Path folderPath = Path.of(folder);
63-
JarHandler.handle(folderPath, Path.of(getSortedPath(folder)));
61+
for (final var folder : folders) {
62+
final Path folderPath = Path.of(folder);
63+
JarHandler.handle(folderPath, Path.of(getSortedPath(folder)));
64+
}
6465
}
6566

66-
String[] filesToDelete = cfg.getOrDefault("Delete-Files", "").split(":");
67-
for (final var path : filesToDelete) {
68-
Files.deleteIfExists(Path.of(path));
67+
if (cfg.containsKey("Delete-Files")) {
68+
String[] filesToDelete = cfg.get("Delete-Files").split(":");
69+
for (final var path : filesToDelete) {
70+
Files.deleteIfExists(Path.of(path));
71+
}
6972
}
7073
}
7174

@@ -80,17 +83,18 @@ public void handleJars() throws IOException {
8083
* @return The last URLClassLoader in the constructed chain, or the system
8184
* classloader if the Classloader-Path is empty or invalid.
8285
*/
83-
public URLClassLoader constructClassloaders() {
86+
public ClassLoader constructClassloaders() {
8487
final var baseDir = Path.of("");
85-
String classloaderPath = cfg.getOrDefault("Classloader-Path", "");
88+
String classloaderPath = cfg.get("Classloader-Path");
89+
if (classloaderPath == null)
90+
throw new IllegalArgumentException("Need to define Classloader-Path");
8691

8792
if (classloaderPath.trim().isEmpty()) {
88-
System.out.println("Classloader-Path is empty. Using system classloader.");
89-
return (URLClassLoader) ClassLoader.getSystemClassLoader();
93+
throw new IllegalStateException("Classloader-Path is empty. Define it...");
9094
}
9195

9296
String[] folderNames = classloaderPath.split(":");
93-
ClassLoader parentClassLoader = ClassLoader.getSystemClassLoader();
97+
ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader().getParent();
9498
URLClassLoader currentClassLoader = null;
9599

96100
for (String folderName : folderNames) {
@@ -111,51 +115,47 @@ public URLClassLoader constructClassloaders() {
111115
continue;
112116
}
113117

114-
try {
115-
// Create a URL for the folder
116-
URL folderUrl = folderFile.toURI().toURL();
117-
118-
// Create a new URLClassLoader with the current folder URL and the parent
119-
URL[] urls = new URL[]{folderUrl};
120-
currentClassLoader = new URLClassLoader(urls, parentClassLoader);
118+
// Create a new URLClassLoader with the current folder URL and the parent
119+
URL[] urls = Util.fetchJars(new File[]{folderFile});
120+
currentClassLoader = new URLClassLoader(urls, parentClassLoader);
121121

122-
// The newly created classloader becomes the parent for the next iteration
123-
parentClassLoader = currentClassLoader;
122+
// The newly created classloader becomes the parent for the next iteration
123+
parentClassLoader = currentClassLoader;
124124

125-
} catch (MalformedURLException e) {
126-
System.err.println("Error creating URL for path " + folderPath.toAbsolutePath() + ": " + e.getMessage());
127-
// Again, depending on requirements, you might want to handle this differently
128-
}
129125
}
130126

131127
// Return the last classloader created, or the system classloader if none were created
132-
return (currentClassLoader != null) ? currentClassLoader : (URLClassLoader) ClassLoader.getSystemClassLoader();
128+
return (currentClassLoader != null) ? currentClassLoader : Thread.currentThread().getContextClassLoader().getParent();
133129
}
134130

135-
public ModuleLayer constructModuleLayer(final ClassLoader classLoader) {
136-
String[] modulePaths = cfg.getOrDefault("Module-Path", "").split(":");
137-
Path[] paths = Arrays.stream(modulePaths)
138-
.map(Path::of)
139-
.toArray(Path[]::new);
140-
141-
Path[] rootPaths = Arrays.stream(cfg.getOrDefault("Root-Module-Path", "").split(":"))
142-
.map(Path::of)
143-
.toArray(Path[]::new);
144-
145-
ModuleFinder moduleFinder = ModuleFinder.of(paths);
146-
Configuration moduleCfg = ModuleLayer.boot()
147-
.configuration()
148-
.resolveAndBind(
149-
moduleFinder,
150-
ModuleFinder.of(),
151-
rootPaths.length != 0 ? Util.getModuleNames(rootPaths[0]) : Set.of()
152-
);
153-
154-
return ModuleLayer.boot().defineModulesWithOneLoader(moduleCfg, classLoader);
131+
public Configuration constructModuleConfiguration() {
132+
if (cfg.containsKey("Module-Path")) {
133+
String[] modulePaths = cfg.get("Module-Path").split(":");
134+
135+
Path[] paths = Arrays.stream(modulePaths)
136+
.map(Path::of)
137+
.toArray(Path[]::new);
138+
139+
Path[] rootPaths = Arrays.stream(cfg.getOrDefault("Root-Module-Path", "").split(":"))
140+
.map(Path::of)
141+
.toArray(Path[]::new);
142+
143+
ModuleFinder moduleFinder = ModuleFinder.of(paths);
144+
145+
return ModuleLayer.boot()
146+
.configuration()
147+
.resolveAndBind(
148+
moduleFinder,
149+
ModuleFinder.of(),
150+
rootPaths.length != 0 ? Util.getModuleNames(rootPaths[0]) : Set.of()
151+
);
152+
} else {
153+
throw new IllegalStateException("Module-Path needs to be defined!");
154+
}
155155
}
156156

157157
public String getMainClass() {
158-
return cfg.getOrDefault("Main-Class", "");
158+
return cfg.get("Main-Class");
159159
}
160160

161161
public static String getSortedPath(String originalPath) {

0 commit comments

Comments
 (0)