List of usage examples for java.util.jar Manifest getEntries
public Map<String, Attributes> getEntries()
From source file:org.echocat.nodoodle.transport.HandlerPacker.java
protected Manifest createManifest(String dataFileName, Collection<String> dependencyFileNames) { if (dataFileName == null) { throw new NullPointerException(); }// w w w . j a v a 2 s.co m if (dependencyFileNames == null) { throw new NullPointerException(); } final Manifest manifest = new Manifest(); final Attributes mainAttributes = manifest.getMainAttributes(); mainAttributes.put(MANIFEST_VERSION, "1.0"); mainAttributes.put(new Attributes.Name("Created-By"), getSmappserVersionString()); mainAttributes.put(CLASS_PATH, StringUtils.join(dependencyFileNames, ' ')); final Attributes extensionAttributes = new Attributes(); extensionAttributes.put(MANIFEST_DATE_FILE, dataFileName); manifest.getEntries().put(MANIFEST_EXTENSION_NAME, extensionAttributes); return manifest; }
From source file:org.eclipse.wb.internal.core.editor.palette.dialogs.ImportArchiveDialog.java
private List<PaletteElementInfo> extractElementsFromJarByManifest(JarInputStream jarStream) throws Exception { List<PaletteElementInfo> elements = Lists.newArrayList(); Manifest manifest = jarStream.getManifest(); // check manifest, if null find it if (manifest == null) { try {/* w w w .j a va2s . co m*/ while (true) { JarEntry entry = jarStream.getNextJarEntry(); if (entry == null) { break; } if (JarFile.MANIFEST_NAME.equalsIgnoreCase(entry.getName())) { // read manifest data byte[] buffer = IOUtils.toByteArray(jarStream); jarStream.closeEntry(); // create manifest manifest = new Manifest(new ByteArrayInputStream(buffer)); break; } } } catch (Throwable e) { DesignerPlugin.log(e); manifest = null; } } if (manifest != null) { // extract all "Java-Bean: True" classes for (Iterator<Map.Entry<String, Attributes>> I = manifest.getEntries().entrySet().iterator(); I .hasNext();) { Map.Entry<String, Attributes> mapElement = I.next(); Attributes attributes = mapElement.getValue(); if (JAVA_BEAN_VALUE.equalsIgnoreCase(attributes.getValue(JAVA_BEAN_KEY))) { String beanClass = mapElement.getKey(); if (beanClass == null || beanClass.length() <= JAVA_BEAN_CLASS_SUFFIX.length()) { continue; } // convert 'aaa/bbb/ccc.class' to 'aaa.bbb.ccc' PaletteElementInfo element = new PaletteElementInfo(); element.className = StringUtils.substringBeforeLast(beanClass, JAVA_BEAN_CLASS_SUFFIX) .replace('/', '.'); element.name = CodeUtils.getShortClass(element.className); elements.add(element); } } } return elements; }
From source file:org.gradle.api.java.archives.internal.DefaultManifest.java
private static void addSectionAttributesToJavaManifest(org.gradle.api.java.archives.Manifest gradleManifest, Manifest javaManifest) { for (Map.Entry<String, Attributes> entry : gradleManifest.getSections().entrySet()) { String sectionName = entry.getKey(); java.util.jar.Attributes sectionAttributes = new java.util.jar.Attributes(); for (Map.Entry<String, Object> attribute : entry.getValue().entrySet()) { String attributeName = attribute.getKey(); String attributeValue = attribute.getValue().toString(); sectionAttributes.putValue(attributeName, attributeValue); }/* www . j a va2 s.co m*/ javaManifest.getEntries().put(sectionName, sectionAttributes); } }
From source file:org.gradle.api.java.archives.internal.DefaultManifest.java
private void addJavaManifestToSections(Manifest javaManifest) { for (Map.Entry<String, java.util.jar.Attributes> sectionEntry : javaManifest.getEntries().entrySet()) { String sectionName = sectionEntry.getKey(); DefaultAttributes sectionAttributes = new DefaultAttributes(); for (Object attributeKey : sectionEntry.getValue().keySet()) { String attributeName = attributeKey.toString(); String attributeValue = sectionEntry.getValue().getValue(attributeName); sectionAttributes.put(attributeName, attributeValue); }//from ww w . jav a 2 s. c o m sections.put(sectionName, sectionAttributes); } }
From source file:org.jahia.utils.maven.plugin.osgi.BuildFrameworkPackageListMojo.java
private void scanJar(Map<String, Map<String, Map<String, VersionLocation>>> packageVersionCounts, File jarFile, String defaultVersion) throws IOException { JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile)); Manifest jarManifest = jarInputStream.getManifest(); // Map<String, String> manifestVersions = new HashMap<String,String>(); String specificationVersion = null; if (jarManifest == null) { getLog().warn("No MANIFEST.MF file found for dependency " + jarFile); } else {// w w w .j a v a 2 s. co m if (jarManifest.getMainAttributes() == null) { getLog().warn("No main attributes found in MANIFEST.MF file found for dependency " + jarFile); } else { specificationVersion = jarManifest.getMainAttributes().getValue("Specification-Version"); if (defaultVersion == null) { if (jarManifest.getMainAttributes().getValue("Bundle-Version") != null) { } else if (specificationVersion != null) { defaultVersion = specificationVersion; } else { defaultVersion = jarManifest.getMainAttributes().getValue("Implementation-Version"); } } String exportPackageHeaderValue = jarManifest.getMainAttributes().getValue("Export-Package"); if (exportPackageHeaderValue != null) { ManifestElement[] manifestElements = new ManifestElement[0]; try { manifestElements = ManifestElement.parseHeader("Export-Package", exportPackageHeaderValue); } catch (BundleException e) { getLog().warn("Error while parsing Export-Package header value for jar " + jarFile, e); } for (ManifestElement manifestElement : manifestElements) { String[] packageNames = manifestElement.getValueComponents(); String version = manifestElement.getAttribute("version"); for (String packageName : packageNames) { updateVersionLocationCounts(packageVersionCounts, jarFile.getCanonicalPath(), version, version, packageName); } } } for (Map.Entry<String, Attributes> manifestEntries : jarManifest.getEntries().entrySet()) { String packageName = manifestEntries.getKey().replaceAll("/", "."); if (packageName.endsWith(".class")) { continue; } if (packageName.endsWith(".")) { packageName = packageName.substring(0, packageName.length() - 1); } if (packageName.endsWith(".*")) { packageName = packageName.substring(0, packageName.length() - 1); } int lastDotPos = packageName.lastIndexOf("."); String lastPackage = packageName; if (lastDotPos > -1) { lastPackage = packageName.substring(lastDotPos + 1); } if (lastPackage.length() > 0 && Character.isUpperCase(lastPackage.charAt(0))) { // ignore non package version continue; } if (StringUtils.isEmpty(packageName) || packageName.startsWith("META-INF") || packageName.startsWith("OSGI-INF") || packageName.startsWith("OSGI-OPT") || packageName.startsWith("WEB-INF") || packageName.startsWith("org.osgi")) { // ignore private package names continue; } String packageVersion = null; if (manifestEntries.getValue().getValue("Specification-Version") != null) { packageVersion = manifestEntries.getValue().getValue("Specification-Version"); } else { packageVersion = manifestEntries.getValue().getValue("Implementation-Version"); } if (packageVersion != null) { getLog().info("Found package version in " + jarFile.getName() + " MANIFEST : " + packageName + " v" + packageVersion); updateVersionLocationCounts(packageVersionCounts, jarFile.getCanonicalPath(), packageVersion, specificationVersion, packageName); // manifestVersions.put(packageName, packageVersion); } } } } JarEntry jarEntry = null; // getLog().debug("Processing file " + artifact.getFile() + "..."); while ((jarEntry = jarInputStream.getNextJarEntry()) != null) { if (!jarEntry.isDirectory()) { String entryName = jarEntry.getName(); String entryPackage = ""; int lastSlash = entryName.lastIndexOf("/"); if (lastSlash > -1) { entryPackage = entryName.substring(0, lastSlash); entryPackage = entryPackage.replaceAll("/", "."); if (StringUtils.isNotEmpty(entryPackage) && !entryPackage.startsWith("META-INF") && !entryPackage.startsWith("OSGI-INF") && !entryPackage.startsWith("OSGI-OPT") && !entryPackage.startsWith("WEB-INF") && !entryPackage.startsWith("org.osgi")) { updateVersionLocationCounts(packageVersionCounts, jarFile.getCanonicalPath(), defaultVersion, specificationVersion, entryPackage); } } } } jarInputStream.close(); }
From source file:org.jboss.windup.decorator.archive.ManifestDecorator.java
protected String extractValue(Manifest mf, List<String> priority) { String val = findValueInAttribute(mf.getMainAttributes(), priority); if (StringUtils.isNotBlank(val)) { return val; }/* w w w .j a v a 2 s. com*/ // prepare the fallback attributes, ordered by name... List<String> attributeNames = new ArrayList<String>(mf.getEntries().keySet()); Collections.sort(attributeNames); for (String attributeName : attributeNames) { val = findValueInAttribute(mf.getAttributes(attributeName), priority); if (StringUtils.isNotBlank(val)) { return val; } } return null; }
From source file:org.jboss.windup.decorator.archive.ManifestVersionMapperDecorator.java
protected boolean matchesAll(Manifest mf) { boolean matched = false; // check the main attributes.... matched = matchAttributes(mf.getMainAttributes()); // if this doesn't match the main attributes, go through attribute groups trying to match. if (!matched) { // prepare the fallback attributes, ordered by name... List<String> attributeNames = new ArrayList<String>(mf.getEntries().keySet()); Collections.sort(attributeNames); for (String attributeName : attributeNames) { matched = matchAttributes(mf.getAttributes(attributeName)); if (matched) { return true; }//from ww w . j av a 2 s. co m } } return matched; }
From source file:org.lockss.plugin.PluginManager.java
/** * Given a file representing a JAR, retrieve a list of available * plugin classes to load.// ww w . ja va2 s . co m */ private List<String> getJarPluginClasses(File blessedJar) throws IOException { JarFile jar = new JarFile(blessedJar); Manifest manifest = jar.getManifest(); Map entries = manifest.getEntries(); List<String> plugins = new ArrayList<String>(); for (Iterator manIter = entries.keySet().iterator(); manIter.hasNext();) { String key = (String) manIter.next(); Attributes attrs = manifest.getAttributes(key); if (attrs.containsKey(LOADABLE_PLUGIN_ATTR)) { String s = StringUtil.replaceString(key, "/", "."); String pluginName = null; if (StringUtil.endsWithIgnoreCase(key, ".class")) { pluginName = StringUtil.replaceString(s, ".class", ""); log.debug2("Adding '" + pluginName + "' to plugin load list."); plugins.add(pluginName); } else if (StringUtil.endsWithIgnoreCase(key, ".xml")) { pluginName = StringUtil.replaceString(s, ".xml", ""); log.debug2("Adding '" + pluginName + "' to plugin load list."); plugins.add(pluginName); } } } jar.close(); return plugins; }
From source file:org.owasp.dependencycheck.analyzer.JarAnalyzer.java
/** * <p>/*w ww. j a v a2s. c o m*/ * Reads the manifest from the JAR file and collects the entries. Some * vendorKey entries are:</p> * <ul><li>Implementation Title</li> * <li>Implementation Version</li> <li>Implementation Vendor</li> * <li>Implementation VendorId</li> <li>Bundle Name</li> <li>Bundle * Version</li> <li>Bundle Vendor</li> <li>Bundle Description</li> <li>Main * Class</li> </ul> * However, all but a handful of specific entries are read in. * * @param dependency A reference to the dependency * @param classInformation a collection of class information * @return whether evidence was identified parsing the manifest * @throws IOException if there is an issue reading the JAR file */ protected boolean parseManifest(Dependency dependency, List<ClassNameInformation> classInformation) throws IOException { boolean foundSomething = false; JarFile jar = null; try { jar = new JarFile(dependency.getActualFilePath()); final Manifest manifest = jar.getManifest(); if (manifest == null) { if (!dependency.getFileName().toLowerCase().endsWith("-sources.jar") && !dependency.getFileName().toLowerCase().endsWith("-javadoc.jar") && !dependency.getFileName().toLowerCase().endsWith("-src.jar") && !dependency.getFileName().toLowerCase().endsWith("-doc.jar")) { LOGGER.debug("Jar file '{}' does not contain a manifest.", dependency.getFileName()); } return false; } final EvidenceCollection vendorEvidence = dependency.getVendorEvidence(); final EvidenceCollection productEvidence = dependency.getProductEvidence(); final EvidenceCollection versionEvidence = dependency.getVersionEvidence(); String source = "Manifest"; String specificationVersion = null; boolean hasImplementationVersion = false; Attributes atts = manifest.getMainAttributes(); for (Entry<Object, Object> entry : atts.entrySet()) { String key = entry.getKey().toString(); String value = atts.getValue(key); if (HTML_DETECTION_PATTERN.matcher(value).find()) { value = Jsoup.parse(value).text(); } if (IGNORE_VALUES.contains(value)) { continue; } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_TITLE.toString())) { foundSomething = true; productEvidence.addEvidence(source, key, value, Confidence.HIGH); addMatchingValues(classInformation, value, productEvidence); } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VERSION.toString())) { hasImplementationVersion = true; foundSomething = true; versionEvidence.addEvidence(source, key, value, Confidence.HIGH); } else if ("specification-version".equalsIgnoreCase(key)) { specificationVersion = key; } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR.toString())) { foundSomething = true; vendorEvidence.addEvidence(source, key, value, Confidence.HIGH); addMatchingValues(classInformation, value, vendorEvidence); } else if (key.equalsIgnoreCase(IMPLEMENTATION_VENDOR_ID)) { foundSomething = true; vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM); addMatchingValues(classInformation, value, vendorEvidence); } else if (key.equalsIgnoreCase(BUNDLE_DESCRIPTION)) { foundSomething = true; addDescription(dependency, value, "manifest", key); addMatchingValues(classInformation, value, productEvidence); } else if (key.equalsIgnoreCase(BUNDLE_NAME)) { foundSomething = true; productEvidence.addEvidence(source, key, value, Confidence.MEDIUM); addMatchingValues(classInformation, value, productEvidence); // //the following caused false positives. // } else if (key.equalsIgnoreCase(BUNDLE_VENDOR)) { // foundSomething = true; // vendorEvidence.addEvidence(source, key, value, Confidence.HIGH); // addMatchingValues(classInformation, value, vendorEvidence); } else if (key.equalsIgnoreCase(BUNDLE_VERSION)) { foundSomething = true; versionEvidence.addEvidence(source, key, value, Confidence.HIGH); } else if (key.equalsIgnoreCase(Attributes.Name.MAIN_CLASS.toString())) { continue; //skipping main class as if this has important information to add // it will be added during class name analysis... if other fields // have the information from the class name then they will get added... } else { key = key.toLowerCase(); if (!IGNORE_KEYS.contains(key) && !key.endsWith("jdk") && !key.contains("lastmodified") && !key.endsWith("package") && !key.endsWith("classpath") && !key.endsWith("class-path") && !key.endsWith("-scm") //todo change this to a regex? && !key.startsWith("scm-") && !value.trim().startsWith("scm:") && !isImportPackage(key, value) && !isPackage(key, value)) { foundSomething = true; if (key.contains("version")) { if (!key.contains("specification")) { versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM); } } else if ("build-id".equals(key)) { int pos = value.indexOf('('); if (pos >= 0) { value = value.substring(0, pos - 1); } pos = value.indexOf('['); if (pos >= 0) { value = value.substring(0, pos - 1); } versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM); } else if (key.contains("title")) { productEvidence.addEvidence(source, key, value, Confidence.MEDIUM); addMatchingValues(classInformation, value, productEvidence); } else if (key.contains("vendor")) { if (key.contains("specification")) { vendorEvidence.addEvidence(source, key, value, Confidence.LOW); } else { vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM); addMatchingValues(classInformation, value, vendorEvidence); } } else if (key.contains("name")) { productEvidence.addEvidence(source, key, value, Confidence.MEDIUM); vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM); addMatchingValues(classInformation, value, vendorEvidence); addMatchingValues(classInformation, value, productEvidence); } else if (key.contains("license")) { addLicense(dependency, value); } else if (key.contains("description")) { addDescription(dependency, value, "manifest", key); } else { productEvidence.addEvidence(source, key, value, Confidence.LOW); vendorEvidence.addEvidence(source, key, value, Confidence.LOW); addMatchingValues(classInformation, value, vendorEvidence); addMatchingValues(classInformation, value, productEvidence); if (value.matches(".*\\d.*")) { final StringTokenizer tokenizer = new StringTokenizer(value, " "); while (tokenizer.hasMoreElements()) { final String s = tokenizer.nextToken(); if (s.matches("^[0-9.]+$")) { versionEvidence.addEvidence(source, key, s, Confidence.LOW); } } } } } } } for (Map.Entry<String, Attributes> item : manifest.getEntries().entrySet()) { final String name = item.getKey(); source = "manifest: " + name; atts = item.getValue(); for (Entry<Object, Object> entry : atts.entrySet()) { final String key = entry.getKey().toString(); final String value = atts.getValue(key); if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_TITLE.toString())) { foundSomething = true; productEvidence.addEvidence(source, key, value, Confidence.MEDIUM); addMatchingValues(classInformation, value, productEvidence); } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VERSION.toString())) { foundSomething = true; versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM); } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR.toString())) { foundSomething = true; vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM); addMatchingValues(classInformation, value, vendorEvidence); } else if (key.equalsIgnoreCase(Attributes.Name.SPECIFICATION_TITLE.toString())) { foundSomething = true; productEvidence.addEvidence(source, key, value, Confidence.MEDIUM); addMatchingValues(classInformation, value, productEvidence); } } } if (specificationVersion != null && !hasImplementationVersion) { foundSomething = true; versionEvidence.addEvidence(source, "specification-version", specificationVersion, Confidence.HIGH); } } finally { if (jar != null) { jar.close(); } } return foundSomething; }
From source file:org.sablo.specification.WebComponentPackage.java
private static List<String> getWebEntrySpecNames(Manifest mf, String attributeName) { List<String> names = new ArrayList<String>(); for (Entry<String, Attributes> entry : mf.getEntries().entrySet()) { if ("true".equalsIgnoreCase((String) entry.getValue().get(new Attributes.Name(attributeName)))) { names.add(entry.getKey());/*w w w . ja v a 2 s .c o m*/ } } return names; }