Example usage for java.util.jar JarEntry JarEntry

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

Introduction

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

Prototype

public JarEntry(JarEntry je) 

Source Link

Document

Creates a new JarEntry with fields taken from the specified JarEntry object.

Usage

From source file:org.apache.sling.osgi.obr.Repository.java

File spoolModified(InputStream ins) throws IOException {
    JarInputStream jis = new JarInputStream(ins);

    // immediately handle the manifest
    JarOutputStream jos;//w w  w. j  av  a2s. co m
    Manifest manifest = jis.getManifest();
    if (manifest == null) {
        throw new IOException("Missing Manifest !");
    }

    String symbolicName = manifest.getMainAttributes().getValue("Bundle-SymbolicName");
    if (symbolicName == null || symbolicName.length() == 0) {
        throw new IOException("Missing Symbolic Name in Manifest !");
    }

    String version = manifest.getMainAttributes().getValue("Bundle-Version");
    Version v = Version.parseVersion(version);
    if (v.getQualifier().indexOf("SNAPSHOT") >= 0) {
        String tStamp;
        synchronized (DATE_FORMAT) {
            tStamp = DATE_FORMAT.format(new Date());
        }
        version = v.getMajor() + "." + v.getMinor() + "." + v.getMicro() + "."
                + v.getQualifier().replaceAll("SNAPSHOT", tStamp);
        manifest.getMainAttributes().putValue("Bundle-Version", version);
    }

    File bundle = new File(this.repoLocation, symbolicName + "-" + v + ".jar");
    OutputStream out = null;
    try {
        out = new FileOutputStream(bundle);
        jos = new JarOutputStream(out, manifest);

        jos.setMethod(JarOutputStream.DEFLATED);
        jos.setLevel(Deflater.BEST_COMPRESSION);

        JarEntry entryIn = jis.getNextJarEntry();
        while (entryIn != null) {
            JarEntry entryOut = new JarEntry(entryIn.getName());
            entryOut.setTime(entryIn.getTime());
            entryOut.setComment(entryIn.getComment());
            jos.putNextEntry(entryOut);
            if (!entryIn.isDirectory()) {
                spool(jis, jos);
            }
            jos.closeEntry();
            jis.closeEntry();
            entryIn = jis.getNextJarEntry();
        }

        // close the JAR file now to force writing
        jos.close();

    } finally {
        IOUtils.closeQuietly(out);
    }

    return bundle;
}

From source file:org.apache.solr.core.TestCoreContainer.java

@Test
public void testSharedLib() throws Exception {
    Path tmpRoot = createTempDir("testSharedLib");

    File lib = new File(tmpRoot.toFile(), "lib");
    lib.mkdirs();//  ww  w . j a va2  s. c  o m

    JarOutputStream jar1 = new JarOutputStream(new FileOutputStream(new File(lib, "jar1.jar")));
    jar1.putNextEntry(new JarEntry("defaultSharedLibFile"));
    jar1.closeEntry();
    jar1.close();

    File customLib = new File(tmpRoot.toFile(), "customLib");
    customLib.mkdirs();

    JarOutputStream jar2 = new JarOutputStream(new FileOutputStream(new File(customLib, "jar2.jar")));
    jar2.putNextEntry(new JarEntry("customSharedLibFile"));
    jar2.closeEntry();
    jar2.close();

    final CoreContainer cc1 = init(tmpRoot, "<solr></solr>");
    try {
        cc1.loader.openResource("defaultSharedLibFile").close();
    } finally {
        cc1.shutdown();
    }

    final CoreContainer cc2 = init(tmpRoot, "<solr><str name=\"sharedLib\">lib</str></solr>");
    try {
        cc2.loader.openResource("defaultSharedLibFile").close();
    } finally {
        cc2.shutdown();
    }

    final CoreContainer cc3 = init(tmpRoot, "<solr><str name=\"sharedLib\">customLib</str></solr>");
    try {
        cc3.loader.openResource("customSharedLibFile").close();
    } finally {
        cc3.shutdown();
    }
}

From source file:org.apache.sqoop.integration.connectorloading.ClasspathTest.java

private void addFileToJar(File source, JarOutputStream target) throws Exception {
    JarEntry entry = new JarEntry(source.getName());
    entry.setTime(source.lastModified());
    target.putNextEntry(entry);/*from  w w  w  . j av a 2s  .  co  m*/
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));

    long bufferSize = source.length();
    if (bufferSize < Integer.MIN_VALUE || bufferSize > Integer.MAX_VALUE) {
        throw new RuntimeException("file to large to be added to jar");
    }

    byte[] buffer = new byte[(int) bufferSize];
    while (true) {
        int count = in.read(buffer);
        if (count == -1)
            break;
        target.write(buffer, 0, count);
    }
    target.closeEntry();
    if (in != null)
        in.close();
}

