Search in sources :

Example 6 with PluginModel

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

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

the class RegistryServiceTest method testFilter.

@Test
public void testFilter() {
    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());
    Assert.assertEquals(pluginB.getPluginName(), registryService.referenceService(ITest.class).getServiceMetadata().getServiceProvider().getServiceProviderName());
    Assert.assertEquals(pluginA.getPluginName(), registryService.referenceService(ITest.class, new PluginNameServiceFilter(pluginA.getPluginName())).getServiceMetadata().getServiceProvider().getServiceProviderName());
    Assert.assertNull(registryService.referenceService(ITest.class, new PluginNameServiceFilter("not exist")));
    Assert.assertEquals("ArkContainer", registryService.referenceService(ITest.class, new ServiceFilter() {

        @Override
        public boolean match(ServiceProvider serviceProvider) {
            return ServiceProviderType.ARK_CONTAINER.equals(serviceProvider.getServiceProviderType());
        }
    }).getServiceMetadata().getServiceProvider().getServiceProviderName());
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) PluginNameServiceFilter(com.alipay.sofa.ark.container.registry.PluginNameServiceFilter) ServiceFilter(com.alipay.sofa.ark.spi.registry.ServiceFilter) 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) ServiceProvider(com.alipay.sofa.ark.spi.registry.ServiceProvider) PluginServiceProvider(com.alipay.sofa.ark.container.registry.PluginServiceProvider) ArkContainerServiceProvider(com.alipay.sofa.ark.container.registry.ArkContainerServiceProvider) PluginServiceProvider(com.alipay.sofa.ark.container.registry.PluginServiceProvider) PluginNameServiceFilter(com.alipay.sofa.ark.container.registry.PluginNameServiceFilter) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest) Test(org.junit.Test)

Example 8 with PluginModel

use of com.alipay.sofa.ark.container.model.PluginModel 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 9 with PluginModel

use of com.alipay.sofa.ark.container.model.PluginModel 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 10 with PluginModel

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

the class PluginCommandProviderTest method testPluginCommandProcess.

@Test
public void testPluginCommandProcess(@Mocked final PluginManagerService pluginManagerService) {
    URL[] urls = ((URLClassLoader) this.getClass().getClassLoader()).getURLs();
    final Plugin pluginA = new PluginModel().setPluginName("pluginA").setImportClasses("com.google.common.io.AppendableWriter").setImportPackages(" com.google.common.cache, com.google.common.base ").setImportResources(" import_resource1").setExportPackages("com.alipay.sofa.*").setExportResources(" export_resource1, export_resource2").setPluginActivator("com.alipay.sofa.pluginA.Activator").setClassPath(urls).setGroupId("com.alipay.sofa").setArtifactId("ark-mock-pluginA").setVersion("1.0.0");
    final Plugin pluginB = new PluginModel().setPluginName("pluginB").setImportClasses("com.google.common.io.AppendableWriter").setImportPackages(" com.google.common.cache, com.google.common.base ").setImportResources(" import_resource1").setExportPackages("com.alipay.sofa.*").setExportResources(" export_resource1, export_resource2").setPluginActivator("com.alipay.sofa.pluginA.Activator").setClassPath(urls).setGroupId("com.alipay.sofa").setArtifactId("ark-mock-pluginB").setVersion("1.0.0");
    final Set<String> set = new HashSet<>();
    set.add("pluginA");
    set.add("pluginB");
    new Expectations() {

        {
            pluginManagerService.getAllPluginNames();
            result = set;
            minTimes = 0;
            pluginManagerService.getPluginByName("pluginA");
            result = pluginA;
            minTimes = 0;
            pluginManagerService.getPluginByName("pluginB");
            result = pluginB;
            minTimes = 0;
        }
    };
    PluginCommandProvider pluginCommandProvider = new PluginCommandProvider();
    try {
        Field field = PluginCommandProvider.class.getDeclaredField("pluginManagerService");
        field.setAccessible(true);
        field.set(pluginCommandProvider, pluginManagerService);
    } catch (Throwable throwable) {
    // ignore
    }
    String errorMessage = "Error command format. Pls type 'plugin -h' to get help message\n";
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plu")));
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin -h pluginA")));
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin -b pluginA")));
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plu")));
    Assert.assertTrue(pluginCommandProvider.getHelp().equals(pluginCommandProvider.handleCommand("plugin -h")));
    Assert.assertTrue(errorMessage.equals(pluginCommandProvider.handleCommand("plugin ")));
    String details = pluginCommandProvider.handleCommand("plugin -m -d -s pluginA");
    Assert.assertTrue(details.contains("Activator"));
    Assert.assertTrue(details.contains("GroupId"));
    details = pluginCommandProvider.handleCommand("plugin -d pluginB");
    Assert.assertTrue(details.contains("GroupId"));
    Assert.assertFalse(details.contains("Activator"));
    details = pluginCommandProvider.handleCommand("plugin -m plugin.");
    Assert.assertTrue(details.contains("pluginA"));
    Assert.assertTrue(details.contains("pluginB"));
    details = pluginCommandProvider.handleCommand("plugin -m pluginC");
    Assert.assertTrue(details.contains("no matched plugin candidates."));
}
Also used : Expectations(mockit.Expectations) PluginModel(com.alipay.sofa.ark.container.model.PluginModel) Field(java.lang.reflect.Field) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) Plugin(com.alipay.sofa.ark.spi.model.Plugin) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PluginModel (com.alipay.sofa.ark.container.model.PluginModel)47 Test (org.junit.Test)42 BaseTest (com.alipay.sofa.ark.container.BaseTest)34 ITest (com.alipay.sofa.ark.container.testdata.ITest)25 URL (java.net.URL)16 PluginContextImpl (com.alipay.sofa.ark.container.model.PluginContextImpl)9 HashSet (java.util.HashSet)9 BizModel (com.alipay.sofa.ark.container.model.BizModel)8 PluginClassLoader (com.alipay.sofa.ark.container.service.classloader.PluginClassLoader)7 Plugin (com.alipay.sofa.ark.spi.model.Plugin)7 TestObjectA (com.alipay.sofa.ark.container.testdata.impl.TestObjectA)5 PluginManagerService (com.alipay.sofa.ark.spi.service.plugin.PluginManagerService)5 ArkContainerTest (com.alipay.sofa.ark.container.ArkContainerTest)4 HandleArchiveStage (com.alipay.sofa.ark.container.pipeline.HandleArchiveStage)4 URLClassLoader (java.net.URLClassLoader)4 Attributes (java.util.jar.Attributes)4 DefaultServiceFilter (com.alipay.sofa.ark.container.registry.DefaultServiceFilter)3 PluginServiceProvider (com.alipay.sofa.ark.container.registry.PluginServiceProvider)3 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)3 ContainerServiceProvider (com.alipay.sofa.ark.container.registry.ContainerServiceProvider)2