Search in sources :

Example 6 with JarArchiveEntry

use of org.apache.commons.compress.archivers.jar.JarArchiveEntry in project tomee by apache.

the class MonkeyTest method prepareProject.

private File prepareProject() throws IOException {
    final File target = new File("target/MonkeyTest_run" + System.currentTimeMillis() + "/mvn/target");
    target.mkdirs();
    final File classes = new File(target, "classes");
    classes.mkdirs();
    writeBinary(classes, "target/test-classes/test/patch/MyMain.class", "test/patch/MyMain.class");
    writeBinary(classes, "target/test-classes/test/patch/foo/Another.class", "test/patch/foo/Another.class");
    final File tomee = new File(target, "tomee");
    final File lib = new File(tomee, "lib");
    lib.mkdirs();
    // create the jar to patch, it is invalid but when patched it should work
    JarArchiveOutputStream stream = null;
    try {
        stream = new JarArchiveOutputStream(new FileOutputStream(new File(lib, "t.jar")));
        stream.putArchiveEntry(new JarArchiveEntry("test/patch/MyMain.class"));
        stream.write("invalid".getBytes());
        stream.closeArchiveEntry();
        stream.putArchiveEntry(new JarArchiveEntry("test/patch/foo/Another.class"));
        stream.write("invalid-too".getBytes());
        stream.closeArchiveEntry();
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IO.close(stream);
    }
    return tomee;
}
Also used : JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) JarArchiveOutputStream(org.apache.commons.compress.archivers.jar.JarArchiveOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 7 with JarArchiveEntry

use of org.apache.commons.compress.archivers.jar.JarArchiveEntry in project incubator-systemml by apache.

the class BuildLite method getAllClassesInJar.

/**
 * Obtain a list of all classes in a jar file corresponding to a referenced
 * class.
 *
 * @param classInJarFile
 * @return list of all the commons-math3 classes in the referenced
 *         commons-math3 jar file
 * @throws IOException
 *             if an IOException occurs
 * @throws ClassNotFoundException
 *             if a ClassNotFoundException occurs
 */
private static List<String> getAllClassesInJar(Class<?> classInJarFile) throws IOException, ClassNotFoundException {
    List<String> classPathNames = new ArrayList<>();
    String jarLocation = classInJarFile.getProtectionDomain().getCodeSource().getLocation().getPath();
    File f = new File(jarLocation);
    try (FileInputStream fis = new FileInputStream(f);
        JarArchiveInputStream jais = new JarArchiveInputStream(fis)) {
        while (true) {
            JarArchiveEntry jae = jais.getNextJarEntry();
            if (jae == null) {
                break;
            }
            String name = jae.getName();
            if (name.endsWith(".class")) {
                classPathNames.add(name);
            }
        }
    }
    String jarName = jarLocation.substring(jarLocation.lastIndexOf("/") + 1);
    addClassPathNamesToJarsAndClasses(jarName, classPathNames);
    return classPathNames;
}
Also used : JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

JarArchiveEntry (org.apache.commons.compress.archivers.jar.JarArchiveEntry)7 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)3 JarArchiveInputStream (org.apache.commons.compress.archivers.jar.JarArchiveInputStream)3 JarArchiveOutputStream (org.apache.commons.compress.archivers.jar.JarArchiveOutputStream)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)1 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 RemoteServer (org.apache.openejb.config.RemoteServer)1