Skip to content

Commit 287276d

Browse files
committed
Added LaunchTarget system, should help alot with future work...
MangoBotLaunchTarget.java will be the built in launch target. Only is valid if nothing else exists...
1 parent 468cbdb commit 287276d

File tree

5 files changed

+149
-36
lines changed

5 files changed

+149
-36
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ plugins {
2525
apply plugin: 'maven-publish'
2626

2727
group = 'org.mangorage'
28-
version = getLatestGitTag() + "." + getLatestGitVersion()
28+
version = getLatestGitTag() + "." + getLatestGitVersion() + "-beta"
2929

3030
println("Version -> " + version)
3131

src/main/java/module-info.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
opens org.mangorage.bootstrap;
66
exports org.mangorage.bootstrap.api.transformer;
77
exports org.mangorage.bootstrap.api.module;
8+
exports org.mangorage.bootstrap.api.launch;
89

910
uses org.mangorage.bootstrap.api.transformer.IClassTransformer;
1011
uses org.mangorage.bootstrap.api.module.IModuleConfigurator;
12+
uses org.mangorage.bootstrap.api.launch.ILaunchTarget;
13+
14+
provides org.mangorage.bootstrap.api.launch.ILaunchTarget with org.mangorage.bootstrap.internal.impl.MangoBotLaunchTarget;
1115
}
Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
package org.mangorage.bootstrap;
22

3-
import org.mangorage.bootstrap.internal.JarHandler;
4-
import org.mangorage.bootstrap.internal.MangoLoader;
3+
import org.mangorage.bootstrap.api.launch.ILaunchTarget;
54
import org.mangorage.bootstrap.internal.Util;
6-
7-
import java.io.IOException;
5+
import org.mangorage.bootstrap.internal.impl.MangoBotLaunchTarget;
86
import java.lang.module.Configuration;
97
import java.lang.module.ModuleFinder;
108
import java.nio.file.Files;
119
import java.nio.file.Path;
10+
import java.util.HashMap;
1211
import java.util.List;
13-
14-
import static org.mangorage.bootstrap.internal.Util.*;
12+
import java.util.Map;
13+
import java.util.ServiceLoader;
14+
import java.util.Set;
1515

