Search in sources :

Example 36 with PluginModel

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

the class PluginClassLoaderTest method testSlashResource.

@Test
public void testSlashResource() throws Throwable {
    URLClassLoader urlClassLoader = (URLClassLoader) this.getClass().getClassLoader();
    Field ucpFiled = URLClassLoader.class.getDeclaredField("ucp");
    ucpFiled.setAccessible(true);
    URLClassPath ucp = (URLClassPath) ucpFiled.get(urlClassLoader);
    PluginClassLoader pluginClassLoader = new PluginClassLoader("pluginName", ucp.getURLs());
    PluginModel mockPlugin = new PluginModel();
    mockPlugin.setPluginName("pluginName").setClassPath(new URL[] {}).setImportResources(StringUtils.EMPTY_STRING).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages(ClassUtils.getPackageName(ITest.class.getCanonicalName())).setExportClasses(StringUtils.EMPTY_STRING).setPluginClassLoader(pluginClassLoader);
    pluginManagerService.registerPlugin(mockPlugin);
    URL url = pluginClassLoader.getResource("");
    Assert.assertNotNull(url);
    Assert.assertEquals(url, this.getClass().getResource("/"));
}
Also used : Field(java.lang.reflect.Field) PluginModel(com.alipay.sofa.ark.container.model.PluginModel) URLClassLoader(java.net.URLClassLoader) URLClassPath(sun.misc.URLClassPath) URL(java.net.URL) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest)

Example 37 with PluginModel

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

the class PluginClassLoaderTest method testMultiExportResource.

@Test
public void testMultiExportResource() throws Exception {
    String resourceName = "multi_export.xml";
    PluginModel pluginA = new PluginModel();
    pluginA.setPluginName("pluginA").setPriority("100").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages(ClassUtils.getPackageName(ITest.class.getCanonicalName())).setExportClasses("").setImportResources(StringUtils.EMPTY_STRING).setExportResources(resourceName).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
    PluginModel pluginB = new PluginModel();
    pluginB.setPluginName("pluginB").setPriority("1").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setImportResources(StringUtils.EMPTY_STRING).setExportResources(resourceName).setPluginClassLoader(new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath()));
    PluginModel pluginC = new PluginModel();
    pluginC.setPluginName("pluginC").setPriority("1000").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setImportResources(StringUtils.EMPTY_STRING).setExportResources(resourceName).setPluginClassLoader(new PluginClassLoader(pluginC.getPluginName(), pluginC.getClassPath()));
    PluginModel pluginD = new PluginModel();
    pluginD.setPluginName("pluginD").setClassPath(new URL[0]).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setImportResources(resourceName).setExportResources(StringUtils.EMPTY_STRING).setPluginClassLoader(new PluginClassLoader(pluginD.getPluginName(), pluginD.getClassPath()));
    pluginManagerService.registerPlugin(pluginA);
    pluginManagerService.registerPlugin(pluginB);
    pluginManagerService.registerPlugin(pluginC);
    pluginManagerService.registerPlugin(pluginD);
    classloaderService.prepareExportClassAndResourceCache();
    pluginDeployService.deploy();
    Enumeration<URL> urlEnumeration = pluginD.getPluginClassLoader().getResources(resourceName);
    Assert.assertEquals(3, Collections.list(urlEnumeration).size());
    List<ClassLoader> classLoaders = classloaderService.findExportResourceClassLoadersInOrder(resourceName);
    Assert.assertEquals(3, classLoaders.size());
    Assert.assertEquals(pluginB.getPluginClassLoader(), classLoaders.get(0));
    Assert.assertEquals(pluginA.getPluginClassLoader(), classLoaders.get(1));
    Assert.assertEquals(pluginC.getPluginClassLoader(), classLoaders.get(2));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest)

Example 38 with PluginModel

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

the class EventTest method testEvent.

