Skip to content

Commit bde41de

Browse files
committed
Improve connector dependency deployment
1 parent 4ea7214 commit bde41de

File tree

4 files changed

+77
-15
lines changed

4 files changed

+77
-15
lines changed

modules/core/src/main/java/org/apache/synapse/deployers/ClassMediatorDeployer.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import org.apache.commons.logging.LogFactory;
2828
import org.apache.synapse.SynapseConstants;
2929
import org.apache.synapse.config.SynapseConfiguration;
30+
import org.apache.synapse.libraries.LibClassLoader;
3031

3132
import java.io.File;
33+
import java.net.MalformedURLException;
3234

3335
public class ClassMediatorDeployer extends AbstractDeployer {
3436

@@ -61,13 +63,20 @@ public void init(ConfigurationContext configurationContext) {
6163
*/
6264
public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException {
6365

64-
String mediatorPath = FilenameUtils.normalize(deploymentFileData.getAbsolutePath());
65-
66-
log.info("Deploying Class mediators from file : " + mediatorPath);
67-
ClassLoader mediatorLoader = Utils.getClassLoader(ClassMediatorDeployer.class.getClassLoader(),
68-
mediatorPath, false);
69-
getDeploymentStore().addClassMediatorClassLoader(mediatorPath, mediatorLoader);
70-
66+
String mediatorPath = FilenameUtils.normalize(deploymentFileData.getAbsolutePath());
67+
68+
log.info("Deploying library from file : " + mediatorPath);
69+
if (deploymentFileData.getClassLoader() != null) {
70+
try {
71+
((LibClassLoader) deploymentFileData.getClassLoader()).addURL(new File(mediatorPath).toURI().toURL());
72+
} catch (MalformedURLException e) {
73+
throw new DeploymentException("Error adding URL to lib class loader", e);
74+
}
75+
} else {
76+
ClassLoader mediatorLoader = Utils.getClassLoader(ClassMediatorDeployer.class.getClassLoader(),
77+
mediatorPath, false);
78+
getDeploymentStore().addClassMediatorClassLoader(mediatorPath, mediatorLoader);
79+
}
7180
}
7281

7382
/**
@@ -78,7 +87,7 @@ public void deploy(DeploymentFileData deploymentFileData) throws DeploymentExcep
7887
*/
7988
public void undeploy(String fileName) throws DeploymentException {
8089
String mediatorPath = FilenameUtils.normalize(fileName);
81-
log.info("Undeploying Class mediator : " +
90+
log.info("Undeploying library : " +
8291
mediatorPath.substring(mediatorPath.lastIndexOf(File.separator)+1));
8392
getDeploymentStore().removeClassMediatorClassLoader(mediatorPath);
8493
}
@@ -99,5 +108,4 @@ public void setDirectory(String s) {
99108
public void setExtension(String s) {
100109
// Changing the extension is not supported
101110
}
102-
103111
}

modules/core/src/main/java/org/apache/synapse/deployers/LibraryArtifactDeployer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void deploy(DeploymentFileData deploymentFileData) throws DeploymentExcep
5959
SynapseArtifactDeploymentStore deploymentStore = getSynapseConfiguration()
6060
.getArtifactDeploymentStore();
6161

62-
Library lib = LibDeployerUtils.createSynapseLibrary(libFilePath);
62+
Library lib = LibDeployerUtils.createSynapseLibrary(libFilePath, deploymentFileData.getClassLoader());
6363
String libArtifactName = lib.getQName().toString();
6464
if (this.getSynapseConfiguration().getSynapseLibraries().get(lib.getQName().toString()) != null) {
6565
log.warn("Hot deployment thread picked up an already deployed synapse library - Ignoring");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*
18+
*/
19+
20+
package org.apache.synapse.libraries;
21+
22+
import java.net.URLClassLoader;
23+
import java.net.URL;
24+
25+
public class LibClassLoader extends URLClassLoader {
26+
27+
public LibClassLoader(URL[] urls, ClassLoader parent) {
28+
29+
super(urls, parent);
30+
}
31+
32+
public void addURL(URL url) {
33+
34+
super.addURL(url);
35+
}
36+
37+
}

modules/core/src/main/java/org/apache/synapse/libraries/util/LibDeployerUtils.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.synapse.config.SynapsePropertiesLoader;
3232
import org.apache.synapse.config.xml.SynapseXMLConfigurationFactory;
3333
import org.apache.synapse.deployers.SynapseArtifactDeploymentException;
34+
import org.apache.synapse.libraries.LibClassLoader;
3435
import org.apache.synapse.libraries.imports.SynapseImport;
3536
import org.apache.synapse.libraries.model.Library;
3637
import org.apache.synapse.libraries.model.LibraryArtifact;
@@ -39,6 +40,7 @@
3940
import javax.xml.namespace.QName;
4041
import javax.xml.stream.XMLStreamException;
4142
import java.io.*;
43+
import java.net.MalformedURLException;
4244
import java.util.*;
4345
import java.util.zip.ZipEntry;
4446
import java.util.zip.ZipFile;
@@ -57,9 +59,11 @@ public class LibDeployerUtils {
5759

5860
private static final Log log = LogFactory.getLog(LibDeployerUtils.class);
5961

62+
public static Library createSynapseLibrary(String libPath) throws DeploymentException {
63+
return createSynapseLibrary(libPath, null);
64+
}
6065

61-
62-
public static Library createSynapseLibrary(String libPath) {
66+
public static Library createSynapseLibrary(String libPath, ClassLoader classLoader) throws DeploymentException {
6367
File libFile = new File(libPath);
6468
//extract
6569
String extractPath = LibDeployerUtils.extractSynapseLib(libFile);
@@ -72,10 +76,23 @@ public static Library createSynapseLibrary(String libPath) {
7276
if (deployedLibClassLoader == null) {
7377
//create a ClassLoader for loading this synapse lib classes/resources
7478
try {
75-
ClassLoader libLoader = Utils.getClassLoader(LibDeployerUtils.class.getClassLoader(),
79+
if (classLoader != null) {
80+
synapseLib.setLibClassLoader(classLoader);
81+
SynapseConfiguration.addLibraryClassLoader(libArtifactName, classLoader);
82+
if (classLoader instanceof LibClassLoader) {
83+
try {
84+
((LibClassLoader) classLoader).addURL(new File(extractPath).toURI().toURL());
85+
} catch (MalformedURLException e) {
86+
throw new DeploymentException("Error while adding URL to the classloader", e);
87+
}
88+
}
89+
} else {
90+
ClassLoader libLoader = Utils.getClassLoader(LibDeployerUtils.class.getClassLoader(),
7691
extractPath, false);
77-
SynapseConfiguration.addLibraryClassLoader(libArtifactName, libLoader);
78-
synapseLib.setLibClassLoader(libLoader);
92+
SynapseConfiguration.addLibraryClassLoader(libArtifactName, libLoader);
93+
synapseLib.setLibClassLoader(libLoader);
94+
}
95+
7996
} catch (DeploymentException e) {
8097
throw new SynapseArtifactDeploymentException("Error setting up lib classpath for Synapse" +
8198
" Library : " + libFile.getAbsolutePath(), e);

0 commit comments

Comments
 (0)