Example usage for java.util.jar Attributes getValue

List of usage examples for java.util.jar Attributes getValue

Introduction

In this page you can find the example usage for java.util.jar Attributes getValue.

Prototype

public String getValue(Name name) 

Source Link

Document

Returns the value of the specified Attributes.Name, or null if the attribute was not found.

Usage

From source file:hudson.ClassicPluginStrategy.java

/**
 * Computes the classloader that takes the class masking into account.
 *
 * <p>/*from  w  ww.j  av  a  2 s .com*/
 * This mechanism allows plugins to have their own versions for libraries that core bundles.
 */
private ClassLoader getBaseClassLoader(Attributes atts, ClassLoader base) {
    String masked = atts.getValue("Mask-Classes");
    if (masked != null)
        base = new MaskingClassLoader(base, masked.trim().split("[ \t\r\n]+"));
    return base;
}

From source file:org.eclipse.ebr.maven.BundleMojo.java

private File generateFinalBundleManifest() throws MojoExecutionException {
    try {//from  w ww .  j  a  v  a 2 s.c  o  m
        File mfile = new File(outputDirectory, "META-INF/MANIFEST.MF");
        final InputStream is = new FileInputStream(mfile);
        Manifest mf;
        try {
            mf = new Manifest(is);
        } finally {
            is.close();
        }
        final Attributes attributes = mf.getMainAttributes();

        if (attributes.getValue(Name.MANIFEST_VERSION) == null) {
            attributes.put(Name.MANIFEST_VERSION, "1.0");
        }

        // shameless self-promotion
        attributes.putValue(CREATED_BY, "Eclipse Bundle Recipe Maven Plug-in");

        final String expandedVersion = getExpandedVersion();
        attributes.putValue(BUNDLE_VERSION, expandedVersion);

        mfile = getFinalBundleManifestFile();
        mfile.getParentFile().mkdirs();
        final BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(mfile));
        try {
            mf.write(os);
        } finally {
            os.close();
        }

        return mfile;
    } catch (final Exception e) {
        throw new MojoExecutionException("Error generating bundle manifest: " + e.getMessage(), e);
    }
}

From source file:org.eclipse.ebr.maven.BundleMojo.java

private File generateSourceBundleManifest() throws MojoExecutionException {
    try {/*from   w  ww.  j  a  v  a 2 s  .  c o  m*/
        generateSourceBundleL10nFile();

        final Manifest mf = new Manifest();
        final Attributes attributes = mf.getMainAttributes();

        if (attributes.getValue(Name.MANIFEST_VERSION) == null) {
            attributes.put(Name.MANIFEST_VERSION, "1.0");
        }

        final String expandedVersion = getExpandedVersion();
        attributes.putValue(BUNDLE_VERSION, expandedVersion);
        attributes.putValue(BUNDLE_MANIFESTVERSION, "2");
        attributes.putValue(BUNDLE_SYMBOLICNAME, getSourceBundleSymbolicName());
        attributes.putValue(BUNDLE_NAME, I18N_KEY_PREFIX + I18N_KEY_BUNDLE_NAME);
        attributes.putValue(BUNDLE_VENDOR, I18N_KEY_PREFIX + I18N_KEY_BUNDLE_VENDOR);
        //attributes.putValue(BUNDLE_LOCALIZATION, BUNDLE_LOCALIZATION_DEFAULT_BASENAME);
        attributes.putValue("Eclipse-SourceBundle",
                project.getArtifactId() + ";version=\"" + expandedVersion + "\";roots:=\".\"");
        attributes.putValue(CREATED_BY, "Eclipse Bundle Recipe Maven Plug-in");

        final File mfile = getSourceBundleManifestFile();
        mfile.getParentFile().mkdirs();
        final BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(mfile));
        try {
            mf.write(os);
        } finally {
            os.close();
        }

        return mfile;
    } catch (final Exception e) {
        throw new MojoExecutionException("Error generating source bundle manifest: " + e.getMessage(), e);
    }
}

From source file:org.universAAL.itests.IntegrationTest.java