@Test
public void testEvent() {
    Biz biz = new BizModel().setBizName("test-biz").setBizVersion("1.0.0").setBizState(BizState.RESOLVED);
    Plugin plugin = new PluginModel().setPluginName("test-plugin").setVersion("1.0.0");
    eventAdminService.sendEvent(new AfterBizStartupEvent(biz));
    Assert.assertTrue(result.get(0).equalsIgnoreCase(Constants.BIZ_EVENT_TOPIC_AFTER_INVOKE_BIZ_START));
    eventAdminService.sendEvent(new BeforeBizStartupEvent(biz));
    Assert.assertTrue(result.get(1).equalsIgnoreCase(Constants.BIZ_EVENT_TOPIC_BEFORE_INVOKE_BIZ_START));
    eventAdminService.sendEvent(new BeforeBizStopEvent(biz));
    Assert.assertTrue(result.get(2).equalsIgnoreCase(Constants.BIZ_EVENT_TOPIC_BEFORE_INVOKE_BIZ_STOP));
    eventAdminService.sendEvent(new AfterBizStopEvent(biz));
    Assert.assertTrue(result.get(3).equalsIgnoreCase(Constants.BIZ_EVENT_TOPIC_AFTER_INVOKE_BIZ_STOP));
    eventAdminService.sendEvent(new BeforeBizSwitchEvent(biz));
    Assert.assertTrue(result.get(4).equalsIgnoreCase(Constants.BIZ_EVENT_TOPIC_BEFORE_INVOKE_BIZ_SWITCH));
    eventAdminService.sendEvent(new AfterBizSwitchEvent(biz));
    Assert.assertTrue(result.get(5).equalsIgnoreCase(Constants.BIZ_EVENT_TOPIC_AFTER_INVOKE_BIZ_SWITCH));
    eventAdminService.sendEvent(new AfterPluginStartupEvent(plugin));
    Assert.assertTrue(result.get(6).equalsIgnoreCase(Constants.PLUGIN_EVENT_TOPIC_AFTER_INVOKE_PLUGIN_START));
    eventAdminService.sendEvent(new AfterPluginStopEvent(plugin));
    Assert.assertTrue(result.get(7).equalsIgnoreCase(Constants.PLUGIN_EVENT_TOPIC_AFTER_INVOKE_PLUGIN_STOP));
    eventAdminService.sendEvent(new BeforePluginStartupEvent(plugin));
    Assert.assertTrue(result.get(8).equalsIgnoreCase(Constants.PLUGIN_EVENT_TOPIC_BEFORE_INVOKE_PLUGIN_START));
    eventAdminService.sendEvent(new BeforePluginStopEvent(plugin));
    Assert.assertTrue(result.get(9).equalsIgnoreCase(Constants.PLUGIN_EVENT_TOPIC_BEFORE_INVOKE_PLUGIN_STOP));
    eventAdminService.sendEvent(new AfterFinishDeployEvent());
    Assert.assertTrue(result.get(10).equalsIgnoreCase(Constants.ARK_EVENT_TOPIC_AFTER_FINISH_DEPLOY_STAGE));
    eventAdminService.sendEvent(new AfterFinishStartupEvent());
    Assert.assertTrue(result.get(11).equalsIgnoreCase(Constants.ARK_EVENT_TOPIC_AFTER_FINISH_STARTUP_STAGE));
    eventAdminService.sendEvent(new TestArkEvent(""));
    Assert.assertTrue(result.get(12).equalsIgnoreCase("test-ark"));
    eventAdminService.sendEvent(new BeforeBizRecycleEvent(biz));
    Assert.assertTrue(result.get(13).equalsIgnoreCase(Constants.BIZ_EVENT_TOPIC_BEFORE_RECYCLE_BIZ));
}
Also used : BeforePluginStopEvent(com.alipay.sofa.ark.spi.event.plugin.BeforePluginStopEvent) AfterPluginStartupEvent(com.alipay.sofa.ark.spi.event.plugin.AfterPluginStartupEvent) AfterBizStartupEvent(com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent) AfterFinishStartupEvent(com.alipay.sofa.ark.spi.event.AfterFinishStartupEvent) BeforeBizSwitchEvent(com.alipay.sofa.ark.spi.event.biz.BeforeBizSwitchEvent) BizModel(com.alipay.sofa.ark.container.model.BizModel) AfterBizStopEvent(com.alipay.sofa.ark.spi.event.biz.AfterBizStopEvent) BeforeBizRecycleEvent(com.alipay.sofa.ark.spi.event.biz.BeforeBizRecycleEvent) PluginModel(com.alipay.sofa.ark.container.model.PluginModel) BeforePluginStartupEvent(com.alipay.sofa.ark.spi.event.plugin.BeforePluginStartupEvent) Biz(com.alipay.sofa.ark.spi.model.Biz) BeforeBizStartupEvent(com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent) AfterFinishDeployEvent(com.alipay.sofa.ark.spi.event.AfterFinishDeployEvent) AfterBizSwitchEvent(com.alipay.sofa.ark.spi.event.biz.AfterBizSwitchEvent) BeforeBizStopEvent(com.alipay.sofa.ark.spi.event.biz.BeforeBizStopEvent) Plugin(com.alipay.sofa.ark.spi.model.Plugin) AfterPluginStopEvent(com.alipay.sofa.ark.spi.event.plugin.AfterPluginStopEvent) BaseTest(com.alipay.sofa.ark.container.BaseTest) Test(org.junit.Test)

