Changeset 922


Ignore:
Timestamp:
Nov 8, 2010, 1:20:39 PM (7 years ago)
Author:
stefan
Message:

added SystemUtils?

Location:
trunk/src/util
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/util/SystemUtils.java

    r744 r922  
    11package util;
    22
    3 import java.io.File;
     3import java.io.*;
     4import java.util.ArrayList;
    45import java.util.regex.Pattern;
    56
    67/**
    7  * SystemUtils includes some special system functions.
     8 * System utilities.
     9 *
     10 * @author Stefan Endrullis
     11 * @version 1.3
    812 */
    913public class SystemUtils {
    10   private static Pattern absoluteFile = Pattern.compile("^(?:/|[A-Z]:).*");
     14  /**
     15   * Executes a specified command.
     16   *
     17   * @param cmd specified command
     18   * @return output of the command
     19   * @throws IOException if an I/O error occurs
     20   * @throws InterruptedException if the current thread is interrupted by another thread
     21   *   while it is waiting for the completion of the command execution.
     22   */
     23  public static ArrayList<String> exec(String cmd) throws IOException, InterruptedException {
     24    Process process = Runtime.getRuntime().exec(cmd);
    1125
    12   /**
    13    * Return the name of the operating system.
    14    *
    15    * @return name of the operating system
    16    */
    17   public static String getOSName() {
    18     return System.getProperty("os.name");
     26    return proceed(process, cmd);
    1927  }
    2028
    2129  /**
    22    * Returns if the operating system is a ms windows system.
     30   * Executes a specified command.
    2331   *
    24    * @return true if os is windows
     32   * @param cmd specified command
     33   * @return output of the command
     34   * @throws IOException if an I/O error occurs
     35   * @throws InterruptedException if the current thread is interrupted by another thread
     36   *   while it is waiting for the completion of the command execution.
    2537   */
    26   public static boolean isWinOS() {
    27     return System.getProperty("os.name").toLowerCase().startsWith("win");
     38  public static ArrayList<String> exec(String[] cmd) throws IOException, InterruptedException {
     39    Process process = Runtime.getRuntime().exec(cmd);
     40
     41    String cmdString = cmd[0];
     42    for (int i = 0; i < cmd.length; i++) {
     43      cmdString += " \"" + cmd[i] + "\"";
     44    }
     45
     46    return proceed(process, cmdString);
    2847  }
    2948
    3049  /**
    31    * Returns if the simple operating system type (windows/unix).
     50   * Waits for the completion of the command execution and checks the return code.
    3251   *
    33    * @return windows or unix
     52   * @param process process running the command
     53   * @param cmd command string
     54   * @return output of the command
     55   * @throws IOException if an I/O error occurs
     56   * @throws InterruptedException if the current thread is interrupted by another thread
     57   *   while it is waiting for the completion of the command execution.
    3458   */
    35   public static String getSimpleOSType() {
    36     return isWinOS() ? "windows" : "unix";
     59  private static ArrayList<String> proceed(Process process, String cmd) throws IOException, InterruptedException {
     60    InputStream in = process.getInputStream();
     61    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
     62
     63    ArrayList<String> lines = new ArrayList<String>();
     64    String line;
     65    while((line = reader.readLine()) != null) lines.add(line);
     66
     67    int returnCode = process.waitFor();
     68    if(returnCode != 0) {
     69      InputStream errIn = process.getErrorStream();
     70      BufferedReader errReader = new BufferedReader(new InputStreamReader(errIn));
     71
     72      ArrayList<String> errLines = new ArrayList<String>();
     73      String errLine;
     74      while((errLine = errReader.readLine()) != null) errLines.add(errLine);
     75
     76      throw new ExecutionFailedException(returnCode, "Execution of \"" + cmd + "\" failed!", errLines);
     77    }
     78
     79    return lines;
    3780  }
    3881
    39   public static File newFile(File parent, String fileName) {
    40     if (absoluteFile.matcher(fileName).matches()) return new File(fileName);
    41     return new File(parent, fileName);
    42   }
     82  /**
     83   * Return the name of the operating system.
     84   *
     85   * @return name of the operating system
     86   */
     87  public static String getOSName() {
     88    return System.getProperty("os.name");
     89  }
     90
     91  /**
     92   * Returns if the operating system is a ms windows system.
     93   *
     94   * @return true if os is windows
     95   */
     96  public static boolean isWinOS() {
     97    return System.getProperty("os.name").toLowerCase().startsWith("win");
     98  }
     99
     100  /**
     101   * Returns if the simple operating system type (windows/unix).
     102   *
     103   * @return windows or unix
     104   */
     105  public static String getSimpleOSType() {
     106    return isWinOS() ? "windows" : "unix";
     107  }
    43108}
Note: See TracChangeset for help on using the changeset viewer.