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("/"));
}
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));
}
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));
}
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)));
}
}
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()));
}
Aggregations