Example 39 with PluginModel

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

the class ExtensionServiceTest method testNotExtensibleService.

@Test
public void testNotExtensibleService() {
    PluginManagerService pluginManagerService = ArkServiceContainerHolder.getContainer().getService(PluginManagerService.class);
    PluginModel pluginModel = new PluginModel().setPluginClassLoader(this.getClass().getClassLoader()).setPluginName("mock-plugin");
    pluginManagerService.registerPlugin(pluginModel);
    try {
        ArkServiceLoader.loadExtensionFromArkPlugin(ServiceC.class, "", "mock-plugin");
    } catch (ArkRuntimeException ex) {
        Assert.assertTrue(ex.getMessage().contains(String.format("is not annotated by %s.", Extensible.class)));
    }
}
Also used : PluginManagerService(com.alipay.sofa.ark.spi.service.plugin.PluginManagerService) PluginModel(com.alipay.sofa.ark.container.model.PluginModel) Extensible(com.alipay.sofa.ark.spi.service.extension.Extensible) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException) ArkContainerTest(com.alipay.sofa.ark.container.ArkContainerTest) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest)

Example 40 with PluginModel

use of com.alipay.sofa.ark.container.model.PluginModel 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(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportClasses("").setExportPackages(ClassUtils.getPackageName(ITest.class.getName())).setImportResources(StringUtils.EMPTY_STRING).setExportResources(StringUtils.EMPTY_STRING).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
    pluginManagerService.registerPlugin(pluginA);
    pluginDeployService.deploy();
    classloaderService.prepareExportClassAndResourceCache();
    BizModel bizModel = new BizModel().setBizState(BizState.RESOLVED);
    bizModel.setBizName("biz A").setBizVersion("1.0.0").setClassPath(new URL[] { classPathURL }).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);
    Assert.assertEquals(pluginA.getPluginClassLoader().loadClass(ITest.class.getName()), bizModel.getBizClassLoader().loadClass(ITest.class.getName()));
}
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

PluginModel (com.alipay.sofa.ark.container.model.PluginModel)47 Test (org.junit.Test)42 BaseTest (com.alipay.sofa.ark.container.BaseTest)34 ITest (com.alipay.sofa.ark.container.testdata.ITest)25 URL (java.net.URL)16 PluginContextImpl (com.alipay.sofa.ark.container.model.PluginContextImpl)9 HashSet (java.util.HashSet)9 BizModel (com.alipay.sofa.ark.container.model.BizModel)8 PluginClassLoader (com.alipay.sofa.ark.container.service.classloader.PluginClassLoader)7 Plugin (com.alipay.sofa.ark.spi.model.Plugin)7 TestObjectA (com.alipay.sofa.ark.container.testdata.impl.TestObjectA)5 PluginManagerService (com.alipay.sofa.ark.spi.service.plugin.PluginManagerService)5 ArkContainerTest (com.alipay.sofa.ark.container.ArkContainerTest)4 HandleArchiveStage (com.alipay.sofa.ark.container.pipeline.HandleArchiveStage)4 URLClassLoader (java.net.URLClassLoader)4 Attributes (java.util.jar.Attributes)4 DefaultServiceFilter (com.alipay.sofa.ark.container.registry.DefaultServiceFilter)3 PluginServiceProvider (com.alipay.sofa.ark.container.registry.PluginServiceProvider)3 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)3 ContainerServiceProvider (com.alipay.sofa.ark.container.registry.ContainerServiceProvider)2