From bde41de26f488118c3e0694ce3555cf542ec2a11 Mon Sep 17 00:00:00 2001 From: Arunan Sugunakumar Date: Tue, 19 Nov 2024 16:27:21 +0530 Subject: [PATCH] Improve connector dependency deployment --- .../deployers/ClassMediatorDeployer.java | 26 ++++++++----- .../deployers/LibraryArtifactDeployer.java | 2 +- .../synapse/libraries/LibClassLoader.java | 37 +++++++++++++++++++ .../libraries/util/LibDeployerUtils.java | 27 +++++++++++--- 4 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 modules/core/src/main/java/org/apache/synapse/libraries/LibClassLoader.java 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 955b0a1dfb..73b6d0153e 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 @@ -27,8 +27,10 @@ import org.apache.commons.logging.LogFactory; import org.apache.synapse.SynapseConstants; import org.apache.synapse.config.SynapseConfiguration; +import org.apache.synapse.libraries.LibClassLoader; import java.io.File; +import java.net.MalformedURLException; public class ClassMediatorDeployer extends AbstractDeployer { @@ -61,13 +63,20 @@ public void init(ConfigurationContext configurationContext) { */ public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException { - String mediatorPath = FilenameUtils.normalize(deploymentFileData.getAbsolutePath()); - - log.info("Deploying Class mediators from file : " + mediatorPath); - ClassLoader mediatorLoader = Utils.getClassLoader(ClassMediatorDeployer.class.getClassLoader(), - mediatorPath, false); - getDeploymentStore().addClassMediatorClassLoader(mediatorPath, mediatorLoader); - + String mediatorPath = FilenameUtils.normalize(deploymentFileData.getAbsolutePath()); + + log.info("Deploying library from file : " + mediatorPath); + if (deploymentFileData.getClassLoader() != null) { + 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 mediatorLoader = Utils.getClassLoader(ClassMediatorDeployer.class.getClassLoader(), + mediatorPath, false); + getDeploymentStore().addClassMediatorClassLoader(mediatorPath, mediatorLoader); + } } /** @@ -78,7 +87,7 @@ public void deploy(DeploymentFileData deploymentFileData) throws DeploymentExcep */ public void undeploy(String fileName) throws DeploymentException { String mediatorPath = FilenameUtils.normalize(fileName); - log.info("Undeploying Class mediator : " + + log.info("Undeploying library : " + mediatorPath.substring(mediatorPath.lastIndexOf(File.separator)+1)); getDeploymentStore().removeClassMediatorClassLoader(mediatorPath); } @@ -99,5 +108,4 @@ public void setDirectory(String s) { public void setExtension(String s) { // Changing the extension is not supported } - } diff --git a/modules/core/src/main/java/org/apache/synapse/deployers/LibraryArtifactDeployer.java b/modules/core/src/main/java/org/apache/synapse/deployers/LibraryArtifactDeployer.java index cdc0208fde..06688ae42b 100644 --- a/modules/core/src/main/java/org/apache/synapse/deployers/LibraryArtifactDeployer.java +++ b/modules/core/src/main/java/org/apache/synapse/deployers/LibraryArtifactDeployer.java @@ -59,7 +59,7 @@ public void deploy(DeploymentFileData deploymentFileData) throws DeploymentExcep SynapseArtifactDeploymentStore deploymentStore = getSynapseConfiguration() .getArtifactDeploymentStore(); - Library lib = LibDeployerUtils.createSynapseLibrary(libFilePath); + Library lib = LibDeployerUtils.createSynapseLibrary(libFilePath, deploymentFileData.getClassLoader()); String libArtifactName = lib.getQName().toString(); if (this.getSynapseConfiguration().getSynapseLibraries().get(lib.getQName().toString()) != null) { log.warn("Hot deployment thread picked up an already deployed synapse library - Ignoring"); 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 new file mode 100644 index 0000000000..e3d4bd64e9 --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/libraries/LibClassLoader.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.synapse.libraries; + +import java.net.URLClassLoader; +import java.net.URL; + +public class LibClassLoader extends URLClassLoader { + + public LibClassLoader(URL[] urls, ClassLoader parent) { + + super(urls, parent); + } + + public void addURL(URL url) { + + super.addURL(url); + } + +} 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 956b6c8b3d..128ffc0140 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 @@ -31,6 +31,7 @@ import org.apache.synapse.config.SynapsePropertiesLoader; import org.apache.synapse.config.xml.SynapseXMLConfigurationFactory; import org.apache.synapse.deployers.SynapseArtifactDeploymentException; +import org.apache.synapse.libraries.LibClassLoader; import org.apache.synapse.libraries.imports.SynapseImport; import org.apache.synapse.libraries.model.Library; import org.apache.synapse.libraries.model.LibraryArtifact; @@ -39,6 +40,7 @@ import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; import java.io.*; +import java.net.MalformedURLException; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -57,9 +59,11 @@ public class LibDeployerUtils { private static final Log log = LogFactory.getLog(LibDeployerUtils.class); + public static Library createSynapseLibrary(String libPath) throws DeploymentException { + return createSynapseLibrary(libPath, null); + } - - public static Library createSynapseLibrary(String libPath) { + public static Library createSynapseLibrary(String libPath, ClassLoader classLoader) throws DeploymentException { File libFile = new File(libPath); //extract String extractPath = LibDeployerUtils.extractSynapseLib(libFile); @@ -72,10 +76,23 @@ public static Library createSynapseLibrary(String libPath) { if (deployedLibClassLoader == null) { //create a ClassLoader for loading this synapse lib classes/resources try { - ClassLoader libLoader = Utils.getClassLoader(LibDeployerUtils.class.getClassLoader(), + if (classLoader != null) { + synapseLib.setLibClassLoader(classLoader); + SynapseConfiguration.addLibraryClassLoader(libArtifactName, classLoader); + if (classLoader instanceof LibClassLoader) { + try { + ((LibClassLoader) classLoader).addURL(new File(extractPath).toURI().toURL()); + } catch (MalformedURLException e) { + throw new DeploymentException("Error while adding URL to the classloader", e); + } + } + } else { + ClassLoader libLoader = Utils.getClassLoader(LibDeployerUtils.class.getClassLoader(), extractPath, false); - SynapseConfiguration.addLibraryClassLoader(libArtifactName, libLoader); - synapseLib.setLibClassLoader(libLoader); + SynapseConfiguration.addLibraryClassLoader(libArtifactName, libLoader); + synapseLib.setLibClassLoader(libLoader); + } + } catch (DeploymentException e) { throw new SynapseArtifactDeploymentException("Error setting up lib classpath for Synapse" + " Library : " + libFile.getAbsolutePath(), e);