Search in sources :

Example 31 with PluginModel

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

the class PluginClassloaderTest method testExportAndImport.

@Test
public void testExportAndImport() throws Exception {
    PluginModel pluginA = new PluginModel();
    pluginA.setPluginName("plugin A").setClassPath(new URL[] { classPathURL }).setImportClasses(Collections.<String>emptySet()).setImportPackages(Collections.<String>emptySet()).setExportIndex(new HashSet<>(Collections.singletonList(ITest.class.getName()))).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
    PluginModel pluginB = new PluginModel();
    pluginB.setPluginName("plugin B").setPriority(1).setClassPath(new URL[] { classPathURL }).setImportClasses(new HashSet<>(Collections.singletonList(ITest.class.getName()))).setImportPackages(Collections.<String>emptySet()).setExportIndex(Collections.<String>emptySet()).setPluginClassLoader(new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath()));
    pluginManagerService.registerPlugin(pluginA);
    pluginManagerService.registerPlugin(pluginB);
    classloaderService.prepareExportClassCache();
    pluginDeployService.deploy();
    Assert.assertEquals(pluginA.getPluginClassLoader().loadClass(ITest.class.getName()), pluginB.getPluginClassLoader().loadClass(ITest.class.getName()));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) HashSet(java.util.HashSet) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest)

Example 32 with PluginModel

use of com.alipay.sofa.ark.container.model.PluginModel 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());
}
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) PluginActivatorA(com.alipay.sofa.ark.container.testdata.Activator.PluginActivatorA) PluginActivatorC(com.alipay.sofa.ark.container.testdata.Activator.PluginActivatorC) PluginActivatorB(com.alipay.sofa.ark.container.testdata.Activator.PluginActivatorB) 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 33 with PluginModel

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

the class PluginClassLoaderTest method testExportAndNotImport.

@Test
public void testExportAndNotImport() throws Exception {
    PluginModel pluginA = new PluginModel();
    pluginA.setPluginName("plugin A").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("com.alipay.sofa.ark").setExportClasses("").setExportResources(StringUtils.EMPTY_STRING).setImportResources(StringUtils.EMPTY_STRING).setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
    PluginModel pluginB = new PluginModel();
    pluginB.setPluginName("plugin B").setPriority("1").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setExportResources(StringUtils.EMPTY_STRING).setImportResources(StringUtils.EMPTY_STRING).setPluginClassLoader(new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath()));
    pluginManagerService.registerPlugin(pluginA);
    pluginManagerService.registerPlugin(pluginB);
    classloaderService.prepareExportClassAndResourceCache();
    pluginDeployService.deploy();
    Assert.assertNotEquals(pluginA.getPluginClassLoader().loadClass(ITest.class.getName()), pluginB.getPluginClassLoader().loadClass(ITest.class.getName()));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) URL(java.net.URL) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest)

Example 34 with PluginModel

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

the class PluginClassLoaderTest method testExportResource.

@Test
public void testExportResource() {
    PluginModel pluginA = new PluginModel();
    pluginA.setPluginName("pluginA").setClassPath(new URL[] { classPathURL }).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages(ClassUtils.getPackageName(ITest.class.getCanonicalName())).setExportClasses("").setImportResources(StringUtils.EMPTY_STRING).setExportResources("pluginA_export_resource1.xml,pluginA_export_resource2.xml").setPluginClassLoader(new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));
    PluginModel pluginB = new PluginModel();
    pluginB.setPluginName("pluginB").setPriority("1").setClassPath(new URL[0]).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages("").setExportClasses("").setImportResources("pluginA_export_resource1.xml").setExportResources(StringUtils.EMPTY_STRING).setPluginClassLoader(new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath()));
    pluginManagerService.registerPlugin(pluginA);
    pluginManagerService.registerPlugin(pluginB);
    classloaderService.prepareExportClassAndResourceCache();
    pluginDeployService.deploy();
    Assert.assertNotNull(pluginB.getPluginClassLoader().getResource("pluginA_export_resource1.xml"));
    Assert.assertNull(pluginB.getPluginClassLoader().getResource("pluginA_export_resource2.xml"));
    Assert.assertNull(pluginB.getPluginClassLoader().getResource("pluginA_not_export_resource.xml"));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) URL(java.net.URL) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest)

Example 35 with PluginModel

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

the class PluginClassLoaderTest method testGetJdkResource.

@Test
public void testGetJdkResource() throws IOException {
    PluginModel mockPlugin = new PluginModel();
    mockPlugin.setPluginName("Mock plugin").setClassPath(new URL[] {}).setImportResources(StringUtils.EMPTY_STRING).setImportClasses(StringUtils.EMPTY_STRING).setImportPackages(StringUtils.EMPTY_STRING).setExportPackages(ITest.class.getCanonicalName()).setPluginClassLoader(new PluginClassLoader(mockPlugin.getPluginName(), mockPlugin.getClassPath()));
    pluginManagerService.registerPlugin(mockPlugin);
    ClassLoader cl = mockPlugin.getPluginClassLoader();
    String name = "META-INF/services/javax.script.ScriptEngineFactory";
    URL res1 = cl.getResource(name);
    Assert.assertNotNull(res1);
    URL res2 = ClassLoader.getSystemClassLoader().getResource(name);
    Assert.assertEquals(res2, res1);
    Enumeration<URL> enu1 = cl.getResources(name);
    Assert.assertTrue(enu1.hasMoreElements());
    Enumeration<URL> enu2 = ClassLoader.getSystemClassLoader().getResources(name);
    Assert.assertEquals(Sets.newHashSet(Collections.list(enu2)), Sets.newHashSet(Collections.list(enu1)));
}
Also used : PluginModel(com.alipay.sofa.ark.container.model.PluginModel) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) Test(org.junit.Test) BaseTest(com.alipay.sofa.ark.container.BaseTest) ITest(com.alipay.sofa.ark.container.testdata.ITest)

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