Example usage for java.util.jar Manifest Manifest

List of usage examples for java.util.jar Manifest Manifest

Introduction

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

Prototype

public Manifest() 

Source Link

Document

Constructs a new, empty Manifest.

Usage

From source file:com.facebook.buck.jvm.java.DefaultJavaLibraryIntegrationTest.java

@Test
public void shouldIncludeUserSuppliedManifestIfProvided() throws IOException {
    setUpProjectWorkspaceForScenario("manifest");

    Manifest m = new Manifest();
    Attributes attrs = new Attributes();
    attrs.putValue("Data", "cheese");
    m.getEntries().put("Example", attrs);
    m.write(System.out);/*from ww  w. j a  v  a  2  s .  co  m*/

    Path path = workspace.buildAndReturnOutput("//:library");

    try (InputStream is = Files.newInputStream(path); JarInputStream jis = new JarInputStream(is)) {
        Manifest manifest = jis.getManifest();
        String value = manifest.getEntries().get("Example").getValue("Data");
        assertEquals("cheese", value);
    }
}

From source file:org.apache.phoenix.end2end.UserDefinedFunctionsIT.java

/**
 * Compiles the test class with bogus code into a .class file.
 * Upon finish, the bogus jar will be left at dynamic.jar.dir location
 *///  w ww. j a  va 2s  .  co m
private static void compileTestClass(String className, String program, int counter) throws Exception {
    String javaFileName = className + ".java";
    File javaFile = new File(javaFileName);
    String classFileName = className + ".class";
    File classFile = new File(classFileName);
    String jarName = "myjar" + counter + ".jar";
    String jarPath = "." + File.separator + jarName;
    File jarFile = new File(jarPath);
    try {
        String packageName = "org.apache.phoenix.end2end";
        FileOutputStream fos = new FileOutputStream(javaFileName);
        fos.write(program.getBytes());
        fos.close();

        JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
        int result = jc.run(null, null, null, javaFileName);
        assertEquals(0, result);

        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        FileOutputStream jarFos = new FileOutputStream(jarPath);
        JarOutputStream jarOutputStream = new JarOutputStream(jarFos, manifest);
        String pathToAdd = packageName.replace('.', '/') + '/';
        String jarPathStr = new String(pathToAdd);
        Set<String> pathsInJar = new HashSet<String>();

        while (pathsInJar.add(jarPathStr)) {
            int ix = jarPathStr.lastIndexOf('/', jarPathStr.length() - 2);
            if (ix < 0) {
                break;
            }
            jarPathStr = jarPathStr.substring(0, ix);
        }
        for (String pathInJar : pathsInJar) {
            jarOutputStream.putNextEntry(new JarEntry(pathInJar));
            jarOutputStream.closeEntry();
        }

        jarOutputStream.putNextEntry(new JarEntry(pathToAdd + classFile.getName()));
        byte[] allBytes = new byte[(int) classFile.length()];
        FileInputStream fis = new FileInputStream(classFile);
        fis.read(allBytes);
        fis.close();
        jarOutputStream.write(allBytes);
        jarOutputStream.closeEntry();
        jarOutputStream.close();
        jarFos.close();

        assertTrue(jarFile.exists());
        Connection conn = driver.connect(url, EMPTY_PROPS);
        Statement stmt = conn.createStatement();
        stmt.execute("add jars '" + jarFile.getAbsolutePath() + "'");
    } finally {
        if (javaFile != null)
            javaFile.delete();
        if (classFile != null)
            classFile.delete();
        if (jarFile != null)
            jarFile.delete();
    }
}

From source file:org.olat.core.util.i18n.I18nManager.java

/**
 * Create a jar file that contains the translations for the given languages.
 * <p>//from w w w  .j  av a  2  s. c  o  m
 * Note that this file is created in a temporary place in olatdata/tmp. It is
 * in the responsibility of the caller of this method to remove the file when
 * not needed anymore.
 * 
 * @param languageKeys
 * @param fileName the name of the file.
 * @return The file handle to the created file or NULL if no such file could
 *         be created (e.g. there already exists a file with this file name)
 */
public File createLanguageJarFile(Collection<String> languageKeys, String fileName) {
    // Create file olatdata temporary directory
    File file = new File(WebappHelper.getTmpDir() + "/" + fileName);
    file.getParentFile().mkdirs();

    FileOutputStream stream = null;
    JarOutputStream out = null;
    try {
        // Open stream for jar file
        stream = new FileOutputStream(file);
        out = new JarOutputStream(stream, new Manifest());
        // Use now as last modified date of resources
        long now = System.currentTimeMillis();
        // Add all languages
        for (String langKey : languageKeys) {
            Locale locale = getLocaleOrNull(langKey);
            // Add all bundles in the current language
            for (String bundleName : I18nModule.getBundleNamesContainingI18nFiles()) {
                Properties propertyFile = getPropertiesWithoutResolvingRecursively(locale, bundleName);
                String entryFileName = bundleName.replace(".", "/") + "/" + I18N_DIRNAME + "/"
                        + buildI18nFilename(locale);
                // Create jar entry for this path, name and last modified
                JarEntry jarEntry = new JarEntry(entryFileName);
                jarEntry.setTime(now);
                // Write properties to jar file
                out.putNextEntry(jarEntry);
                propertyFile.store(out, null);
                if (isLogDebugEnabled()) {
                    logDebug("Adding file::" + entryFileName + " + to jar", null);
                }
            }
        }
        logDebug("Finished writing jar file::" + file.getAbsolutePath(), null);
    } catch (Exception e) {
        logError("Could not write jar file", e);
        return null;
    } finally {
        try {
            out.close();
            stream.close();
        } catch (IOException e) {
            logError("Could not close stream of jar file", e);
            return null;
        }
    }
    return file;
}