Example usage for java.util.jar Attributes put

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

Introduction

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

Prototype

public Object put(Object name, Object value) 

Source Link

Document

Associates the specified value with the specified attribute name (key) in this Map.

Usage

From source file:com.thoughtworks.go.plugin.infra.plugininfo.GoPluginOSGiManifestTest.java

private void addHeaderToManifest(String header, String value) throws IOException {
    FileInputStream manifestInputStream = new FileInputStream(manifestFile);
    Manifest manifest = new Manifest(manifestInputStream);
    Attributes entries = manifest.getMainAttributes();
    entries.put(new Attributes.Name(header), value);

    FileOutputStream manifestOutputStream = new FileOutputStream(manifestFile, false);
    manifest.write(manifestOutputStream);
    manifestOutputStream.close();/*w ww  .j  a v  a  2 s  .co m*/
    manifestInputStream.close();
}

From source file:io.fabric8.vertx.maven.plugin.utils.PackageHelper.java

/**
 *
 *///w  ww  . ja va  2 s  .  c o m
protected void generateManifest() {
    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    attributes.put(Attributes.Name.MAIN_CLASS, mainClass);
    //This is a typical situation when application is launched with custom launcher
    if (mainVerticle != null) {
        attributes.put(MAIN_VERTICLE, mainVerticle);
    }

    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        manifest.write(bout);
        bout.close();
        byte[] bytes = bout.toByteArray();
        //TODO: merge existing manifest with current one
        this.archive.setManifest(new ByteArrayAsset(bytes));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.facebook.buck.java.JarDirectoryStepTest.java

private Manifest createManifestWithExampleSection(Map<String, String> attributes) {
    Manifest manifest = new Manifest();
    Attributes attrs = new Attributes();
    for (Map.Entry<String, String> stringStringEntry : attributes.entrySet()) {
        attrs.put(new Attributes.Name(stringStringEntry.getKey()), stringStringEntry.getValue());
    }//from  www  . java2 s.  c o m
    manifest.getEntries().put("example", attrs);
    return manifest;
}

From source file:fr.inria.atlanmod.neo4emf.neo4jresolver.runtimes.internal.Neo4JRuntimesManager.java

public void initializeRuntimeMetadata(AbstractNeo4jRuntimeInstaller installer, IPath path) {
    File dirFile = path.toFile();
    File manifestFile = path.append(META_INF).append(MANIFEST_MF).toFile();
    FileOutputStream outputStream = null;
    try {//from w w w.jav a  2  s .co  m
        FileUtils.forceMkdir(manifestFile.getParentFile());
        outputStream = new FileOutputStream(manifestFile);
        Manifest manifest = new Manifest();
        Attributes atts = manifest.getMainAttributes();
        atts.put(Attributes.Name.MANIFEST_VERSION, "1.0");
        atts.putValue(BUNDLE_MANIFEST_VERSION, "2");
        atts.putValue(BUNDLE_NAME, installer.getName());
        atts.putValue(BUNDLE_SYMBOLIC_NAME, installer.getId());
        atts.putValue(BUNDLE_VERSION, installer.getVersion());
        atts.putValue(BUNDLE_CLASS_PATH, buildJarFilesList(dirFile));
        atts.putValue(EXPORT_PACKAGE, buildPackagesList(dirFile));
        manifest.write(outputStream);
        load();
    } catch (Exception e) {
        Logger.log(Logger.SEVERITY_ERROR, e);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:com.thoughtworks.go.plugin.infra.plugininfo.GoPluginOSGiManifest.java

private void updateManifest(String symbolicName, String classPath, String bundleActivator) throws IOException {
    try (FileInputStream manifestInputStream = new FileInputStream(manifestLocation)) {
        Manifest manifest = new Manifest(manifestInputStream);
        Attributes mainAttributes = manifest.getMainAttributes();

        if (mainAttributes.containsKey(new Attributes.Name(BUNDLE_SYMBOLICNAME))) {
            descriptor.markAsInvalid(Arrays.asList(
                    "Plugin JAR is invalid. MANIFEST.MF already contains header: " + BUNDLE_SYMBOLICNAME),
                    null);/*  w w  w  .j  ava2  s .  c  o m*/
            return;
        }
        mainAttributes.put(new Attributes.Name(BUNDLE_SYMBOLICNAME), symbolicName);
        mainAttributes.put(new Attributes.Name(BUNDLE_CLASSPATH), classPath);
        mainAttributes.put(new Attributes.Name(BUNDLE_ACTIVATOR), bundleActivator);

        descriptor.updateBundleInformation(symbolicName, classPath, bundleActivator);

        try (FileOutputStream manifestOutputStream = new FileOutputStream(manifestLocation)) {
            manifest.write(manifestOutputStream);
        }
    }
}

From source file:org.eclipse.gemini.blueprint.test.AbstractOnTheFlyBundleCreatorTests.java

/**
 * Creates the default manifest in case none if found on the disk. By
 * default, the imports are synthetised based on the test class bytecode.
 * //from w w w .  j ava  2  s .  c om
 * @return default manifest for the jar created on the fly
 */
protected Manifest createDefaultManifest() {
    Manifest manifest = new Manifest();
    Attributes attrs = manifest.getMainAttributes();

    // manifest versions
    attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    attrs.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");

    String description = getName() + "-" + getClass().getName();
    // name/description
    attrs.putValue(Constants.BUNDLE_NAME, "TestBundle-" + description);
    attrs.putValue(Constants.BUNDLE_SYMBOLICNAME, "TestBundle-" + description);
    attrs.putValue(Constants.BUNDLE_DESCRIPTION, "on-the-fly test bundle");

    // activator
    attrs.putValue(Constants.BUNDLE_ACTIVATOR, JUnitTestActivator.class.getName());

    // add Import-Package entry
    addImportPackage(manifest);

    if (logger.isDebugEnabled())
        logger.debug("Created manifest:" + manifest.getMainAttributes().entrySet());
    return manifest;
}

From source file:net.cliseau.composer.javacor.MissingToolException.java

/**
 * Create a JAR file for startup of the unit.
 *
 * The created JAR file contains all the specified archive files and contains a
 * manifest which in particular determines the class path for the JAR file.
 * All files in startupArchiveFileNames are deleted during the execution of
 * this method./* w ww  .  j a v a  2 s.co m*/
 *
 * @param fileName Name of the file to write the result to.
 * @param startupArchiveFileNames Names of files to include in the JAR file.
 * @param startupDependencies Names of classpath entries to include in the JAR file classpath.
 * @exception IOException Thrown if file operations fail, such as creating the JAR file or reading from the input file(s).
 */
private void createJAR(final String fileName, final Collection<String> startupArchiveFileNames,
        final Collection<String> startupDependencies) throws IOException, InvalidConfigurationException {
    // Code inspired by:
    //   http://www.java2s.com/Code/Java/File-Input-Output/CreateJarfile.htm
    //   http://www.massapi.com/class/java/util/jar/Manifest.java.html

    // construct manifest with appropriate "Class-path" property
    Manifest starterManifest = new Manifest();
    Attributes starterAttributes = starterManifest.getMainAttributes();
    // Remark for those who read this code to learn something:
    // If one forgets to set the MANIFEST_VERSION attribute, then
    // silently *nothing* (except for a line break) will be written
    // to the JAR file manifest!
    starterAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    starterAttributes.put(Attributes.Name.MAIN_CLASS, getStartupName());
    starterAttributes.put(Attributes.Name.CLASS_PATH,
            StringUtils.join(startupDependencies, manifestClassPathSeparator));

    // create output JAR file
    FileOutputStream fos = new FileOutputStream(fileName);
    JarOutputStream jos = new JarOutputStream(fos, starterManifest);

    // add the entries for the starter archive's files
    for (String archFileName : startupArchiveFileNames) {
        File startupArchiveFile = new File(archFileName);
        JarEntry startupEntry = new JarEntry(startupArchiveFile.getName());
        startupEntry.setTime(startupArchiveFile.lastModified());
        jos.putNextEntry(startupEntry);

        // copy the content of the starter archive's file
        // TODO: if we used Apache Commons IO 2.1, then the following
        //       code block could be simplified as:
        //       FileUtils.copyFile(startupArchiveFile, jos);
        FileInputStream fis = new FileInputStream(startupArchiveFile);
        byte buffer[] = new byte[1024 /*bytes*/];
        while (true) {
            int nRead = fis.read(buffer, 0, buffer.length);
            if (nRead <= 0)
                break;
            jos.write(buffer, 0, nRead);
        }
        fis.close();
        // end of FileUtils.copyFile() substitution code
        jos.closeEntry();

        startupArchiveFile.delete(); // cleanup the disk a bit
    }

    jos.close();
    fos.close();
}

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.// w ww. j a va  2  s.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.apache.felix.deploymentadmin.itest.util.DPSigner.java

private Manifest createSignatureFile(Manifest manifest) throws IOException {
    byte[] mfRawBytes;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        manifest.write(baos);//from w w w .j a  va  2  s.c  o  m
        mfRawBytes = baos.toByteArray();
    }

    Manifest sf = new Manifest();
    Attributes sfMain = sf.getMainAttributes();
    Map<String, Attributes> sfEntries = sf.getEntries();

    sfMain.put(Attributes.Name.SIGNATURE_VERSION, "1.0");
    sfMain.putValue("Created-By", "Apache Felix DeploymentPackageBuilder");
    sfMain.putValue(m_digestAlg + "-Digest-Manifest", calculateDigest(mfRawBytes));
    sfMain.putValue(m_digestAlg + "-Digest-Manifest-Main-Attribute",
            calculateDigest(getRawBytesMainAttributes(manifest)));

    for (Entry<String, Attributes> entry : manifest.getEntries().entrySet()) {
        String name = entry.getKey();
        byte[] entryData = getRawBytesAttributes(entry.getValue());

        sfEntries.put(name, getDigestAttributes(entryData));
    }
    return sf;
}

From source file:org.echocat.nodoodle.transport.HandlerPacker.java

protected Manifest createManifest(String dataFileName, Collection<String> dependencyFileNames) {
    if (dataFileName == null) {
        throw new NullPointerException();
    }// ww w  .  j  a va  2  s  . c  om
    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;
}