Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Module Logic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianDev committed Dec 2, 2023
1 parent 80c3b71 commit 59e57b7
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class NetworkModule implements Module {
private ModuleMeta meta = null;
private File file = null;
private JarArchive archive = null;
private ClassLoader classLoader = null;
private ModuleClassLoader classLoader = null;
private ModuleStatus status = ModuleStatus.INACTIVE;
private Logger logger = null;
private boolean loaded = false;
Expand Down Expand Up @@ -73,6 +73,10 @@ public final File getFile() {

@Override
public final @NotNull ClassLoader getClassLoader() {
return (ClassLoader) this.classLoader;
}

public final @NotNull ModuleClassLoader getModuleClassLoader() {
return this.classLoader;
}

Expand All @@ -83,7 +87,7 @@ public final ModuleStatus getStatus() {
}

@ApiStatus.Internal
public final void initialize(@NotNull ModuleMeta meta, @NotNull File file, @NotNull JarArchive archive, @NotNull ClassLoader classLoader, @NotNull Logger logger) {
public final void initialize(@NotNull ModuleMeta meta, @NotNull File file, @NotNull JarArchive archive, @NotNull ModuleClassLoader classLoader, @NotNull Logger logger) {
if (this.loaded)
throw new UnsupportedOperationException("Module has already been initialized!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public interface GroupClassLoader {
*/
void addLoader(Module module, ClassLoader loader);

/**
* Adds a module class loader to the group classloader's classpath.
* @param loader module's class loader.
*/
void addModuleLoader(ModuleClassLoader loader);

/**
* Streams all the {@link ClassLoader}s stored in the group class loader.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fr.atlasworld.network.api.module.exception.unchecked;

public class LoadingModuleException extends ExecutionModuleException {
public LoadingModuleException() {
super();
}

public LoadingModuleException(String message) {
super(message);
}

public LoadingModuleException(String message, Throwable cause) {
super(message, cause);
}

public LoadingModuleException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ protected void onActivate(@NotNull ModuleActivationContext ctx) {
}

ctx.getServer().getCommandManager().register(this, MyTestCommand::register);

throw new UnsupportedOperationException("Just for testing.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ private Class<?> loadOtherModuleClass(@NotNull String name, @NotNull Collection<
return results;
}

public void addModuleLoader(ModuleClassLoader loader) {
this.moduleLoaders.putIfAbsent(loader.getModule(), loader);
}

@Override
public void addLoader(Module module, ClassLoader loader) {
this.externalLoaders.putIfAbsent(module, new LinkedList<>());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.atlasworld.network.core.module.provider;

import fr.atlasworld.network.api.module.exception.unchecked.LoadingModuleException;
import fr.atlasworld.network.api.util.archive.JarArchive;
import fr.atlasworld.network.core.module.provider.factory.FileModuleFactory;
import fr.atlasworld.network.core.module.provider.factory.ModuleFactory;
Expand All @@ -15,7 +16,6 @@
* Load Modules from files.
*/
public class FileModuleProvider implements ModuleProvider<File> {
private static final Logger LOGGER = LogUtils.getLogger();

@Override
public void provide(File ctx, ModuleEntryStore.ModuleEntryHolderPool pool) throws IOException {
Expand All @@ -32,7 +32,7 @@ public void provide(File ctx, ModuleEntryStore.ModuleEntryHolderPool pool) throw
ModuleFactory factory = FileModuleFactory.createFactory(new JarArchive(jar, ctx));
pool.register(factory);
} catch (Exception e) {
throw new RuntimeException(ctx.getName() + " failed to load!", e);
throw new LoadingModuleException(ctx.getName() + " failed to load!", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ public static SystemModuleClassLoader createClassLoader(JarArchive archive, Modu
ClassPathLoader libraryClassLoader, Logger moduleLogger,
GroupClassLoader gClassLoader) throws IOException {

return new SystemModuleClassLoader(archive.getFile().toPath(), config, parent, libraryClassLoader, gClassLoader,
SystemModuleClassLoader loader = new SystemModuleClassLoader(archive.getFile().toPath(), config, parent, libraryClassLoader, gClassLoader,
archive, moduleLogger);

return loader;
}

public static SystemModuleClassLoader createClassLoader(JarArchive archive, ModuleMeta config, URL[] libraries, Logger moduleLogger) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.atlasworld.network.core.module.store;

import fr.atlasworld.network.api.module.exception.unchecked.LoadingModuleException;
import fr.atlasworld.network.core.logging.LogUtils;
import fr.atlasworld.network.core.module.provider.DirectoryModuleProvider;
import org.slf4j.Logger;
Expand Down Expand Up @@ -39,6 +40,8 @@ public void load(ModuleEntryStore holder) {
LOGGER.error("Failed to load modules: ", e);
} catch (UncheckedIOException e) {
LOGGER.error("Failed to load module: ", e);
} catch (LoadingModuleException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.atlasworld.network.api.module.exception.unchecked.ExecutionModuleException;
import fr.atlasworld.network.api.module.exception.unchecked.UncheckedModuleException;
import fr.atlasworld.network.api.module.lifecycle.ModuleActivationContext;
import fr.atlasworld.network.core.module.loader.SystemGroupClassLoader;
import fr.atlasworld.network.core.module.provider.factory.ModuleFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -46,6 +47,7 @@ public void activeModules(ModuleActivationContext ctx) {
module.activate(ctx);
} catch (Throwable throwable) {
LOGGER.error("Module '{}' activation failed:", module.getMeta().getId(), new ExecutionModuleException(throwable));
this.loadedModules.remove(module);
module.crash(throwable);
}
}
Expand Down Expand Up @@ -79,7 +81,11 @@ private Collection<NetworkModule> create() {
return this.savedFactories.values().stream()
.map(factory -> {
try {
return factory.create();
NetworkModule module = factory.create();

SystemGroupClassLoader.getGroupLoader().addModuleLoader(module.getModuleClassLoader()); // Add to the group

return module;
} catch (UncheckedModuleException e) {
LOGGER.error("Failed to load '{}'!", factory.getConfig().getId(), e.getCause());
return null;
Expand Down

0 comments on commit 59e57b7

Please sign in to comment.