@Test public void testExtractContainedLibraries() throws Exception { String s = "testExtractContainedLibraries"; byte[] nestedJarContent = s.getBytes(ConfigConstants.DEFAULT_CHARSET); File fakeJar = temporaryFolder.newFile("test.jar"); try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fakeJar))) { ZipEntry entry = new ZipEntry("lib/internalTest.jar"); zos.putNextEntry(entry); zos.write(nestedJarContent); zos.closeEntry(); } final List<File> files = PackagedProgram.extractContainedLibraries(fakeJar.toURI().toURL()); Assert.assertEquals(1, files.size()); Assert.assertArrayEquals(nestedJarContent, Files.readAllBytes(files.iterator().next().toPath())); }
public static List<File> extractContainedLibraries(URL jarFile) throws ProgramInvocationException { Random rnd = new Random(); JarFile jar = null; try { jar = new JarFile(new File(jarFile.toURI())); final List<JarEntry> containedJarFileEntries = new ArrayList<JarEntry>(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (name.length() > 8 && name.startsWith("lib/") && name.endsWith(".jar")) { containedJarFileEntries.add(entry); } } if (containedJarFileEntries.isEmpty()) { return Collections.emptyList(); } else { final List<File> extractedTempLibraries = new ArrayList<File>(containedJarFileEntries.size()); final byte[] buffer = new byte[4096]; boolean incomplete = true; try { for (int i = 0; i < containedJarFileEntries.size(); i++) { final JarEntry entry = containedJarFileEntries.get(i); String name = entry.getName(); name = name.replace('/', '_'); File tempFile; try { tempFile = File.createTempFile(rnd.nextInt(Integer.MAX_VALUE) + "_", name); tempFile.deleteOnExit(); } catch (IOException e) { throw new ProgramInvocationException( "An I/O error occurred while creating temporary file to extract nested library '" + entry.getName() + "'.", e); } extractedTempLibraries.add(tempFile); OutputStream out = null; InputStream in = null; try { out = new FileOutputStream(tempFile); in = new BufferedInputStream(jar.getInputStream(entry)); int numRead = 0; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); } } catch (IOException e) { throw new ProgramInvocationException("An I/O error occurred while extracting nested library '" + entry.getName() + "' to temporary file '" + tempFile.getAbsolutePath() + "'."); } finally { if (out != null) { out.close(); } if (in != null) { in.close(); } } } incomplete = false; } finally { if (incomplete) { deleteExtractedLibraries(extractedTempLibraries); } } return extractedTempLibraries; } } catch (Throwable t) { throw new ProgramInvocationException("Unknown I/O error while extracting contained jar files.", t); } finally { if (jar != null) { try { jar.close(); } catch (Throwable t) {} } } }
PackagedProgram { public static List<File> extractContainedLibraries(URL jarFile) throws ProgramInvocationException { Random rnd = new Random(); JarFile jar = null; try { jar = new JarFile(new File(jarFile.toURI())); final List<JarEntry> containedJarFileEntries = new ArrayList<JarEntry>(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (name.length() > 8 && name.startsWith("lib/") && name.endsWith(".jar")) { containedJarFileEntries.add(entry); } } if (containedJarFileEntries.isEmpty()) { return Collections.emptyList(); } else { final List<File> extractedTempLibraries = new ArrayList<File>(containedJarFileEntries.size()); final byte[] buffer = new byte[4096]; boolean incomplete = true; try { for (int i = 0; i < containedJarFileEntries.size(); i++) { final JarEntry entry = containedJarFileEntries.get(i); String name = entry.getName(); name = name.replace('/', '_'); File tempFile; try { tempFile = File.createTempFile(rnd.nextInt(Integer.MAX_VALUE) + "_", name); tempFile.deleteOnExit(); } catch (IOException e) { throw new ProgramInvocationException( "An I/O error occurred while creating temporary file to extract nested library '" + entry.getName() + "'.", e); } extractedTempLibraries.add(tempFile); OutputStream out = null; InputStream in = null; try { out = new FileOutputStream(tempFile); in = new BufferedInputStream(jar.getInputStream(entry)); int numRead = 0; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); } } catch (IOException e) { throw new ProgramInvocationException("An I/O error occurred while extracting nested library '" + entry.getName() + "' to temporary file '" + tempFile.getAbsolutePath() + "'."); } finally { if (out != null) { out.close(); } if (in != null) { in.close(); } } } incomplete = false; } finally { if (incomplete) { deleteExtractedLibraries(extractedTempLibraries); } } return extractedTempLibraries; } } catch (Throwable t) { throw new ProgramInvocationException("Unknown I/O error while extracting contained jar files.", t); } finally { if (jar != null) { try { jar.close(); } catch (Throwable t) {} } } } }
PackagedProgram { public static List<File> extractContainedLibraries(URL jarFile) throws ProgramInvocationException { Random rnd = new Random(); JarFile jar = null; try { jar = new JarFile(new File(jarFile.toURI())); final List<JarEntry> containedJarFileEntries = new ArrayList<JarEntry>(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (name.length() > 8 && name.startsWith("lib/") && name.endsWith(".jar")) { containedJarFileEntries.add(entry); } } if (containedJarFileEntries.isEmpty()) { return Collections.emptyList(); } else { final List<File> extractedTempLibraries = new ArrayList<File>(containedJarFileEntries.size()); final byte[] buffer = new byte[4096]; boolean incomplete = true; try { for (int i = 0; i < containedJarFileEntries.size(); i++) { final JarEntry entry = containedJarFileEntries.get(i); String name = entry.getName(); name = name.replace('/', '_'); File tempFile; try { tempFile = File.createTempFile(rnd.nextInt(Integer.MAX_VALUE) + "_", name); tempFile.deleteOnExit(); } catch (IOException e) { throw new ProgramInvocationException( "An I/O error occurred while creating temporary file to extract nested library '" + entry.getName() + "'.", e); } extractedTempLibraries.add(tempFile); OutputStream out = null; InputStream in = null; try { out = new FileOutputStream(tempFile); in = new BufferedInputStream(jar.getInputStream(entry)); int numRead = 0; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); } } catch (IOException e) { throw new ProgramInvocationException("An I/O error occurred while extracting nested library '" + entry.getName() + "' to temporary file '" + tempFile.getAbsolutePath() + "'."); } finally { if (out != null) { out.close(); } if (in != null) { in.close(); } } } incomplete = false; } finally { if (incomplete) { deleteExtractedLibraries(extractedTempLibraries); } } return extractedTempLibraries; } } catch (Throwable t) { throw new ProgramInvocationException("Unknown I/O error while extracting contained jar files.", t); } finally { if (jar != null) { try { jar.close(); } catch (Throwable t) {} } } } PackagedProgram(File jarFile, String... args);  PackagedProgram(File jarFile, List<URL> classpaths, String... args);  PackagedProgram(File jarFile, @Nullable String entryPointClassName, String... args);  PackagedProgram(File jarFile, List<URL> classpaths, @Nullable String entryPointClassName, String... args);  PackagedProgram(Class<?> entryPointClass, String... args); }
PackagedProgram { public static List<File> extractContainedLibraries(URL jarFile) throws ProgramInvocationException { Random rnd = new Random(); JarFile jar = null; try { jar = new JarFile(new File(jarFile.toURI())); final List<JarEntry> containedJarFileEntries = new ArrayList<JarEntry>(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (name.length() > 8 && name.startsWith("lib/") && name.endsWith(".jar")) { containedJarFileEntries.add(entry); } } if (containedJarFileEntries.isEmpty()) { return Collections.emptyList(); } else { final List<File> extractedTempLibraries = new ArrayList<File>(containedJarFileEntries.size()); final byte[] buffer = new byte[4096]; boolean incomplete = true; try { for (int i = 0; i < containedJarFileEntries.size(); i++) { final JarEntry entry = containedJarFileEntries.get(i); String name = entry.getName(); name = name.replace('/', '_'); File tempFile; try { tempFile = File.createTempFile(rnd.nextInt(Integer.MAX_VALUE) + "_", name); tempFile.deleteOnExit(); } catch (IOException e) { throw new ProgramInvocationException( "An I/O error occurred while creating temporary file to extract nested library '" + entry.getName() + "'.", e); } extractedTempLibraries.add(tempFile); OutputStream out = null; InputStream in = null; try { out = new FileOutputStream(tempFile); in = new BufferedInputStream(jar.getInputStream(entry)); int numRead = 0; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); } } catch (IOException e) { throw new ProgramInvocationException("An I/O error occurred while extracting nested library '" + entry.getName() + "' to temporary file '" + tempFile.getAbsolutePath() + "'."); } finally { if (out != null) { out.close(); } if (in != null) { in.close(); } } } incomplete = false; } finally { if (incomplete) { deleteExtractedLibraries(extractedTempLibraries); } } return extractedTempLibraries; } } catch (Throwable t) { throw new ProgramInvocationException("Unknown I/O error while extracting contained jar files.", t); } finally { if (jar != null) { try { jar.close(); } catch (Throwable t) {} } } } PackagedProgram(File jarFile, String... args);  PackagedProgram(File jarFile, List<URL> classpaths, String... args);  PackagedProgram(File jarFile, @Nullable String entryPointClassName, String... args);  PackagedProgram(File jarFile, List<URL> classpaths, @Nullable String entryPointClassName, String... args);  PackagedProgram(Class<?> entryPointClass, String... args); void setSavepointRestoreSettings(SavepointRestoreSettings savepointSettings); SavepointRestoreSettings getSavepointSettings(); String[] getArguments(); String getMainClassName(); boolean isUsingInteractiveMode(); boolean isUsingProgramEntryPoint(); JobWithJars getPlanWithoutJars(); JobWithJars getPlanWithJars(); String getPreviewPlan(); @Nullable String getDescription(); void invokeInteractiveModeForExecution(); List<URL> getClasspaths(); ClassLoader getUserCodeClassLoader(); List<URL> getAllLibraries(); void deleteExtractedLibraries(); static List<File> extractContainedLibraries(URL jarFile); static void deleteExtractedLibraries(List<File> tempLibraries); }
PackagedProgram { public static List<File> extractContainedLibraries(URL jarFile) throws ProgramInvocationException { Random rnd = new Random(); JarFile jar = null; try { jar = new JarFile(new File(jarFile.toURI())); final List<JarEntry> containedJarFileEntries = new ArrayList<JarEntry>(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (name.length() > 8 && name.startsWith("lib/") && name.endsWith(".jar")) { containedJarFileEntries.add(entry); } } if (containedJarFileEntries.isEmpty()) { return Collections.emptyList(); } else { final List<File> extractedTempLibraries = new ArrayList<File>(containedJarFileEntries.size()); final byte[] buffer = new byte[4096]; boolean incomplete = true; try { for (int i = 0; i < containedJarFileEntries.size(); i++) { final JarEntry entry = containedJarFileEntries.get(i); String name = entry.getName(); name = name.replace('/', '_'); File tempFile; try { tempFile = File.createTempFile(rnd.nextInt(Integer.MAX_VALUE) + "_", name); tempFile.deleteOnExit(); } catch (IOException e) { throw new ProgramInvocationException( "An I/O error occurred while creating temporary file to extract nested library '" + entry.getName() + "'.", e); } extractedTempLibraries.add(tempFile); OutputStream out = null; InputStream in = null; try { out = new FileOutputStream(tempFile); in = new BufferedInputStream(jar.getInputStream(entry)); int numRead = 0; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); } } catch (IOException e) { throw new ProgramInvocationException("An I/O error occurred while extracting nested library '" + entry.getName() + "' to temporary file '" + tempFile.getAbsolutePath() + "'."); } finally { if (out != null) { out.close(); } if (in != null) { in.close(); } } } incomplete = false; } finally { if (incomplete) { deleteExtractedLibraries(extractedTempLibraries); } } return extractedTempLibraries; } } catch (Throwable t) { throw new ProgramInvocationException("Unknown I/O error while extracting contained jar files.", t); } finally { if (jar != null) { try { jar.close(); } catch (Throwable t) {} } } } PackagedProgram(File jarFile, String... args);  PackagedProgram(File jarFile, List<URL> classpaths, String... args);  PackagedProgram(File jarFile, @Nullable String entryPointClassName, String... args);  PackagedProgram(File jarFile, List<URL> classpaths, @Nullable String entryPointClassName, String... args);  PackagedProgram(Class<?> entryPointClass, String... args); void setSavepointRestoreSettings(SavepointRestoreSettings savepointSettings); SavepointRestoreSettings getSavepointSettings(); String[] getArguments(); String getMainClassName(); boolean isUsingInteractiveMode(); boolean isUsingProgramEntryPoint(); JobWithJars getPlanWithoutJars(); JobWithJars getPlanWithJars(); String getPreviewPlan(); @Nullable String getDescription(); void invokeInteractiveModeForExecution(); List<URL> getClasspaths(); ClassLoader getUserCodeClassLoader(); List<URL> getAllLibraries(); void deleteExtractedLibraries(); static List<File> extractContainedLibraries(URL jarFile); static void deleteExtractedLibraries(List<File> tempLibraries); static final String MANIFEST_ATTRIBUTE_ASSEMBLER_CLASS; static final String MANIFEST_ATTRIBUTE_MAIN_CLASS; }