Search in sources :

Example 1 with JarPluginArchive

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);
}
Also used : JarPluginArchive(com.alipay.sofa.ark.loader.JarPluginArchive) PluginArchive(com.alipay.sofa.ark.spi.archive.PluginArchive) JarFileArchive(com.alipay.sofa.ark.loader.archive.JarFileArchive) JarFile(com.alipay.sofa.ark.loader.jar.JarFile) File(java.io.File) URL(java.net.URL) JarPluginArchive(com.alipay.sofa.ark.loader.JarPluginArchive) Plugin(com.alipay.sofa.ark.spi.model.Plugin) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Example 2 with JarPluginArchive

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);
}
Also used : JarFileArchive(com.alipay.sofa.ark.loader.archive.JarFileArchive) JarFile(com.alipay.sofa.ark.loader.jar.JarFile) JarPluginArchive(com.alipay.sofa.ark.loader.JarPluginArchive)

Example 3 with 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);
}
Also used : ArrayList(java.util.ArrayList) URL(java.net.URL) JarPluginArchive(com.alipay.sofa.ark.loader.JarPluginArchive)

Example 4 with JarPluginArchive

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()));
}
Also used : JarFileArchive(com.alipay.sofa.ark.loader.archive.JarFileArchive) JarFile(com.alipay.sofa.ark.loader.jar.JarFile) JarFile(com.alipay.sofa.ark.loader.jar.JarFile) File(java.io.File) URL(java.net.URL) JarPluginArchive(com.alipay.sofa.ark.loader.JarPluginArchive) HashSet(java.util.HashSet) Plugin(com.alipay.sofa.ark.spi.model.Plugin) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Aggregations

JarPluginArchive (com.alipay.sofa.ark.loader.JarPluginArchive)4 JarFileArchive (com.alipay.sofa.ark.loader.archive.JarFileArchive)3 JarFile (com.alipay.sofa.ark.loader.jar.JarFile)3 URL (java.net.URL)3 BaseTest (com.alipay.sofa.ark.container.BaseTest)2 Plugin (com.alipay.sofa.ark.spi.model.Plugin)2 File (java.io.File)2 Test (org.junit.Test)2 PluginArchive (com.alipay.sofa.ark.spi.archive.PluginArchive)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1