List of usage examples for java.util.jar JarFile getManifest
public Manifest getManifest() throws IOException
From source file:edu.cmu.tetrad.cli.util.Args.java
public static void showHelp(String algorithmName, Options options) { StringBuilder sb = new StringBuilder("java -jar"); try {/*from w w w. java 2 s . co m*/ JarFile jarFile = new JarFile(Args.class.getProtectionDomain().getCodeSource().getLocation().getPath(), true); Manifest manifest = jarFile.getManifest(); Attributes attributes = manifest.getMainAttributes(); String artifactId = attributes.getValue("Implementation-Title"); String version = attributes.getValue("Implementation-Version"); sb.append(String.format(" %s-%s.jar", artifactId, version)); } catch (IOException exception) { sb.append(" causal-cmd.jar"); } sb.append(" --algorithm "); sb.append(algorithmName); HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(-1); formatter.printHelp(sb.toString(), options, true); }
From source file:sx.blah.discord.modules.ModuleLoader.java
/** * Gets the <code>Module-Requires</code> attribute list from the given jar file manifest. * * @param file The jar file to extract the manifest attribute from. * @return The value of the attribute./* w ww. j a v a2 s . co m*/ * @throws IOException If the jar file read operation fails. */ private static String[] getModuleRequires(File file) throws IOException { JarFile jarFile = new JarFile(file); Manifest manifest = jarFile.getManifest(); Attributes.Name moduleRequiresLower = new Attributes.Name("module-requires"); //TODO remove Attributes.Name moduleRequiresUpper = new Attributes.Name("Module-Requires"); if (manifest != null && manifest.getMainAttributes() != null //TODO remove && manifest.getMainAttributes().containsKey(moduleRequiresLower)) { String value = manifest.getMainAttributes().getValue(moduleRequiresLower); Discord4J.LOGGER.warn(LogMarkers.MODULES, "File {} uses the 'module-requires' attribute instead of 'Module-Requires', please rename the attribute!", file.getName()); return value.contains(";") ? value.split(";") : new String[] { value }; } else if (manifest != null && manifest.getMainAttributes() != null && manifest.getMainAttributes().containsKey(moduleRequiresUpper)) { String value = manifest.getMainAttributes().getValue(moduleRequiresUpper); return value.contains(";") ? value.split(";") : new String[] { value }; } else { return new String[0]; } }
From source file:org.wso2.carbon.automation.engine.frameworkutils.CodeCoverageUtils.java
private synchronized static void addEmmaDynamicImportPackage(String jarFilePath) throws IOException { if (!jarFilePath.endsWith(".jar")) { throw new IllegalArgumentException( "Jar file should have the extension .jar. " + jarFilePath + " is invalid"); }//www .ja v a 2 s .c om JarFile jarFile = new JarFile(jarFilePath); Manifest manifest = jarFile.getManifest(); if (manifest == null) { throw new IllegalArgumentException(jarFilePath + " does not contain a MANIFEST.MF file"); } String fileSeparator = (File.separatorChar == '\\') ? "\\" : File.separator; String jarFileName = jarFilePath; if (jarFilePath.lastIndexOf(fileSeparator) != -1) { jarFileName = jarFilePath.substring(jarFilePath.lastIndexOf(fileSeparator) + 1); } ArchiveManipulator archiveManipulator = null; String tempExtractedDir = null; try { archiveManipulator = new ArchiveManipulator(); tempExtractedDir = System.getProperty("basedir") + File.separator + "target" + File.separator + jarFileName.substring(0, jarFileName.lastIndexOf('.')); ArchiveExtractorUtil.extractFile(jarFilePath, tempExtractedDir); } catch (Exception e) { log.error("Could not extract the file", e); } finally { jarFile.close(); } String dynamicImports = manifest.getMainAttributes().getValue("DynamicImport-Package"); if (dynamicImports != null) { manifest.getMainAttributes().putValue("DynamicImport-Package", dynamicImports + ",com.vladium.*"); } else { manifest.getMainAttributes().putValue("DynamicImport-Package", "com.vladium.*"); } File newManifest = new File( tempExtractedDir + File.separator + "META-INF" + File.separator + "MANIFEST.MF"); FileOutputStream manifestOut = null; try { manifestOut = new FileOutputStream(newManifest); manifest.write(manifestOut); } catch (IOException e) { log.error("Could not write content to new MANIFEST.MF file", e); } finally { if (manifestOut != null) { manifestOut.close(); } } if (tempExtractedDir != null) archiveManipulator.archiveDir(jarFilePath, tempExtractedDir); FileUtils.forceDelete(newManifest); }
From source file:org.nuxeo.osgi.application.loader.FrameworkLoader.java
protected static boolean isBundle(File f) { Manifest mf;/* w w w .j a va2 s .com*/ try { if (f.isFile()) { // jar file JarFile jf = new JarFile(f); try { mf = jf.getManifest(); } finally { jf.close(); } if (mf == null) { return false; } } else if (f.isDirectory()) { // directory f = new File(f, "META-INF/MANIFEST.MF"); if (!f.isFile()) { return false; } mf = new Manifest(); FileInputStream input = new FileInputStream(f); try { mf.read(input); } finally { input.close(); } } else { return false; } } catch (IOException e) { return false; } return mf.getMainAttributes().containsKey(SYMBOLIC_NAME); }
From source file:org.rhq.core.clientapi.descriptor.AgentPluginDescriptorUtil.java
/** * Obtains the manifest of the plugin file represented by the given deployment info. * Use this method rather than calling deploymentInfo.getManifest() * (workaround for https://jira.jboss.org/jira/browse/JBAS-6266). * * @param pluginFile the plugin file/* w ww . ja v a2s. co m*/ * @return the deployed plugin's manifest */ private static Manifest getManifest(File pluginFile) { try { JarFile jarFile = new JarFile(pluginFile); try { Manifest manifest = jarFile.getManifest(); return manifest; } finally { jarFile.close(); } } catch (Exception ignored) { return null; // this is OK, it just means we do not have a manifest } }
From source file:de.uzk.hki.da.utils.Utilities.java
public static String getBuildNumber() { String buildNumber = "fakeBuildNumber"; try {/* ww w .j a v a2 s . c o m*/ JarFile file = null; Manifest mf = null; file = new JarFile(new File( Utilities.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath())); mf = file.getManifest(); Attributes attr = mf.getMainAttributes(); buildNumber = attr.getValue("buildNumber"); } catch (Exception e) { logger.debug("Can not extract the build number from "); } return buildNumber; }
From source file:info.evanchik.eclipse.karaf.core.KarafCorePluginUtils.java
/** * Reads a specified MANIFEST.MF entry from the JAR * * @param src/*from w ww. ja va2 s. c o m*/ * the JAR file * @param manifestHeader * the name of the header to read * @return the header as read from the MANIFEST.MF or null if it does not * exist or there was a problem reading the JAR * @throws IOException * if there is a problem reading the JAR */ public static String getJarManifestHeader(final File src, final String manifestHeader) throws IOException { final JarFile jar = new JarFile(src); final Manifest mf = jar.getManifest(); return (String) mf.getMainAttributes().get(manifestHeader); }
From source file:org.orbisgis.core.plugin.BundleTools.java
/** * Register in the host bundle the provided list of bundle reference * @param hostBundle Host BundleContext/*from w w w . j ava 2 s.c om*/ * @param nonDefaultBundleDeploying Bundle Reference array to deploy bundles in a non default way (install&start) */ public static void installBundles(BundleContext hostBundle, BundleReference[] nonDefaultBundleDeploying) { //Create a Map of nonDefaultBundleDeploying by their artifactId Map<String, BundleReference> customDeployBundles = new HashMap<String, BundleReference>( nonDefaultBundleDeploying.length); for (BundleReference ref : nonDefaultBundleDeploying) { customDeployBundles.put(ref.getArtifactId(), ref); } // List bundles in the /bundle subdirectory File bundleFolder = new File(BUNDLE_DIRECTORY); if (!bundleFolder.exists()) { return; } File[] files = bundleFolder.listFiles(); List<File> jarList = new ArrayList<File>(); if (files != null) { for (File file : files) { if (FilenameUtils.isExtension(file.getName(), "jar")) { jarList.add(file); } } } if (!jarList.isEmpty()) { Map<String, Bundle> installedBundleMap = new HashMap<String, Bundle>(); // Keep a reference to bundles in the framework cache for (Bundle bundle : hostBundle.getBundles()) { String key = bundle.getSymbolicName() + "_" + bundle.getVersion(); installedBundleMap.put(key, bundle); } // final List<Bundle> installedBundleList = new LinkedList<Bundle>(); for (File jarFile : jarList) { // Extract version and symbolic name of the bundle String key = ""; try { List<PackageDeclaration> packageDeclarations = new ArrayList<PackageDeclaration>(); BundleReference jarRef = parseJarManifest(jarFile, packageDeclarations); key = jarRef.getArtifactId() + "_" + jarRef.getVersion(); } catch (IOException ex) { LOGGER.error(ex.getLocalizedMessage(), ex); } // Retrieve from the framework cache the bundle at this location Bundle b = installedBundleMap.remove(key); // Read Jar manifest without installing it BundleReference reference = new BundleReference(""); // Default deploy try { JarFile jar = new JarFile(jarFile); Manifest manifest = jar.getManifest(); if (manifest != null && manifest.getMainAttributes() != null) { String artifact = manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME); BundleReference customRef = customDeployBundles.get(artifact); if (customRef != null) { reference = customRef; } } } catch (Exception ex) { LOGGER.error(I18N.tr("Could not read bundle manifest"), ex); } try { if (b != null) { String installedBundleLocation = b.getLocation(); if (!installedBundleLocation.equals(jarFile.toURI().toString())) { //if the location is not the same reinstall it b.uninstall(); b = null; } } // If the bundle is not in the framework cache install it if ((b == null) && reference.isAutoInstall()) { b = hostBundle.installBundle(jarFile.toURI().toString()); if (!isFragment(b) && reference.isAutoStart()) { installedBundleList.add(b); } } else if ((b != null)) { b.update(); } } catch (BundleException ex) { LOGGER.error("Error while installing bundle in bundle directory", ex); } } // Start new bundles for (Bundle bundle : installedBundleList) { try { bundle.start(); } catch (BundleException ex) { LOGGER.error("Error while starting bundle in bundle directory", ex); } } } }
From source file:org.kse.crypto.jcepolicy.JcePolicyUtil.java
/** * Get a JCE policy's crypto strength.// w ww . j a va2 s. com * * @param jcePolicy * JCE policy * @return Crypto strength * @throws CryptoException * If there was a problem getting the crypto strength */ public static CryptoStrength getCryptoStrength(JcePolicy jcePolicy) throws CryptoException { JarFile jarFile = null; try { File file = getJarFile(jcePolicy); // if there is no policy file at all, we assume that we are running under OpenJDK if (!file.exists()) { return UNLIMITED; } jarFile = new JarFile(file); Manifest jarManifest = jarFile.getManifest(); String strength = jarManifest.getMainAttributes().getValue("Crypto-Strength"); // workaround for IBM JDK: test for maximum key size if (strength == null) { return unlimitedStrengthTest(); } if (strength.equals(LIMITED.manifestValue())) { return LIMITED; } else { return UNLIMITED; } } catch (IOException ex) { throw new CryptoException( MessageFormat.format(res.getString("NoGetCryptoStrength.exception.message"), jcePolicy), ex); } finally { IOUtils.closeQuietly(jarFile); } }
From source file:com.jrummyapps.busybox.signing.ZipSigner.java
/** Add the SHA1 of every file to the manifest, creating it if necessary. */ private static Manifest addDigestsToManifest(final JarFile jar) throws IOException, GeneralSecurityException { final Manifest input = jar.getManifest(); final Manifest output = new Manifest(); final Attributes main = output.getMainAttributes(); main.putValue("Manifest-Version", MANIFEST_VERSION); main.putValue("Created-By", CREATED_BY); // We sort the input entries by name, and add them to the output manifest in sorted order. // We expect that the output map will be deterministic. final TreeMap<String, JarEntry> byName = new TreeMap<>(); for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) { JarEntry entry = e.nextElement(); byName.put(entry.getName(), entry); }/*from w ww. ja v a 2 s . c o m*/ final MessageDigest md = MessageDigest.getInstance("SHA1"); final byte[] buffer = new byte[4096]; int num; for (JarEntry entry : byName.values()) { final String name = entry.getName(); if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !name.equals(CERT_SF_NAME) && !name.equals(CERT_RSA_NAME)) { InputStream data = jar.getInputStream(entry); while ((num = data.read(buffer)) > 0) { md.update(buffer, 0, num); } Attributes attr = null; if (input != null) { attr = input.getAttributes(name); } attr = attr != null ? new Attributes(attr) : new Attributes(); attr.putValue("SHA1-Digest", base64encode(md.digest())); output.getEntries().put(name, attr); } } return output; }