UnixUtil.java
00001 package edu.virtualschool.jwaa;
00002
00003 import java.io.BufferedReader;
00004 import java.io.InputStream;
00005 import java.io.InputStreamReader;
00006
00011 public class UnixUtil
00012 {
00013 public final static String system(String cmd)
00014 {
00015 int ret = 0;
00016 try
00017 {
00018 String c = "/bin/sh -c " + cmd;
00019 Process process = Runtime.getRuntime().exec(c);
00020 ret = process.waitFor();
00021 if (ret != 0)
00022 return "UnixUtil.system(\"" + cmd + "\")=" + ret;
00023 return getString(process.getInputStream());
00024 }
00025 catch (Exception e)
00026 {
00027 return "UnixUtil.system("+cmd+")=" + ret + ": " + e;
00028 }
00029 }
00030 private final static String getString(InputStream is)
00031 throws Exception
00032 {
00033 StringBuffer buf = new StringBuffer();
00034 InputStreamReader isr = new InputStreamReader(is);
00035 BufferedReader bis = new BufferedReader(isr);
00036 for (String line = ""; line != null; line = bis.readLine())
00037 buf.append(line);
00038 return buf.toString();
00039 }
00040 public static void system(GenericPage page, String commandLine, boolean escapeXml)
00041 throws Fault
00042 {
00043 try
00044 {
00045 Process process = Runtime.getRuntime().exec(commandLine);
00046 int procReturn = process.waitFor();
00047 if (procReturn != 0) throw new Fault
00048 ("process \"" + commandLine + "\" exited with value " + procReturn);
00049 page.send("<ol>");
00050 StreamUtil.sendStream(page, process.getInputStream(), escapeXml);
00051 page.send("</ol>");
00052 }
00053 catch (Exception e) { e.printStackTrace(); }
00054 }
00055 }