Search in sources :

Example 6 with ArtifactClasses

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

the class ArtifactInspector method inspectArtifact.

/**
   * Inspect the given artifact to determine the classes contained in the artifact.
   *
   * @param artifactId the id of the artifact to inspect
   * @param artifactFile the artifact file
   * @param parentClassLoader the parent classloader to use when inspecting plugins contained in the artifact.
   *                          For example, a ProgramClassLoader created from the artifact the input artifact extends
   * @return metadata about the classes contained in the artifact
   * @throws IOException if there was an exception opening the jar file
   * @throws InvalidArtifactException if the artifact is invalid. For example, if the application main class is not
   *                                  actually an Application.
   */
ArtifactClasses inspectArtifact(Id.Artifact artifactId, File artifactFile, @Nullable ClassLoader parentClassLoader) throws IOException, InvalidArtifactException {
    Path tmpDir = Paths.get(cConf.get(Constants.CFG_LOCAL_DATA_DIR), cConf.get(Constants.AppFabric.TEMP_DIR)).toAbsolutePath();
    Files.createDirectories(tmpDir);
    Location artifactLocation = Locations.toLocation(artifactFile);
    Path stageDir = Files.createTempDirectory(tmpDir, artifactFile.getName());
    try {
        File unpackedDir = BundleJarUtil.unJar(artifactLocation, Files.createTempDirectory(stageDir, "unpacked-").toFile());
        try (CloseableClassLoader artifactClassLoader = artifactClassLoaderFactory.createClassLoader(unpackedDir)) {
            ArtifactClasses.Builder builder = inspectApplications(artifactId, ArtifactClasses.builder(), artifactLocation, artifactClassLoader);
            try (PluginInstantiator pluginInstantiator = new PluginInstantiator(cConf, parentClassLoader == null ? artifactClassLoader : parentClassLoader, Files.createTempDirectory(stageDir, "plugins-").toFile())) {
                pluginInstantiator.addArtifact(artifactLocation, artifactId.toArtifactId());
                inspectPlugins(builder, artifactFile, artifactId.toArtifactId(), pluginInstantiator);
            }
            return builder.build();
        }
    } catch (EOFException | ZipException e) {
        throw new InvalidArtifactException("Artifact " + artifactId + " is not a valid zip file.", e);
    } finally {
        try {
            DirUtils.deleteDirectoryContents(stageDir.toFile());
        } catch (IOException e) {
            LOG.warn("Exception raised while deleting directory {}", stageDir, e);
        }
    }
}
Also used : Path(java.nio.file.Path) ArtifactClasses(co.cask.cdap.api.artifact.ArtifactClasses) EOFException(java.io.EOFException) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) ZipException(java.util.zip.ZipException) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) Location(org.apache.twill.filesystem.Location)

Example 7 with ArtifactClasses

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

the class ArtifactRepository method inspectArtifact.

private ArtifactClasses inspectArtifact(Id.Artifact artifactId, File artifactFile, @Nullable Set<PluginClass> additionalPlugins, @Nullable ClassLoader parentClassLoader) throws IOException, InvalidArtifactException {
    ArtifactClasses artifactClasses = artifactInspector.inspectArtifact(artifactId, artifactFile, parentClassLoader);
    validatePluginSet(artifactClasses.getPlugins());
    if (additionalPlugins == null || additionalPlugins.isEmpty()) {
        return artifactClasses;
    } else {
        return ArtifactClasses.builder().addApps(artifactClasses.getApps()).addPlugins(artifactClasses.getPlugins()).addPlugins(additionalPlugins).build();
    }
}
Also used : ArtifactClasses(co.cask.cdap.api.artifact.ArtifactClasses)

Aggregations

ArtifactClasses (co.cask.cdap.api.artifact.ArtifactClasses)7 PluginClass (co.cask.cdap.api.plugin.PluginClass)4 Location (org.apache.twill.filesystem.Location)4 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)3 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)3 CloseableClassLoader (co.cask.cdap.api.artifact.CloseableClassLoader)3 ArtifactInfo (co.cask.cdap.api.artifact.ArtifactInfo)2 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)2 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)2 EntityImpersonator (co.cask.cdap.security.impersonation.EntityImpersonator)2 File (java.io.File)2 IOException (java.io.IOException)2 Manifest (java.util.jar.Manifest)2 Test (org.junit.Test)2 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)1 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)1 Schema (co.cask.cdap.api.data.schema.Schema)1 MyApp (co.cask.cdap.client.artifact.MyApp)1 Plugin1 (co.cask.cdap.client.artifact.plugin.Plugin1)1 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)1