Search in sources :

Example 1 with BizModel

use of com.alipay.sofa.ark.container.model.BizModel in project sofa-ark by alipay.

the class BizClassloaderTest method testGetPluginClassResource.

@Test
public void testGetPluginClassResource() throws Exception {
    PluginModel pluginA = new PluginModel();
    pluginA.setPluginName("plugin A").setClassPath(new URL[] { classPathURL }).setImportClasses(Collections.<String>emptySet()).setImportPackages(Collections.<String>emptySet()).setExportIndex(new HashSet<>(Collections.singletonList(ITest.class.getName()))).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
    pluginManagerService.registerPlugin(pluginA);
    pluginDeployService.deploy();
    classloaderService.prepareExportClassCache();
    BizModel bizModel = new BizModel();
    bizModel.setBizName("biz A").setClassPath(new URL[] { classPathURL }).setClassLoader(new BizClassLoader(bizModel.getBizName(), bizModel.getClassPath()));
    bizManagerService.registerBiz(bizModel);
    Assert.assertNotNull(bizModel.getBizClassLoader().getResource(ITest.class.getName().replace(".", "/") + ".class"));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) BizModel(com.alipay.sofa.ark.container.model.BizModel) URL(java.net.URL) HashSet(java.util.HashSet) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest)

Example 2 with BizModel

use of com.alipay.sofa.ark.container.model.BizModel in project sofa-ark by alipay.

the class BizClassloaderTest method testImport.

@Test
public void testImport() throws Exception {
    PluginModel pluginA = new PluginModel();
    pluginA.setPluginName("plugin A").setClassPath(new URL[] { classPathURL }).setImportClasses(Collections.<String>emptySet()).setImportPackages(Collections.<String>emptySet()).setExportIndex(new HashSet<>(Collections.singletonList(ITest.class.getName()))).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
    pluginManagerService.registerPlugin(pluginA);
    pluginDeployService.deploy();
    classloaderService.prepareExportClassCache();
    BizModel bizModel = new BizModel();
    bizModel.setBizName("biz A").setClassPath(new URL[] { classPathURL }).setClassLoader(new BizClassLoader(bizModel.getBizName(), bizModel.getClassPath()));
    bizManagerService.registerBiz(bizModel);
    Assert.assertEquals(pluginA.getPluginClassLoader().loadClass(ITest.class.getName()), bizModel.getBizClassLoader().loadClass(ITest.class.getName()));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) BizModel(com.alipay.sofa.ark.container.model.BizModel) URL(java.net.URL) HashSet(java.util.HashSet) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest)

Example 3 with BizModel

use of com.alipay.sofa.ark.container.model.BizModel in project sofa-ark by alipay.

the class BizFactoryServiceImpl method createBiz.

@Override
public Biz createBiz(BizArchive bizArchive) throws IOException {
    AssertUtils.isTrue(isArkBiz(bizArchive), "Archive must be a ark biz!");
    BizModel bizModel = new BizModel();
    Attributes manifestMainAttributes = bizArchive.getManifest().getMainAttributes();
    bizModel.setBizState(BizState.RESOLVED).setBizName(manifestMainAttributes.getValue(ARK_BIZ_NAME)).setBizVersion(manifestMainAttributes.getValue(ARK_BIZ_VERSION)).setMainClass(manifestMainAttributes.getValue(MAIN_CLASS_ATTRIBUTE)).setPriority(manifestMainAttributes.getValue(PRIORITY_ATTRIBUTE)).setWebContextPath(manifestMainAttributes.getValue(WEB_CONTEXT_PATH)).setDenyImportPackages(manifestMainAttributes.getValue(DENY_IMPORT_PACKAGES)).setDenyImportClasses(manifestMainAttributes.getValue(DENY_IMPORT_CLASSES)).setDenyImportResources(manifestMainAttributes.getValue(DENY_IMPORT_RESOURCES)).setInjectPluginDependencies(getInjectDependencies(manifestMainAttributes.getValue(INJECT_PLUGIN_DEPENDENCIES))).setInjectExportPackages(manifestMainAttributes.getValue(INJECT_EXPORT_PACKAGES)).setClassPath(bizArchive.getUrls()).setClassLoader(new BizClassLoader(bizModel.getIdentity(), getBizUcp(bizModel.getClassPath()), bizArchive instanceof ExplodedBizArchive));
    return bizModel;
}
Also used : Attributes(java.util.jar.Attributes) ExplodedBizArchive(com.alipay.sofa.ark.loader.ExplodedBizArchive) BizModel(com.alipay.sofa.ark.container.model.BizModel) BizClassLoader(com.alipay.sofa.ark.container.service.classloader.BizClassLoader)

Example 4 with BizModel

use of com.alipay.sofa.ark.container.model.BizModel in project sofa-ark by alipay.

the class BizClassLoaderTest method testLoadClassFromAgentClassLoader.

