
io.sealights.agents.plugin.upgrade.utils.JarUtils Maven / Gradle / Ivy
package io.sealights.agents.plugin.upgrade.utils;
import io.sealights.agents.plugin.Utils.StringUtils;
import java.io.*;
import java.util.UUID;
import java.util.jar.JarFile;
/**
* Created by shahar on 8/1/2016.
*/
public class JarUtils {
public static JarFile loadExternalJar(File agent, String jarNameWithoutExtension,
UserMessageHelper userMessageHelper, String storageFolderOrFile,
boolean isOverridePath) {
try {
InputStream jarStream = new FileInputStream(agent);
return loadJar(jarStream, jarNameWithoutExtension, userMessageHelper, storageFolderOrFile, isOverridePath);
} catch (Exception e) {
throw new RuntimeException("Failed to load external jar. jarNameWithoutExtension:'" + jarNameWithoutExtension + "'. Error:", e);
}
}
/**
* This method get input stream of a jar and return JarFile instance of a copy of that jar.
*
* @param jarStream the agent input stream from which we should copy.
* @param jarNameWithoutExtension
* @param userMessageHelper
* @param storageFolderOrFile path to the sealights storage folder or an override path for the temp test listener.
* @param isOverridePath is parameter 'storageFolderOrFile' an override path.
* @return JarFile instance of the created temp file.
*/
private static JarFile loadJar(InputStream jarStream, String jarNameWithoutExtension,
UserMessageHelper userMessageHelper, String storageFolderOrFile, boolean isOverridePath) {
try {
File file = createFileForInputStream(jarNameWithoutExtension, storageFolderOrFile, isOverridePath);
userMessageHelper.println("Created tmp file: " + file.getAbsolutePath());
file.deleteOnExit();
copyInputStreamToFile(jarStream, file);
return new JarFile(file);
} catch (Exception e) {
throw new RuntimeException("Failed to load jar. jarNameWithoutExtension:'" + jarNameWithoutExtension + "'. Error:", e);
}
}
private static File createFileForInputStream(
String jarNameWithoutExtension, String storageFolderOrFile, boolean isOverridePath) throws IOException {
File file;
if (isOverridePath) {
file = new File(storageFolderOrFile);
if (FileAndFolderUtils.verifyFileExistsSafe(file))
return file;
} else if (!StringUtils.isNullOrEmpty(storageFolderOrFile)) {
// If files storage specified - create a temp file inside it instead of OS tmp folder.
String tempFileName = jarNameWithoutExtension + "_" + UUID.randomUUID() + ".jar";
file = new File(storageFolderOrFile, tempFileName);
if (FileAndFolderUtils.verifyFileExistsSafe(file))
return file;
}
// Files storage is not specified or problem occurred - create a temp file in OS default tmp location.
return File.createTempFile(jarNameWithoutExtension, ".jar");
}
private static void copyInputStreamToFile(InputStream inputStream, File targetFile) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
} catch (Exception e) {
// TODO: should pass logger
e.printStackTrace();
} finally {
closeSafe(outputStream);
closeSafe(inputStream);
}
}
private static void closeSafe(Closeable closeable){
try {
if (closeable != null) {
closeable.close();
}
}catch (Exception e){
// TODO: should pass logger
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy