// Path: agent/src/main/java/org/apache/hms/agent/Agent.java
// public class Agent {
//   private static Log log = LogFactory.getLog(Agent.class);
//   
//   private static Agent instance = null;
//   private Server server = null;
//   private static URL serverConf = null;
// 
//   public static Agent getInstance() {
//     if(instance==null) {
//       instance = new Agent();
//     }
//     return instance;
//   }
// 
//   public void start() {
//     try {
//       System.out.close();
//       System.err.close();
//       instance = this;
//       run();
//     } catch(Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//       System.exit(-1);
//     }
//   }
// 
//   public void run() {
//     server = new Server(4080);
// 
//     XmlConfiguration configuration;
//     try {
//       Context root = new Context(server, "/", Context.SESSIONS);
//       ServletHolder sh = new ServletHolder(ServletContainer.class);
//       sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
//       sh.setInitParameter("com.sun.jersey.config.property.packages", "org.apache.hms.agent.rest");
//       root.addServlet(sh, "/*");
//       server.setStopAtShutdown(true);
//       server.start();
//     } catch (Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
// 
//   public void stop() throws Exception {
//     try {
//       server.stop();
//     } catch (Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
// 
//   public static void main(String[] args) {
//     Agent agent = Agent.getInstance();
//     agent.start();
//   }
// 
// }
// 
// Path: common/src/main/java/org/apache/hms/common/util/ExceptionUtil.java
// public class ExceptionUtil {
//   public static String getStackTrace(Throwable t) {
//     StringWriter sw = new StringWriter();
//     PrintWriter pw = new PrintWriter(sw);
//     t.printStackTrace(pw);
//     pw.flush();
//     return sw.toString();
//   }
// }


import java.io.DataInputStream;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hms.agent.Agent;
import org.apache.hms.common.entity.ScriptCommand;
import org.apache.hms.common.rest.Response;
import org.apache.hms.common.util.ExceptionUtil;
    Process proc;
    try {
      String[] parameters = cmd.getParms();
      int size = 0;
      if(parameters!=null) {
        size = parameters.length;
      }
      String[] cmdArray = new String[size+1];
      cmdArray[0]=cmd.getScript();
      for(int i=0;i<size;i++) {
        cmdArray[i+1]=parameters[i];      
      }
      proc = Runtime.getRuntime().exec(cmdArray);
      DataInputStream in = new DataInputStream(proc.getInputStream());
      DataInputStream err = new DataInputStream(proc.getErrorStream());
      String str;
      while ((str = in.readLine()) != null) {
        stdout.append(str);
        stdout.append("\n");
      }
      while ((str = err.readLine()) != null) {
        errorBuffer.append(str);
        errorBuffer.append("\n");
      }
      int exitCode = proc.waitFor();
      r.setCode(exitCode);
      r.setError(errorBuffer.toString());
      r.setOutput(stdout.toString());
    } catch (Exception e) {
      r.setCode(1);
// Path: agent/src/main/java/org/apache/hms/agent/Agent.java
// public class Agent {
//   private static Log log = LogFactory.getLog(Agent.class);
//   
//   private static Agent instance = null;
//   private Server server = null;
//   private static URL serverConf = null;
// 
//   public static Agent getInstance() {
//     if(instance==null) {
//       instance = new Agent();
//     }
//     return instance;
//   }
// 
//   public void start() {
//     try {
//       System.out.close();
//       System.err.close();
//       instance = this;
//       run();
//     } catch(Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//       System.exit(-1);
//     }
//   }
// 
//   public void run() {
//     server = new Server(4080);
// 
//     XmlConfiguration configuration;
//     try {
//       Context root = new Context(server, "/", Context.SESSIONS);
//       ServletHolder sh = new ServletHolder(ServletContainer.class);
//       sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
//       sh.setInitParameter("com.sun.jersey.config.property.packages", "org.apache.hms.agent.rest");
//       root.addServlet(sh, "/*");
//       server.setStopAtShutdown(true);
//       server.start();
//     } catch (Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
// 
//   public void stop() throws Exception {
//     try {
//       server.stop();
//     } catch (Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
// 
//   public static void main(String[] args) {
//     Agent agent = Agent.getInstance();
//     agent.start();
//   }
// 
// }
// 
// Path: common/src/main/java/org/apache/hms/common/util/ExceptionUtil.java
// public class ExceptionUtil {
//   public static String getStackTrace(Throwable t) {
//     StringWriter sw = new StringWriter();
//     PrintWriter pw = new PrintWriter(sw);
//     t.printStackTrace(pw);
//     pw.flush();
//     return sw.toString();
//   }
// }


// Path: agent/src/main/java/org/apache/hms/agent/dispatcher/ShellRunner.java
import java.io.DataInputStream;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hms.agent.Agent;
import org.apache.hms.common.entity.ScriptCommand;
import org.apache.hms.common.rest.Response;
import org.apache.hms.common.util.ExceptionUtil;

    Process proc;
    try {
      String[] parameters = cmd.getParms();
      int size = 0;
      if(parameters!=null) {
        size = parameters.length;
      }
      String[] cmdArray = new String[size+1];
      cmdArray[0]=cmd.getScript();
      for(int i=0;i<size;i++) {
        cmdArray[i+1]=parameters[i];      
      }
      proc = Runtime.getRuntime().exec(cmdArray);
      DataInputStream in = new DataInputStream(proc.getInputStream());
      DataInputStream err = new DataInputStream(proc.getErrorStream());
      String str;
      while ((str = in.readLine()) != null) {
        stdout.append(str);
        stdout.append("\n");
      }
      while ((str = err.readLine()) != null) {
        errorBuffer.append(str);
        errorBuffer.append("\n");
      }
      int exitCode = proc.waitFor();
      r.setCode(exitCode);
      r.setError(errorBuffer.toString());
      r.setOutput(stdout.toString());
    } catch (Exception e) {
      r.setCode(1);
      r.setError(ExceptionUtil.getStackTrace(e));