use of java.util.jar.Manifest in project intellij-community by JetBrains.
the class ManifestFileUtil method createManifestFileConfiguration.
@NotNull
public static ManifestFileConfiguration createManifestFileConfiguration(@NotNull VirtualFile manifestFile) {
final String path = manifestFile.getPath();
Manifest manifest = readManifest(manifestFile);
String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
final String classpathText = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
final List<String> classpath = new ArrayList<>();
if (classpathText != null) {
classpath.addAll(StringUtil.split(classpathText, " "));
}
return new ManifestFileConfiguration(path, classpath, mainClass, manifestFile.isWritable());
}
use of java.util.jar.Manifest in project intellij-community by JetBrains.
the class ManifestFileUtil method updateManifest.
public static void updateManifest(@NotNull final VirtualFile file, @Nullable final String mainClass, @Nullable final List<String> classpath, final boolean replaceValues) {
final Manifest manifest = readManifest(file);
final Attributes mainAttributes = manifest.getMainAttributes();
if (mainClass != null) {
mainAttributes.put(Attributes.Name.MAIN_CLASS, mainClass);
} else if (replaceValues) {
mainAttributes.remove(Attributes.Name.MAIN_CLASS);
}
if (classpath != null && !classpath.isEmpty()) {
List<String> updatedClasspath;
if (replaceValues) {
updatedClasspath = classpath;
} else {
updatedClasspath = new ArrayList<>();
final String oldClasspath = (String) mainAttributes.get(Attributes.Name.CLASS_PATH);
if (!StringUtil.isEmpty(oldClasspath)) {
updatedClasspath.addAll(StringUtil.split(oldClasspath, " "));
}
for (String path : classpath) {
if (!updatedClasspath.contains(path)) {
updatedClasspath.add(path);
}
}
}
mainAttributes.put(Attributes.Name.CLASS_PATH, StringUtil.join(updatedClasspath, " "));
} else if (replaceValues) {
mainAttributes.remove(Attributes.Name.CLASS_PATH);
}
ManifestBuilder.setVersionAttribute(mainAttributes);
ApplicationManager.getApplication().runWriteAction(() -> {
try {
final OutputStream outputStream = file.getOutputStream(ManifestFileUtil.class);
try {
manifest.write(outputStream);
} finally {
outputStream.close();
}
} catch (IOException e) {
LOG.info(e);
}
});
}
use of java.util.jar.Manifest in project Activiti by Activiti.
the class BarDeploymentListener method canHandle.
public boolean canHandle(File artifact) {
JarFile jar = null;
try {
if (!artifact.getName().endsWith(".bar")) {
return false;
}
jar = new JarFile(artifact);
// Only handle non OSGi bundles
Manifest m = jar.getManifest();
if (m != null && m.getMainAttributes().getValue(new Attributes.Name(BUNDLE_SYMBOLICNAME)) != null && m.getMainAttributes().getValue(new Attributes.Name(BUNDLE_VERSION)) != null) {
return false;
}
return true;
} catch (Exception e) {
return false;
} finally {
if (jar != null) {
try {
jar.close();
} catch (IOException e) {
LOGGER.error("Unable to close jar", e);
}
}
}
}
use of java.util.jar.Manifest in project XobotOS by xamarin.
the class PackageParser method collectCertificates.
public boolean collectCertificates(Package pkg, int flags) {
pkg.mSignatures = null;
WeakReference<byte[]> readBufferRef;
byte[] readBuffer = null;
synchronized (mSync) {
readBufferRef = mReadBuffer;
if (readBufferRef != null) {
mReadBuffer = null;
readBuffer = readBufferRef.get();
}
if (readBuffer == null) {
readBuffer = new byte[8192];
readBufferRef = new WeakReference<byte[]>(readBuffer);
}
}
try {
JarFile jarFile = new JarFile(mArchiveSourcePath);
Certificate[] certs = null;
if ((flags & PARSE_IS_SYSTEM) != 0) {
// If this package comes from the system image, then we
// can trust it... we'll just use the AndroidManifest.xml
// to retrieve its signatures, not validating all of the
// files.
JarEntry jarEntry = jarFile.getJarEntry(ANDROID_MANIFEST_FILENAME);
certs = loadCertificates(jarFile, jarEntry, readBuffer);
if (certs == null) {
Slog.e(TAG, "Package " + pkg.packageName + " has no certificates at entry " + jarEntry.getName() + "; ignoring!");
jarFile.close();
mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
return false;
}
if (DEBUG_JAR) {
Slog.i(TAG, "File " + mArchiveSourcePath + ": entry=" + jarEntry + " certs=" + (certs != null ? certs.length : 0));
if (certs != null) {
final int N = certs.length;
for (int i = 0; i < N; i++) {
Slog.i(TAG, " Public key: " + certs[i].getPublicKey().getEncoded() + " " + certs[i].getPublicKey());
}
}
}
} else {
Enumeration<JarEntry> entries = jarFile.entries();
final Manifest manifest = jarFile.getManifest();
while (entries.hasMoreElements()) {
final JarEntry je = entries.nextElement();
if (je.isDirectory())
continue;
final String name = je.getName();
if (name.startsWith("META-INF/"))
continue;
if (ANDROID_MANIFEST_FILENAME.equals(name)) {
final Attributes attributes = manifest.getAttributes(name);
pkg.manifestDigest = ManifestDigest.fromAttributes(attributes);
}
final Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
if (DEBUG_JAR) {
Slog.i(TAG, "File " + mArchiveSourcePath + " entry " + je.getName() + ": certs=" + certs + " (" + (certs != null ? certs.length : 0) + ")");
}
if (localCerts == null) {
Slog.e(TAG, "Package " + pkg.packageName + " has no certificates at entry " + je.getName() + "; ignoring!");
jarFile.close();
mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
return false;
} else if (certs == null) {
certs = localCerts;
} else {
// Ensure all certificates match.
for (int i = 0; i < certs.length; i++) {
boolean found = false;
for (int j = 0; j < localCerts.length; j++) {
if (certs[i] != null && certs[i].equals(localCerts[j])) {
found = true;
break;
}
}
if (!found || certs.length != localCerts.length) {
Slog.e(TAG, "Package " + pkg.packageName + " has mismatched certificates at entry " + je.getName() + "; ignoring!");
jarFile.close();
mParseError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
return false;
}
}
}
}
}
jarFile.close();
synchronized (mSync) {
mReadBuffer = readBufferRef;
}
if (certs != null && certs.length > 0) {
final int N = certs.length;
pkg.mSignatures = new Signature[certs.length];
for (int i = 0; i < N; i++) {
pkg.mSignatures[i] = new Signature(certs[i].getEncoded());
}
} else {
Slog.e(TAG, "Package " + pkg.packageName + " has no certificates; ignoring!");
mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
return false;
}
} catch (CertificateEncodingException e) {
Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
return false;
} catch (IOException e) {
Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
return false;
} catch (RuntimeException e) {
Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
return false;
}
return true;
}
use of java.util.jar.Manifest in project jdk8u_jdk by JetBrains.
the class JarUtils method createJar.
/**
* Create jar file with specified files. If a specified file does not exist,
* a new jar entry will be created with the file name itself as the content.
*/
public static void createJar(String dest, Path filesLocation, String... fileNames) throws IOException {
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(dest), new Manifest())) {
for (String fileName : fileNames) {
System.out.println(String.format("Adding %s to %s", fileName, dest));
// add an archive entry, and write a file
jos.putNextEntry(new JarEntry(fileName));
File file;
if (filesLocation != null) {
file = filesLocation.resolve(fileName).toFile();
} else {
file = new File(fileName);
}
try (FileInputStream fis = new FileInputStream(file)) {
Utils.transferBetweenStreams(fis, jos);
} catch (FileNotFoundException e) {
jos.write(fileName.getBytes());
}
}
}
System.out.println();
}
Aggregations