Search in sources :

Example 1 with PluginContextImpl

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

the class RegistryServiceTest method testPublishDuplicateServiceInPlugin.

@Test
public void testPublishDuplicateServiceInPlugin() throws Exception {
    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(PluginActivatorADup.class.getName()).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath())).setPluginContext(new PluginContextImpl(pluginA));
    pluginManagerService.registerPlugin(pluginA);
    classloaderService.prepareExportClassCache();
    pluginDeployService.deploy();
    Assert.assertEquals(1, registryService.referenceServices(ITest.class).size());
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) PluginContextImpl(com.alipay.sofa.ark.container.model.PluginContextImpl) PluginActivatorADup(com.alipay.sofa.ark.container.testdata.Activator.PluginActivatorADup) HashSet(java.util.HashSet) PluginClassLoader(com.alipay.sofa.ark.container.service.classloader.PluginClassLoader) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Example 2 with PluginContextImpl

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

the class RegistryServiceTest method testContainerServiceNotFoundByPlugin.

@Test
public void testContainerServiceNotFoundByPlugin() {
    registryService.publishService(ITest.class, new TestObjectA());
    Assert.assertNotNull(registryService.referenceService(ITest.class));
    PluginModel pluginA = new PluginModel();
    pluginA.setPluginName("plugin A").setPriority(10).setPluginContext(new PluginContextImpl(pluginA));
    pluginManagerService.registerPlugin(pluginA);
    Assert.assertNull(pluginA.getPluginContext().referenceService(ITest.class));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) PluginContextImpl(com.alipay.sofa.ark.container.model.PluginContextImpl) TestObjectA(com.alipay.sofa.ark.container.testdata.impl.TestObjectA) ITest(com.alipay.sofa.ark.container.testdata.ITest) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Example 3 with PluginContextImpl

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

the class HandleArchiveStage method transformPluginArchive.

private Plugin transformPluginArchive(PluginArchive pluginArchive) throws Exception {
    PluginModel plugin = new PluginModel();
    Attributes manifestMainAttributes = pluginArchive.getManifest().getMainAttributes();
    return 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(Integer.valueOf(manifestMainAttributes.getValue(PRIORITY_ATTRIBUTE))).setPluginActivator(manifestMainAttributes.getValue(ACTIVATOR_ATTRIBUTE)).setClassPath(pluginArchive.getUrls()).setExportClasses(new HashSet<>(Arrays.asList(filterEmptyString(manifestMainAttributes.getValue(EXPORT_CLASSES_ATTRIBUTE))))).setExportPackages(new HashSet<>(Arrays.asList(filterEmptyString(manifestMainAttributes.getValue(EXPORT_PACKAGES_ATTRIBUTE))))).setImportClasses(new HashSet<>(Arrays.asList(filterEmptyString(manifestMainAttributes.getValue(IMPORT_CLASSES_ATTRIBUTE))))).setImportPackages(new HashSet<>(Arrays.asList(filterEmptyString(manifestMainAttributes.getValue(IMPORT_PACKAGES_ATTRIBUTE))))).setExportIndex(pluginArchive.getExportIndex()).setPluginClassLoader(new PluginClassLoader(plugin.getPluginName(), plugin.getClassPath())).setPluginContext(new PluginContextImpl(plugin));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) PluginContextImpl(com.alipay.sofa.ark.container.model.PluginContextImpl) Attributes(java.util.jar.Attributes) PluginClassLoader(com.alipay.sofa.ark.container.service.classloader.PluginClassLoader)

Example 4 with PluginContextImpl

use of com.alipay.sofa.ark.container.model.PluginContextImpl 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());
}
Also used : PluginContextImpl(com.alipay.sofa.ark.container.model.PluginContextImpl) TestObjectA(com.alipay.sofa.ark.container.testdata.impl.TestObjectA) PluginActivatorA(com.alipay.sofa.ark.container.testdata.activator.PluginActivatorA) URL(java.net.URL) ServiceReference(com.alipay.sofa.ark.spi.registry.ServiceReference) PluginModel(com.alipay.sofa.ark.container.model.PluginModel) DefaultServiceFilter(com.alipay.sofa.ark.container.registry.DefaultServiceFilter) ITest(com.alipay.sofa.ark.container.testdata.ITest) ContainerServiceProvider(com.alipay.sofa.ark.container.registry.ContainerServiceProvider) PluginActivatorC(com.alipay.sofa.ark.container.testdata.activator.PluginActivatorC) PluginActivatorB(com.alipay.sofa.ark.container.testdata.activator.PluginActivatorB) PluginServiceProvider(com.alipay.sofa.ark.container.registry.PluginServiceProvider) PluginClassLoader(com.alipay.sofa.ark.container.service.classloader.PluginClassLoader) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Example 5 with PluginContextImpl

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) throws IOException, IllegalArgumentException {
    AssertUtils.isTrue(isArkPlugin(pluginArchive), "Archive must be a ark plugin!");
    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(pluginArchive.getUrls()).setPluginUrl(pluginArchive.getUrl()).setExportClasses(manifestMainAttributes.getValue(EXPORT_CLASSES_ATTRIBUTE)).setExportPackages(manifestMainAttributes.getValue(EXPORT_PACKAGES_ATTRIBUTE)).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;
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) PluginContextImpl(com.alipay.sofa.ark.container.model.PluginContextImpl) Attributes(java.util.jar.Attributes) PluginClassLoader(com.alipay.sofa.ark.container.service.classloader.PluginClassLoader)

Aggregations

PluginContextImpl (com.alipay.sofa.ark.container.model.PluginContextImpl)9 PluginModel (com.alipay.sofa.ark.container.model.PluginModel)9 PluginClassLoader (com.alipay.sofa.ark.container.service.classloader.PluginClassLoader)7 BaseTest (com.alipay.sofa.ark.container.BaseTest)5 ITest (com.alipay.sofa.ark.container.testdata.ITest)5 Test (org.junit.Test)5 Attributes (java.util.jar.Attributes)4 TestObjectA (com.alipay.sofa.ark.container.testdata.impl.TestObjectA)3 DefaultServiceFilter (com.alipay.sofa.ark.container.registry.DefaultServiceFilter)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 ContainerServiceProvider (com.alipay.sofa.ark.container.registry.ContainerServiceProvider)1 PluginServiceProvider (com.alipay.sofa.ark.container.registry.PluginServiceProvider)1 PluginActivatorA (com.alipay.sofa.ark.container.testdata.Activator.PluginActivatorA)1 PluginActivatorADup (com.alipay.sofa.ark.container.testdata.Activator.PluginActivatorADup)1 PluginActivatorB (com.alipay.sofa.ark.container.testdata.Activator.PluginActivatorB)1 PluginActivatorC (com.alipay.sofa.ark.container.testdata.Activator.PluginActivatorC)1 PluginActivatorA (com.alipay.sofa.ark.container.testdata.activator.PluginActivatorA)1 PluginActivatorADup (com.alipay.sofa.ark.container.testdata.activator.PluginActivatorADup)1 PluginActivatorB (com.alipay.sofa.ark.container.testdata.activator.PluginActivatorB)1