/**
 * This method copies contents of target/classes to target/test-classes.
 * Thanks to that regular classes of given bundle can be used for testing
 * without a need to load the bundle from maven repository. It is very
 * important because in the maven build cycle "test" precedes "install". If
 * this method will not be invoked, when bundle does not exist in the maven
 * repository there is a deadlock - bundle cannot be tested because it is
 * not in the repo and bundle cannot be installed in the repo because tests
 * fail./*from   w w w  . j a  va 2s  .co  m*/
 *
 * Additionally method rewrites bundle manifest for purpose of adding
 * imports to packages related to itests bundle.
 *
 * @throws IOException
 *
 */
private void prepareClassesToTests() throws Exception {
    FileUtils.copyDirectory(new File("./target/classes"), new File("./target/test-classes"));
    File separatedArtifactDepsFile = new File(IntegrationTestConsts.SEPARATED_ARTIFACT_DEPS);
    if (separatedArtifactDepsFile.exists()) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(separatedArtifactDepsFile)));
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (!line.isEmpty()) {
                unzipInpuStream(new URL(line).openStream(), "target/test-classes");
            }
        }
    }

    Manifest bundleMf = new Manifest(new FileInputStream("./target/classes/META-INF/MANIFEST.MF"));
    Attributes mainAttribs = bundleMf.getMainAttributes();

    bundleSymbolicName = mainAttribs.getValue("Bundle-SymbolicName");
    bundleVersion = mainAttribs.getValue("Bundle-Version");
    bundleVersion = bundleVersion.replaceFirst("\\.SNAPSHOT", "-SNAPSHOT");

    mainAttribs.put(new Attributes.Name("Import-Package"),
            mainAttribs.getValue("Import-Package") + ",org.universAAL.itests,org.springframework.util");
    String dynamicImports = mainAttribs.getValue("DynamicImport-Package");
    if (dynamicImports == null) {
        dynamicImports = "*";
        mainAttribs.put(new Attributes.Name("DynamicImport-Package"), dynamicImports);
    }

    bundleMf.write(new FileOutputStream("./target/test-classes/META-INF/MANIFEST.MF"));
}

From source file:org.araqne.pkg.PackageManagerService.java

private String getBundleSymbolicName(File file) {
    JarFile jar = null;/*  ww  w  .  j av a 2s. com*/
    try {
        jar = new JarFile(file);
        Attributes attrs = jar.getManifest().getMainAttributes();
        // metadata can be added followed by semicolon (e.g. ;singleton)
        return attrs.getValue("Bundle-SymbolicName").split(";")[0].trim();
    } catch (IOException e) {
        logger.error("package manager: symbolic name not found", e);
        return null;
    } finally {
        if (jar != null)
            try {
                jar.close();
            } catch (IOException e) {
            }
    }
}

From source file:com.android.builder.internal.packaging.sign.SignatureExtension.java

/**
 * Adds / updates the signature for an entry. If this entry has no signature, or its digest
 * doesn't match the one in the signature file (or manifest), it will be updated.
 *
 * @param entry the entry/*from   ww w  . ja  v a2  s  .c o  m*/
 * @throws IOException failed to compute the entry's digest
 */
private void setDigestForEntry(@NonNull StoredEntry entry) throws IOException {
    String entryName = entry.getCentralDirectoryHeader().getName();
    byte[] entryDigestArray = mMessageDigest.digest(entry.read());
    String entryDigest = new String(Base64.encodeBase64(entryDigestArray), Charsets.US_ASCII);

    Attributes signatureAttributes = mSignatureFile.getEntries().get(entryName);
    if (signatureAttributes == null) {
        signatureAttributes = new Attributes();
        mSignatureFile.getEntries().put(entryName, signatureAttributes);
        mDirty = true;
    }

    if (!entryDigest.equals(signatureAttributes.getValue(mDigestAlgorithm.entryAttributeName))) {
        signatureAttributes.putValue(mDigestAlgorithm.entryAttributeName, entryDigest);
        mDirty = true;
    }

    /*
     * setAttribute will not mark the manifest as changed if the attribute is already there
     * and with the same value.
     */
    mManifestExtension.setAttribute(entryName, mDigestAlgorithm.entryAttributeName, entryDigest);
}

From source file:hudson.ClassicPluginStrategy.java

/**
 * Creates the classloader that can load all the specified jar files and delegate to the given parent.
 *//*from  ww  w.ja  va 2  s  . c o  m*/
