Search in sources :

Example 1 with PluginSelector

use of io.cdap.cdap.api.plugin.PluginSelector in project cdap by caskdata.

the class ArtifactRepositoryTest method testPluginSelector.

@Test
public void testPluginSelector() throws Exception {
    // No plugin yet
    ArtifactRange appArtifactRange = new ArtifactRange(APP_ARTIFACT_ID.getNamespace().getId(), APP_ARTIFACT_ID.getName(), APP_ARTIFACT_ID.getVersion(), true, APP_ARTIFACT_ID.getVersion(), true);
    try {
        artifactRepository.findPlugin(NamespaceId.DEFAULT, appArtifactRange, "plugin", "TestPlugin2", new PluginSelector());
        Assert.fail();
    } catch (PluginNotExistsException e) {
    // expected
    }
    File pluginDir = DirUtils.createTempDir(tmpDir);
    // Create a plugin jar. It contains two plugins, TestPlugin and TestPlugin2 inside.
    Id.Artifact artifact1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "myPlugin", "1.0");
    Manifest manifest = createManifest(ManifestFields.EXPORT_PACKAGE, TestPlugin.class.getPackage().getName());
    File jarFile = createPluginJar(TestPlugin.class, new File(tmpDir, "myPlugin-1.0.jar"), manifest);
    // Build up the plugin repository.
    Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(APP_ARTIFACT_ID.getNamespace().getId(), APP_ARTIFACT_ID.getName(), new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    artifactRepository.addArtifact(artifact1Id, jarFile, parents, null);
    // Should get the only version.
    Map.Entry<ArtifactDescriptor, PluginClass> plugin = artifactRepository.findPlugin(NamespaceId.DEFAULT, appArtifactRange, "plugin", "TestPlugin2", new PluginSelector());
    Assert.assertNotNull(plugin);
    Assert.assertEquals(new ArtifactVersion("1.0"), plugin.getKey().getArtifactId().getVersion());
    Assert.assertEquals("TestPlugin2", plugin.getValue().getName());
    Locations.linkOrCopyOverwrite(plugin.getKey().getLocation(), new File(pluginDir, Artifacts.getFileName(plugin.getKey().getArtifactId())));
    // Create another plugin jar with later version and update the repository
    Id.Artifact artifact2Id = Id.Artifact.from(Id.Namespace.DEFAULT, "myPlugin", "2.0");
    jarFile = createPluginJar(TestPlugin.class, new File(tmpDir, "myPlugin-2.0.jar"), manifest);
    artifactRepository.addArtifact(artifact2Id, jarFile, parents, null);
    // Should select the latest version
    plugin = artifactRepository.findPlugin(NamespaceId.DEFAULT, appArtifactRange, "plugin", "TestPlugin2", new PluginSelector());
    Assert.assertNotNull(plugin);
    Assert.assertEquals(new ArtifactVersion("2.0"), plugin.getKey().getArtifactId().getVersion());
    Assert.assertEquals("TestPlugin2", plugin.getValue().getName());
    Locations.linkOrCopyOverwrite(plugin.getKey().getLocation(), new File(pluginDir, Artifacts.getFileName(plugin.getKey().getArtifactId())));
    // Load the Plugin class from the classLoader.
    try (PluginInstantiator instantiator = new PluginInstantiator(cConf, appClassLoader, pluginDir)) {
        ClassLoader pluginClassLoader = instantiator.getArtifactClassLoader(plugin.getKey().getArtifactId());
        Class<?> pluginClass = pluginClassLoader.loadClass(TestPlugin2.class.getName());
        // Use a custom plugin selector to select with smallest version
        plugin = artifactRepository.findPlugin(NamespaceId.DEFAULT, appArtifactRange, "plugin", "TestPlugin2", new PluginSelector() {

            @Override
            public Map.Entry<ArtifactId, PluginClass> select(SortedMap<ArtifactId, PluginClass> plugins) {
                return plugins.entrySet().iterator().next();
            }
        });
        Assert.assertNotNull(plugin);
        Assert.assertEquals(new ArtifactVersion("1.0"), plugin.getKey().getArtifactId().getVersion());
        Assert.assertEquals("TestPlugin2", plugin.getValue().getName());
        // Load the Plugin class again from the current plugin selected
        // The plugin class should be different (from different ClassLoader)
        // The empty class should be the same (from the plugin lib ClassLoader)
        pluginClassLoader = instantiator.getArtifactClassLoader(plugin.getKey().getArtifactId());
        Assert.assertNotSame(pluginClass, pluginClassLoader.loadClass(TestPlugin2.class.getName()));
        // From the pluginClassLoader, loading export classes from the template jar should be allowed
        Class<?> cls = pluginClassLoader.loadClass(PluginTestRunnable.class.getName());
        // The class should be loaded from the parent artifact's classloader
        Assert.assertSame(appClassLoader.loadClass(PluginTestRunnable.class.getName()), cls);
        // From the plugin classloader, all cdap api classes is loadable as well.
        cls = pluginClassLoader.loadClass(Application.class.getName());
        // The Application class should be the same as the one in the system classloader
        Assert.assertSame(Application.class, cls);
    }
}
Also used : PluginSelector(io.cdap.cdap.api.plugin.PluginSelector) TestPlugin2(io.cdap.cdap.internal.app.plugins.test.TestPlugin2) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginTestRunnable(io.cdap.cdap.internal.app.runtime.artifact.app.plugin.PluginTestRunnable) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Manifest(java.util.jar.Manifest) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) SortedMap(java.util.SortedMap) ProgramClassLoader(io.cdap.cdap.internal.app.runtime.ProgramClassLoader) FilterClassLoader(io.cdap.cdap.common.lang.FilterClassLoader) TestPlugin(io.cdap.cdap.internal.app.plugins.test.TestPlugin) PluginInstantiator(io.cdap.cdap.internal.app.runtime.plugin.PluginInstantiator) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginId(io.cdap.cdap.proto.id.PluginId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with PluginSelector

use of io.cdap.cdap.api.plugin.PluginSelector in project cdap by caskdata.

the class PluginFinderTestBase method testFindPlugin.

@Test
public void testFindPlugin() throws Exception {
    // Deploy some artifacts
    File appArtifact = createAppJar(PluginTestApp.class);
    File pluginArtifact = createPluginJar(Plugin1.class);
    ArtifactId appArtifactId = NamespaceId.DEFAULT.artifact("app", "1.0");
    artifactRepo.addArtifact(Id.Artifact.fromEntityId(appArtifactId), appArtifact);
    ArtifactId pluginArtifactId = NamespaceId.DEFAULT.artifact("plugin", "1.0");
    artifactRepo.addArtifact(Id.Artifact.fromEntityId(pluginArtifactId), pluginArtifact, Collections.singleton(new ArtifactRange(appArtifactId.getNamespace(), appArtifactId.getArtifact(), new ArtifactVersion(appArtifactId.getVersion()), true, new ArtifactVersion(appArtifactId.getVersion()), true)), null);
    // Find the plugin
    PluginFinder finder = getPluginFinder();
    Map.Entry<ArtifactDescriptor, PluginClass> entry = finder.findPlugin(NamespaceId.DEFAULT, appArtifactId, "dummy", "Plugin1", new PluginSelector());
    Assert.assertNotNull(entry);
    Assert.assertEquals(pluginArtifactId.toApiArtifactId(), entry.getKey().getArtifactId());
    Assert.assertEquals("Plugin1", entry.getValue().getName());
    Assert.assertEquals("dummy", entry.getValue().getType());
    // Looking for a non-existing should throw
    try {
        finder.findPlugin(NamespaceId.DEFAULT, appArtifactId, "dummy2", "pluginx", new PluginSelector());
        Assert.fail("A PluginNotExistsException is expected");
    } catch (PluginNotExistsException e) {
    // expected
    }
    // Deploy the same plugin artifact to the system namespace, without any parent
    ArtifactId systemPluginArtifactId = NamespaceId.SYSTEM.artifact("pluginsystem", "1.0");
    artifactRepo.addArtifact(Id.Artifact.fromEntityId(systemPluginArtifactId), pluginArtifact);
    entry = finder.findPlugin(NamespaceId.DEFAULT, appArtifactId, "dummy", "Plugin1", new PluginSelector());
    // The selector always select the last one, hence the USER once will be selected
    Assert.assertNotNull(entry);
    Assert.assertEquals(pluginArtifactId.toApiArtifactId(), entry.getKey().getArtifactId());
    Assert.assertEquals("Plugin1", entry.getValue().getName());
    Assert.assertEquals("dummy", entry.getValue().getType());
    // Use a different selector that returns the first entry, hence expect the SYSTEM one being returned
    entry = finder.findPlugin(NamespaceId.DEFAULT, appArtifactId, "dummy", "Plugin1", new PluginSelector() {

        @Override
        public Map.Entry<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> select(SortedMap<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> plugins) {
            return plugins.entrySet().iterator().next();
        }
    });
    Assert.assertNotNull(entry);
    Assert.assertEquals(systemPluginArtifactId.toApiArtifactId(), entry.getKey().getArtifactId());
    Assert.assertEquals("Plugin1", entry.getValue().getName());
    Assert.assertEquals("dummy", entry.getValue().getType());
}
Also used : PluginSelector(io.cdap.cdap.api.plugin.PluginSelector) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) SortedMap(java.util.SortedMap) PluginClass(io.cdap.cdap.api.plugin.PluginClass) File(java.io.File) Map(java.util.Map) SortedMap(java.util.SortedMap) Test(org.junit.Test)

