Skip to content

Commit

Permalink
feat: module load by spi (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh authored Jul 4, 2022
1 parent a6a1676 commit 46b3f79
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 47 deletions.
69 changes: 22 additions & 47 deletions src/main/java/org/itxtech/mcl/module/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import org.itxtech.mcl.Loader;

import java.io.File;
import java.lang.reflect.Modifier;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.ServiceLoader;
import java.util.jar.JarFile;

/*
Expand Down Expand Up @@ -76,56 +74,33 @@ public void loadAllModules() throws Exception {
return;
}

for (var pkg : loader.config.modulePackages) {
var file = pkg.split(":");
var clzPkg = file[1].replace(".", "/") + "/";
String filename;
JarFile jarFile;
if (file[0].equals("mcl")) {
var jar = new File(URLDecoder.decode(ModuleManager.class.getProtectionDomain()
.getCodeSource().getLocation().getFile(), StandardCharsets.UTF_8));
filename = jar.getName();
jarFile = new JarFile(jar);
} else {
var jar = new File("modules/" + file[0] + ".jar");
filename = jar.getName();
jarFile = new JarFile(jar);
Agent.appendJarFile(jarFile);
var folder = new File("modules");
folder.mkdir();

var list = folder.listFiles(file -> file.getName().endsWith(".jar"));
if (list != null) {
for (var file : list) {
var jar = new JarFile(file);
Agent.appendJarFile(jar);
}
var entries = jarFile.entries();
while (entries.hasMoreElements()) {
var entry = entries.nextElement().getRealName();
if (entry.startsWith(clzPkg) && entry.endsWith(".class") && !entry.contains("$")) {
try {
var clz = Class.forName(
entry.replace("/", ".").replace(".class", ""),
false,
ClassLoader.getSystemClassLoader()
);
}

if (!MclModule.class.isAssignableFrom(clz)) {
loader.logger.debug("Skipped " + clz.getName() + " from " + filename + " because it's not a mcl module.");
continue;
}
if (clz.isInterface() || Modifier.isAbstract(clz.getModifiers())) {
loader.logger.debug("Skipped " + clz.getName() + " from " + filename + " because it's abstract.");
continue;
}
var serviceLoader = ServiceLoader.load(MclModule.class);

var module = (MclModule) clz.getDeclaredConstructor().newInstance();
if (!loader.config.disabledModules.contains(module.getName())) {
loader.logger.debug("Loading module: \"" + module.getName() + "\" from \"" + filename + "\". Class: " + module.getClass().getCanonicalName());
modules.put(module.getName(), module);
serviceLoader.stream().forEach(provider -> {
try {
var module = provider.get();
if (!loader.config.disabledModules.contains(module.getName())) {
loader.logger.debug("Loading module: \"" + module.getName() + "\". Class: " + module.getClass().getCanonicalName());
modules.put(module.getName(), module);

module.init(loader);
module.prepare();
}
} catch (Exception e) {
loader.logger.logException(e);
}
module.init(loader);
module.prepare();
}
} catch (Exception e) {
loader.logger.logException(e);
}
}
});
if (modules.size() == 0) {
loader.logger.warning("No module has been loaded. Exiting.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
org.itxtech.mcl.module.builtin.Addon
org.itxtech.mcl.module.builtin.Announcement
org.itxtech.mcl.module.builtin.Boot
org.itxtech.mcl.module.builtin.Conf
org.itxtech.mcl.module.builtin.MDownloader
org.itxtech.mcl.module.builtin.Mrm
org.itxtech.mcl.module.builtin.OracleJdk
org.itxtech.mcl.module.builtin.PkgAnn
org.itxtech.mcl.module.builtin.Repo
org.itxtech.mcl.module.builtin.RepoCache
org.itxtech.mcl.module.builtin.Updater

0 comments on commit 46b3f79

Please sign in to comment.