use of com.alipay.sofa.ark.loader.JarPluginArchive in project sofa-ark by alipay.
the class PluginFactoryServiceTest method testCreateEmbedPlugin.
@Test
public void testCreateEmbedPlugin() throws IOException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL samplePlugin = cl.getResource("sample-plugin.jar");
PluginArchive archive = new JarPluginArchive(new JarFileArchive(new File(samplePlugin.getFile())));
Plugin plugin = pluginFactoryService.createEmbedPlugin(archive, this.getClass().getClassLoader());
Assert.assertNotNull(plugin);
}
use of com.alipay.sofa.ark.loader.JarPluginArchive in project sofa-ark by alipay.
the class PluginFactoryServiceImpl method createPlugin.
@Override
public Plugin createPlugin(File file) throws IOException {
JarFile pluginFile = new JarFile(file);
JarFileArchive jarFileArchive = new JarFileArchive(pluginFile);
JarPluginArchive jarPluginArchive = new JarPluginArchive(jarFileArchive);
return createPlugin(jarPluginArchive);
}
use of com.alipay.sofa.ark.loader.JarPluginArchive in project sofa-ark by alipay.
the class PluginFactoryServiceImpl method getFinalPluginUrls.
public URL[] getFinalPluginUrls(PluginArchive pluginArchive, URL[] extensions, String pluginName) throws IOException {
// get config by PLUGIN-EXPORT key
URL[] urls = pluginArchive.getUrls();
String excludeArtifact = ArkConfigs.getStringValue(String.format(PLUGIN_EXTENSION_FORMAT, pluginName));
if (StringUtils.isEmpty(excludeArtifact) || extensions == null) {
return urls;
}
pluginArchive.setExtensionUrls(extensions);
ArrayList<URL> urlList = new ArrayList<>(Arrays.asList(urls));
List<URL> preRemoveList = new ArrayList<>();
urlList.remove(null);
for (URL url : urlList) {
String[] dependencies = excludeArtifact.split(STRING_SEMICOLON);
for (String dependency : dependencies) {
String artifactId = dependency.split(STRING_COLON)[0];
String version = dependency.split(STRING_COLON)[1];
if (url.getPath().endsWith(artifactId + "-" + version + ".jar!/")) {
preRemoveList.add(url);
break;
}
}
}
urlList.removeAll(preRemoveList);
if (pluginArchive instanceof JarPluginArchive) {
URL[] extensionUrls = ((JarPluginArchive) pluginArchive).getExtensionUrls();
if (extensionUrls != null) {
urlList.addAll(Arrays.asList(extensionUrls));
}
}
Object[] objects = urlList.toArray();
urls = new URL[objects.length];
return urlList.toArray(urls);
}
use of com.alipay.sofa.ark.loader.JarPluginArchive in project sofa-ark by alipay.
the class PluginFactoryServiceTest method testCreatePluginWithExtensions.
@Test
public void testCreatePluginWithExtensions() throws Throwable {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL samplePlugin = cl.getResource("sample-plugin.jar");
File file = new File(samplePlugin.getFile());
JarFile pluginFile = new JarFile(file);
JarFileArchive jarFileArchive = new JarFileArchive(pluginFile);
JarPluginArchive jarPluginArchive = new JarPluginArchive(jarFileArchive);
// inject
URL[] extensions = new URL[1];
URL sampleBiz = cl.getResource("sample-biz.jar");
JarFile bizFile = new JarFile(new File(sampleBiz.getFile()));
extensions[0] = bizFile.getUrl();
// export
Set<String> exportPackages = new HashSet<>();
exportPackages.add("com.alipay.test.export.*");
ArkConfigs.putStringValue(String.format(PLUGIN_EXTENSION_FORMAT, "sample-ark-plugin"), "tracer-core:3.0.10");
Plugin plugin = pluginFactoryService.createPlugin(jarPluginArchive, extensions, exportPackages);
Assert.assertNotNull(plugin);
Assert.assertEquals(plugin.getExportPackages().size(), 2);
Assert.assertTrue(Arrays.asList(plugin.getClassPath()).contains(bizFile.getUrl()));
}
Aggregations