Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LibClassLoader to recursively add jars to classpath #2311

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading