Search in sources :

Example 1 with ServiceReference

use of com.alipay.sofa.ark.spi.registry.ServiceReference in project sofa-ark by alipay.

the class RegistryServiceImpl method publishService.

@Override
public <T> ServiceReference<T> publishService(Class<T> ifClass, T implObject, ServiceProvider serviceProvider) {
    ServiceMetadata serviceMetadata = new ServiceMetadataImpl(ifClass.getName(), ifClass, serviceProvider);
    if (!services.containsKey(serviceMetadata.getServiceName())) {
        services.putIfAbsent(serviceMetadata.getServiceName(), new CopyOnWriteArrayList<ServiceReference<?>>());
    }
    List<ServiceReference<?>> serviceReferences = services.get(serviceMetadata.getServiceName());
    for (ServiceReference<?> serviceReference : serviceReferences) {
        if (serviceMetadata.equals(serviceReference.getServiceMetadata())) {
            LOGGER.warn(String.format("Service: %s publish by: %s already exist", serviceMetadata.getServiceName(), serviceProvider));
            return (ServiceReference<T>) serviceReference;
        }
    }
    ServiceReference<T> serviceReference = new ServiceReferenceImpl<>(serviceMetadata, implObject);
    LOGGER.info(String.format("Service: %s publish by: %s succeed", serviceMetadata.getServiceName(), serviceProvider));
    serviceReferences.add(serviceReference);
    return serviceReference;
}
Also used : ServiceMetadataImpl(com.alipay.sofa.ark.container.registry.ServiceMetadataImpl) ServiceMetadata(com.alipay.sofa.ark.spi.registry.ServiceMetadata) ServiceReference(com.alipay.sofa.ark.spi.registry.ServiceReference) ServiceReferenceImpl(com.alipay.sofa.ark.container.registry.ServiceReferenceImpl)

Example 2 with ServiceReference

use of com.alipay.sofa.ark.spi.registry.ServiceReference 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());
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) DefaultServiceFilter(com.alipay.sofa.ark.container.registry.DefaultServiceFilter) ServiceFilter(com.alipay.sofa.ark.spi.registry.ServiceFilter) TestObjectC(com.alipay.sofa.ark.container.testdata.impl.TestObjectC) DefaultServiceFilter(com.alipay.sofa.ark.container.registry.DefaultServiceFilter) TestObjectA(com.alipay.sofa.ark.container.testdata.impl.TestObjectA) TestObjectB(com.alipay.sofa.ark.container.testdata.impl.TestObjectB) ServiceProvider(com.alipay.sofa.ark.spi.registry.ServiceProvider) ContainerServiceProvider(com.alipay.sofa.ark.container.registry.ContainerServiceProvider) PluginServiceProvider(com.alipay.sofa.ark.container.registry.PluginServiceProvider) ContainerServiceProvider(com.alipay.sofa.ark.container.registry.ContainerServiceProvider) PluginServiceProvider(com.alipay.sofa.ark.container.registry.PluginServiceProvider) ServiceReference(com.alipay.sofa.ark.spi.registry.ServiceReference) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Example 3 with ServiceReference

use of com.alipay.sofa.ark.spi.registry.ServiceReference 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 4 with ServiceReference

use of com.alipay.sofa.ark.spi.registry.ServiceReference in project sofa-ark by alipay.

the class OperationTransformerTest method testTransformUnstableState.

@Test
public void testTransformUnstableState() {
    Exception ex = null;
    try {
        List<Biz> bizList = new ArrayList<>();
        Biz biz1 = Mockito.mock(Biz.class);
        Biz biz2 = Mockito.mock(Biz.class);
        Biz biz3 = Mockito.mock(Biz.class);
        bizList.add(biz1);
        bizList.add(biz2);
        bizList.add(biz3);
        when(biz1.getBizName()).thenReturn("nameA");
        when(biz1.getBizVersion()).thenReturn("vA");
        when(biz1.getBizState()).thenReturn(BizState.ACTIVATED);
        when(biz1.getBizName()).thenReturn("nameA");
        when(biz1.getBizVersion()).thenReturn("vB");
        when(biz1.getBizState()).thenReturn(BizState.RESOLVED);
        when(biz1.getBizName()).thenReturn("nameB");
        when(biz1.getBizVersion()).thenReturn("vA");
        when(biz1.getBizState()).thenReturn(BizState.ACTIVATED);
        PluginContext pluginContext = Mockito.mock(PluginContext.class);
        ServiceReference serviceReference = Mockito.mock(ServiceReference.class);
        BizManagerService bizManagerService = Mockito.mock(BizManagerService.class);
        when(serviceReference.getService()).thenReturn(bizManagerService);
        when(pluginContext.referenceService(any(Class.class))).thenReturn(serviceReference);
        when(bizManagerService.getBizInOrder()).thenReturn(bizList);
        OperationTransformer.transformToBizOperation("nameA:vA:activated;nameA:vB:deactivated;nameB:vB:activated", pluginContext);
    } catch (Exception e) {
        ex = e;
    }
    Assert.assertNotNull(ex);
    Assert.assertTrue(ex.getMessage().contains("Exist illegal biz"));
}
Also used : Biz(com.alipay.sofa.ark.spi.model.Biz) PluginContext(com.alipay.sofa.ark.spi.model.PluginContext) ArrayList(java.util.ArrayList) BizManagerService(com.alipay.sofa.ark.spi.service.biz.BizManagerService) ServiceReference(com.alipay.sofa.ark.spi.registry.ServiceReference) Test(org.junit.Test)

Aggregations

ServiceReference (com.alipay.sofa.ark.spi.registry.ServiceReference)4 Test (org.junit.Test)3 BaseTest (com.alipay.sofa.ark.container.BaseTest)2 PluginModel (com.alipay.sofa.ark.container.model.PluginModel)2 ContainerServiceProvider (com.alipay.sofa.ark.container.registry.ContainerServiceProvider)2 DefaultServiceFilter (com.alipay.sofa.ark.container.registry.DefaultServiceFilter)2 PluginServiceProvider (com.alipay.sofa.ark.container.registry.PluginServiceProvider)2 ITest (com.alipay.sofa.ark.container.testdata.ITest)2 TestObjectA (com.alipay.sofa.ark.container.testdata.impl.TestObjectA)2 PluginContextImpl (com.alipay.sofa.ark.container.model.PluginContextImpl)1 ServiceMetadataImpl (com.alipay.sofa.ark.container.registry.ServiceMetadataImpl)1 ServiceReferenceImpl (com.alipay.sofa.ark.container.registry.ServiceReferenceImpl)1 PluginClassLoader (com.alipay.sofa.ark.container.service.classloader.PluginClassLoader)1 PluginActivatorA (com.alipay.sofa.ark.container.testdata.activator.PluginActivatorA)1 PluginActivatorB (com.alipay.sofa.ark.container.testdata.activator.PluginActivatorB)1 PluginActivatorC (com.alipay.sofa.ark.container.testdata.activator.PluginActivatorC)1 TestObjectB (com.alipay.sofa.ark.container.testdata.impl.TestObjectB)1 TestObjectC (com.alipay.sofa.ark.container.testdata.impl.TestObjectC)1 Biz (com.alipay.sofa.ark.spi.model.Biz)1 PluginContext (com.alipay.sofa.ark.spi.model.PluginContext)1