use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class PluginClassloaderTest method testExportAndImport.
@Test
public void testExportAndImport() 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()));
PluginModel pluginB = new PluginModel();
pluginB.setPluginName("plugin B").setPriority(1).setClassPath(new URL[] { classPathURL }).setImportClasses(new HashSet<>(Collections.singletonList(ITest.class.getName()))).setImportPackages(Collections.<String>emptySet()).setExportIndex(Collections.<String>emptySet()).setPluginClassLoader(new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath()));
pluginManagerService.registerPlugin(pluginA);
pluginManagerService.registerPlugin(pluginB);
classloaderService.prepareExportClassCache();
pluginDeployService.deploy();
Assert.assertEquals(pluginA.getPluginClassLoader().loadClass(ITest.class.getName()), pluginB.getPluginClassLoader().loadClass(ITest.class.getName()));
}
use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class RegistryServiceTest method testMultipleService.
@Test
public void testMultipleService() throws Exception {
// 非插件发布的服务,优先级别最低
registryService.publishService(ITest.class, new TestObjectA());
PluginModel pluginA = new PluginModel();
pluginA.setPluginName("plugin A").setPriority(10).setClassPath(new URL[] { classPathURL }).setImportClasses(Collections.<String>emptySet()).setImportPackages(Collections.<String>emptySet()).setExportIndex(new HashSet<>(Collections.singletonList(INTERFACE_CLASS))).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(new HashSet<>(Collections.singletonList(INTERFACE_CLASS))).setImportPackages(Collections.<String>emptySet()).setExportIndex(Collections.<String>emptySet()).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(new HashSet<>(Collections.singletonList(INTERFACE_CLASS))).setImportPackages(Collections.<String>emptySet()).setExportIndex(Collections.<String>emptySet()).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.prepareExportClassCache();
pluginDeployService.deploy();
Assert.assertEquals(4, registryService.referenceServices(ITest.class).size());
Assert.assertEquals(3, pluginA.getPluginContext().referenceServices(ITest.class).size());
// 应该获取到优先级别比较高的服务
Assert.assertEquals(pluginB.getPluginName(), pluginc.getPluginContext().referenceService(ITest.class).getServiceMetadata().getServiceProvider().getServiceProviderName());
// 通过插件名寻找服务
Assert.assertEquals(pluginA.getPluginName(), pluginc.getPluginContext().referenceService(ITest.class, pluginA.getPluginName()).getServiceMetadata().getServiceProvider().getServiceProviderName());
}
use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class PluginClassLoaderTest method testExportAndNotImport.
@Test
public void testExportAndNotImport() throws Exception {
PluginModel pluginA = new PluginModel();
pluginA.setPluginName("plugin A").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("com.alipay.sofa.ark").setExportClasses("").setExportResources(StringUtils.EMPTY_STRING).setImportResources(StringUtils.EMPTY_STRING).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
PluginModel pluginB = new PluginModel();
pluginB.setPluginName("plugin B").setPriority("1").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setExportResources(StringUtils.EMPTY_STRING).setImportResources(StringUtils.EMPTY_STRING).setPluginClassLoader(new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath()));
pluginManagerService.registerPlugin(pluginA);
pluginManagerService.registerPlugin(pluginB);
classloaderService.prepareExportClassAndResourceCache();
pluginDeployService.deploy();
Assert.assertNotEquals(pluginA.getPluginClassLoader().loadClass(ITest.class.getName()), pluginB.getPluginClassLoader().loadClass(ITest.class.getName()));
}
use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class PluginClassLoaderTest method testExportResource.
@Test
public void testExportResource() {
PluginModel pluginA = new PluginModel();
pluginA.setPluginName("pluginA").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages(ClassUtils.getPackageName(ITest.class.getCanonicalName())).setExportClasses("").setImportResources(StringUtils.EMPTY_STRING).setExportResources("pluginA_export_resource1.xml,pluginA_export_resource2.xml").setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
PluginModel pluginB = new PluginModel();
pluginB.setPluginName("pluginB").setPriority("1").setClassPath(new URL[0]).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setImportResources("pluginA_export_resource1.xml").setExportResources(StringUtils.EMPTY_STRING).setPluginClassLoader(new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath()));
pluginManagerService.registerPlugin(pluginA);
pluginManagerService.registerPlugin(pluginB);
classloaderService.prepareExportClassAndResourceCache();
pluginDeployService.deploy();
Assert.assertNotNull(pluginB.getPluginClassLoader().getResource("pluginA_export_resource1.xml"));
Assert.assertNull(pluginB.getPluginClassLoader().getResource("pluginA_export_resource2.xml"));
Assert.assertNull(pluginB.getPluginClassLoader().getResource("pluginA_not_export_resource.xml"));
}
use of com.alipay.sofa.ark.container.model.PluginModel in project sofa-ark by alipay.
the class PluginClassLoaderTest method testGetJdkResource.
@Test
public void testGetJdkResource() throws IOException {
PluginModel mockPlugin = new PluginModel();
mockPlugin.setPluginName("Mock plugin").setClassPath(new URL[] {}).setImportResources(StringUtils.EMPTY_STRING).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages(ITest.class.getCanonicalName()).setPluginClassLoader(new PluginClassLoader(mockPlugin.getPluginName(), mockPlugin.getClassPath()));
pluginManagerService.registerPlugin(mockPlugin);
ClassLoader cl = mockPlugin.getPluginClassLoader();
String name = "META-INF/services/javax.script.ScriptEngineFactory";
URL res1 = cl.getResource(name);
Assert.assertNotNull(res1);
URL res2 = ClassLoader.getSystemClassLoader().getResource(name);
Assert.assertEquals(res2, res1);
Enumeration<URL> enu1 = cl.getResources(name);
Assert.assertTrue(enu1.hasMoreElements());
Enumeration<URL> enu2 = ClassLoader.getSystemClassLoader().getResources(name);
Assert.assertEquals(Sets.newHashSet(Collections.list(enu2)), Sets.newHashSet(Collections.list(enu1)));
}
Aggregations