protected ClassLoader createClassLoader(List<File> paths, ClassLoader parent, Attributes atts)
        throws IOException {
    if (atts != null) {
        String usePluginFirstClassLoader = atts.getValue("PluginFirstClassLoader");
        if (Boolean.valueOf(usePluginFirstClassLoader)) {
            PluginFirstClassLoader classLoader = new PluginFirstClassLoader();
            classLoader.setParentFirst(false);
            classLoader.setParent(parent);
            classLoader.addPathFiles(paths);
            return classLoader;
        }
    }

    AntClassLoader2 classLoader = new AntClassLoader2(parent);
    classLoader.addPathFiles(paths);
    return classLoader;
}

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 {/* ww  w .  j av a 2  s  .c  om*/
            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.apache.sling.osgi.obr.Resource.java

private Resource(URL resourceURL, Attributes attrs, long size) throws IOException {

    this.capabilities = new TreeMap<String, List<Object>>();
    this.requirements = new TreeMap<String, List<Object>>();

    this.resourceURL = resourceURL;
    this.name = this.getName(resourceURL.getPath());

    this.presentationName = attrs.getValue(Constants.BUNDLE_NAME);
    this.symbolicName = attrs.getValue(Constants.BUNDLE_SYMBOLICNAME);
    this.uri = attrs.getValue(Constants.BUNDLE_UPDATELOCATION);
    this.version = attrs.getValue(Constants.BUNDLE_VERSION);
    this.description = attrs.getValue(Constants.BUNDLE_DESCRIPTION);
    this.size = size;
    this.documentation = attrs.getValue(Constants.BUNDLE_DOCURL);
    this.copyright = attrs.getValue(Constants.BUNDLE_COPYRIGHT);
    this.vendor = attrs.getValue(Constants.BUNDLE_VENDOR);
    this.contact = attrs.getValue(Constants.BUNDLE_CONTACTADDRESS);
    this.license = attrs.getValue("Bundle-LicenseURL");
    this.source = attrs.getValue("Bundle-SourceURL");
    String categoryString = attrs.getValue(Constants.BUNDLE_CATEGORY);

    // require symbolicName
    if (this.symbolicName == null || this.symbolicName.length() == 0) {
        throw new IOException("Missing Bundle-SymbolicName, not a valid Bundle");
    }//w  w  w. jav a2s  .c o m

    // default version valu
    if (this.version == null || this.version.length() == 0) {
        this.version = "0.0.0";
    }

    // default value for the id
    this.id = this.symbolicName + ":" + this.version;

    this.categories = new TreeSet<String>();
    if (categoryString != null) {
        String[] catList = categoryString.split(",");
        for (int i = 0; catList != null && i < catList.length; i++) {
            String cat = catList[i].trim();
            if (cat.length() > 0) {
                this.categories.add(cat);
            }
        }
    }

    this.addCapability(new BundleCapability(attrs));

    this.addExportedServices(attrs.getValue(Constants.EXPORT_SERVICE));
    this.addExportedPackages(attrs.getValue(Constants.EXPORT_PACKAGE));
    this.addImportedServices(attrs.getValue(Constants.IMPORT_SERVICE));
    this.addImportedPackages(attrs.getValue(Constants.IMPORT_PACKAGE));

    // parse bundle specifications of Assembly Bundle
    this.addBundleSpecs(attrs.getValue(ASSEMBLY_BUNDLES));
}

From source file:massbank.admin.VersionManager.java

/**
 * jart@Co?[W?//w w  w  . j av a2 s.  co  m
 * @param path t@CpX
 * @return o?[W?
 */
private VersionInfo getVerJarFile(String path) {
    VersionInfo verInfo = null;
    try {
        // }jtFXgo?[W?
        JarFile jar = new JarFile(new File(path));
        Manifest manifest = jar.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        String item1 = attributes.getValue("Implementation-Version");
        if (item1 == null) {
            item1 = "-";
        }
        // Xy?[X?o?[Wt?o
        String[] item2 = item1.trim().split(" ");
        String ver = item2[0];
        String date = "-";
        if (item2.length > 1) {
            date = item2[1];
        }
        verInfo = new VersionInfo(null, ver, date);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return verInfo;
}