From source file:org.apache.sysml.utils.lite.BuildLite.java

/**
 * Build a lite jar based on the consolidated class names.
 * /*from   www  . ja  v a2  s .  c om*/
 * @param consolidateClassPathNames
 *            the consolidated class names
 * @throws IOException
 *             if an IOException occurs
 */
private static void createJarFromConsolidatedClassPathNames(Set<String> consolidateClassPathNames)
        throws IOException {
    System.out.println("\nCreating " + liteJarLocation + " file");
    ClassLoader cl = BuildLite.class.getClassLoader();

    Manifest mf = new Manifest();
    Attributes attr = mf.getMainAttributes();
    attr.putValue("" + Attributes.Name.MANIFEST_VERSION, "1.0");

    File file = new File(liteJarLocation);
    try (FileOutputStream fos = new FileOutputStream(file);
            JarOutputStream jos = new JarOutputStream(fos, mf)) {
        int numFilesWritten = 0;
        for (String classPathName : consolidateClassPathNames) {
            writeMessage(classPathName, ++numFilesWritten);
            InputStream is = cl.getResourceAsStream(classPathName);
            byte[] bytes = IOUtils.toByteArray(is);

            JarEntry je = new JarEntry(classPathName);
            jos.putNextEntry(je);
            jos.write(bytes);
        }

        writeIdentifierFileToLiteJar(jos, ++numFilesWritten);
        writeAdditionalResourcesToJar(jos, numFilesWritten);
    }

}

From source file:org.apache.sysml.utils.lite.BuildLite.java

/**
 * Write an identifier file to the lite jar that can be used to identify
 * that the lite jar is being used.//from  w  ww  .  j a  v a 2s .co  m
 * 
 * @param jos
 *            output stream to the jar being written
 * @param numFilesWritten
 *            the number of files written to the jar so far
 * @throws IOException
 *             if an IOException occurs
 */
private static void writeIdentifierFileToLiteJar(JarOutputStream jos, int numFilesWritten) throws IOException {
    writeMessage(LITE_JAR_IDENTIFIER_FILE, numFilesWritten);
    JarEntry je = new JarEntry(LITE_JAR_IDENTIFIER_FILE);
    jos.putNextEntry(je);
    String created = "Created " + (new Date());
    String userName = System.getProperty("user.name");
    if (userName != null) {
        created = created + " by " + userName;
    }
    jos.write(created.getBytes());
}

From source file:org.apache.sysml.utils.lite.BuildLite.java

/**
 * Write the additional resources to the jar.
 * //from   w w w .  j  a va  2 s .c o  m
 * @param jos
 *            output stream to the jar being written
 * @param numFilesWritten
 *            the number of files written to the jar so far
 * @throws IOException
 *             if an IOException occurs
 */
private static void writeAdditionalResourcesToJar(JarOutputStream jos, int numFilesWritten) throws IOException {
    for (String resource : additionalResources) {
        writeMessage(resource, ++numFilesWritten);
        JarEntry je = new JarEntry(resource);
        jos.putNextEntry(je);
        ClassLoader cl = BuildLite.class.getClassLoader();
        InputStream is = cl.getResourceAsStream(resource);
        byte[] bytes = IOUtils.toByteArray(is);
        jos.write(bytes);
    }
}

From source file:org.bimserver.plugins.VirtualFile.java

private void createJar(JarOutputStream jarOutputStream) throws IOException {
    for (VirtualFile virtualFile : files.values()) {
        if (virtualFile.isDirectory()) {
            virtualFile.createJar(jarOutputStream);
        } else {//from   w  w w. jav a  2  s .  c o  m
            JarEntry jarEntry = new JarEntry(virtualFile.getName().replace(File.separator, "/"));
            jarOutputStream.putNextEntry(jarEntry);
            InputStream inputStream = virtualFile.openInputStream();
            IOUtils.copy(inputStream, jarOutputStream);
            inputStream.close();
        }
    }
}

From source file:org.bonitasoft.engine.io.IOUtil.java

