// Path: src/main/java/fi/solita/phantomrunner/stream/StreamPiper.java
// public class StreamPiper implements Runnable {
// 
//     private final InputStream in;
//     private final OutputStream out;
//     
//     public StreamPiper(InputStream in, OutputStream out) {
//         this.in = in;
//         this.out = out;
//     }
//     
//     @Override
//     public void run() {
//         try {
//             int read = -1;
//             while ((read = in.read()) != -1) {
//                 out.write(read);
//             }
//         } catch (IOException ioe) {
//             throw new RuntimeException(ioe);
//         }
//     }
// }
// 
// Path: src/main/java/fi/solita/phantomrunner/util/Strings.java
// public class Strings {
// 
//     public static String firstMatch(String str, String regex) {
//         Pattern p = Pattern.compile(regex);
//         Matcher m = p.matcher(str);
//         if (!m.find()) {
//             throw new IllegalArgumentException("No match found for String: " + str);
//         }
//         return str.substring(m.start(), m.end());
//     }
//     
//     public static List<String> splitTokens(String str, String regex) {
//         return splitTokens(str, regex, 0);
//     }
//     
//     public static List<String> splitTokens(String str, String regex, int flags) {
//         Pattern p = Pattern.compile(regex, flags);
//         Matcher m = p.matcher(str);
//         List<String> result = new ArrayList<String>();
//         while (m.find()) {
//             result.add(m.group());
//         }
//         return result;
//     }
//     
//     public static String indentLines(String str, int indentAmount) {
//         StringBuilder builder = new StringBuilder();
//         for (String line : str.split("\n")) {
//             for (int i = 0; i < indentAmount; i++) {
//                 builder.append(' ');
//             }
//             builder.append(line);
//         }
//         return builder.toString();
//     }
// 
//     public static String streamToString(InputStream stream) throws IOException {
//         StringWriter writer = new StringWriter();
//         IOUtil.copy(stream, writer, "UTF-8");
//         return writer.toString();
//     }
//     
//     public static List<String> stringList(String...str) {
//         return Arrays.asList(str);
//     }
// 
//     public static List<String> stringList(String str, String... strs) {
//         Builder<String> builder = ImmutableList.builder();
//         builder.add(str);
//         builder.add(strs);
//         return builder.build();
//     }
// }


import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import fi.solita.phantomrunner.stream.StreamPiper;
import fi.solita.phantomrunner.util.Strings;

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 Solita Oy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without
 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
 * OR OTHER DEALINGS IN THE SOFTWARE.
 */
package fi.solita.phantomrunner;



/**
 * Class abstracting the PhantomJs process. Registers a shutdown hook for killing the process when JVM
 * exits and provides a input stream piping to standard out.
 */
public class PhantomProcess {

    private final String path;
    private final Process phantomProcess;
    
    public PhantomProcess(PhantomConfiguration config, String...jsFilePaths) {
        this.path = config.phantomPath();
        
        try {

// Path: src/main/java/fi/solita/phantomrunner/stream/StreamPiper.java
// public class StreamPiper implements Runnable {
// 
//     private final InputStream in;
//     private final OutputStream out;
//     
//     public StreamPiper(InputStream in, OutputStream out) {
//         this.in = in;
//         this.out = out;
//     }
//     
//     @Override
//     public void run() {
//         try {
//             int read = -1;
//             while ((read = in.read()) != -1) {
//                 out.write(read);
//             }
//         } catch (IOException ioe) {
//             throw new RuntimeException(ioe);
//         }
//     }
// }
// 
// Path: src/main/java/fi/solita/phantomrunner/util/Strings.java
// public class Strings {
// 
//     public static String firstMatch(String str, String regex) {
//         Pattern p = Pattern.compile(regex);
//         Matcher m = p.matcher(str);
//         if (!m.find()) {
//             throw new IllegalArgumentException("No match found for String: " + str);
//         }
//         return str.substring(m.start(), m.end());
//     }
//     
//     public static List<String> splitTokens(String str, String regex) {
//         return splitTokens(str, regex, 0);
//     }
//     
//     public static List<String> splitTokens(String str, String regex, int flags) {
//         Pattern p = Pattern.compile(regex, flags);
//         Matcher m = p.matcher(str);
//         List<String> result = new ArrayList<String>();
//         while (m.find()) {
//             result.add(m.group());
//         }
//         return result;
//     }
//     
//     public static String indentLines(String str, int indentAmount) {
//         StringBuilder builder = new StringBuilder();
//         for (String line : str.split("\n")) {
//             for (int i = 0; i < indentAmount; i++) {
//                 builder.append(' ');
//             }
//             builder.append(line);
//         }
//         return builder.toString();
//     }
// 
//     public static String streamToString(InputStream stream) throws IOException {
//         StringWriter writer = new StringWriter();
//         IOUtil.copy(stream, writer, "UTF-8");
//         return writer.toString();
//     }
//     
//     public static List<String> stringList(String...str) {
//         return Arrays.asList(str);
//     }
// 
//     public static List<String> stringList(String str, String... strs) {
//         Builder<String> builder = ImmutableList.builder();
//         builder.add(str);
//         builder.add(strs);
//         return builder.build();
//     }
// }


// Path: src/main/java/fi/solita/phantomrunner/PhantomProcess.java
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import fi.solita.phantomrunner.stream.StreamPiper;
import fi.solita.phantomrunner.util.Strings;

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 Solita Oy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without
 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
 * OR OTHER DEALINGS IN THE SOFTWARE.
 */
package fi.solita.phantomrunner;



/**
 * Class abstracting the PhantomJs process. Registers a shutdown hook for killing the process when JVM
 * exits and provides a input stream piping to standard out.
 */
public class PhantomProcess {

    private final String path;
    private final Process phantomProcess;
    
    public PhantomProcess(PhantomConfiguration config, String...jsFilePaths) {
        this.path = config.phantomPath();
        
        try {

            this.phantomProcess = new ProcessBuilder(Strings.stringList(path, jsFilePaths)).start();
