Search in sources :

Example 1 with Plugin

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

the class ClassloaderServiceImpl method findResourceExportClassloader.

@Override
public ClassLoader findResourceExportClassloader(String resourceName) {
    if (resourceName.contains(ARK_EXPORT_RESOURCE)) {
        String pluginName = resourceName.substring(0, resourceName.indexOf(ARK_EXPORT_RESOURCE));
        Plugin plugin = pluginManagerService.getPluginByName(pluginName);
        if (plugin != null) {
            return plugin.getPluginClassLoader();
        }
    }
    return null;
}
Also used : Plugin(com.alipay.sofa.ark.spi.model.Plugin)

Example 2 with Plugin

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

the class HandleArchiveStage method process.

@Override
public void process(PipelineContext pipelineContext) throws ArkRuntimeException {
    try {
        if (ArkConfigs.isEmbedEnable()) {
            processEmbed(pipelineContext);
            return;
        }
        ExecutableArchive executableArchive = pipelineContext.getExecutableArchive();
        List<BizArchive> bizArchives = executableArchive.getBizArchives();
        List<PluginArchive> pluginArchives = executableArchive.getPluginArchives();
        if (useDynamicConfig()) {
            AssertUtils.isFalse(StringUtils.isEmpty(ArkConfigs.getStringValue(Constants.MASTER_BIZ)), "Master biz should be configured when using dynamic config.");
        }
        int bizCount = 0;
        for (BizArchive bizArchive : bizArchives) {
            Biz biz = bizFactoryService.createBiz(bizArchive);
            if (bizArchive instanceof DirectoryBizArchive) {
                if (!((DirectoryBizArchive) bizArchive).isTestMode()) {
                    bizManagerService.registerBiz(biz);
                    bizCount += 1;
                }
            } else if (useDynamicConfig()) {
                if (biz.getBizName().equals(ArkConfigs.getStringValue(Constants.MASTER_BIZ))) {
                    bizManagerService.registerBiz(biz);
                    bizCount += 1;
                } else {
                    LOGGER.warn("The biz of {} is ignored when using dynamic config.", biz.getIdentity());
                }
            } else {
                if (!isBizExcluded(biz)) {
                    bizManagerService.registerBiz(biz);
                    bizCount += 1;
                } else {
                    LOGGER.warn(String.format("The biz of %s is excluded.", biz.getIdentity()));
                }
            }
        }
        // master biz should be specified when deploy multi biz, otherwise the only biz would be token as master biz
        if (bizCount > 1) {
            AssertUtils.isFalse(StringUtils.isEmpty(ArkConfigs.getStringValue(Constants.MASTER_BIZ)), "Master biz should be configured when deploy multi biz.");
            String masterBizName = ArkConfigs.getStringValue(Constants.MASTER_BIZ);
            for (Biz biz : bizManagerService.getBizInOrder()) {
                if (masterBizName.equals(biz.getBizName())) {
                    ArkClient.setMasterBiz(biz);
                }
            }
        } else {
            List<Biz> bizList = bizManagerService.getBizInOrder();
            if (!bizList.isEmpty() && StringUtils.isEmpty(ArkConfigs.getStringValue(Constants.MASTER_BIZ))) {
                ArkConfigs.putStringValue(Constants.MASTER_BIZ, bizList.get(0).getBizName());
                ArkClient.setMasterBiz(bizList.get(0));
            }
        }
        URL[] exportUrls = null;
        Set<String> exportPackages = new HashSet<>();
        Biz masterBiz = ArkClient.getMasterBiz();
        for (BizArchive bizArchive : bizArchives) {
            Attributes mainAttributes = bizArchive.getManifest().getMainAttributes();
            String bizName = mainAttributes.getValue(ARK_BIZ_NAME);
            // extension from master biz
            if (bizArchive instanceof JarBizArchive && masterBiz.getBizName().equalsIgnoreCase(bizName)) {
                String exportPackageStr = mainAttributes.getValue(INJECT_EXPORT_PACKAGES);
                exportPackages.addAll(StringUtils.strToSet(exportPackageStr, MANIFEST_VALUE_SPLIT));
                exportUrls = ((JarBizArchive) bizArchive).getExportUrls();
            }
        }
        for (PluginArchive pluginArchive : pluginArchives) {
            Plugin plugin = pluginFactoryService.createPlugin(pluginArchive, exportUrls, exportPackages);
            if (!isPluginExcluded(plugin)) {
                pluginManagerService.registerPlugin(plugin);
            } else {
                LOGGER.warn(String.format("The plugin of %s is excluded.", plugin.getPluginName()));
            }
        }
    } catch (Throwable ex) {
        throw new ArkRuntimeException(ex.getMessage(), ex);
    }
}
Also used : ExecutableArchive(com.alipay.sofa.ark.spi.archive.ExecutableArchive) DirectoryBizArchive(com.alipay.sofa.ark.loader.DirectoryBizArchive) PluginArchive(com.alipay.sofa.ark.spi.archive.PluginArchive) Attributes(java.util.jar.Attributes) URL(java.net.URL) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException) Biz(com.alipay.sofa.ark.spi.model.Biz) JarBizArchive(com.alipay.sofa.ark.loader.JarBizArchive) BizArchive(com.alipay.sofa.ark.spi.archive.BizArchive) DirectoryBizArchive(com.alipay.sofa.ark.loader.DirectoryBizArchive) JarBizArchive(com.alipay.sofa.ark.loader.JarBizArchive) HashSet(java.util.HashSet) Plugin(com.alipay.sofa.ark.spi.model.Plugin)

