Package com.cloudbees.camaaloth

Source Code of com.cloudbees.camaaloth.FataMorgana

package com.cloudbees.camaaloth;

import com.jcabi.log.VerboseProcess;
import org.apache.ant.compress.taskdefs.Unzip;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.net.JarURLConnection;
import java.net.ServerSocket;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
* <pre>
*       _                           _
*      / `._        \+O+/        _.' \
*     ( @ : `.      //`\\      .' : @ )
*      \  `.  `.   ((a a))   .'  .'  /
*       \;' `.  `.((( - ))).'  .' `;/
*        \`.  `.  ((()=()))  .'  .'/
*         ) :-._`/`(("Y"))`\'_.-: (
*         (`..../ /(_ * _)\ \....')
*          >---/ /  )   (  \ \---<
*         / .'.\ \_/\\_//\_/ /.'. \
*         |o _.-\/_) '*' (_\/-._ o|
*         |`'   ;/         \;   `'|
*         ".o_.-/           \-._o."
*           "._/             \_."
*             /               \
*            /                 \
*           /                   \
*          /                     \
*         /                       \
*         `----....._____.....----'
* </pre>
* Helper class for Merlin to do his magic work.
*
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class FataMorgana {

    public static void unzip(File zip, File dest) {
        Unzip u = new Unzip();
        u.setSrc(zip);
        u.setDest(dest);
        u.execute();
    }

    public static File wget(URL url) throws IOException {
        File zip = Files.createTempFile("tmp", "zip").toFile();
        FileUtils.copyURLToFile(url, zip);
        return zip;
    }

    public static int allocatePort() throws IOException {
        ServerSocket server = null;
        try {
            server = new ServerSocket(0);
            return server.getLocalPort();
        } finally {
            if (server != null) server.close();
        }
    }

    public static void execute(Map<String, String> environment, Path pwd, String ... command) {
        ProcessBuilder proc = new ProcessBuilder(command);
        proc.directory(pwd.toFile());
        proc.environment().putAll(environment);
        new VerboseProcess(proc).stdout();
    }

    public static void copyResourcesRecursively(URL originUrl, File destination) throws Exception {
        URLConnection urlConnection = originUrl.openConnection();
        if (urlConnection instanceof JarURLConnection) {
            copyJarResourcesRecursively(destination, (JarURLConnection) urlConnection);
        } else {
            copyFilesRecusively(new File(originUrl.getPath()), destination);
        }
    }

    public static void copyJarResourcesRecursively(File destination, JarURLConnection jarConnection ) throws IOException {
        JarFile jarFile = jarConnection.getJarFile();
        Enumeration<JarEntry> entries = jarFile.entries();
        while(entries.hasMoreElements()) {
            JarEntry e = entries.nextElement();
            if (e.getName().startsWith(jarConnection.getEntryName())) {
                String fileName = StringUtils.removeStart(e.getName(), jarConnection.getEntryName());
                if (!e.isDirectory()) {
                    try (InputStream in = jarFile.getInputStream(e);
                         Writer out = new FileWriter(new File(destination, fileName))) {
                        IOUtils.copy(in, out);
                    }
                } else {
                    new File(destination, fileName).mkdir();
                }
            }
        }
    }

    public static void copyFilesRecusively(final File toCopy, final File destDir) throws IOException {
        if (!toCopy.isDirectory()) {
            FileUtils.copyFile(toCopy, new File(destDir, toCopy.getName()));
        } else {
            final File newDestDir = new File(destDir, toCopy.getName());
            newDestDir.mkdir();
            File[] files = toCopy.listFiles();
            for (int i = 0; i < files.length; i++) {
                File child = files[i];
                copyFilesRecusively(child, newDestDir);
            }
        }
    }

}
TOP

Related Classes of com.cloudbees.camaaloth.FataMorgana

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.