1616
public final class Bootstrap {
1717

18-
public static void main(String[] args) throws IOException {
18+
public static void main(String[] args) throws Throwable {
19+
if (!(args.length >= 2)) {
20+
throw new IllegalStateException("Need to define a launchTarget, --launchTarget mangobot");
21+
}
22+
23+
if (!args[0].equals("--launchTarget")) {
24+
throw new IllegalStateException("Need to have --launchTarget be defined first...");
25+
}
26+
27+
final var launchTarget = args[1];
28+
1929
ModuleLayer parent = null;
2030

2131
if (Bootstrap.class.getModule() != null) {
@@ -24,39 +34,52 @@ public static void main(String[] args) throws IOException {
2434
parent = ModuleLayer.boot(); // We dont have a module for Bootstrap..., so assume we are using the boot layer...
2535
}
2636

27-
final var librariesPath = Path.of("libraries");
28-
final var pluginsPath = Path.of("plugins");
29-
30-
JarHandler.safeHandle(Path.of("libraries"), Path.of("sorted-libraries"));
31-
32-
List<Path> deleteFiles = List.of(
33-
Path.of("sorted-libraries").resolve("okio-3.6.0.jar")
37+
// Where additional launch targets can be defined...
38+
Path launchPath = Path.of(
39+
"launch"
3440
);
3541

36-
for (Path deleteFile : deleteFiles) {
37-
Files.deleteIfExists(deleteFile);
38-
}
39-
40-
final var moduleCfg = Configuration.resolve(
41-
ModuleFinder.of(pluginsPath),
42-
List.of(
43-
parent.configuration()
44-
),
45-
ModuleFinder.of(
46-
Path.of("sorted-libraries")
47-
),
48-
Util.getModuleNames(pluginsPath)
42+
final var moduleCfg = Configuration
43+
.resolveAndBind(
44+
ModuleFinder.of(
45+
launchPath
46+
),
47+
List.of(
48+
parent.configuration()
49+
),
50+
ModuleFinder.of(),
51+
Files.exists(launchPath) ?
52+
Util.getModuleNames(
53+
launchPath
54+
) : Set.of()
55+
);
56+
57+
final var moduleLayerController = ModuleLayer.defineModulesWithOneLoader(
58+
moduleCfg,
59+
List.of(parent),
60+
Thread.currentThread().getContextClassLoader()
4961
);
5062

51-
final var moduleCl = new MangoLoader(moduleCfg.modules(), Thread.currentThread().getContextClassLoader());
52-
53-
final var moduleLayerController = ModuleLayer.defineModules(moduleCfg, List.of(parent), s -> moduleCl);
54-
final var moduleLayer = moduleLayerController.layer();
55-
56-
Thread.currentThread().setContextClassLoader(moduleCl);
63+
final Map<String, ILaunchTarget> launchTargetMap = new HashMap<>();
64+
ServiceLoader.load(ILaunchTarget.class)
65+
.stream()
66+
.forEach(provider -> {
67+
if (provider.type() != MangoBotLaunchTarget.class) {
68+
final var target = provider.get();
69+
launchTargetMap.put(target.getId(), target);
70+
}
71+
});
72+
73+
// Only add if we dont have any other launch targets...
74+
if (launchTargetMap.isEmpty()) {
75+
final var defaultLaunchTarget = new MangoBotLaunchTarget();
76+
launchTargetMap.put(defaultLaunchTarget.getId(), defaultLaunchTarget);
77+
}
5778

58-
moduleCl.load();
79+
if (!launchTargetMap.containsKey(launchTarget)) {
80+
throw new IllegalStateException("Cant find launch target '%s'".formatted(launchTarget));
81+
}
5982

60-
callMain("org.mangorage.entrypoint.MangoBotCore", args, moduleLayer.findModule("org.mangorage.mangobotcore").get());
83+
launchTargetMap.get(launchTarget).launch(parent, args);
6184
}
6285
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.mangorage.bootstrap.api.launch;
2+
3+
public interface ILaunchTarget {
4+
String getId();
5+
6+
void launch(ModuleLayer parent, String[] args) throws Throwable;
7+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.mangorage.bootstrap.internal.impl;
2+
3+
import org.mangorage.bootstrap.api.launch.ILaunchTarget;
4+
import org.mangorage.bootstrap.internal.JarHandler;
5+
import org.mangorage.bootstrap.internal.MangoLoader;
6+
import org.mangorage.bootstrap.internal.Util;
7+
8+
import java.lang.module.Configuration;
9+
import java.lang.module.ModuleFinder;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.util.List;
13+
14+
import static org.mangorage.bootstrap.internal.Util.callMain;
15+
16+
public final class MangoBotLaunchTarget implements ILaunchTarget {
17+
@Override
18+
public String getId() {
19+
return "mangobot";
20+
}
21+
22+
@Override
23+
public void launch(ModuleLayer parent, String[] args) throws Throwable {
24+
final var librariesPath = Path.of("libraries");
25+
final var sortedLibraries = Path.of("sorted-libraries");
26+
final var pluginsPath = Path.of("plugins");
27+
28+
JarHandler.safeHandle(librariesPath, sortedLibraries);
29+
30+
List<Path> deleteFiles = List.of(
31+
sortedLibraries.resolve("okio-3.6.0.jar")
32+
);
33+
34+
for (Path deleteFile : deleteFiles) {
35+
Files.deleteIfExists(deleteFile);
36+
}
37+
38+
final var moduleLibrariesCfg = Configuration.resolve(
39+
ModuleFinder.of(sortedLibraries),
40+
List.of(
41+
parent.configuration()
42+
),
43+
ModuleFinder.of(),
44+
Util.getModuleNames(
45+
sortedLibraries
46+
)
47+
);
48+
49+
final var moduleLibrariesCL = new MangoLoader(moduleLibrariesCfg.modules(), Thread.currentThread().getContextClassLoader());
50+
51+
final var moduleLibrariesLayerController = ModuleLayer.defineModules(moduleLibrariesCfg, List.of(parent), s -> moduleLibrariesCL);
52+
final var moduleLibrariesLayer = moduleLibrariesLayerController.layer();
53+
54+
55+
56+
final var modulePluginsCfg = Configuration.resolve(
57+
ModuleFinder.of(pluginsPath),
58+
List.of(
59+
moduleLibrariesLayer.configuration()
60+
),
61+
ModuleFinder.of(),
62+
Util.getModuleNames(
63+
pluginsPath
64+
)
65+
);
66+
67+
final var modulePluginsCL = new MangoLoader(modulePluginsCfg.modules(), moduleLibrariesCL);
68+
69+
final var modulePluginsLayerController = ModuleLayer.defineModules(modulePluginsCfg, List.of(moduleLibrariesLayer), s -> modulePluginsCL);
70+
final var modulePluginsLayer = modulePluginsLayerController.layer();
71+
72+
Thread.currentThread().setContextClassLoader(modulePluginsCL);
73+
74+
moduleLibrariesCL.load();
75+
modulePluginsCL.load();
76+
77+
callMain("org.mangorage.entrypoint.MangoBotCore", args, modulePluginsLayer.findModule("org.mangorage.mangobotcore").get());
78+
}
79+
}

0 commit comments

Comments
 (0)