Skip to content

Commit

Permalink
Implement automatic loading of jar extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Nov 29, 2024
1 parent 4f2375f commit dd2a7be
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion compiler/core/src/zserio/tools/ExtensionManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Extension>();
this.commandLineArguments = commandLineArguments;
ServiceLoader<Extension> loader = ServiceLoader.load(Extension.class, getClass().getClassLoader());
ServiceLoader<Extension> loader = ServiceLoader.load(Extension.class, classLoader);
Iterator<Extension> it = loader.iterator();
while (it.hasNext())
{
Expand Down Expand Up @@ -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<URL> urlArray = new ArrayList<URL>();
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
{
Expand Down

0 comments on commit dd2a7be

Please sign in to comment.