use of io.cdap.cdap.proto.id.PluginId in project cdap by caskdata.
the class ArtifactRepositoryTest method testPluginMetadata.
@Test
public void testPluginMetadata() throws Exception {
// 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);
PluginId testPlugin1 = new PluginId("default", "myPlugin", "1.0", "TestPlugin", "plugin");
Metadata expected = new Metadata(MetadataScope.SYSTEM, ImmutableSet.of("tag1", "tag2", "tag3"), ImmutableMap.of("k1", "v1", "k2", "v2"));
Assert.assertEquals(expected, metadataAdmin.getMetadata(testPlugin1.toMetadataEntity()));
PluginId testPlugin2 = new PluginId("default", "myPlugin", "1.0", "TestPlugin2", "plugin");
expected = new Metadata(MetadataScope.SYSTEM, ImmutableSet.of("test-tag1", "test-tag2", "test-tag3"), ImmutableMap.of("key1", "val1", "key2", "val2"));
Assert.assertEquals(expected, metadataAdmin.getMetadata(testPlugin2.toMetadataEntity()));
// test metadata is cleaned up when the artifact gets deleted
artifactRepository.deleteArtifact(artifact1Id);
Assert.assertEquals(Metadata.EMPTY, metadataAdmin.getMetadata(testPlugin1.toMetadataEntity()));
Assert.assertEquals(Metadata.EMPTY, metadataAdmin.getMetadata(testPlugin2.toMetadataEntity()));
}
use of io.cdap.cdap.proto.id.PluginId in project cdap by caskdata.
the class DefaultArtifactRepository method deleteArtifact.
@Override
public void deleteArtifact(Id.Artifact artifactId) throws Exception {
ArtifactDetail artifactDetail = artifactStore.getArtifact(artifactId);
io.cdap.cdap.proto.id.ArtifactId artifact = artifactId.toEntityId();
// delete the artifact first and then privileges. Not the other way to avoid orphan artifact
// which does not have any privilege if the artifact delete from store fails. see CDAP-6648
artifactStore.delete(artifactId);
List<MetadataMutation> mutations = new ArrayList<>();
// drop artifact metadata
mutations.add(new MetadataMutation.Drop(artifact.toMetadataEntity()));
Set<PluginClass> plugins = artifactDetail.getMeta().getClasses().getPlugins();
// drop plugin metadata
plugins.forEach(pluginClass -> {
PluginId pluginId = new PluginId(artifact.getNamespace(), artifact.getArtifact(), artifact.getVersion(), pluginClass.getName(), pluginClass.getType());
mutations.add(new MetadataMutation.Drop(pluginId.toMetadataEntity()));
});
metadataServiceClient.batch(mutations);
}
use of io.cdap.cdap.proto.id.PluginId in project cdap by caskdata.
the class DefaultArtifactInspector method inspectPlugins.
/**
* Inspects the plugin file and extracts plugin classes information.
*/
private void inspectPlugins(ArtifactClasses.Builder builder, File artifactFile, io.cdap.cdap.proto.id.ArtifactId artifactId, PluginInstantiator pluginInstantiator, Set<PluginClass> additionalPlugins, List<MetadataMutation> mutations) throws IOException, InvalidArtifactException {
ArtifactId artifact = artifactId.toApiArtifactId();
PluginClassLoader pluginClassLoader = pluginInstantiator.getArtifactClassLoader(artifact);
inspectAdditionalPlugins(artifact, additionalPlugins, pluginClassLoader);
// See if there are export packages. Plugins should be in those packages
Set<String> exportPackages = getExportPackages(artifactFile);
if (exportPackages.isEmpty()) {
return;
}
try {
for (Class<?> cls : getPluginClasses(exportPackages, pluginClassLoader)) {
Plugin pluginAnnotation = cls.getAnnotation(Plugin.class);
if (pluginAnnotation == null) {
continue;
}
Map<String, PluginPropertyField> pluginProperties = Maps.newHashMap();
try {
String configField = getProperties(TypeToken.of(cls), pluginProperties);
String pluginName = getPluginName(cls);
PluginId pluginId = new PluginId(artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), pluginName, pluginAnnotation.type());
MetadataMutation mutation = getMetadataMutation(pluginId, cls);
if (mutation != null) {
mutations.add(mutation);
}
PluginClass pluginClass = PluginClass.builder().setName(pluginName).setType(pluginAnnotation.type()).setCategory(getPluginCategory(cls)).setClassName(cls.getName()).setConfigFieldName(configField).setProperties(pluginProperties).setRequirements(getArtifactRequirements(cls)).setDescription(getPluginDescription(cls)).build();
builder.addPlugin(pluginClass);
} catch (UnsupportedTypeException e) {
LOG.warn("Plugin configuration type not supported. Plugin ignored. {}", cls, e);
}
}
} catch (Throwable t) {
throw new InvalidArtifactException(String.format("Class could not be found while inspecting artifact for plugins. " + "Please check dependencies are available, and that the correct parent artifact was specified. " + "Error class: %s, message: %s.", t.getClass(), t.getMessage()), t);
}
}
use of io.cdap.cdap.proto.id.PluginId in project cdap by caskdata.
the class ProfileMetadataMessageProcessor method addPluginMetadataDelete.
private void addPluginMetadataDelete(ApplicationId appId, ApplicationSpecification appSpec, List<MetadataMutation> deletes) {
LOG.trace("Deleting plugin metadata for {}", appSpec.getName());
String namespace = appId.getNamespace();
String appKey = String.format("%s:%s", namespace, appSpec.getName());
Set<ScopedNameOfKind> pluginSet = Collections.singleton(new ScopedNameOfKind(MetadataKind.PROPERTY, MetadataScope.SYSTEM, appKey));
for (Plugin plugin : appSpec.getPlugins().values()) {
PluginId pluginId = new PluginId(namespace, plugin);
deletes.add(new MetadataMutation.Remove(pluginId.toMetadataEntity(), pluginSet));
}
}
Aggregations