Skip to content

Commit

Permalink
feat: extension that does not have main class is now supported
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Oct 9, 2024
1 parent 18ec055 commit fe67e2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ public void loadExtensions(String[] args) {
}

private void loadExtension(Path extensionPath, String[] args) {
log.info(I18n.get().tr(TrKeys.A_EXTENSION_LOADING, extensionPath));
log.info(I18n.get().tr(TrKeys.A_EXTENSION_LOADING, extensionPath.getFileName()));
Allay.EXTRA_RESOURCE_CLASS_LOADER.addJar(extensionPath);

// Try to load the main class of the extension if it exists
var mainClass = findMainClass(extensionPath);
if (mainClass == null) {
// Main class can be null
return;
}

if (!Extension.class.isAssignableFrom(mainClass)) {
throw new ExtensionException(I18n.get().tr(TrKeys.A_EXTENSION_MAINCLASS_TYPEINVALID, mainClass.getName()));
}
Expand All @@ -57,8 +65,11 @@ private void loadExtension(Path extensionPath, String[] args) {
private Class<?> findMainClass(Path extensionPath) {
var jarFileSystem = FileSystems.newFileSystem(extensionPath);
try {
var entrance = (String) JSONUtils.fromMap(Files.readString(jarFileSystem.getPath("extension.json"))).get("entrance");
Allay.EXTRA_RESOURCE_CLASS_LOADER.addJar(extensionPath);
var extensionDescriptorPath = jarFileSystem.getPath("extension.json");
if (!Files.exists(extensionDescriptorPath)) {
return null;
}
var entrance = (String) JSONUtils.fromMap(Files.readString(extensionDescriptorPath)).get("entrance");
return Allay.EXTRA_RESOURCE_CLASS_LOADER.loadClass(entrance);
} catch (ClassNotFoundException e1) {
throw new ExtensionException(I18n.get().tr(TrKeys.A_EXTENSION_ENTRANCE_MISSING, extensionPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,12 @@ public DynamicURLClassLoader(URL[] urls, ClassLoader parent) {
public void addURL(URL url) {
super.addURL(url);
}

public void addJar(File jarFile) {
Preconditions.checkNotNull(jarFile);
public void addJar(Path jarPath) {
Preconditions.checkNotNull(jarPath);
try {
addURL(jarFile.toURI().toURL());
addURL(jarPath.toUri().toURL());
} catch (MalformedURLException e) {
log.error(e.getCause().getMessage());
}
}

public void addJar(Path jarPath) {
Preconditions.checkNotNull(jarPath);
addJar(jarPath.toFile());
}

public void addJar(String jarPath) {
Preconditions.checkNotNull(jarPath);
addJar(new File(jarPath));
}
}

0 comments on commit fe67e2a

Please sign in to comment.