/** * Read the version information from a file with a given file name. The file must be a jar * manifest file and all its entries are searched for package names and their specification * version information. All information is collected in a map for later lookup of package names * and their versions. * * @param versionFileName name of the jar file containing version information */ public static String readVersionFromFile(String applicationName, String versionFileName) { try { FileInputStream fileInput = new FileInputStream(versionFileName); Manifest manifest = new Manifest(); manifest.read(fileInput); Map entries = manifest.getEntries(); // Now write out the pre-entry attributes Iterator entryIterator = entries.entrySet().iterator(); while (entryIterator.hasNext()) { Map.Entry currentEntry = (Map.Entry) entryIterator.next(); String packageName = currentEntry.getKey().toString(); packageName = normalizePackageName(packageName); Attributes attributes = (Attributes) currentEntry.getValue(); String packageSpecVersion = attributes.getValue(Attributes.Name.SPECIFICATION_VERSION); packageSpecVersion = extractVersionInfo(packageSpecVersion); return packageSpecVersion; } } catch (IOException exception) { exception.printStackTrace(); } // no version found return null; }
// optimal side-effects private synchronized boolean isSuperMan() throws IOException { if (superMan == null) { superMan = super.getManifest(); } if (superMan != null) { superAttr = superMan.getMainAttributes(); superEntries = superMan.getEntries(); return true; } else return false; }
private static String getVersionString() { String findContainingJar = JarManager.findContainingJar(Main.class); try { StringBuffer buffer = new StringBuffer(); JarFile jar = new JarFile(findContainingJar); final Manifest manifest = jar.getManifest(); final Map<String, Attributes> attrs = manifest.getEntries(); Attributes attr = attrs.get("org/apache/pig"); String version = (String) attr.getValue("Implementation-Version"); String svnRevision = (String) attr.getValue("Svn-Revision"); String buildTime = (String) attr.getValue("Build-TimeStamp"); // we use a version string similar to svn // svn, version 1.4.4 (r25188) // compiled Sep 23 2007, 22:32:34 return "Apache Pig version " + version + " (r" + svnRevision + ") \ncompiled " + buildTime; } catch (Exception e) { throw new RuntimeException("unable to read pigs manifest file", e); } }
public String verify(JarFile jar, String... algorithms) throws IOException { if (algorithms == null || algorithms.length == 0) algorithms = new String[] {"MD5", "SHA"}; else if (algorithms.length == 1 && algorithms[0].equals("-")) return null; try { Manifest m = jar.getManifest(); if (m.getEntries().isEmpty()) return "No name sections"; for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) { JarEntry je = e.nextElement(); if (MANIFEST_ENTRY.matcher(je.getName()).matches()) continue; Attributes nameSection = m.getAttributes(je.getName()); if (nameSection == null) return "No name section for " + je.getName(); for (String algorithm : algorithms) { try { MessageDigest md = MessageDigest.getInstance(algorithm); String expected = nameSection.getValue(algorithm + "-Digest"); if (expected != null) { byte digest[] = Base64.decodeBase64(expected); copy(jar.getInputStream(je), md); if (!Arrays.equals(digest, md.digest())) return "Invalid digest for " + je.getName() + ", " + expected + " != " + Base64.encodeBase64(md.digest()); } else reporter.error("could not find digest for " + algorithm + "-Digest"); } catch (NoSuchAlgorithmException nsae) { return "Missing digest algorithm " + algorithm; } } } } catch (Exception e) { return "Failed to verify due to exception: " + e.getMessage(); } return null; }
public Manifest getManifest() throws IOException { if (!isSuperMan()) { return null; } Manifest man = new Manifest(); Attributes attr = man.getMainAttributes(); attr.putAll((Map) superAttr.clone()); // now deep copy the manifest entries if (superEntries != null) { Map entries = man.getEntries(); Iterator it = superEntries.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); Attributes at = (Attributes) superEntries.get(key); entries.put(key, at.clone()); } } return man; }