Search in sources :

Example 11 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException in project kontraktor by RuedigerMoeller.

the class JNPM method downLoadAndInstall.

private IPromise downLoadAndInstall(String tarUrl, String moduleName, String resolvedVersion, File targetDir) {
    Promise p = new Promise();
    String moduleKey = createModuleKey(moduleName, targetDir);
    List<Promise> promlist = packagesUnderway.get(moduleKey);
    if (promlist != null) {
        if (promlist.size() == 0) {
            // timing: has already arrived and proms resolved
            p.resolve(true);
        } else {
            packagesUnderway.get(moduleKey).add(p);
        }
        return p;
    }
    Log.Info(this, String.format("installing '%s' in %s.", moduleName + "@" + resolvedVersion, targetDir.getAbsolutePath()));
    ArrayList list = new ArrayList();
    packagesUnderway.put(moduleKey, list);
    list.add(p);
    checkThread();
    http.getContentBytes(tarUrl).then((resp, err) -> {
        execInThreadPool(() -> {
            // multithread unpacking (java io blocks, so lets mass multithread)
            byte[] b = resp;
            try {
                b = AsyncHttpActor.unGZip(b, b.length * 10);
                File outputDir = new File(targetDir, moduleName);
                unTar(new ByteArrayInputStream(b), outputDir);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } catch (ArchiveException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }).then(() -> {
            checkThread();
            // Log.Info(this,String.format("installed '%s' in %s.", moduleName+"@"+ resolvedVersion, nodeModulesDir.getAbsolutePath()));
            packagesUnderway.get(moduleKey).forEach(prom -> prom.resolve(true));
            packagesUnderway.get(moduleKey).clear();
        });
    });
    return p;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) ArchiveException(org.apache.commons.compress.archivers.ArchiveException)

Example 12 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException in project n4js by eclipse.

the class NpmExportWizard method performFinish.

@Override
public boolean performFinish() {
    String destination = exportPage.getDestinationValue();
    List<IProject> toExport = exportPage.getChosenProjects();
    boolean shouldPackAsTarball = exportPage.getShouldPackAsTarball();
    File folder = new File(destination);
    // remap all IProjects
    List<? extends IN4JSProject> toExportIN4JSProjects = mapToIN4JSProjects(toExport);
    if (runTools() && toolRunnerPage.isToolrunRequested()) {
        // bring to front.
        ((WizardDialog) getContainer()).showPage(toolRunnerPage);
    }
    try {
        npmExporter.export(toExportIN4JSProjects, folder);
        if (shouldPackAsTarball) {
            npmExporter.tarAndZip(toExportIN4JSProjects, folder);
        }
        boolean runIt = runTools() && toolRunnerPage.queryRunTool();
        if (runIt) {
            final List<String> toolCommand = toolRunnerPage.getCommand();
            getContainer().run(true, true, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        List<String> cmds = newArrayList();
                        // cmds.add("echo"); // prepend with echo for debug TODO remove
                        // cmds.addAll(toolCommand);
                        cmds.add("bash");
                        cmds.add("-login");
                        cmds.add("-c");
                        // cmds.addAll(toolCommand);
                        cmds.add(Joiner.on(" ").join(toolCommand));
                        System.out.println("Comman will be: " + Joiner.on(" :: ").join(cmds));
                        for (IN4JSProject p : toExportIN4JSProjects) {
                            String info = "Processing " + p.toString() + "\n";
                            System.out.println(info);
                            toolRunnerPage.appendText(info);
                            File dir = npmExporter.exportDestination(p, folder);
                            ProcessBuilder pb = new ProcessBuilder();
                            pb.directory(dir);
                            pb.command(cmds);
                            pb.redirectErrorStream(true);
                            Process proc = pb.start();
                            // handle each of proc's streams in a separate thread
                            ExecutorService handlerThreadPool = Executors.newFixedThreadPool(3);
                            // handlerThreadPool.submit(new Runnable() {
                            // @Override
                            // public void run() {
                            // // we want to write to the stdin of the process
                            // BufferedWriter stdin = new BufferedWriter(
                            // new OutputStreamWriter(proc.getOutputStream()));
                            // 
                            // // read from our own stdin so we can write it to proc's stdin
                            // BufferedReader myStdin =
                            // new BufferedReader(new InputStreamReader(System.in));
                            // String line = null;
                            // try {
                            // do {
                            // line = myStdin.readLine();
                            // stdin.write(String.format("%s%n", line));
                            // stdin.flush();
                            // } while(! "exit".equalsIgnoreCase(line));
                            // } catch(IOException e) {
                            // e.printStackTrace();
                            // }
                            // }
                            // });
                            handlerThreadPool.submit(new Runnable() {

                                @Override
                                public void run() {
                                    // we want to read the stdout of the process
                                    BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                                    String line;
                                    try {
                                        while (null != (line = stdout.readLine())) {
                                            System.err.printf("[stderr] %s%n", line);
                                            toolRunnerPage.appendConsoleOut(line);
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            handlerThreadPool.submit(new Runnable() {

                                @Override
                                public void run() {
                                    // we want to read the stderr of the process
                                    BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
                                    String line;
                                    try {
                                        while (null != (line = stderr.readLine())) {
                                            System.err.printf("[stderr] %s%n", line);
                                            toolRunnerPage.appendConsoleErr(line);
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            // wait for the process to terminate
                            int exitCode = proc.waitFor();
                            System.out.printf("Process terminated with exit code %d%n", exitCode);
                            handlerThreadPool.shutdown();
                        }
                        // done with all projects.
                        // wait for close.
                        toolRunnerPage.queryCloseDialog();
                    } catch (Exception e) {
                        throw new InvocationTargetException(e);
                    }
                }
            });
        }
    } catch (IOException | ArchiveException | CompressorException e) {
        e.printStackTrace();
        Status s = new Status(ERROR, NpmExporterActivator.PLUGIN_ID, "Error occured during export.", e);
        N4JSActivator.getInstance().getLog().log(s);
        return false;
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // successfully done, then store relevant history:
    exportPage.finish();
    if (runTools()) {
        toolRunnerPage.finish();
    }
    return true;
}
Also used : ArchiveException(org.apache.commons.compress.archivers.ArchiveException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) CompressorException(org.apache.commons.compress.compressors.CompressorException) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Status(org.eclipse.core.runtime.Status) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompressorException(org.apache.commons.compress.compressors.CompressorException) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ExecutorService(java.util.concurrent.ExecutorService) BufferedReader(java.io.BufferedReader) File(java.io.File) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 13 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException in project incubator-gobblin by apache.

the class AzkabanJobHelper method addFilesToZip.

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "OBL_UNSATISFIED_OBLIGATION", justification = "Lombok construct of @Cleanup is handing this, but not detected by FindBugs")
private static void addFilesToZip(File zipFile, List<File> filesToAdd) throws IOException {
    try {
        @Cleanup OutputStream archiveStream = new FileOutputStream(zipFile);
        @Cleanup ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);
        for (File fileToAdd : filesToAdd) {
            ZipArchiveEntry entry = new ZipArchiveEntry(fileToAdd.getName());
            archive.putArchiveEntry(entry);
            @Cleanup BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileToAdd));
            IOUtils.copy(input, archive);
            archive.closeArchiveEntry();
        }
        archive.finish();
    } catch (ArchiveException e) {
        throw new IOException("Issue with creating archive", e);
    }
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) BufferedInputStream(java.io.BufferedInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) Cleanup(lombok.Cleanup) File(java.io.File) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) FileInputStream(java.io.FileInputStream)

Example 14 with ArchiveException

use of org.apache.commons.compress.archivers.ArchiveException in project datashare by ICIJ.

the class Plugin method install.

@Override
public void install(File pluginFile, Path pluginsDir) throws IOException {
    File[] candidateFiles = ofNullable(pluginsDir.toFile().listFiles((file, s) -> file.isDirectory())).orElse(new File[0]);
    List<File> previousVersionInstalled = getPreviousVersionInstalled(candidateFiles, getBaseName(getUrlFileName()));
    if (previousVersionInstalled.size() > 0) {
        logger.info("removing previous versions {}", previousVersionInstalled);
        for (File file : previousVersionInstalled) FileUtils.deleteDirectory(file);
    }
    logger.info("installing plugin from file {} into {}", pluginFile, pluginsDir);
    InputStream is = new BufferedInputStream(new FileInputStream(pluginFile));
    if (pluginFile.getName().endsWith("gz")) {
        is = new BufferedInputStream(new GZIPInputStream(is));
    }
    try (ArchiveInputStream zippedArchiveInputStream = new ArchiveStreamFactory().createArchiveInputStream(is)) {
        ArchiveEntry entry;
        while ((entry = zippedArchiveInputStream.getNextEntry()) != null) {
            final File outputFile = new File(pluginsDir.toFile(), entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    if (!outputFile.mkdirs()) {
                        throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                    }
                }
            } else {
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(zippedArchiveInputStream, outputFileStream);
                outputFileStream.close();
            }
        }
    } catch (ArchiveException e) {
        throw new RuntimeException(e);
    }
    if (isTemporaryFile(pluginFile))
        pluginFile.delete();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream)

Aggregations

ArchiveException (org.apache.commons.compress.archivers.ArchiveException)14 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)9 IOException (java.io.IOException)6 File (java.io.File)5 BufferedInputStream (java.io.BufferedInputStream)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)4 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)4 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)3 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)3 CompressorException (org.apache.commons.compress.compressors.CompressorException)3 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 DeploymentConfiguration (com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)1 ArtifactService (com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService)1