Search in sources :

Example 1 with PluginId

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()));
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Metadata(io.cdap.cdap.spi.metadata.Metadata) 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) Manifest(java.util.jar.Manifest) PluginId(io.cdap.cdap.proto.id.PluginId) File(java.io.File) Test(org.junit.Test)

Example 2 with PluginId

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);
}
Also used : MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) ArrayList(java.util.ArrayList) PluginClass(io.cdap.cdap.api.plugin.PluginClass) PluginId(io.cdap.cdap.proto.id.PluginId)

Example 3 with PluginId

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);
    }
}
Also used : ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginId(io.cdap.cdap.proto.id.PluginId) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) UnsupportedTypeException(io.cdap.cdap.api.data.schema.UnsupportedTypeException) PluginClass(io.cdap.cdap.api.plugin.PluginClass) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) PluginClassLoader(io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader) Plugin(io.cdap.cdap.api.annotation.Plugin)

Example 4 with PluginId

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));
    }
}
Also used : MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) ScopedNameOfKind(io.cdap.cdap.spi.metadata.ScopedNameOfKind) PluginId(io.cdap.cdap.proto.id.PluginId) Plugin(io.cdap.cdap.api.plugin.Plugin)

Aggregations

PluginId (io.cdap.cdap.proto.id.PluginId)4 MetadataMutation (io.cdap.cdap.spi.metadata.MetadataMutation)3 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)2 PluginClass (io.cdap.cdap.api.plugin.PluginClass)2 Plugin (io.cdap.cdap.api.annotation.Plugin)1 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)1 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)1 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)1 Plugin (io.cdap.cdap.api.plugin.Plugin)1 PluginPropertyField (io.cdap.cdap.api.plugin.PluginPropertyField)1 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)1 Id (io.cdap.cdap.common.id.Id)1 PluginClassLoader (io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 Metadata (io.cdap.cdap.spi.metadata.Metadata)1 ScopedNameOfKind (io.cdap.cdap.spi.metadata.ScopedNameOfKind)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Manifest (java.util.jar.Manifest)1 Test (org.junit.Test)1