Search in sources :

Example 1 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class ArtifactRepository method getArtifactsInfo.

/**
   * return list of {@link ArtifactInfo} in the namespace
   * @param namespace
   * @return list of {@link ArtifactInfo}
   * @throws Exception
   */
public List<ArtifactInfo> getArtifactsInfo(NamespaceId namespace) throws Exception {
    final List<ArtifactDetail> artifactDetails = artifactStore.getArtifacts(namespace);
    List<ArtifactInfo> artifactInfoList = Lists.transform(artifactDetails, new Function<ArtifactDetail, ArtifactInfo>() {

        @Nullable
        @Override
        public ArtifactInfo apply(@Nullable ArtifactDetail input) {
            // transform artifactDetail to artifactInfo
            ArtifactId artifactId = input.getDescriptor().getArtifactId();
            return new ArtifactInfo(artifactId.getName(), artifactId.getVersion().getVersion(), artifactId.getScope(), input.getMeta().getClasses(), input.getMeta().getProperties(), input.getMeta().getUsableBy());
        }
    });
    // todo - CDAP-11560 should filter in artifact store
    return Collections.unmodifiableList(filterAuthorizedArtifactInfos(artifactInfoList, namespace));
}
Also used : ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) Nullable(javax.annotation.Nullable)

Example 2 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class ArtifactRepository method addArtifact.

/**
   * Inspects and builds plugin and application information for the given artifact, adding an additional set of
   * plugin classes to the plugins found through inspection. This method is used when all plugin classes
   * cannot be derived by inspecting the artifact but need to be explicitly set. This is true for 3rd party plugins
   * like jdbc drivers.
   *
   * @param artifactId the id of the artifact to inspect and store
   * @param artifactFile the artifact to inspect and store
   * @param parentArtifacts artifacts the given artifact extends.
   *                        If null, the given artifact does not extend another artifact
   * @param additionalPlugins the set of additional plugin classes to add to the plugins found through inspection.
   *                          If null, no additional plugin classes will be added
   * @param properties properties for the artifact
   * @throws IOException if there was an exception reading from the artifact store
   * @throws ArtifactRangeNotFoundException if none of the parent artifacts could be found
   * @throws UnauthorizedException if the user is not authorized to add an artifact in the specified namespace. To add
   *                               an artifact, a user must have {@link Action#WRITE} on the namespace in which
   *                               the artifact is being added. If authorization is successful, and
   *                               the artifact is added successfully, then the user gets all {@link Action privileges}
   *                               on the added artifact.
   */
@VisibleForTesting
public ArtifactDetail addArtifact(final Id.Artifact artifactId, final File artifactFile, @Nullable Set<ArtifactRange> parentArtifacts, @Nullable Set<PluginClass> additionalPlugins, Map<String, String> properties) throws Exception {
    if (additionalPlugins != null) {
        validatePluginSet(additionalPlugins);
    }
    parentArtifacts = parentArtifacts == null ? Collections.<ArtifactRange>emptySet() : parentArtifacts;
    CloseableClassLoader parentClassLoader = null;
    EntityImpersonator entityImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
    if (!parentArtifacts.isEmpty()) {
        validateParentSet(artifactId, parentArtifacts);
        parentClassLoader = createParentClassLoader(artifactId, parentArtifacts, entityImpersonator);
    }
    try {
        ArtifactClasses artifactClasses = inspectArtifact(artifactId, artifactFile, additionalPlugins, parentClassLoader);
        ArtifactMeta meta = new ArtifactMeta(artifactClasses, parentArtifacts, properties);
        ArtifactDetail artifactDetail = artifactStore.write(artifactId, meta, Files.newInputStreamSupplier(artifactFile), entityImpersonator);
        ArtifactDescriptor descriptor = artifactDetail.getDescriptor();
        // info hides some fields that are available in detail, such as the location of the artifact
        ArtifactInfo artifactInfo = new ArtifactInfo(descriptor.getArtifactId(), artifactDetail.getMeta().getClasses(), artifactDetail.getMeta().getProperties());
        // add system metadata for artifacts
        writeSystemMetadata(artifactId.toEntityId(), artifactInfo);
        return artifactDetail;
    } finally {
        Closeables.closeQuietly(parentClassLoader);
    }
}
Also used : ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ArtifactClasses(co.cask.cdap.api.artifact.ArtifactClasses) EntityImpersonator(co.cask.cdap.security.impersonation.EntityImpersonator) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class ServiceArtifactTestRun method testServiceArtifact.

