Search in sources :

Example 1 with PluginNotExistsException

use of co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException 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<co.cask.cdap.api.artifact.ArtifactId, PluginClass> select(SortedMap<co.cask.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(co.cask.cdap.api.plugin.PluginSelector) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) SortedMap(java.util.SortedMap) PluginClass(co.cask.cdap.api.plugin.PluginClass) File(java.io.File) Map(java.util.Map) SortedMap(java.util.SortedMap) Test(org.junit.Test)

Example 2 with PluginNotExistsException

use of co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException in project cdap by caskdata.

the class RemotePluginFinder method findPlugin.

@Override
public Map.Entry<ArtifactDescriptor, PluginClass> findPlugin(NamespaceId pluginNamespaceId, ArtifactId parentArtifactId, String pluginType, String pluginName, PluginSelector selector) throws PluginNotExistsException {
    try {
        return Retries.callWithRetries(() -> {
            List<PluginInfo> infos = getPlugins(pluginNamespaceId, parentArtifactId, pluginType, pluginName);
            if (infos.isEmpty()) {
                throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
            }
            SortedMap<co.cask.cdap.api.artifact.ArtifactId, PluginClass> plugins = new TreeMap<>();
            for (PluginInfo info : infos) {
                ArtifactSummary artifactSummary = info.getArtifact();
                co.cask.cdap.api.artifact.ArtifactId pluginArtifactId = new co.cask.cdap.api.artifact.ArtifactId(artifactSummary.getName(), new ArtifactVersion(artifactSummary.getVersion()), artifactSummary.getScope());
                PluginClass pluginClass = new PluginClass(info.getType(), info.getName(), info.getDescription(), info.getClassName(), info.getConfigFieldName(), info.getProperties(), info.getEndpoints());
                plugins.put(pluginArtifactId, pluginClass);
            }
            Map.Entry<co.cask.cdap.api.artifact.ArtifactId, PluginClass> selected = selector.select(plugins);
            if (selected == null) {
                throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
            }
            Location artifactLocation = getArtifactLocation(Artifacts.toArtifactId(pluginNamespaceId, selected.getKey()));
            return Maps.immutableEntry(new ArtifactDescriptor(selected.getKey(), artifactLocation), selected.getValue());
        }, retryStrategy);
    } catch (PluginNotExistsException e) {
        throw e;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) TreeMap(java.util.TreeMap) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) IOException(java.io.IOException) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) PluginClass(co.cask.cdap.api.plugin.PluginClass) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Location(org.apache.twill.filesystem.Location)

Example 3 with PluginNotExistsException

use of co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException in project cdap by caskdata.

the class ArtifactStoreTest method testPluginParentVersions.

