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 connector dependency deployment #2239

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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 {

Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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);
}
Expand All @@ -99,5 +108,4 @@ public void setDirectory(String s) {
public void setExtension(String s) {
// Changing the extension is not supported
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Loading