Search in sources :

Example 26 with Plugin

use of com.alipay.sofa.ark.spi.model.Plugin in project sofa-ark by alipay.

the class GlobalEventHandlerTest method testEvent.

@Test
public void testEvent() {
    Biz biz = new BizModel().setBizName("test-biz").setBizVersion("1.0.0").setBizState(BizState.RESOLVED);
    Plugin plugin = new PluginModel().setPluginName("test-plugin").setVersion("1.0.0");
    eventAdminService.sendEvent(new AfterBizStartupEvent(biz));
    Assert.assertTrue(result.size() == 3);
    Assert.assertTrue(result.contains("AbstractArkEvent->AfterBizStartupEvent"));
    eventAdminService.sendEvent(new BeforePluginStartupEvent(plugin));
    Assert.assertTrue(result.size() == 5);
    // test for ArkEvent.class.isAssignableFrom(event.getClass()
    eventAdminService.sendEvent(new ArkEvent() {

        @Override
        public String getTopic() {
            return "ark-event";
        }
    });
    Assert.assertTrue(result.size() == 7);
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) BeforePluginStartupEvent(com.alipay.sofa.ark.spi.event.plugin.BeforePluginStartupEvent) Biz(com.alipay.sofa.ark.spi.model.Biz) AfterBizStartupEvent(com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent) BizModel(com.alipay.sofa.ark.container.model.BizModel) ArkEvent(com.alipay.sofa.ark.spi.event.ArkEvent) AbstractArkEvent(com.alipay.sofa.ark.spi.event.AbstractArkEvent) Plugin(com.alipay.sofa.ark.spi.model.Plugin) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Example 27 with Plugin

use of com.alipay.sofa.ark.spi.model.Plugin in project sofa-ark by alipay.

the class ClassloaderServiceImpl method isClassInImport.

@Override
public boolean isClassInImport(String pluginName, String className) {
    Plugin plugin = pluginManagerService.getPluginByName(pluginName);
    AssertUtils.assertNotNull(plugin, "plugin: " + pluginName + " is null");
    for (String importName : plugin.getImportIndex()) {
        if (className.startsWith(importName)) {
            return true;
        }
    }
    return false;
}
Also used : Plugin(com.alipay.sofa.ark.spi.model.Plugin)

Example 28 with Plugin

use of com.alipay.sofa.ark.spi.model.Plugin 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)

Example 29 with Plugin

use of com.alipay.sofa.ark.spi.model.Plugin in project sofa-ark by alipay.

the class ClassLoaderServiceImpl method isResourceInImport.

@Override
public boolean isResourceInImport(String pluginName, String resourceName) {
    Plugin plugin = pluginManagerService.getPluginByName(pluginName);
    AssertUtils.assertNotNull(plugin, "plugin: " + pluginName + " is null");
    for (String importResource : plugin.getImportResources()) {
        if (importResource.equals(resourceName)) {
            return true;
        }
    }
    for (String importResource : plugin.getImportPrefixResourceStems()) {
        if (resourceName.startsWith(importResource)) {
            return true;
        }
    }
    for (String importResource : plugin.getImportSuffixResourceStems()) {
        if (resourceName.endsWith(importResource)) {
            return true;
        }
    }
    return false;
}
Also used : Plugin(com.alipay.sofa.ark.spi.model.Plugin)

Example 30 with Plugin

use of com.alipay.sofa.ark.spi.model.Plugin in project sofa-ark by alipay.

the class ClassLoaderServiceImpl method prepareExportClassAndResourceCache.

@Override
public void prepareExportClassAndResourceCache() {
    for (Plugin plugin : pluginManagerService.getPluginsInOrder()) {
        for (String exportIndex : plugin.getExportPackageNodes()) {
            exportNodeAndClassLoaderMap.putIfAbsent(exportIndex, plugin.getPluginClassLoader());
        }
        for (String exportIndex : plugin.getExportPackageStems()) {
            exportStemAndClassLoaderMap.putIfAbsent(exportIndex, plugin.getPluginClassLoader());
        }
        for (String exportIndex : plugin.getExportClasses()) {
            exportClassAndClassLoaderMap.putIfAbsent(exportIndex, plugin.getPluginClassLoader());
        }
        for (String resource : plugin.getExportResources()) {
            exportResourceAndClassLoaderMap.putIfAbsent(resource, new LinkedList<>());
            exportResourceAndClassLoaderMap.get(resource).add(plugin.getPluginClassLoader());
        }
        for (String resource : plugin.getExportPrefixResourceStems()) {
            exportPrefixStemResourceAndClassLoaderMap.putIfAbsent(resource, new LinkedList<>());
            exportPrefixStemResourceAndClassLoaderMap.get(resource).add(plugin.getPluginClassLoader());
        }
        for (String resource : plugin.getExportSuffixResourceStems()) {
            exportSuffixStemResourceAndClassLoaderMap.putIfAbsent(resource, new LinkedList<>());
            exportSuffixStemResourceAndClassLoaderMap.get(resource).add(plugin.getPluginClassLoader());
        }
    }
}
Also used : Plugin(com.alipay.sofa.ark.spi.model.Plugin)

Aggregations

Plugin (com.alipay.sofa.ark.spi.model.Plugin)42 Test (org.junit.Test)22 BaseTest (com.alipay.sofa.ark.container.BaseTest)20 PluginModel (com.alipay.sofa.ark.container.model.PluginModel)14 URL (java.net.URL)14 Biz (com.alipay.sofa.ark.spi.model.Biz)10 File (java.io.File)10 JarFile (com.alipay.sofa.ark.loader.jar.JarFile)6 PluginArchive (com.alipay.sofa.ark.spi.archive.PluginArchive)6 HashSet (java.util.HashSet)6 BizModel (com.alipay.sofa.ark.container.model.BizModel)4 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)4 JarPluginArchive (com.alipay.sofa.ark.loader.JarPluginArchive)4 JarFileArchive (com.alipay.sofa.ark.loader.archive.JarFileArchive)4 ExecutableArchive (com.alipay.sofa.ark.spi.archive.ExecutableArchive)4 AfterBizStartupEvent (com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent)4 BeforePluginStartupEvent (com.alipay.sofa.ark.spi.event.plugin.BeforePluginStartupEvent)4 OrderComparator (com.alipay.sofa.ark.common.util.OrderComparator)2 DirectoryBizArchive (com.alipay.sofa.ark.loader.DirectoryBizArchive)2 JarBizArchive (com.alipay.sofa.ark.loader.JarBizArchive)2