use of com.alipay.sofa.ark.container.model.PluginContextImpl 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.PluginContextImpl in project sofa-ark by alipay.
the class PluginFactoryServiceImpl method createPlugin.
@Override
public Plugin createPlugin(PluginArchive pluginArchive, URL[] extensions, Set<String> exportPackages) throws IOException, IllegalArgumentException {
AssertUtils.isTrue(isArkPlugin(pluginArchive), "Archive must be a ark plugin!");
if (extensions == null || extensions.length == 0) {
return createPlugin(pluginArchive);
}
PluginModel plugin = new PluginModel();
Attributes manifestMainAttributes = pluginArchive.getManifest().getMainAttributes();
plugin.setPluginName(manifestMainAttributes.getValue(PLUGIN_NAME_ATTRIBUTE)).setGroupId(manifestMainAttributes.getValue(GROUP_ID_ATTRIBUTE)).setArtifactId(manifestMainAttributes.getValue(ARTIFACT_ID_ATTRIBUTE)).setVersion(manifestMainAttributes.getValue(PLUGIN_VERSION_ATTRIBUTE)).setPriority(manifestMainAttributes.getValue(PRIORITY_ATTRIBUTE)).setPluginActivator(manifestMainAttributes.getValue(ACTIVATOR_ATTRIBUTE)).setClassPath(getFinalPluginUrls(pluginArchive, extensions, plugin.getPluginName())).setPluginUrl(pluginArchive.getUrl()).setExportClasses(manifestMainAttributes.getValue(EXPORT_CLASSES_ATTRIBUTE)).setExportPackages(manifestMainAttributes.getValue(EXPORT_PACKAGES_ATTRIBUTE), exportPackages).setImportClasses(manifestMainAttributes.getValue(IMPORT_CLASSES_ATTRIBUTE)).setImportPackages(manifestMainAttributes.getValue(IMPORT_PACKAGES_ATTRIBUTE)).setImportResources(manifestMainAttributes.getValue(IMPORT_RESOURCES_ATTRIBUTE)).setExportResources(manifestMainAttributes.getValue(EXPORT_RESOURCES_ATTRIBUTE)).setPluginClassLoader(new PluginClassLoader(plugin.getPluginName(), plugin.getClassPath())).setPluginContext(new PluginContextImpl(plugin));
return plugin;
}
use of com.alipay.sofa.ark.container.model.PluginContextImpl in project sofa-ark by alipay.
the class PluginFactoryServiceImpl method createEmbedPlugin.
@Override
public Plugin createEmbedPlugin(PluginArchive pluginArchive, ClassLoader masterClassLoader) throws IOException {
AssertUtils.isTrue(isArkPlugin(pluginArchive), "Archive must be a ark plugin!");
PluginModel plugin = new PluginModel();
Attributes manifestMainAttributes = pluginArchive.getManifest().getMainAttributes();
boolean enableExportClass = "true".equals(System.getProperty(PLUGIN_EXPORT_CLASS_ENABLE));
plugin.setPluginName(manifestMainAttributes.getValue(PLUGIN_NAME_ATTRIBUTE)).setGroupId(manifestMainAttributes.getValue(GROUP_ID_ATTRIBUTE)).setArtifactId(manifestMainAttributes.getValue(ARTIFACT_ID_ATTRIBUTE)).setVersion(manifestMainAttributes.getValue(PLUGIN_VERSION_ATTRIBUTE)).setPriority(manifestMainAttributes.getValue(PRIORITY_ATTRIBUTE)).setPluginActivator(manifestMainAttributes.getValue(ACTIVATOR_ATTRIBUTE)).setClassPath(((URLClassLoader) masterClassLoader).getURLs()).setPluginUrl(pluginArchive.getUrl()).setExportClasses(enableExportClass ? manifestMainAttributes.getValue(EXPORT_CLASSES_ATTRIBUTE) : null).setExportPackages(enableExportClass ? manifestMainAttributes.getValue(EXPORT_PACKAGES_ATTRIBUTE) : null).setImportClasses(manifestMainAttributes.getValue(IMPORT_CLASSES_ATTRIBUTE)).setImportPackages(manifestMainAttributes.getValue(IMPORT_PACKAGES_ATTRIBUTE)).setImportResources(manifestMainAttributes.getValue(IMPORT_RESOURCES_ATTRIBUTE)).setExportResources(manifestMainAttributes.getValue(EXPORT_RESOURCES_ATTRIBUTE)).setPluginClassLoader(masterClassLoader).setPluginContext(new PluginContextImpl(plugin));
return plugin;
}
use of com.alipay.sofa.ark.container.model.PluginContextImpl in project sofa-ark by alipay.
the class ServiceRegistrationTest method testPublishDuplicateServiceInPlugin.
@Test
public void testPublishDuplicateServiceInPlugin() throws Exception {
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(StringUtils.EMPTY_STRING).setImportResources(StringUtils.EMPTY_STRING).setExportResources(StringUtils.EMPTY_STRING).setPluginActivator(PluginActivatorADup.class.getName()).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath())).setPluginContext(new PluginContextImpl(pluginA));
pluginManagerService.registerPlugin(pluginA);
classloaderService.prepareExportClassAndResourceCache();
pluginDeployService.deploy();
Assert.assertEquals(1, registryService.referenceServices(pluginA.getPluginClassLoader().loadClass(ITest.class.getCanonicalName())).size());
int c = registryService.unPublishServices(new DefaultServiceFilter().setProviderType(ServiceProviderType.ARK_PLUGIN));
Assert.assertTrue(c == 1);
}
Aggregations