use of java.util.jar.JarOutputStream in project weave by continuuity.
the class YarnWeavePreparer method saveLauncher.
/**
* Creates the launcher.jar for launch the main application.
*/
private void saveLauncher(Map<String, LocalFile> localFiles) throws URISyntaxException, IOException {
LOG.debug("Create and copy {}", Constants.Files.LAUNCHER_JAR);
Location location = createTempLocation(Constants.Files.LAUNCHER_JAR);
final String launcherName = WeaveLauncher.class.getName();
// Create a jar file with the WeaveLauncher optionally a json serialized classpath.json in it.
final JarOutputStream jarOut = new JarOutputStream(location.getOutputStream());
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = getClass().getClassLoader();
}
Dependencies.findClassDependencies(classLoader, new Dependencies.ClassAcceptor() {
@Override
public boolean accept(String className, URL classUrl, URL classPathUrl) {
Preconditions.checkArgument(className.startsWith(launcherName), "Launcher jar should not have dependencies: %s", className);
try {
jarOut.putNextEntry(new JarEntry(className.replace('.', '/') + ".class"));
InputStream is = classUrl.openStream();
try {
ByteStreams.copy(is, jarOut);
} finally {
is.close();
}
} catch (IOException e) {
throw Throwables.propagate(e);
}
return true;
}
}, WeaveLauncher.class.getName());
try {
if (!classPaths.isEmpty()) {
jarOut.putNextEntry(new JarEntry("classpath"));
jarOut.write(Joiner.on(':').join(classPaths).getBytes(Charsets.UTF_8));
}
} finally {
jarOut.close();
}
LOG.debug("Done {}", Constants.Files.LAUNCHER_JAR);
localFiles.put(Constants.Files.LAUNCHER_JAR, createLocalFile(Constants.Files.LAUNCHER_JAR, location));
}
use of java.util.jar.JarOutputStream in project jetty.project by eclipse.
the class WarURLConnection method substitueManifest.
/**
* Use PipedOuputStream and PipedInputStream to do the transformation without making
* a new temporary file ust to replace the manifest.
* @param newmanifest The new manifest
* @param rawIn The file input stream or equivalent. not the jar input stream.
*/
public static InputStream substitueManifest(final Manifest newmanifest, final InputStream rawIn) throws IOException {
final PipedOutputStream pOut = new PipedOutputStream();
PipedInputStream pIn = new PipedInputStream(pOut);
Runnable run = new Runnable() {
public void run() {
JarInputStream jin = null;
JarOutputStream dest = null;
try {
jin = new JarInputStream(rawIn, false);
dest = new JarOutputStream(pOut, newmanifest);
ZipEntry next = jin.getNextEntry();
while (next != null) {
if (next.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
continue;
}
dest.putNextEntry(next);
if (next.getSize() > 0) {
IO.copy(jin, dest, next.getSize());
}
next = jin.getNextJarEntry();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (dest != null)
IO.close(dest);
if (jin != null)
IO.close(jin);
IO.close(pOut);
}
}
};
Thread th = new Thread(run);
th.start();
return pIn;
}
use of java.util.jar.JarOutputStream in project elasticsearch by elastic.
the class JarHellTests method makeJar.
URL makeJar(Path dir, String name, Manifest manifest, String... files) throws IOException {
Path jarpath = dir.resolve(name);
ZipOutputStream out;
if (manifest == null) {
out = new JarOutputStream(Files.newOutputStream(jarpath, StandardOpenOption.CREATE));
} else {
out = new JarOutputStream(Files.newOutputStream(jarpath, StandardOpenOption.CREATE), manifest);
}
for (String file : files) {
out.putNextEntry(new ZipEntry(file));
}
out.close();
return jarpath.toUri().toURL();
}
use of java.util.jar.JarOutputStream in project buck by facebook.
the class ClassNodeListSupplierTest method testOneJar.
@Test
public void testOneJar() throws IOException {
File jar = new File(tmpDir.getRoot(), "primary.jar");
ZipOutputStream jarOut = new JarOutputStream(new FileOutputStream(jar));
jarOut.putNextEntry(new JarEntry("com/facebook/buck/android/ClassNodeListSupplierTest.class"));
writeClassBytes(ClassNodeListSupplierTest.class, jarOut);
jarOut.close();
Supplier<ImmutableList<ClassNode>> supplier = ClassNodeListSupplier.createMemoized(ImmutableList.of(jar.toPath()));
ImmutableList<ClassNode> classNodes = supplier.get();
assertEquals(1, classNodes.size());
assertEquals(Type.getType(ClassNodeListSupplierTest.class).getInternalName(), classNodes.get(0).name);
// Memoized should always return the same object
assertSame(classNodes, supplier.get());
}
use of java.util.jar.JarOutputStream in project buck by facebook.
the class AccumulateClassNamesStepTest method testExecuteAccumulateClassNamesStepOnJarFile.
@Test
public void testExecuteAccumulateClassNamesStepOnJarFile() throws IOException {
// Create a JAR file.
String name = "example.jar";
File jarFile = tmp.newFile(name);
try (JarOutputStream out = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jarFile)))) {
out.putNextEntry(new ZipEntry("com/example/Foo.class"));
out.closeEntry();
out.putNextEntry(new ZipEntry("com/example/Bar.class"));
out.closeEntry();
out.putNextEntry(new ZipEntry("com/example/not_a_class.png"));
out.closeEntry();
out.putNextEntry(new ZipEntry("com/example/subpackage/Baz.class"));
out.closeEntry();
}
// Create the AccumulateClassNamesStep and execute it.
ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath());
AccumulateClassNamesStep accumulateClassNamesStep = new AccumulateClassNamesStep(filesystem, Optional.of(Paths.get(name)), Paths.get("output.txt"));
ExecutionContext context = TestExecutionContext.newInstance();
accumulateClassNamesStep.execute(context);
String contents = Files.toString(new File(tmp.getRoot(), "output.txt"), Charsets.UTF_8);
String separator = AccumulateClassNamesStep.CLASS_NAME_HASH_CODE_SEPARATOR;
assertEquals("Verify that the contents are sorted alphabetically and ignore non-.class files.", Joiner.on('\n').join("com/example/Bar" + separator + SHA1_FOR_EMPTY_STRING, "com/example/Foo" + separator + SHA1_FOR_EMPTY_STRING, "com/example/subpackage/Baz" + separator + SHA1_FOR_EMPTY_STRING) + '\n', contents);
}
Aggregations