public static byte[] generateJar(final Map<String, byte[]> resources) throws IOException {
    if (resources == null || resources.isEmpty()) {
        final String message = "No resources available";
        throw new IOException(message);
    }/* w w  w.j  a  v a  2  s  . c o m*/

    ByteArrayOutputStream baos = null;
    JarOutputStream jarOutStream = null;
    try {
        baos = new ByteArrayOutputStream();
        jarOutStream = new JarOutputStream(new BufferedOutputStream(baos));
        for (final Map.Entry<String, byte[]> resource : resources.entrySet()) {
            jarOutStream.putNextEntry(new JarEntry(resource.getKey()));
            jarOutStream.write(resource.getValue());
        }
        jarOutStream.flush();
        baos.flush();
    } finally {
        if (jarOutStream != null) {
            jarOutStream.close();
        }
        if (baos != null) {
            baos.close();
        }
    }

    return baos.toByteArray();
}

From source file:org.cloudata.core.parallel.hadoop.CloudataMapReduceUtil.java

private static Path makeJarToHDFS(FileSystem fs, Path parentPath, File file) throws IOException {
    Path path = new Path(parentPath, file.getName() + ".jar");

    JarOutputStream out = new JarOutputStream(fs.create(path));
    out.putNextEntry(new JarEntry(file.getName()));

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

    byte[] buf = new byte[1024];

    try {//  www  . ja  v a2  s.c  om
        int readBytes = 0;
        while ((readBytes = in.read(buf)) > 0) {
            out.write(buf, 0, readBytes);
        }
    } finally {
        if (out != null) {
            out.close();
        }

        if (in != null) {
            in.close();
        }
    }

    return path;
}

From source file:org.codehaus.mojo.cassandra.AbstractCassandraMojo.java

/**
 * Create a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry for
 * all classpath elements. Copied from surefire (ForkConfiguration#createJar())
 *
 * @param jarFile   The jar file to create/update
 * @param mainClass The main class to run.
 * @throws java.io.IOException if something went wrong.
 *//*from   ww w .  j  ava  2s .  c  o  m*/
protected void createCassandraJar(File jarFile, String mainClass, File cassandraDir) throws IOException {
    File conf = new File(cassandraDir, "conf");
    FileOutputStream fos = null;
    JarOutputStream jos = null;
    try {
        fos = new FileOutputStream(jarFile);
        jos = new JarOutputStream(fos);
        jos.setLevel(JarOutputStream.STORED);
        jos.putNextEntry(new JarEntry("META-INF/MANIFEST.MF"));

        Manifest man = new Manifest();

        // we can't use StringUtils.join here since we need to add a '/' to
        // the end of directory entries - otherwise the jvm will ignore them.
        StringBuilder cp = new StringBuilder();
        cp.append(new URL(conf.toURI().toASCIIString()).toExternalForm());
        cp.append(' ');
        getLog().debug("Adding plugin artifact: " + ArtifactUtils.versionlessKey(pluginArtifact)
                + " to the classpath");
        cp.append(new URL(pluginArtifact.getFile().toURI().toASCIIString()).toExternalForm());
        cp.append(' ');

        for (Artifact artifact : this.pluginDependencies) {
            getLog().debug("Adding plugin dependency artifact: " + ArtifactUtils.versionlessKey(artifact)
                    + " to the classpath");
            // NOTE: if File points to a directory, this entry MUST end in '/'.
            cp.append(new URL(artifact.getFile().toURI().toASCIIString()).toExternalForm());
            cp.append(' ');
        }

        if (addMainClasspath || addTestClasspath) {
            if (addTestClasspath) {
                getLog().debug("Adding: " + testClassesDirectory + " to the classpath");
                cp.append(new URL(testClassesDirectory.toURI().toASCIIString()).toExternalForm());
                cp.append(' ');
            }
            if (addMainClasspath) {
                getLog().debug("Adding: " + classesDirectory + " to the classpath");
                cp.append(new URL(classesDirectory.toURI().toASCIIString()).toExternalForm());
                cp.append(' ');
            }
            for (Artifact artifact : (Set<Artifact>) this.project.getArtifacts()) {
                if ("jar".equals(artifact.getType()) && !Artifact.SCOPE_PROVIDED.equals(artifact.getScope())
                        && (!Artifact.SCOPE_TEST.equals(artifact.getScope()) || addTestClasspath)) {
                    getLog().debug("Adding dependency: " + ArtifactUtils.versionlessKey(artifact)
                            + " to the classpath");
                    // NOTE: if File points to a directory, this entry MUST end in '/'.
                    cp.append(new URL(artifact.getFile().toURI().toASCIIString()).toExternalForm());
                    cp.append(' ');
                }
            }
        }

        man.getMainAttributes().putValue("Manifest-Version", "1.0");
        man.getMainAttributes().putValue("Class-Path", cp.toString().trim());
        man.getMainAttributes().putValue("Main-Class", mainClass);

        man.write(jos);
    } finally {
        IOUtil.close(jos);
        IOUtil.close(fos);
    }
}