@Test
public void testLoadClassFromAgentClassLoader() throws ClassNotFoundException {
    BizModel bizModel = new BizModel().setBizState(BizState.RESOLVED);
    bizModel.setBizName("MockBiz").setBizVersion("1.0.0").setClassPath(new URL[] {}).setClassLoader(new BizClassLoader(bizModel.getIdentity(), bizModel.getClassPath()));
    bizModel.setDenyImportResources(StringUtils.EMPTY_STRING);
    bizModel.setDenyImportClasses(StringUtils.EMPTY_STRING);
    bizModel.setDenyImportPackages(StringUtils.EMPTY_STRING);
    bizManagerService.registerBiz(bizModel);
    BizClassLoader bizClassLoader = (BizClassLoader) bizModel.getBizClassLoader();
    Assert.assertNotNull(bizClassLoader.loadClass("SampleClass", false));
    Class clazz = bizClassLoader.loadClass(ArkClient.class.getCanonicalName());
    Assert.assertTrue(clazz.getClassLoader().equals(classloaderService.getArkClassLoader()));
}
Also used : BizModel(com.alipay.sofa.ark.container.model.BizModel) ArkClient(com.alipay.sofa.ark.api.ArkClient) URL(java.net.URL) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Example 5 with BizModel

use of com.alipay.sofa.ark.container.model.BizModel in project sofa-ark by alipay.

the class BizClassLoaderTest method testDenyImport.

@Test
public void testDenyImport() throws Exception {
    PluginModel pluginA = new PluginModel();
    pluginA.setPluginName("pluginA").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setImportResources(StringUtils.EMPTY_STRING).setExportResources("pluginA_export_resource1.xml,pluginA_export_resource2.xml").setExportClasses(ITest.class.getName()).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
    pluginManagerService.registerPlugin(pluginA);
    pluginDeployService.deploy();
    classloaderService.prepareExportClassAndResourceCache();
    BizModel bizModel = new BizModel().setBizState(BizState.RESOLVED);
    bizModel.setBizName("bizA").setBizVersion("1.0.0").setClassPath(new URL[] {}).setClassLoader(new BizClassLoader(bizModel.getIdentity(), bizModel.getClassPath()));
    bizModel.setDenyImportResources(StringUtils.EMPTY_STRING);
    bizModel.setDenyImportPackages(StringUtils.EMPTY_STRING);
    bizModel.setDenyImportClasses(StringUtils.EMPTY_STRING);
    bizManagerService.registerBiz(bizModel);
    Assert.assertNotNull(bizModel.getBizClassLoader().getResource("pluginA_export_resource1.xml"));
    Assert.assertNotNull(bizModel.getBizClassLoader().getResource("pluginA_export_resource2.xml"));
    bizModel.setDenyImportResources("pluginA_export_resource2.xml");
    invalidClassLoaderCache(bizModel.getBizClassLoader());
    Assert.assertNull(bizModel.getBizClassLoader().getResource("pluginA_export_resource2.xml"));
    Assert.assertTrue(bizModel.getBizClassLoader().loadClass(ITest.class.getName()).getClassLoader() instanceof PluginClassLoader);
    bizModel.setDenyImportPackages("com.alipay.sofa.ark.container.testdata");
    invalidClassLoaderCache(bizModel.getBizClassLoader());
    Assert.assertFalse(bizModel.getBizClassLoader().loadClass(ITest.class.getName()).getClassLoader() instanceof PluginClassLoader);
    bizModel.setDenyImportPackages(StringUtils.EMPTY_STRING);
    bizModel.setDenyImportClasses(ITest.class.getCanonicalName());
    invalidClassLoaderCache(bizModel.getBizClassLoader());
    Assert.assertFalse(bizModel.getBizClassLoader().loadClass(ITest.class.getName()).getClassLoader() instanceof PluginClassLoader);
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) ITest(com.alipay.sofa.ark.container.testdata.ITest) BizModel(com.alipay.sofa.ark.container.model.BizModel) URL(java.net.URL) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Aggregations

BizModel (com.alipay.sofa.ark.container.model.BizModel)31 Test (org.junit.Test)22 BaseTest (com.alipay.sofa.ark.container.BaseTest)18 Biz (com.alipay.sofa.ark.spi.model.Biz)12 ITest (com.alipay.sofa.ark.container.testdata.ITest)11 URL (java.net.URL)11 PluginModel (com.alipay.sofa.ark.container.model.PluginModel)8 HandleArchiveStage (com.alipay.sofa.ark.container.pipeline.HandleArchiveStage)4 BizManagerService (com.alipay.sofa.ark.spi.service.biz.BizManagerService)3 URLClassLoader (java.net.URLClassLoader)3 HashSet (java.util.HashSet)3 BizClassLoader (com.alipay.sofa.ark.container.service.classloader.BizClassLoader)2 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)2 ExplodedBizArchive (com.alipay.sofa.ark.loader.ExplodedBizArchive)2 AfterBizStartupEvent (com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent)2 BeforePluginStartupEvent (com.alipay.sofa.ark.spi.event.plugin.BeforePluginStartupEvent)2 Plugin (com.alipay.sofa.ark.spi.model.Plugin)2 Attributes (java.util.jar.Attributes)2 ArkClient (com.alipay.sofa.ark.api.ArkClient)1 JarBizArchive (com.alipay.sofa.ark.loader.JarBizArchive)1