@Test
public void testServiceArtifact() throws Exception {
    ApplicationManager appManager = deployWithArtifact(ServiceArtifactApp.class, artifactJar);
    ServiceManager serviceManager = appManager.getServiceManager("artifact").start();
    URL serviceURL = serviceManager.getServiceURL(30, TimeUnit.SECONDS);
    Assert.assertNotNull(serviceURL);
    URL listURL = serviceURL.toURI().resolve("list").toURL();
    try (Reader reader = new InputStreamReader(listURL.openStream(), StandardCharsets.UTF_8)) {
        List<ArtifactInfo> artifacts = new Gson().fromJson(reader, new TypeToken<List<ArtifactInfo>>() {
        }.getType());
        // It should have the test app, and two plugin artifacts
        Assert.assertEquals(3, artifacts.size());
        Assert.assertTrue(artifacts.stream().anyMatch(info -> info.getName().equals(ServiceArtifactApp.class.getSimpleName())));
        Assert.assertTrue(artifacts.stream().anyMatch(info -> info.getName().equals("dummybase")));
        Assert.assertTrue(artifacts.stream().anyMatch(info -> info.getName().equals("dummy")));
    }
    URL loadURL = serviceURL.toURI().resolve("load?parent=dummybase&plugin=dummy&class=" + DummyPlugin.class.getName()).toURL();
    HttpURLConnection urlConn = (HttpURLConnection) loadURL.openConnection();
    Assert.assertEquals(200, urlConn.getResponseCode());
    try (Reader reader = new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8)) {
        Assert.assertEquals(DummyPlugin.class.getName(), CharStreams.toString(reader));
    }
    serviceManager.stop();
    serviceManager.waitForStatus(false);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) NamespaceId(co.cask.cdap.proto.id.NamespaceId) TypeToken(com.google.gson.reflect.TypeToken) BeforeClass(org.junit.BeforeClass) URL(java.net.URL) JarEntry(java.util.jar.JarEntry) CharStreams(com.google.common.io.CharStreams) Gson(com.google.gson.Gson) ClassRule(org.junit.ClassRule) JarOutputStream(java.util.jar.JarOutputStream) Constants(co.cask.cdap.common.conf.Constants) TestFrameworkTestBase(co.cask.cdap.test.base.TestFrameworkTestBase) Service(co.cask.cdap.api.service.Service) ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ApplicationManager(co.cask.cdap.test.ApplicationManager) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) IOException(java.io.IOException) Reader(java.io.Reader) TestConfiguration(co.cask.cdap.test.TestConfiguration) InputStreamReader(java.io.InputStreamReader) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ByteStreams(com.google.common.io.ByteStreams) ArtifactManager(co.cask.cdap.api.artifact.ArtifactManager) ServiceManager(co.cask.cdap.test.ServiceManager) Assert(org.junit.Assert) ApplicationManager(co.cask.cdap.test.ApplicationManager) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ServiceManager(co.cask.cdap.test.ServiceManager) TypeToken(com.google.gson.reflect.TypeToken) Test(org.junit.Test)

Example 4 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class LocalArtifactManager method listArtifacts.

@Override
public List<ArtifactInfo> listArtifacts() throws IOException {
    return Retries.callWithRetries(() -> {
        try {
            List<ArtifactInfo> result = new ArrayList<>(artifactRepository.getArtifactsInfo(namespaceId));
            result.addAll(artifactRepository.getArtifactsInfo(NamespaceId.SYSTEM));
            return result;
        } catch (IOException | RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new IOException(e);
        }
    }, retryStrategy);
}
Also used : ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException)

Example 5 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class ExistingEntitySystemMetadataWriter method writeSystemMetadataForArtifacts.

private void writeSystemMetadataForArtifacts(NamespaceId namespace) throws IOException {
    for (ArtifactDetail artifactDetail : artifactStore.getArtifacts(namespace)) {
        co.cask.cdap.api.artifact.ArtifactId artifact = artifactDetail.getDescriptor().getArtifactId();
        ArtifactInfo artifactInfo = new ArtifactInfo(artifact, artifactDetail.getMeta().getClasses(), artifactDetail.getMeta().getProperties());
        ArtifactId artifactId = namespace.artifact(artifact.getName(), artifact.getVersion().getVersion());
        SystemMetadataWriter writer = new ArtifactSystemMetadataWriter(metadataStore, artifactId, artifactInfo);
        writer.write();
    }
}
Also used : ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ArtifactId(co.cask.cdap.proto.id.ArtifactId) DatasetSystemMetadataWriter(co.cask.cdap.data2.metadata.system.DatasetSystemMetadataWriter) ProgramSystemMetadataWriter(co.cask.cdap.data2.metadata.system.ProgramSystemMetadataWriter) ViewSystemMetadataWriter(co.cask.cdap.data2.metadata.system.ViewSystemMetadataWriter) SystemMetadataWriter(co.cask.cdap.data2.metadata.system.SystemMetadataWriter) AppSystemMetadataWriter(co.cask.cdap.data2.metadata.system.AppSystemMetadataWriter) ArtifactSystemMetadataWriter(co.cask.cdap.data2.metadata.system.ArtifactSystemMetadataWriter) StreamSystemMetadataWriter(co.cask.cdap.data2.metadata.system.StreamSystemMetadataWriter) ArtifactSystemMetadataWriter(co.cask.cdap.data2.metadata.system.ArtifactSystemMetadataWriter) ArtifactDetail(co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail)

Aggregations

ArtifactInfo (co.cask.cdap.api.artifact.ArtifactInfo)14 ArtifactId (co.cask.cdap.proto.id.ArtifactId)8 IOException (java.io.IOException)5 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)4 Test (org.junit.Test)4 ArtifactClasses (co.cask.cdap.api.artifact.ArtifactClasses)3 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)3 File (java.io.File)3 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)2 ArtifactScope (co.cask.cdap.api.artifact.ArtifactScope)2 CloseableClassLoader (co.cask.cdap.api.artifact.CloseableClassLoader)2 Schema (co.cask.cdap.api.data.schema.Schema)2 Table (co.cask.cdap.cli.util.table.Table)2 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)2 ArtifactDetail (co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 EntityImpersonator (co.cask.cdap.security.impersonation.EntityImpersonator)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 ConfigTestApp (co.cask.cdap.ConfigTestApp)1