Search in sources :

Example 1 with BizArchive

use of com.alipay.sofa.ark.spi.archive.BizArchive 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 2 with BizArchive

use of com.alipay.sofa.ark.spi.archive.BizArchive in project sofa-ark by alipay.

the class BizFactoryServiceImpl method createBiz.

@Override
public Biz createBiz(File file) throws IOException {
    BizArchive bizArchive;
    if (ArkConfigs.isEmbedEnable()) {
        File unpackFile = new File(file.getAbsolutePath() + "-unpack");
        if (!unpackFile.exists()) {
            unpackFile = FileUtils.unzip(file, file.getAbsolutePath() + "-unpack");
        }
        if (file.exists()) {
            file.delete();
        }
        file = unpackFile;
        bizArchive = new ExplodedBizArchive(unpackFile);
    } else {
        JarFile bizFile = new JarFile(file);
        JarFileArchive jarFileArchive = new JarFileArchive(bizFile);
        bizArchive = new JarBizArchive(jarFileArchive);
    }
    BizModel biz = (BizModel) createBiz(bizArchive);
    biz.setBizTempWorkDir(file);
    return biz;
}
Also used : JarBizArchive(com.alipay.sofa.ark.loader.JarBizArchive) JarFileArchive(com.alipay.sofa.ark.loader.archive.JarFileArchive) ExplodedBizArchive(com.alipay.sofa.ark.loader.ExplodedBizArchive) BizModel(com.alipay.sofa.ark.container.model.BizModel) JarFile(com.alipay.sofa.ark.loader.jar.JarFile) JarFile(com.alipay.sofa.ark.loader.jar.JarFile) File(java.io.File) BizArchive(com.alipay.sofa.ark.spi.archive.BizArchive) JarBizArchive(com.alipay.sofa.ark.loader.JarBizArchive) ExplodedBizArchive(com.alipay.sofa.ark.loader.ExplodedBizArchive)

Example 3 with BizArchive

use of com.alipay.sofa.ark.spi.archive.BizArchive in project sofa-ark by alipay.

the class ClasspathLauncherTest method testFilterAgentClasspath.

@Test
public void testFilterAgentClasspath() throws Exception {
    URL url = this.getClass().getClassLoader().getResource("sample-biz.jar");
    URL[] agentUrl = ClassLoaderUtils.getAgentClassPath();
    Assert.assertEquals(1, agentUrl.length);
    List<URL> urls = new ArrayList<>();
    urls.add(url);
    urls.addAll(Arrays.asList(agentUrl));
    ClasspathLauncher.ClassPathArchive classPathArchive = new ClasspathLauncher.ClassPathArchive(this.getClass().getCanonicalName(), null, urls.toArray(new URL[] {}));
    List<BizArchive> bizArchives = classPathArchive.getBizArchives();
    Assert.assertEquals(1, bizArchives.size());
    Assert.assertEquals(2, urls.size());
}
Also used : ArrayList(java.util.ArrayList) URL(java.net.URL) BizArchive(com.alipay.sofa.ark.spi.archive.BizArchive) EmbedClassPathArchive(com.alipay.sofa.ark.loader.EmbedClassPathArchive) Test(org.junit.Test)

Example 4 with BizArchive

use of com.alipay.sofa.ark.spi.archive.BizArchive in project sofa-ark by alipay.

the class ClasspathLauncherTest method testSpringBootFatJar.

@Test
public void testSpringBootFatJar() throws Exception {
    URL url = this.getClass().getClassLoader().getResource("sample-springboot-fat-biz.jar");
    URL[] agentUrl = ClassLoaderUtils.getAgentClassPath();
    Assert.assertEquals(1, agentUrl.length);
    List<URL> urls = new ArrayList<>();
    JarFileArchive jarFileArchive = new JarFileArchive(new File(url.getFile()));
    List<Archive> archives = jarFileArchive.getNestedArchives(this::isNestedArchive);
    for (Archive archive : archives) {
        urls.add(archive.getUrl());
    }
    urls.addAll(Arrays.asList(agentUrl));
    EmbedClassPathArchive classPathArchive = new EmbedClassPathArchive(this.getClass().getCanonicalName(), null, urls.toArray(new URL[] {}));
    List<BizArchive> bizArchives = classPathArchive.getBizArchives();
    Assert.assertEquals(0, bizArchives.size());
    Assert.assertNotNull(classPathArchive.getContainerArchive());
    Assert.assertEquals(1, classPathArchive.getPluginArchives().size());
    Assert.assertEquals(archives.size() + 1, urls.size());
    Assert.assertEquals(3, classPathArchive.getConfClasspath().size());
    URLClassLoader classLoader = new URLClassLoader(classPathArchive.getContainerArchive().getUrls());
    try {
        Class clazz = classLoader.loadClass("com.alipay.sofa.ark.bootstrap.ArkLauncher");
        Assert.assertTrue(clazz != null);
    } catch (Exception e) {
        Assert.assertTrue("loadClass class failed ", false);
    }
}
Also used : EmbedClassPathArchive(com.alipay.sofa.ark.loader.EmbedClassPathArchive) BizArchive(com.alipay.sofa.ark.spi.archive.BizArchive) JarFileArchive(com.alipay.sofa.ark.loader.archive.JarFileArchive) EmbedClassPathArchive(com.alipay.sofa.ark.loader.EmbedClassPathArchive) Archive(com.alipay.sofa.ark.spi.archive.Archive) ArrayList(java.util.ArrayList) URL(java.net.URL) IOException(java.io.IOException) JarFileArchive(com.alipay.sofa.ark.loader.archive.JarFileArchive) URLClassLoader(java.net.URLClassLoader) File(java.io.File) BizArchive(com.alipay.sofa.ark.spi.archive.BizArchive) Test(org.junit.Test)

Aggregations

BizArchive (com.alipay.sofa.ark.spi.archive.BizArchive)4 URL (java.net.URL)3 EmbedClassPathArchive (com.alipay.sofa.ark.loader.EmbedClassPathArchive)2 JarBizArchive (com.alipay.sofa.ark.loader.JarBizArchive)2 JarFileArchive (com.alipay.sofa.ark.loader.archive.JarFileArchive)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 BizModel (com.alipay.sofa.ark.container.model.BizModel)1 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)1 DirectoryBizArchive (com.alipay.sofa.ark.loader.DirectoryBizArchive)1 ExplodedBizArchive (com.alipay.sofa.ark.loader.ExplodedBizArchive)1 JarFile (com.alipay.sofa.ark.loader.jar.JarFile)1 Archive (com.alipay.sofa.ark.spi.archive.Archive)1 ExecutableArchive (com.alipay.sofa.ark.spi.archive.ExecutableArchive)1 PluginArchive (com.alipay.sofa.ark.spi.archive.PluginArchive)1 Biz (com.alipay.sofa.ark.spi.model.Biz)1 Plugin (com.alipay.sofa.ark.spi.model.Plugin)1 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1