use of java.util.jar.Manifest in project jetty.project by eclipse.
the class WarBundleManifestGenerator method createBundleManifest.
public static Manifest createBundleManifest(Manifest originalManifest, URL url, JarFile jarFile) {
Manifest res = new Manifest();
res.getMainAttributes().putAll(createBundleManifest(originalManifest.getMainAttributes(), url.toString(), jarFile));
return res;
}
use of java.util.jar.Manifest in project jetty.project by eclipse.
the class JarResource method copyTo.
/* ------------------------------------------------------------ */
@Override
public void copyTo(File directory) throws IOException {
if (!exists())
return;
if (LOG.isDebugEnabled())
LOG.debug("Extract " + this + " to " + directory);
String urlString = this.getURL().toExternalForm().trim();
int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
if (endOfJarUrl < 0)
throw new IOException("Not a valid jar url: " + urlString);
URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
String subEntryName = (endOfJarUrl + 2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/") ? true : false);
if (LOG.isDebugEnabled())
LOG.debug("Extracting entry = " + subEntryName + " from jar " + jarFileURL);
URLConnection c = jarFileURL.openConnection();
c.setUseCaches(false);
try (InputStream is = c.getInputStream();
JarInputStream jin = new JarInputStream(is)) {
JarEntry entry;
boolean shouldExtract;
while ((entry = jin.getNextJarEntry()) != null) {
String entryName = entry.getName();
if ((subEntryName != null) && (entryName.startsWith(subEntryName))) {
// is the subentry really a dir?
if (!subEntryIsDir && subEntryName.length() + 1 == entryName.length() && entryName.endsWith("/"))
subEntryIsDir = true;
//extract it.
if (subEntryIsDir) {
//if it is a subdirectory we are looking for, then we
//are looking to extract its contents into the target
//directory. Remove the name of the subdirectory so
//that we don't wind up creating it too.
entryName = entryName.substring(subEntryName.length());
if (!entryName.equals("")) {
//the entry is
shouldExtract = true;
} else
shouldExtract = false;
} else
shouldExtract = true;
} else if ((subEntryName != null) && (!entryName.startsWith(subEntryName))) {
//there is a particular entry we are looking for, and this one
//isn't it
shouldExtract = false;
} else {
//we are extracting everything
shouldExtract = true;
}
if (!shouldExtract) {
if (LOG.isDebugEnabled())
LOG.debug("Skipping entry: " + entryName);
continue;
}
String dotCheck = entryName.replace('\\', '/');
dotCheck = URIUtil.canonicalPath(dotCheck);
if (dotCheck == null) {
if (LOG.isDebugEnabled())
LOG.debug("Invalid entry: " + entryName);
continue;
}
File file = new File(directory, entryName);
if (entry.isDirectory()) {
// Make directory
if (!file.exists())
file.mkdirs();
} else {
// make directory (some jars don't list dirs)
File dir = new File(file.getParent());
if (!dir.exists())
dir.mkdirs();
// Make file
try (OutputStream fout = new FileOutputStream(file)) {
IO.copy(jin, fout);
}
// touch the file.
if (entry.getTime() >= 0)
file.setLastModified(entry.getTime());
}
}
if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF"))) {
Manifest manifest = jin.getManifest();
if (manifest != null) {
File metaInf = new File(directory, "META-INF");
metaInf.mkdir();
File f = new File(metaInf, "MANIFEST.MF");
try (OutputStream fout = new FileOutputStream(f)) {
manifest.write(fout);
}
}
}
}
}
use of java.util.jar.Manifest in project tomcat by apache.
the class ExtensionValidator method addSystemResource.
/**
* Checks to see if the given system JAR file contains a MANIFEST, and adds
* it to the container's manifest resources.
*
* @param jarFile The system JAR whose manifest to add
* @throws IOException Error reading JAR file
*/
public static void addSystemResource(File jarFile) throws IOException {
try (InputStream is = new FileInputStream(jarFile)) {
Manifest manifest = getManifest(is);
if (manifest != null) {
ManifestResource mre = new ManifestResource(jarFile.getAbsolutePath(), manifest, ManifestResource.SYSTEM);
containerManifestResources.add(mre);
}
}
}
use of java.util.jar.Manifest in project buck by facebook.
the class JarDirectoryStepHelper method createManifest.
private static Manifest createManifest(ProjectFilesystem filesystem, ImmutableSortedSet<Path> entriesToJar, Optional<String> mainClass, Optional<Path> manifestFile, boolean mergeManifests) throws IOException {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
if (mergeManifests) {
for (Path entry : entriesToJar) {
entry = filesystem.getPathForRelativePath(entry);
Manifest readManifest;
if (Files.isDirectory(entry)) {
Path manifestPath = entry.resolve(JarFile.MANIFEST_NAME);
if (!Files.exists(manifestPath)) {
continue;
}
try (InputStream inputStream = Files.newInputStream(manifestPath)) {
readManifest = new Manifest(inputStream);
}
} else {
// Assume a zip or jar file.
try (ZipFile zipFile = new ZipFile(entry.toFile())) {
ZipEntry manifestEntry = zipFile.getEntry(JarFile.MANIFEST_NAME);
if (manifestEntry == null) {
continue;
}
try (InputStream inputStream = zipFile.getInputStream(manifestEntry)) {
readManifest = new Manifest(inputStream);
}
}
}
merge(manifest, readManifest);
}
}
// so that values from the user overwrite values from merged manifests.
if (manifestFile.isPresent()) {
Path path = filesystem.getPathForRelativePath(manifestFile.get());
try (InputStream stream = Files.newInputStream(path)) {
Manifest readManifest = new Manifest(stream);
merge(manifest, readManifest);
}
}
// We may have merged manifests and over-written the user-supplied main class. Add it back.
if (mainClass.isPresent()) {
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass.get());
}
return manifest;
}
use of java.util.jar.Manifest 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;
}
Aggregations