1 package com.openedit.archive.update; 2 3 import java.io.IOException ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 public class CommandLine implements Executor { 11 private static final Log log = LogFactory.getLog(CommandLine.class); 12 13 public boolean runExec(List com) { 14 if ( com ==null) 15 return false; 16 String [] cmd = (String []) com.toArray(new String [com.size()]); 17 return exec(cmd); 18 } 19 20 public boolean exec(List commandLine) { 21 if ( commandLine ==null) 22 return false; 23 String [] cmd = new String [commandLine.size()]; 24 int i =0; 25 for (Iterator iter = commandLine.iterator(); iter.hasNext();) { 26 String element = (String ) iter.next(); 27 cmd[i++] = element; 28 } 29 return exec(cmd); 30 } 31 32 public boolean exec(String [] cmd) { 33 if ( cmd ==null) 34 return false; 35 int exitVal = 0; 36 try { 37 Runtime runtime = Runtime.getRuntime(); 38 reportInfo("Running: ", cmd); 39 Process proc = runtime.exec(cmd); 40 exitVal = proc.waitFor(); 41 if (exitVal != 0) { 42 reportError(cmd); 43 } 44 } catch (IOException x) { 45 reportError(cmd); 46 return false; 47 } catch (InterruptedException e) { 48 reportError(cmd); 49 return false; 50 } 51 return exitVal == 0; 52 } 53 54 protected void reportError(String [] cmd) { 55 StringBuffer out = new StringBuffer (); 56 for (int i = 0; i < cmd.length; i++) { 57 out.append(cmd[i]); 58 out.append(" "); 59 } 60 log.error("Error on: " + out); 61 } 62 63 protected void reportInfo(String label, String [] cmd) { 64 StringBuffer out = new StringBuffer (); 65 for (int i = 0; i < cmd.length; i++) { 66 out.append(cmd[i]); 67 out.append(" "); 68 } 69 log.info(label + out); 70 } 71 72 } | Popular Tags |