From dd2a7be9e8f2bd3ede341dbc80aa64d1d37f446c Mon Sep 17 00:00:00 2001 From: Miki Rozloznik Date: Fri, 29 Nov 2024 11:23:37 +0100 Subject: [PATCH] Implement automatic loading of jar extensions --- .../src/zserio/tools/ExtensionManager.java | 84 ++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/compiler/core/src/zserio/tools/ExtensionManager.java b/compiler/core/src/zserio/tools/ExtensionManager.java index 658419e64..0752e33f3 100644 --- a/compiler/core/src/zserio/tools/ExtensionManager.java +++ b/compiler/core/src/zserio/tools/ExtensionManager.java @@ -1,8 +1,14 @@ package zserio.tools; +import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -24,9 +30,11 @@ final class ExtensionManager */ public ExtensionManager(CommandLineArguments commandLineArguments) { + final URLClassLoader urlClassLoader = getUrlClassLoader(); + final ClassLoader classLoader = (urlClassLoader != null) ? urlClassLoader : getClass().getClassLoader(); extensions = new ArrayList(); this.commandLineArguments = commandLineArguments; - ServiceLoader loader = ServiceLoader.load(Extension.class, getClass().getClassLoader()); + ServiceLoader loader = ServiceLoader.load(Extension.class, classLoader); Iterator it = loader.iterator(); while (it.hasNext()) { @@ -94,6 +102,80 @@ public void callExtensions(Root rootNode, ExtensionParameters parameters) throws } } + private URLClassLoader getUrlClassLoader() + { + final String workingDirectory = getWorkingDirectory(); + if (workingDirectory == null) + return null; + + final File[] fileList = new File(workingDirectory).listFiles(); + if (fileList == null) + return null; + + final ArrayList urlArray = new ArrayList(); + try + { + for (File file : fileList) + { + if (isFileZserioExtension(file)) + { + System.out.println("File = " + file); + urlArray.add(new URL("file:" + file.getPath())); + } + } + } + catch (MalformedURLException excpt) + { + return null; + } + + final URLClassLoader urlClassLoader = new URLClassLoader(urlArray.toArray(new URL[0])); + + return urlClassLoader; + } + + private String getWorkingDirectory() + { + final String execPath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); + String decodedExecPath = null; + try + { + decodedExecPath = URLDecoder.decode(execPath, "UTF-8"); + } + catch (UnsupportedEncodingException excpt) + { + return null; + } + + final String workingDirectory = new File(decodedExecPath).getParent(); + + return workingDirectory; + } + + private boolean isFileZserioExtension(File file) + { + if (!file.isFile()) + return false; + + final String fileName = file.getName(); + if (!fileName.endsWith(".jar")) + return false; + + if (!fileName.startsWith("zserio_")) + return false; + + if (fileName.equals("zserio_core.jar")) + return false; + + if (fileName.endsWith("_javadocs.jar")) + return false; + + if (fileName.endsWith("_sources.jar")) + return false; + + return true; + } + private static void check(Root rootNode, Extension extension, ExtensionParameters parameters) throws ZserioExtensionException {