// this test tests that when an artifact specifies a range of artifact versions it extends,
// those versions are honored
@Test
public void testPluginParentVersions() throws Exception {
    // write an artifact that extends parent-[1.0.0, 2.0.0)
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.1.0");
    Set<ArtifactRange> parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Set<PluginClass> plugins = ImmutableSet.of(new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
    ArtifactMeta meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
    writeArtifact(artifactId, meta, "some contents");
    ArtifactDescriptor artifactInfo = artifactStore.getArtifact(artifactId).getDescriptor();
    // check ids that are out of range. They should not return anything
    List<Id.Artifact> badIds = Lists.newArrayList(// ids that are too low
    Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "0.9.9"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0-SNAPSHOT"), // ids that are too high
    Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.0"));
    ArtifactMeta emptyMeta = new ArtifactMeta(ArtifactClasses.builder().build());
    for (Id.Artifact badId : badIds) {
        // write the parent artifact to make sure we don't get ArtifactNotFound exceptions with later calls
        // we're testing range filtering, not the absence of the parent artifact
        writeArtifact(badId, emptyMeta, "content");
        Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId).isEmpty());
        Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId, "atype").isEmpty());
        try {
            artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId, "atype", "plugin1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
            Assert.fail();
        } catch (PluginNotExistsException e) {
        // expected
        }
    }
    // check ids that are in range return what we expect
    List<Id.Artifact> goodIds = Lists.newArrayList(// ids that are too low
    Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0"), // ids that are too high
    Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.9.9"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.99.999"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.0-SNAPSHOT"));
    Map<ArtifactDescriptor, Set<PluginClass>> expectedPluginsMapList = ImmutableMap.of(artifactInfo, plugins);
    Map<ArtifactDescriptor, PluginClass> expectedPluginsMap = ImmutableMap.of(artifactInfo, plugins.iterator().next());
    for (Id.Artifact goodId : goodIds) {
        // make sure parent actually exists
        writeArtifact(goodId, emptyMeta, "content");
        Assert.assertEquals(expectedPluginsMapList, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId));
        Assert.assertEquals(expectedPluginsMapList, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId, "atype"));
        Assert.assertEquals(expectedPluginsMap, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId, "atype", "plugin1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED));
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactId(co.cask.cdap.proto.id.ArtifactId) Id(co.cask.cdap.common.id.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 4 with PluginNotExistsException

use of co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException in project cdap by caskdata.

the class DefaultPluginConfigurer method usePluginClass.

@Nullable
@Override
public <T> Class<T> usePluginClass(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) {
    Plugin plugin;
    try {
        plugin = findPlugin(pluginType, pluginName, pluginId, properties, selector);
    } catch (PluginNotExistsException e) {
        // Plugin not found, hence return null
        return null;
    } catch (ArtifactNotFoundException e) {
        // this shouldn't happen, it means the artifact for this app does not exist.
        throw new IllegalStateException(String.format("Application artifact '%s' no longer exists. Please check if it was deleted.", artifactId));
    }
    try {
        Class<T> cls = pluginInstantiator.loadClass(plugin);
        plugins.put(pluginId, plugin);
        return cls;
    } catch (IOException e) {
        // If the plugin jar is deleted without notifying the artifact service.
        return null;
    } catch (ClassNotFoundException e) {
        // Shouldn't happen
        throw Throwables.propagate(e);
    }
}
Also used : PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) IOException(java.io.IOException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) Plugin(co.cask.cdap.api.plugin.Plugin) Nullable(javax.annotation.Nullable)

Example 5 with PluginNotExistsException

use of co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException in project cdap by caskdata.

the class DefaultPluginConfigurer method usePlugin.

@Nullable
@Override
public <T> T usePlugin(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) {
    Plugin plugin;
    try {
        plugin = findPlugin(pluginType, pluginName, pluginId, properties, selector);
    } catch (PluginNotExistsException e) {
        // Plugin not found, hence return null
        return null;
    } catch (ArtifactNotFoundException e) {
        // this shouldn't happen, it means the artifact for this app does not exist.
        throw new IllegalStateException(String.format("Application artifact '%s' no longer exists. Please check if it was deleted.", artifactId));
    }
    try {
        T instance = pluginInstantiator.newInstance(plugin);
        plugins.put(pluginId, plugin);
        return instance;
    } catch (IOException e) {
        // If the plugin jar is deleted without notifying the artifact service.
        return null;
    } catch (ClassNotFoundException e) {
        // Shouldn't happen
        throw Throwables.propagate(e);
    }
}
Also used : PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) IOException(java.io.IOException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) Plugin(co.cask.cdap.api.plugin.Plugin) Nullable(javax.annotation.Nullable)

Aggregations

PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)11 PluginClass (co.cask.cdap.api.plugin.PluginClass)8 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)6 ArtifactId (co.cask.cdap.proto.id.ArtifactId)6 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 Map (java.util.Map)5 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)4 IOException (java.io.IOException)4 SortedMap (java.util.SortedMap)4 Test (org.junit.Test)4 Plugin (co.cask.cdap.api.plugin.Plugin)3 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)3 Id (co.cask.cdap.common.id.Id)3 HashMap (java.util.HashMap)3 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)2 PluginSelector (co.cask.cdap.api.plugin.PluginSelector)2 ArtifactDescriptor (co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor)2 PluginInfo (co.cask.cdap.proto.artifact.PluginInfo)2 File (java.io.File)2 Nullable (javax.annotation.Nullable)2