use of java.util.jar.JarEntry in project tomcat by apache.
the class AbstractArchiveResourceSet method getResource.
@Override
public final WebResource getResource(String path) {
checkPath(path);
String webAppMount = getWebAppMount();
WebResourceRoot root = getRoot();
if (path.startsWith(webAppMount)) {
String pathInJar = getInternalPath() + path.substring(webAppMount.length(), path.length());
// Always strip off the leading '/' to get the JAR path
if (pathInJar.length() > 0 && pathInJar.charAt(0) == '/') {
pathInJar = pathInJar.substring(1);
}
if (pathInJar.equals("")) {
// This is a directory resource so the path must end with /
if (!path.endsWith("/")) {
path = path + "/";
}
return new JarResourceRoot(root, new File(getBase()), baseUrlString, path);
} else {
Map<String, JarEntry> jarEntries = getArchiveEntries(true);
JarEntry jarEntry = null;
if (!(pathInJar.charAt(pathInJar.length() - 1) == '/')) {
if (jarEntries == null) {
jarEntry = getArchiveEntry(pathInJar + '/');
} else {
jarEntry = jarEntries.get(pathInJar + '/');
}
if (jarEntry != null) {
path = path + '/';
}
}
if (jarEntry == null) {
if (jarEntries == null) {
jarEntry = getArchiveEntry(pathInJar);
} else {
jarEntry = jarEntries.get(pathInJar);
}
}
if (jarEntry == null) {
return new EmptyResource(root, path);
} else {
return createArchiveResource(jarEntry, path, getManifest());
}
}
} else {
return new EmptyResource(root, path);
}
}
use of java.util.jar.JarEntry in project tomcat by apache.
the class AbstractSingleArchiveResourceSet method getArchiveEntries.
@Override
protected HashMap<String, JarEntry> getArchiveEntries(boolean single) {
synchronized (archiveLock) {
if (archiveEntries == null && !single) {
JarFile jarFile = null;
archiveEntries = new HashMap<>();
try {
jarFile = openJarFile();
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
archiveEntries.put(entry.getName(), entry);
}
} catch (IOException ioe) {
// Should never happen
archiveEntries = null;
throw new IllegalStateException(ioe);
} finally {
if (jarFile != null) {
closeJarFile();
}
}
}
return archiveEntries;
}
}
use of java.util.jar.JarEntry 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.JarEntry in project buck by facebook.
the class JarDirectoryStepHelper method createJarFile.
public static int createJarFile(ProjectFilesystem filesystem, Path pathToOutputFile, CustomZipOutputStream outputFile, ImmutableSortedSet<Path> entriesToJar, ImmutableSet<String> alreadyAddedEntriesToOutputFile, Optional<String> mainClass, Optional<Path> manifestFile, boolean mergeManifests, Iterable<Pattern> blacklist, JavacEventSink eventSink, PrintStream stdErr) throws IOException {
Set<String> alreadyAddedEntries = Sets.newHashSet(alreadyAddedEntriesToOutputFile);
// Write the manifest first.
JarEntry metaInf = new JarEntry("META-INF/");
// We want deterministic JARs, so avoid mtimes. -1 is timzeone independent, 0 is not.
metaInf.setTime(ZipConstants.getFakeTime());
outputFile.putNextEntry(metaInf);
outputFile.closeEntry();
alreadyAddedEntries.add("META-INF/");
Manifest manifest = createManifest(filesystem, entriesToJar, mainClass, manifestFile, mergeManifests);
JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME);
// We want deterministic JARs, so avoid mtimes. -1 is timzeone independent, 0 is not.
manifestEntry.setTime(ZipConstants.getFakeTime());
outputFile.putNextEntry(manifestEntry);
manifest.write(outputFile);
outputFile.closeEntry();
alreadyAddedEntries.add(JarFile.MANIFEST_NAME);
Path absoluteOutputPath = filesystem.getPathForRelativePath(pathToOutputFile);
for (Path entry : entriesToJar) {
Path file = filesystem.getPathForRelativePath(entry);
if (Files.isRegularFile(file)) {
Preconditions.checkArgument(!file.equals(absoluteOutputPath), "Trying to put file %s into itself", file);
// Assume the file is a ZIP/JAR file.
copyZipEntriesToJar(file, pathToOutputFile, outputFile, alreadyAddedEntries, eventSink, blacklist);
} else if (Files.isDirectory(file)) {
addFilesInDirectoryToJar(filesystem, file, outputFile, alreadyAddedEntries, blacklist, eventSink);
} else {
throw new IllegalStateException("Must be a file or directory: " + file);
}
}
if (mainClass.isPresent() && !mainClassPresent(mainClass.get(), alreadyAddedEntries)) {
stdErr.print(String.format("ERROR: Main class %s does not exist.\n", mainClass.get()));
return 1;
}
return 0;
}
use of java.util.jar.JarEntry in project enumerable by hraberg.
the class LambdaCompiler method unjar.
File unjar(JarFile jarFile) throws IOException {
InputStream in = null;
OutputStream out = null;
File tempDir = new File(getProperty("java.io.tmpdir"), new File(jarFile.getName()).getName() + "-" + currentTimeMillis());
ensureDirCreated(tempDir);
try {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
File file = new File(tempDir, jarEntry.getName());
if (jarEntry.isDirectory()) {
file.mkdir();
} else {
file.getParentFile().mkdirs();
in = jarFile.getInputStream(jarEntry);
out = new FileOutputStream(file);
int read;
while ((read = in.read(buffer)) != -1) out.write(buffer, 0, read);
out.flush();
}
}
return tempDir;
} finally {
if (out != null)
out.close();
if (in != null)
in.close();
jarFile.close();
}
}
Aggregations