Package winterwell.utils

Examples of winterwell.utils.Process


      assert temp1.exists();

      // 1. Render HTML to PDF with wkhtmltopdf
      // The horrendous 7 second delay is to allow time for ajax to run,
      // even on a busy server (2 seconds was too little on egan).
      Process p = new Process("wkhtmltopdf "
          + (waitFor == null ? "" : waitFor) + " "
          + temp1.getAbsolutePath() + " " + file.getAbsolutePath());
      p.setEcho(true);
      p.run();
      int done = p.waitFor(TUnit.MINUTE.getMillisecs());

      if (!file.exists())
        throw new IOException("Failed to create " + file + "\t"
            + p.getError());
      if (p.getOutput().contains("cannot connect to X server")
          || p.getError().contains("cannot connect to X server"))
        throw new IOException(
            "render failed: wkhtmltopdf couldn't connect to an X server");
      // debug spew
      Log.report("html",
          "RenderToPdf: " + p.getOutput() + "\t" + p.getError(),
          Level.FINE);
    } catch (Exception e) {
      throw Utils.runtime(e);
    } finally {
      // clean up
View Full Code Here


      temp1 = File.createTempFile("chart", ".pdf");
      renderToPdf(html, temp1, "--javascript-delay 1000"); // FIXME
      assert temp1.exists() && temp1.length() > 0;

      // 2. Render, trim and convert to PNG with convert
      Process p2 = new Process("convert -trim -antialias -density 300 "
          + temp1.getAbsolutePath() + " " + file.getAbsolutePath());
      p2.run();
      p2.waitFor(TUnit.MINUTE.getMillisecs());

      if (!file.exists())
        throw new IOException("Failed to create " + file + "\t"
            + p2.getError());
    } catch (Exception e) {
      throw Utils.runtime(e);
    } finally {
      // clean up
      if (temp1 != null) {
View Full Code Here

    if (file.isDirectory() && isSymLink(file)) {
      // try an OS call
      if (Utils.getOperatingSystem().contains("linux")
          || Utils.getOperatingSystem().contains("unix")) {
        String path = file.getAbsolutePath();
        Process p = new winterwell.utils.Process("rm -f " + path);
        p.run();
        p.waitFor(1000);
        if (!file.exists())
          return;
        throw new WrappedException(new IOException(
            "Could not delete file " + file + "; " + p.getError()));
      }
    }
    throw new WrappedException(new IOException("Could not delete file "
        + file));
  }
View Full Code Here

  }

  private static void deleteNative(File out) {
    if (!Utils.OSisUnix())
      throw new TodoException("" + out);
    Process p = new winterwell.utils.Process("rm -f "
        + out.getAbsolutePath());
    p.run();
    int ok = p.waitFor();
    if (ok != 0)
      throw new IORException(p.getError());
  }
View Full Code Here

    // Are you after a reverse lookup for a name?
    String x = "";
    if (!returnIP && IP4_ADDRESS.matcher(site).matches()) {
      x = "-x ";
    }
    Process p = new Process("dig +short " + x + site);
    p.run();
    p.waitFor(5000); // this should be fast -- 5 seconds allows aeons of
              // time
    String out = p.getOutput();

    // look for an IPv4 address?
    if (returnIP) {
      Matcher m = IP4_ADDRESS.matcher(out);
      if (m.find())
        return m.group();
      throw new FailureException("Couldn't find IP address for " + site
          + " in " + out);
    }

    // look for a name
    String[] bits = StrUtils.splitLines(out);
    String ip = null;
    for (String string : bits) {
      if (string.isEmpty()) {
        continue;
      }
      if (IP4_ADDRESS.matcher(string).matches()) {
        ip = string;
        continue;
      }
      if (string.endsWith(".")) {
        string = string.substring(0, string.length() - 1);
      }
      return string;
    }

    // try a reverse lookup
    if (ip == null)
      throw new FailureException("Couldn't find server name or ip for "
          + site + " in [" + out + "] " + p.getError());
    return dig(ip, false);
  }
View Full Code Here

      d.browse(uri);
    } catch (UnsupportedOperationException ex) {
      // KDE isn't supported :(
      if (Utils.getOperatingSystem().contains("linux")
          || Utils.getOperatingSystem().contains("unix")) {
        Process p = new Process("xdg-open " + uri);
        p.run();
      } else
        throw ex;
    } catch (IOException e) {
      throw new IORException(e);
    }
View Full Code Here

   * @return
   * @testedby {@link WebUtilsTest#testGetMyIP()}
   */
  public static List<String> getMyIP() {
    if (Utils.OSisUnix()) {
      Process p = new Process("ifconfig");
      try {
        p.run();
        p.waitFor(2000);
        String out = p.getOutput();
        Matcher m = IP4_ADDRESS.matcher(out);
        ArrayList<String> ips = new ArrayList<String>();
        while (m.find()) {
          ips.add(m.group());
        }
        return ips;
      } catch (Exception e) {
        // ifconfig failed?!
        Log.report(e + " " + p.getError(), Level.SEVERE);
        return new ArrayList();
      }
    }
    throw new TodoException();
  }
View Full Code Here

    }
    // Attempt to run external command
    try {
      final File md = File.createTempFile("tmp", ".md");
      FileUtils.write(md, text);
      Process process = new Process(cmd+" "+md.getAbsolutePath());
      process.run();
      int ok = process.waitFor(10000);
      if (ok != 0) throw new FailureException(cmd+" failed:\n"+process.getError());
      String html = process.getOutput();
      FileUtils.delete(md);
      return html;
    } catch (Exception e) {
      throw Utils.runtime(e);
    }
View Full Code Here

TOP

Related Classes of winterwell.utils.Process

Copyright © 2018 www.massapicom. 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.