Skip to content

Commit

Permalink
Improve LibClassLoader to recursively add jars to classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
arunans23 committed Jan 28, 2025
1 parent bf65466 commit b4a0972
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.synapse.libraries.LibClassLoader;

import java.io.File;
import java.net.MalformedURLException;

public class ClassMediatorDeployer extends AbstractDeployer {

Expand Down Expand Up @@ -67,7 +68,11 @@ public void deploy(DeploymentFileData deploymentFileData) throws DeploymentExcep
log.info("Deploying library from file : " + mediatorPath);
ClassLoader classLoader = deploymentFileData.getClassLoader();
if (classLoader instanceof LibClassLoader) {
classLoader = Utils.getClassLoader(classLoader, mediatorPath, false);
try {
((LibClassLoader) deploymentFileData.getClassLoader()).addURL(new File(mediatorPath).toURI().toURL());
} catch (MalformedURLException e) {
throw new DeploymentException("Error adding URL to lib class loader", e);
}
} else {
classLoader = Utils.getClassLoader(ClassMediatorDeployer.class.getClassLoader(), mediatorPath, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

package org.apache.synapse.libraries;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.net.URL;
import java.util.ArrayList;

public class LibClassLoader extends URLClassLoader {

Expand All @@ -34,4 +37,41 @@ public void addURL(URL url) {
super.addURL(url);
}

/**
* If a path of a jar is given, this method will add the jar to the classpath
* If the path is a directory, it will add all the jars in the directory to the classpath
*
* @param path directory to be added
* @throws MalformedURLException
*/
public void addToClassPath(String path) throws MalformedURLException {

File file = new File(path);
ArrayList urls = new ArrayList();
urls.add(file.toURL());
File libfiles = new File(file, "lib");
if (!addFiles(urls, libfiles)) {
libfiles = new File(file, "Lib");
addFiles(urls, libfiles);
}
for (int i = 0; i < urls.size(); ++i) {
super.addURL((URL) urls.get(i));
}
}

private static boolean addFiles(ArrayList urls, final File libFiles) throws MalformedURLException {

if (libFiles.exists() && libFiles.isDirectory()) {
File[] files = libFiles.listFiles();
for (int i = 0; i < files.length; ++i) {
if (files[i].getName().endsWith(".jar")) {
urls.add(files[i].toURL());
}
}
return true;
} else {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ public static Library createSynapseLibrary(String libPath, ClassLoader classLoad
try {
ClassLoader libLoader;
if (classLoader != null) {
libLoader = Utils.getClassLoader(classLoader, extractPath, false);
libLoader = classLoader;
if (classLoader instanceof LibClassLoader) {
try {
((LibClassLoader) classLoader).addToClassPath(extractPath);
} catch (MalformedURLException e) {
throw new DeploymentException("Error while adding URL to the classloader", e);
}
}
} else {
libLoader = Utils.getClassLoader(LibDeployerUtils.class.getClassLoader(),
extractPath, false);
Expand Down

0 comments on commit b4a0972

Please sign in to comment.