use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class PluginManagerServiceTest method testGetPluginByName.
@Test
public void testGetPluginByName() {
PluginModel plugin = new PluginModel();
plugin.setPluginName("plugin A");
pluginManagerService.registerPlugin(plugin);
Assert.assertNotNull(pluginManagerService.getPluginByName(plugin.getPluginName()));
Assert.assertNull(pluginManagerService.getPluginByName("test"));
}
use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class PluginManagerServiceTest method testGetPluginsInOrder.
@Test
public void testGetPluginsInOrder() {
PluginModel pluginA = new PluginModel();
pluginA.setPluginName("plugin A").setPriority("100");
pluginManagerService.registerPlugin(pluginA);
PluginModel pluginB = new PluginModel();
pluginB.setPluginName("plugin B").setPriority("10");
pluginManagerService.registerPlugin(pluginB);
PluginModel pluginC = new PluginModel();
pluginC.setPluginName("plugin C").setPriority("1000");
pluginManagerService.registerPlugin(pluginC);
Assert.assertEquals(3, pluginManagerService.getPluginsInOrder().size());
Assert.assertEquals(pluginB, pluginManagerService.getPluginsInOrder().get(0));
Assert.assertEquals(pluginA, pluginManagerService.getPluginsInOrder().get(1));
Assert.assertEquals(pluginC, pluginManagerService.getPluginsInOrder().get(2));
}
use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class ServiceRegistrationTest method testFilter.
@Test
@SuppressWarnings("unchecked")
public void testFilter() {
final PluginModel pluginA = new PluginModel();
pluginA.setPluginName("plugin A").setPriority("10");
PluginModel pluginB = new PluginModel();
pluginB.setPluginName("plugin B").setPriority("1");
pluginManagerService.registerPlugin(pluginA);
pluginManagerService.registerPlugin(pluginB);
registryService.publishService(ITest.class, new TestObjectA(), new PluginServiceProvider(pluginA));
registryService.publishService(ITest.class, new TestObjectB(), new PluginServiceProvider(pluginB));
registryService.publishService(ITest.class, new TestObjectC(), new ContainerServiceProvider());
List<ServiceReference> references = registryService.referenceServices(new DefaultServiceFilter().setServiceInterface(ITest.class).setProviderType(ServiceProviderType.ARK_PLUGIN));
Assert.assertTrue(2 == references.size());
PluginServiceProvider provider = (PluginServiceProvider) references.get(0).getServiceMetadata().getServiceProvider();
Assert.assertEquals(pluginB.getPluginName(), provider.getPluginName());
references = registryService.referenceServices(new ServiceFilter() {
@Override
public boolean match(ServiceReference serviceReference) {
ServiceProvider serviceProvider = serviceReference.getServiceMetadata().getServiceProvider();
if (serviceProvider instanceof PluginServiceProvider) {
if (((PluginServiceProvider) serviceProvider).getPluginName().equals(pluginA.getPluginName())) {
return true;
}
}
return false;
}
});
Assert.assertTrue(1 == references.size());
provider = (PluginServiceProvider) references.get(0).getServiceMetadata().getServiceProvider();
Assert.assertEquals(pluginA.getPluginName(), provider.getPluginName());
references = registryService.referenceServices(new DefaultServiceFilter().setServiceInterface(ITest.class));
Assert.assertTrue(3 == references.size());
references = registryService.referenceServices(new DefaultServiceFilter().setProviderType(ServiceProviderType.ARK_CONTAINER).setServiceInterface(ITest.class));
Assert.assertTrue(1 == references.size());
Assert.assertEquals("TestObject C", ((TestObjectC) references.get(0).getService()).test());
}
use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class ServiceRegistrationTest method testMultipleService.
@Test
@SuppressWarnings("unchecked")
public void testMultipleService() throws Exception {
// 非插件发布的服务,优先级别最低
registryService.publishService(ITest.class, new TestObjectA(), new ContainerServiceProvider());
PluginModel pluginA = new PluginModel();
pluginA.setPluginName("plugin A").setPriority("10").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages(ClassUtils.getPackageName(INTERFACE_CLASS)).setExportClasses("").setImportResources(StringUtils.EMPTY_STRING).setExportResources(StringUtils.EMPTY_STRING).setPluginActivator(PluginActivatorA.class.getName()).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath())).setPluginContext(new PluginContextImpl(pluginA));
PluginModel pluginB = new PluginModel();
pluginB.setPluginName("plugin B").setPriority("1").setClassPath(new URL[] { classPathURL }).setImportClasses(INTERFACE_CLASS).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setImportResources(StringUtils.EMPTY_STRING).setExportResources(StringUtils.EMPTY_STRING).setPluginActivator(PluginActivatorB.class.getName()).setPluginClassLoader(new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath())).setPluginContext(new PluginContextImpl(pluginB));
PluginModel pluginC = new PluginModel();
pluginC.setPluginName("plugin C").setPriority("100").setClassPath(new URL[] { classPathURL }).setImportClasses(INTERFACE_CLASS).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setImportResources(StringUtils.EMPTY_STRING).setExportResources(StringUtils.EMPTY_STRING).setPluginActivator(PluginActivatorC.class.getName()).setPluginClassLoader(new PluginClassLoader(pluginC.getPluginName(), pluginC.getClassPath())).setPluginContext(new PluginContextImpl(pluginC));
pluginManagerService.registerPlugin(pluginA);
pluginManagerService.registerPlugin(pluginB);
pluginManagerService.registerPlugin(pluginC);
classloaderService.prepareExportClassAndResourceCache();
pluginDeployService.deploy();
Class iTest = pluginA.getPluginClassLoader().loadClass(ITest.class.getCanonicalName());
Assert.assertEquals(3, pluginA.getPluginContext().referenceServices(new DefaultServiceFilter().setServiceInterface(iTest)).size());
// 应该获取到优先级别比较高的服务
ServiceReference reference = pluginC.getPluginContext().referenceService(iTest);
PluginServiceProvider provider = (PluginServiceProvider) reference.getServiceMetadata().getServiceProvider();
Assert.assertEquals(pluginB.getPluginName(), provider.getPluginName());
List<ServiceReference> references = pluginC.getPluginContext().referenceServices(new DefaultServiceFilter().setServiceInterface(iTest));
provider = (PluginServiceProvider) references.get(0).getServiceMetadata().getServiceProvider();
Assert.assertEquals(pluginB.getPluginName(), provider.getPluginName());
}
use of com.alipay.sofa.ark.container.model.PluginModel 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);
}
Aggregations