Search in sources :

Example 1 with JarArchiveEntry

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

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

the class JarPatcher method jar.

private void jar(final int method, final JarArchiveOutputStream jar, final File f, final String prefix) throws IOException {
    final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
    final ZipArchiveEntry zip = new ZipArchiveEntry(f, path);
    zip.setMethod(method);
    final JarArchiveEntry archiveEntry = new JarArchiveEntry(zip);
    jar.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        jar.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                jar(method, jar, child.getCanonicalFile(), prefix);
            }
        }
    } else {
        final InputStream is = new FileInputStream(f);
        IOUtils.copy(is, jar);
        is.close();
        jar.closeArchiveEntry();
    }
}
Also used : JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with JarArchiveEntry

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

the class JarPatcher method unjar.

private int unjar(final File exploded, final File from) {
    int method = -1;
    JarArchiveInputStream stream = null;
    try {
        stream = new JarArchiveInputStream(new FileInputStream(from));
        JarArchiveEntry entry;
        while ((entry = stream.getNextJarEntry()) != null) {
            final File archiveEntry = new File(exploded, entry.getName());
            archiveEntry.getParentFile().mkdirs();
            if (entry.isDirectory()) {
                archiveEntry.mkdir();
                continue;
            }
            final OutputStream out = new FileOutputStream(archiveEntry);
            IOUtils.copy(stream, out);
            out.close();
            if (method < 0) {
                method = entry.getMethod();
            }
        }
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IO.close(stream);
    }
    return method;
}
Also used : JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JarArchiveOutputStream(org.apache.commons.compress.archivers.jar.JarArchiveOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with JarArchiveEntry

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

the class ExecMojo method addToJar.

private void addToJar(final ArchiveOutputStream os, final Class<?> clazz) throws IOException {
    final String name = clazz.getName().replace('.', '/') + ".class";
    os.putArchiveEntry(new JarArchiveEntry(name));
    IOUtils.copy(getClass().getResourceAsStream('/' + name), os);
    os.closeArchiveEntry();
}
Also used : JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry)

Example 5 with JarArchiveEntry

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

the class ExecMojo method createExecutableJar.

private void createExecutableJar() throws Exception {
    mkdirs(execFile.getParentFile());
    final Properties config = new Properties();
    config.put("distribution", distributionName);
    config.put("workingDir", runtimeWorkingDir);
    config.put("command", DEFAULT_SCRIPT.equals(script) ? (skipArchiveRootFolder ? "" : catalinaBase.getName() + "/") + DEFAULT_SCRIPT : script);
    final List<String> jvmArgs = generateJVMArgs();
    final String catalinaOpts = toString(jvmArgs, " ");
    config.put("catalinaOpts", catalinaOpts);
    config.put("timestamp", Long.toString(System.currentTimeMillis()));
    // java only
    final String cp = getAdditionalClasspath();
    if (cp != null) {
        config.put("additionalClasspath", cp);
    }
    config.put("shutdownCommand", tomeeShutdownCommand);
    int i = 0;
    boolean encodingSet = catalinaOpts.contains("-Dfile.encoding");
    for (final String jvmArg : jvmArgs) {
        config.put("jvmArg." + i++, jvmArg);
        encodingSet = encodingSet || jvmArg.contains("-Dfile.encoding");
    }
    if (!encodingSet) {
        // forcing encoding for launched process to be able to read conf files
        config.put("jvmArg." + i, "-Dfile.encoding=UTF-8");
    }
    if (preTasks != null) {
        config.put("preTasks", toString(preTasks, ","));
    }
    if (postTasks != null) {
        config.put("postTasks", toString(postTasks, ","));
    }
    config.put("waitFor", Boolean.toString(waitFor));
    // create an executable jar with main runner and zipFile
    final FileOutputStream fileOutputStream = new FileOutputStream(execFile);
    final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, fileOutputStream);
    {
        // distrib
        os.putArchiveEntry(new JarArchiveEntry(distributionName));
        final FileInputStream in = new FileInputStream(zipFile);
        try {
            IOUtils.copy(in, os);
            os.closeArchiveEntry();
        } finally {
            IOUtil.close(in);
        }
    }
    {
        // config
        os.putArchiveEntry(new JarArchiveEntry("configuration.properties"));
        final StringWriter writer = new StringWriter();
        config.store(writer, "");
        IOUtils.copy(new ByteArrayInputStream(writer.toString().getBytes("UTF-8")), os);
        os.closeArchiveEntry();
    }
    {
        // Manifest
        final Manifest manifest = new Manifest();
        final Manifest.Attribute mainClassAtt = new Manifest.Attribute();
        mainClassAtt.setName("Main-Class");
        mainClassAtt.setValue(runnerClass);
        manifest.addConfiguredAttribute(mainClassAtt);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        manifest.write(baos);
        os.putArchiveEntry(new JarArchiveEntry("META-INF/MANIFEST.MF"));
        IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), os);
        os.closeArchiveEntry();
    }
    {
        // Main + utility
        for (final Class<?> clazz : asList(ExecRunner.class, Files.class, Files.PatternFileFilter.class, Files.DeleteThread.class, Files.FileRuntimeException.class, Files.FileDoesNotExistException.class, Files.NoopOutputStream.class, LoaderRuntimeException.class, Pipe.class, IO.class, Zips.class, JarLocation.class, RemoteServer.class, RemoteServer.CleanUpThread.class, OpenEJBRuntimeException.class, Join.class, QuickServerXmlParser.class, Options.class, Options.NullLog.class, Options.TomEEPropertyAdapter.class, Options.NullOptions.class, Options.Log.class, JavaSecurityManagers.class)) {
            addToJar(os, clazz);
        }
    }
    addClasses(additionalClasses, os);
    addClasses(preTasks, os);
    addClasses(postTasks, os);
    IOUtil.close(os);
    IOUtil.close(fileOutputStream);
}
Also used : Options(org.apache.openejb.loader.Options) JarLocation(org.apache.openejb.loader.JarLocation) Properties(java.util.Properties) ExecRunner(org.apache.openejb.maven.plugin.runner.ExecRunner) StringWriter(java.io.StringWriter) LoaderRuntimeException(org.apache.openejb.loader.LoaderRuntimeException) JavaSecurityManagers(org.apache.openejb.util.JavaSecurityManagers) Files(org.apache.openejb.loader.Files) RemoteServer(org.apache.openejb.config.RemoteServer) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) IO(org.apache.openejb.loader.IO) Join(org.apache.openejb.util.Join) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Pipe(org.apache.openejb.util.Pipe) Manifest(org.codehaus.plexus.archiver.jar.Manifest) FileInputStream(java.io.FileInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) QuickServerXmlParser(org.apache.tomee.util.QuickServerXmlParser) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) Zips(org.apache.openejb.loader.Zips) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream)

Aggregations

JarArchiveEntry (org.apache.commons.compress.archivers.jar.JarArchiveEntry)6 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)3 JarArchiveOutputStream (org.apache.commons.compress.archivers.jar.JarArchiveOutputStream)3 JarArchiveInputStream (org.apache.commons.compress.archivers.jar.JarArchiveInputStream)2 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 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 Files (org.apache.openejb.loader.Files)1