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());
}
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);
}
}
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));
}
}
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);
}
}
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);
}
}
Aggregations