Search in sources :

Example 6 with ContainerServiceProvider

use of com.alipay.sofa.ark.container.registry.ContainerServiceProvider 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 7 with ContainerServiceProvider

use of com.alipay.sofa.ark.container.registry.ContainerServiceProvider in project sofa-ark by sofastack.

the class InjectionServiceTest method test.

@Test
public void test() {
    RegistryService registryService = ArkServiceContainerHolder.getContainer().getService(RegistryService.class);
    PluginMockService pluginMockService = new PluginMockService();
    registryService.publishService(PluginMockService.class, pluginMockService, new ContainerServiceProvider());
    Assert.assertNotNull(pluginMockService.getBizFactoryService());
    Assert.assertNotNull(pluginMockService.getBizManagerService());
    Assert.assertNull(pluginMockService.getClassLoaderService());
}
Also used : ContainerServiceProvider(com.alipay.sofa.ark.container.registry.ContainerServiceProvider) RegistryService(com.alipay.sofa.ark.spi.service.registry.RegistryService) BaseTest(com.alipay.sofa.ark.container.BaseTest) Test(org.junit.Test)

Example 8 with ContainerServiceProvider

use of com.alipay.sofa.ark.container.registry.ContainerServiceProvider in project sofa-ark by sofastack.

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 9 with ContainerServiceProvider

use of com.alipay.sofa.ark.container.registry.ContainerServiceProvider in project sofa-ark by sofastack.

the class ServiceRegistrationTest method testContainerService.

@Test
public void testContainerService() {
    registryService.publishService(ITest.class, new TestObjectA(), new ContainerServiceProvider(20000));
    registryService.publishService(ITest.class, new TestObjectB(), new ContainerServiceProvider(100));
    registryService.publishService(ITest.class, new TestObjectC(), new ContainerServiceProvider(200));
    Assert.assertEquals(TestObjectB.OUTPUT, registryService.referenceService(ITest.class).getService().test());
    Assert.assertEquals(3, registryService.referenceServices(ITest.class).size());
}
Also used : TestObjectC(com.alipay.sofa.ark.container.testdata.impl.TestObjectC) TestObjectA(com.alipay.sofa.ark.container.testdata.impl.TestObjectA) TestObjectB(com.alipay.sofa.ark.container.testdata.impl.TestObjectB) ITest(com.alipay.sofa.ark.container.testdata.ITest) ContainerServiceProvider(com.alipay.sofa.ark.container.registry.ContainerServiceProvider) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Example 10 with ContainerServiceProvider

use of com.alipay.sofa.ark.container.registry.ContainerServiceProvider in project sofa-ark by sofastack.

the class ServiceRegistrationTest method testReferenceService.

@Test
public void testReferenceService() {
    registryService.publishService(ITest.class, new TestObjectA(), new ContainerServiceProvider());
    ServiceReference<ITest> iTestServiceReference = registryService.referenceService(ITest.class);
    Assert.assertNotNull(iTestServiceReference);
    Assert.assertEquals(TestObjectA.OUTPUT, iTestServiceReference.getService().test());
}
Also used : TestObjectA(com.alipay.sofa.ark.container.testdata.impl.TestObjectA) ITest(com.alipay.sofa.ark.container.testdata.ITest) ContainerServiceProvider(com.alipay.sofa.ark.container.registry.ContainerServiceProvider) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Aggregations

ContainerServiceProvider (com.alipay.sofa.ark.container.registry.ContainerServiceProvider)18 BaseTest (com.alipay.sofa.ark.container.BaseTest)16 Test (org.junit.Test)16 ITest (com.alipay.sofa.ark.container.testdata.ITest)12 TestObjectA (com.alipay.sofa.ark.container.testdata.impl.TestObjectA)12 DefaultServiceFilter (com.alipay.sofa.ark.container.registry.DefaultServiceFilter)8 TestObjectB (com.alipay.sofa.ark.container.testdata.impl.TestObjectB)6 RegistryService (com.alipay.sofa.ark.spi.service.registry.RegistryService)6 PluginModel (com.alipay.sofa.ark.container.model.PluginModel)4 PluginServiceProvider (com.alipay.sofa.ark.container.registry.PluginServiceProvider)4 TestObjectC (com.alipay.sofa.ark.container.testdata.impl.TestObjectC)4 ServiceReference (com.alipay.sofa.ark.spi.registry.ServiceReference)4 PluginContextImpl (com.alipay.sofa.ark.container.model.PluginContextImpl)2 BizCommandProvider (com.alipay.sofa.ark.container.service.biz.BizCommandProvider)2 DefaultBizDeployer (com.alipay.sofa.ark.container.service.biz.DefaultBizDeployer)2 PluginClassLoader (com.alipay.sofa.ark.container.service.classloader.PluginClassLoader)2 PluginCommandProvider (com.alipay.sofa.ark.container.service.plugin.PluginCommandProvider)2 ArkCommandHandler (com.alipay.sofa.ark.container.session.handler.ArkCommandHandler)2 PluginActivatorA (com.alipay.sofa.ark.container.testdata.activator.PluginActivatorA)2 PluginActivatorB (com.alipay.sofa.ark.container.testdata.activator.PluginActivatorB)2