Search in sources :

Example 36 with Plugin

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

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 37 with Plugin

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

the class BizFactoryServiceTest 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()));
    pluginManagerService.registerPlugin(plugin);
    URL sampleBiz = cl.getResource("sample-biz.jar");
    Biz biz = bizFactoryService.createBiz(new File(sampleBiz.getFile()));
    bizManagerService.registerBiz(biz);
    Assert.assertNotNull(biz);
    Assert.assertNotNull(biz.getBizClassLoader().getResource(Constants.ARK_PLUGIN_MARK_ENTRY));
}
Also used : Biz(com.alipay.sofa.ark.spi.model.Biz) 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)

Example 38 with Plugin

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

the class ClassLoaderServiceTest method testIsClassImport.

@Test
public void testIsClassImport() {
    Plugin plugin = new PluginModel().setPluginName("mockPlugin").setImportClasses(null).setImportPackages("a.c,a.b.c.*,a.b.c");
    pluginManagerService.registerPlugin(plugin);
    Assert.assertTrue(classloaderService.isClassInImport("mockPlugin", "a.c.e"));
    Assert.assertFalse(classloaderService.isClassInImport("mockPlugin", "a.c"));
    Assert.assertFalse(classloaderService.isClassInImport("mockPlugin", "a.c.e.f"));
    Assert.assertFalse(classloaderService.isClassInImport("mockPlugin", "a.b.c"));
    Assert.assertTrue(classloaderService.isClassInImport("mockPlugin", "a.b.c.e"));
    Assert.assertTrue(classloaderService.isClassInImport("mockPlugin", "a.b.c.e.f"));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) Plugin(com.alipay.sofa.ark.spi.model.Plugin) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Example 39 with Plugin

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

the class ClassLoaderServiceTest method testFindExportClass.

@Test
public void testFindExportClass() {
    PluginClassLoader pluginClassLoader = new PluginClassLoader("mockPlugin", new URL[] {});
    Plugin plugin = new PluginModel().setPluginName("mockPlugin").setExportPackages("a.b.*,a.f,a.b.f").setExportClasses("a.e.f.G").setPluginClassLoader(pluginClassLoader).setExportResources("");
    pluginManagerService.registerPlugin(plugin);
    classloaderService.prepareExportClassAndResourceCache();
    Assert.assertNull(classloaderService.findExportClassLoader("a.b"));
    Assert.assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.b.e.f")));
    Assert.assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.f.g")));
    Assert.assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.e.f.G")));
    Assert.assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.b.f.m")));
    Assert.assertTrue(pluginClassLoader.equals(classloaderService.findExportClassLoader("a.b.f.m.g")));
    Assert.assertNull(classloaderService.findExportClassLoader("a.f.h.m"));
    Assert.assertNull(classloaderService.findExportClassLoader("a"));
    pluginManagerService.getPluginsInOrder().remove(plugin);
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) Plugin(com.alipay.sofa.ark.spi.model.Plugin) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Example 40 with Plugin

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

the class BaseTest method registerMockPlugin.

protected void registerMockPlugin() {
    if (arkContainer == null) {
        String[] args = new String[] { "-Ajar=" + jarURL.toExternalForm() };
        arkContainer = (ArkContainer) ArkContainer.main(args);
    }
    PluginManagerService pluginManagerService = ArkServiceContainerHolder.getContainer().getService(PluginManagerService.class);
    Plugin plugin = new PluginModel().setPluginName("mock").setPluginClassLoader(this.getClass().getClassLoader()).setImportClasses("").setImportPackages("").setImportResources("");
    pluginManagerService.registerPlugin(plugin);
}
Also used : PluginManagerService(com.alipay.sofa.ark.spi.service.plugin.PluginManagerService) PluginModel(com.alipay.sofa.ark.container.model.PluginModel) 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