Example 3 with PluginSelector

use of io.cdap.cdap.api.plugin.PluginSelector in project cdap by caskdata.

the class ValidationUtilsTest method getPluginConfigurer.

// Mock PluginConfigurer
private PluginConfigurer getPluginConfigurer(PluginClass pluginClass) {
    return new PluginConfigurer() {

        @Override
        public <T> T usePlugin(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) {
            String tableName = properties.getProperties().get("tableName");
            if (tableName == null || tableName.isEmpty()) {
                throw new InvalidPluginConfigException(pluginClass, Collections.singleton("tableName"), new HashSet<>());
            }
            MockSource.Config config = new MockSource.Config();
            MockSource.ConnectionConfig connectionConfig = new MockSource.ConnectionConfig();
            String schema = properties.getProperties().get("schema");
            String sleep = properties.getProperties().get("sleepInMillis");
            connectionConfig.setTableName(tableName);
            config.setConfig(connectionConfig, schema, null, sleep == null ? null : Long.parseLong(sleep));
            return (T) new MockSource(config);
        }

        @Override
        public <T> Class<T> usePluginClass(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) {
            return null;
        }

        @Override
        public Map<String, String> evaluateMacros(Map<String, String> properties, MacroEvaluator evaluator, MacroParserOptions options) throws InvalidMacroException {
            return null;
        }
    };
}
Also used : MockSource(io.cdap.cdap.etl.mock.batch.MockSource) PluginSelector(io.cdap.cdap.api.plugin.PluginSelector) MacroEvaluator(io.cdap.cdap.api.macro.MacroEvaluator) InvalidPluginConfigException(io.cdap.cdap.api.plugin.InvalidPluginConfigException) MacroParserOptions(io.cdap.cdap.api.macro.MacroParserOptions) PluginConfigurer(io.cdap.cdap.api.plugin.PluginConfigurer) PluginProperties(io.cdap.cdap.api.plugin.PluginProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PluginSelector (io.cdap.cdap.api.plugin.PluginSelector)3 Map (java.util.Map)3 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)2 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)2 PluginClass (io.cdap.cdap.api.plugin.PluginClass)2 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)2 File (java.io.File)2 HashMap (java.util.HashMap)2 SortedMap (java.util.SortedMap)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)1 MacroEvaluator (io.cdap.cdap.api.macro.MacroEvaluator)1 MacroParserOptions (io.cdap.cdap.api.macro.MacroParserOptions)1 InvalidPluginConfigException (io.cdap.cdap.api.plugin.InvalidPluginConfigException)1 PluginConfigurer (io.cdap.cdap.api.plugin.PluginConfigurer)1 PluginProperties (io.cdap.cdap.api.plugin.PluginProperties)1 Id (io.cdap.cdap.common.id.Id)1 FilterClassLoader (io.cdap.cdap.common.lang.FilterClassLoader)1 MockSource (io.cdap.cdap.etl.mock.batch.MockSource)1