Search in sources :

Example 1 with JarArchiveOutputStream

use of org.apache.commons.compress.archivers.jar.JarArchiveOutputStream in project hive by apache.

the class CompileProcessor method compile.

@VisibleForTesting
/**
   * Method converts statement into a file, compiles the file and then packages the file.
   * @param ss
   * @return Response code of 0 for success 1 for failure
   * @throws CompileProcessorException
   */
CommandProcessorResponse compile(SessionState ss) throws CompileProcessorException {
    Project proj = new Project();
    String ioTempDir = System.getProperty(IO_TMP_DIR);
    File ioTempFile = new File(ioTempDir);
    if (!ioTempFile.exists()) {
        throw new CompileProcessorException(ioTempDir + " does not exists");
    }
    if (!ioTempFile.isDirectory() || !ioTempFile.canWrite()) {
        throw new CompileProcessorException(ioTempDir + " is not a writable directory");
    }
    Groovyc g = new Groovyc();
    long runStamp = System.currentTimeMillis();
    String jarId = myId + "_" + runStamp;
    g.setProject(proj);
    Path sourcePath = new Path(proj);
    File destination = new File(ioTempFile, jarId + "out");
    g.setDestdir(destination);
    File input = new File(ioTempFile, jarId + "in");
    sourcePath.setLocation(input);
    g.setSrcdir(sourcePath);
    input.mkdir();
    File fileToWrite = new File(input, this.named);
    try {
        Files.write(this.code, fileToWrite, Charset.forName("UTF-8"));
    } catch (IOException e1) {
        throw new CompileProcessorException("writing file", e1);
    }
    destination.mkdir();
    try {
        g.execute();
    } catch (BuildException ex) {
        throw new CompileProcessorException("Problem compiling", ex);
    }
    File testArchive = new File(ioTempFile, jarId + ".jar");
    JarArchiveOutputStream out = null;
    try {
        out = new JarArchiveOutputStream(new FileOutputStream(testArchive));
        for (File f : destination.listFiles()) {
            JarArchiveEntry jentry = new JarArchiveEntry(f.getName());
            FileInputStream fis = new FileInputStream(f);
            out.putArchiveEntry(jentry);
            IOUtils.copy(fis, out);
            fis.close();
            out.closeArchiveEntry();
        }
        out.finish();
    } catch (IOException e) {
        throw new CompileProcessorException("Exception while writing jar", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException WhatCanYouDo) {
            }
        }
    }
    if (ss != null) {
        ss.add_resource(ResourceType.JAR, testArchive.getAbsolutePath());
    }
    CommandProcessorResponse good = new CommandProcessorResponse(0, testArchive.getAbsolutePath(), null);
    return good;
}
Also used : Path(org.apache.tools.ant.types.Path) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) JarArchiveOutputStream(org.apache.commons.compress.archivers.jar.JarArchiveOutputStream) Groovyc(org.codehaus.groovy.ant.Groovyc) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Project(org.apache.tools.ant.Project) FileOutputStream(java.io.FileOutputStream) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with JarArchiveOutputStream

use of org.apache.commons.compress.archivers.jar.JarArchiveOutputStream 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 3 with JarArchiveOutputStream

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

the class JarPatcher method jar.

private void jar(final int method, final File exploded, final File target) {
    JarArchiveOutputStream stream = null;
    try {
        stream = new JarArchiveOutputStream(new FileOutputStream(target));
        final String prefix = exploded.getCanonicalFile().getAbsolutePath() + File.separator;
        for (final String f : exploded.list()) {
            jar(method, stream, new File(exploded, f).getCanonicalFile(), prefix);
        }
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IO.close(stream);
    }
}
Also used : JarArchiveOutputStream(org.apache.commons.compress.archivers.jar.JarArchiveOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 JarArchiveOutputStream (org.apache.commons.compress.archivers.jar.JarArchiveOutputStream)3 JarArchiveEntry (org.apache.commons.compress.archivers.jar.JarArchiveEntry)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 FileInputStream (java.io.FileInputStream)1 BuildException (org.apache.tools.ant.BuildException)1 Project (org.apache.tools.ant.Project)1 Path (org.apache.tools.ant.types.Path)1 Groovyc (org.codehaus.groovy.ant.Groovyc)1