Search in sources :

Example 1 with JarFile

use of com.alipay.sofa.ark.loader.jar.JarFile in project sofa-ark by alipay.

the class JarFileTest method testJarFile.

@Test
public void testJarFile() throws IOException {
    JarFile jarFile = new JarFile(getTempDemoZip());
    Manifest manifest = jarFile.getManifest();
    Assert.assertTrue(manifest.getMainAttributes().getValue("k1").equals("v1"));
    Assert.assertTrue(manifest.getMainAttributes().getValue("k2").equals("v2"));
    Assert.assertTrue(jarFile.containsEntry(TEST_ENTRY));
    ZipEntry zipEntry = jarFile.getEntry(TEST_ENTRY);
    Assert.assertTrue(zipEntry.getName().equals(TEST_ENTRY));
    Assert.assertTrue(zipEntry.getComment().equals(TEST_ENTRY_COMMENT));
    Assert.assertTrue(compareByteArray(zipEntry.getExtra(), TEST_ENTRY_EXTRA.getBytes()));
    JarEntry jarEntry = jarFile.getJarEntry("lib/junit-4.12.jar");
    JarFile nestJarFile = jarFile.getNestedJarFile(jarEntry);
    Manifest nestManifest = nestJarFile.getManifest();
    Assert.assertTrue(nestManifest.getMainAttributes().getValue("Implementation-Title").equals("JUnit"));
    Assert.assertTrue(nestManifest.getMainAttributes().getValue("Implementation-Version").equals("4.12"));
}
Also used : ZipEntry(java.util.zip.ZipEntry) JarFile(com.alipay.sofa.ark.loader.jar.JarFile) Manifest(java.util.jar.Manifest) JarEntry(com.alipay.sofa.ark.loader.jar.JarEntry) BaseTest(com.alipay.sofa.ark.loader.test.base.BaseTest) Test(org.junit.Test)

Example 2 with JarFile

use of com.alipay.sofa.ark.loader.jar.JarFile 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 JarFile

use of com.alipay.sofa.ark.loader.jar.JarFile 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 4 with JarFile

use of com.alipay.sofa.ark.loader.jar.JarFile 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)

Aggregations

JarFile (com.alipay.sofa.ark.loader.jar.JarFile)4 JarFileArchive (com.alipay.sofa.ark.loader.archive.JarFileArchive)3 JarPluginArchive (com.alipay.sofa.ark.loader.JarPluginArchive)2 File (java.io.File)2 Test (org.junit.Test)2 BaseTest (com.alipay.sofa.ark.container.BaseTest)1 BizModel (com.alipay.sofa.ark.container.model.BizModel)1 ExplodedBizArchive (com.alipay.sofa.ark.loader.ExplodedBizArchive)1 JarBizArchive (com.alipay.sofa.ark.loader.JarBizArchive)1 JarEntry (com.alipay.sofa.ark.loader.jar.JarEntry)1 BaseTest (com.alipay.sofa.ark.loader.test.base.BaseTest)1 BizArchive (com.alipay.sofa.ark.spi.archive.BizArchive)1 Plugin (com.alipay.sofa.ark.spi.model.Plugin)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Manifest (java.util.jar.Manifest)1 ZipEntry (java.util.zip.ZipEntry)1