Example 3 with Plugin

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

the class PluginCommandProviderTest method testPluginCommandProcess.

@Test
public void testPluginCommandProcess(@Mocked final PluginManagerService pluginManagerService) {
    URL[] urls = ((URLClassLoader) this.getClass().getClassLoader()).getURLs();
    final Plugin pluginA = new PluginModel().setPluginName("pluginA").setImportClasses("com.google.common.io.AppendableWriter").setImportPackages(" com.google.common.cache, com.google.common.base ").setImportResources(" import_resource1").setExportPackages("com.alipay.sofa.*").setExportResources(" export_resource1, export_resource2").setPluginActivator("com.alipay.sofa.pluginA.Activator").setClassPath(urls).setGroupId("com.alipay.sofa").setArtifactId("ark-mock-pluginA").setVersion("1.0.0");
    final Plugin pluginB = new PluginModel().setPluginName("pluginB").setImportClasses("com.google.common.io.AppendableWriter").setImportPackages(" com.google.common.cache, com.google.common.base ").setImportResources(" import_resource1").setExportPackages("com.alipay.sofa.*").setExportResources(" export_resource1, export_resource2").setPluginActivator("com.alipay.sofa.pluginA.Activator").setClassPath(urls).setGroupId("com.alipay.sofa").setArtifactId("ark-mock-pluginB").setVersion("1.0.0");
    final Set<String> set = new HashSet<>();
    set.add("pluginA");
    set.add("pluginB");
    new Expectations() {

        {
            pluginManagerService.getAllPluginNames();
            result = set;
            minTimes = 0;
            pluginManagerService.getPluginByName("pluginA");
            result = pluginA;
            minTimes = 0;
            pluginManagerService.getPluginByName("pluginB");
            result = pluginB;
            minTimes = 0;
        }
    };
    PluginCommandProvider pluginCommandProvider = new PluginCommandProvider();
    try {
        Field field = PluginCommandProvider.class.getDeclaredField("pluginManagerService");
        field.setAccessible(true);
        field.set(pluginCommandProvider, pluginManagerService);
    } catch (Throwable throwable) {
    // ignore
    }
    String errorMessage = "Error command format. Pls type 'plugin -h' to get help message\n";
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plu")));
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin -h pluginA")));
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin -b pluginA")));
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plu")));
    Assert.assertTrue(pluginCommandProvider.getHelp().equals(pluginCommandProvider.handleCommand("plugin -h")));
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin ")));
    String details = pluginCommandProvider.handleCommand("plugin -m -d -s pluginA");
    Assert.assertTrue(details.contains("Activator"));
    Assert.assertTrue(details.contains("GroupId"));
    details = pluginCommandProvider.handleCommand("plugin -d pluginB");
    Assert.assertTrue(details.contains("GroupId"));
    Assert.assertFalse(details.contains("Activator"));
    details = pluginCommandProvider.handleCommand("plugin -m plugin.");
    Assert.assertTrue(details.contains("pluginA"));
    Assert.assertTrue(details.contains("pluginB"));
    details = pluginCommandProvider.handleCommand("plugin -m pluginC");
    Assert.assertTrue(details.contains("no matched plugin candidates."));
}
Also used : Expectations(mockit.Expectations) PluginModel(com.alipay.sofa.ark.container.model.PluginModel) Field(java.lang.reflect.Field) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) Plugin(com.alipay.sofa.ark.spi.model.Plugin) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Plugin

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

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

the class PluginFactoryServiceTest method test.

@Test
public void test() throws Throwable {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URL samplePlugin = cl.getResource("sample-plugin.jar");
    Plugin plugin = pluginFactoryService.createPlugin(new File(samplePlugin.getFile()));
    Assert.assertNotNull(plugin);
}
Also used : JarFile(com.alipay.sofa.ark.loader.jar.JarFile) File(java.io.File) URL(java.net.URL) Plugin(com.alipay.sofa.ark.spi.model.Plugin) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Aggregations

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