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