From b4a09728dbb847ff4884d8b82700bc0447deff43 Mon Sep 17 00:00:00 2001 From: Arunan Sugunakumar Date: Tue, 28 Jan 2025 14:26:43 +0530 Subject: [PATCH] Improve LibClassLoader to recursively add jars to classpath --- .../deployers/ClassMediatorDeployer.java | 7 +++- .../synapse/libraries/LibClassLoader.java | 40 +++++++++++++++++++ .../libraries/util/LibDeployerUtils.java | 9 ++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/deployers/ClassMediatorDeployer.java b/modules/core/src/main/java/org/apache/synapse/deployers/ClassMediatorDeployer.java index ee6c2367c9..b9e0c3c7b4 100644 --- a/modules/core/src/main/java/org/apache/synapse/deployers/ClassMediatorDeployer.java +++ b/modules/core/src/main/java/org/apache/synapse/deployers/ClassMediatorDeployer.java @@ -30,6 +30,7 @@ import org.apache.synapse.libraries.LibClassLoader; import java.io.File; +import java.net.MalformedURLException; public class ClassMediatorDeployer extends AbstractDeployer { @@ -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); } diff --git a/modules/core/src/main/java/org/apache/synapse/libraries/LibClassLoader.java b/modules/core/src/main/java/org/apache/synapse/libraries/LibClassLoader.java index e3d4bd64e9..cb8d9af328 100644 --- a/modules/core/src/main/java/org/apache/synapse/libraries/LibClassLoader.java +++ b/modules/core/src/main/java/org/apache/synapse/libraries/LibClassLoader.java @@ -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 { @@ -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; + } + } + } diff --git a/modules/core/src/main/java/org/apache/synapse/libraries/util/LibDeployerUtils.java b/modules/core/src/main/java/org/apache/synapse/libraries/util/LibDeployerUtils.java index f21b6ca2cf..ca753e120e 100644 --- a/modules/core/src/main/java/org/apache/synapse/libraries/util/LibDeployerUtils.java +++ b/modules/core/src/main/java/org/apache/synapse/libraries/util/LibDeployerUtils.java @@ -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);