diff options
author | Pavel Aharoni <pa0916@att.com> | 2017-06-11 14:33:57 +0300 |
---|---|---|
committer | Pavel Aharoni <pa0916@att.com> | 2017-06-11 14:33:57 +0300 |
commit | 04387deeab820e75c6d37d2ddd5b514cb7bcfd9e (patch) | |
tree | 2f4dfacba74ec390c4371c62dc96af2b1d5f19ef /jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java | |
parent | fff70fce14ebe7dec3a4d52b7b90cee4414d0562 (diff) |
[SDC-32] separate Tosca Parser from DC
Change-Id: I7e7f31ff2bd92fec22031f75b7051d129a21d01b
Signed-off-by: Pavel Aharoni <pa0916@att.com>
Diffstat (limited to 'jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java')
-rw-r--r-- | jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java | 87 |
1 files changed, 0 insertions, 87 deletions
diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java deleted file mode 100644 index 44feaa3..0000000 --- a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.openecomp.sdc.toscaparser.utils; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.io.MoreFiles; -import com.google.common.io.RecursiveDeleteOption; - -public class JarExtractor { - - private static final Logger LOGGER = LoggerFactory.getLogger(JarExtractor.class); - private static final String PYTHON_DEPENDENCIES_PATH = "Lib/site-packages"; - private static final String TEMP_DIR_PREFIX = "__tosca__"; - - public Path extractPyhtonPackages() throws IOException { - String codePath = getCodePath(); - if (!isRunningInJar(codePath)) { - LOGGER.info("#extractPyhtonPackages - Nothing to extract, we're not running in a jar file."); - return null; - } - - Path tempDirPath = createTempDirectory(); - String tempDirName = tempDirPath.toString(); - extractJarDirectory(codePath, tempDirName); - LOGGER.info("#extractPyhtonPackages - End. Extracted python dependencies to {}", tempDirName); - return tempDirPath; - } - - private Path createTempDirectory() throws IOException { - Path tempDir = Files.createTempDirectory(TEMP_DIR_PREFIX); - LOGGER.debug("#createTempDirectory - tempDir created: {}", tempDir); - return tempDir; - } - - private void extractJarDirectory(String jarDir, String tempDir) throws IOException { - try (JarFile jarFile = new JarFile(jarDir)) { - Enumeration<JarEntry> entries = jarFile.entries(); - while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); - if (shouldExtract(entry)) { - extract(jarFile, entry, tempDir); - } - } - } - } - - private void extract(JarFile jarFile, JarEntry entry, String tempDirName) throws IOException { - try (InputStream source = jarFile.getInputStream(entry)) { - Path targetPath = Paths.get(tempDirName, entry.getName()); - Files.createDirectories(targetPath.getParent()); - Files.copy(source, targetPath); - } - } - - private boolean shouldExtract(JarEntry entry) { - return !entry.isDirectory() && entry.getName() - .startsWith(PYTHON_DEPENDENCIES_PATH); - } - - private String getCodePath() { - String codePath = this.getClass() - .getProtectionDomain() - .getCodeSource() - .getLocation() - .getFile(); - LOGGER.debug("#getCodePath - codePath: {}", codePath); - return codePath; - } - - private boolean isRunningInJar(String path) { - return path.endsWith(".jar"); - } - - public void deleteDirectory(Path path) throws IOException { - MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE); - LOGGER.info("#deleteDirectory - deleted temp directory: {}", path); - } -} |