1 4 package com.openedit.util; 5 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.util.List ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import com.openedit.OpenEditException; 14 15 public class Exec 16 { 17 private static final Log log = LogFactory.getLog(Exec.class); 18 protected boolean fieldTrackOutput; 19 protected String fieldErrorOutput; 20 protected String fieldStandardOutput; 21 22 public boolean runExec(List com) throws OpenEditException 23 { 24 setErrorOutput(null); 25 setStandardOutput(null); 26 String [] inCommand = (String []) com.toArray(new String [com.size()]); 27 try 28 { 29 log.debug("Running: " + com); 30 Process proc = Runtime.getRuntime().exec(inCommand); 31 int ret = 0; 32 if (log.isDebugEnabled() || isTrackOutput()) 33 { 34 InputStreamHandler reader1 = new InputStreamHandler(); 36 reader1.setStream(proc.getInputStream()); 37 38 InputStreamHandler errreader = new InputStreamHandler(); 39 errreader.setStream(proc.getErrorStream()); 40 reader1.start(); 41 42 errreader.start(); 43 44 ret = proc.waitFor(); 45 46 reader1.join(); 47 errreader.join(); 48 String stdo = reader1.getBuffer().toString(); 49 if (stdo.length() > 0) 50 { 51 setStandardOutput(stdo); 52 } 53 String stder = errreader.getBuffer().toString(); 54 if (stder.length() > 0) 55 { 56 setErrorOutput(stdo); 57 } 58 } 59 else 60 { 61 ret = proc.waitFor(); 62 } 63 if( ret != 0 ) 64 { 65 log.error("error on: " + com); 66 return false; 67 } 68 return true; 69 70 } 71 catch (Exception ex) 72 { 73 throw new OpenEditException(ex); 74 } 75 } 76 77 public boolean isTrackOutput() 78 { 79 return fieldTrackOutput; 80 } 81 82 public void setTrackOutput(boolean inTrackOutput) 83 { 84 fieldTrackOutput = inTrackOutput; 85 } 86 87 public String getErrorOutput() 88 { 89 return fieldErrorOutput; 90 } 91 92 public void setErrorOutput(String inErrorOutput) 93 { 94 fieldErrorOutput = inErrorOutput; 95 } 96 97 public String getStandardOutput() 98 { 99 return fieldStandardOutput; 100 } 101 102 public void setStandardOutput(String inStandardOutput) 103 { 104 fieldStandardOutput = inStandardOutput; 105 } 106 107 class InputStreamHandler extends Thread { 108 109 protected InputStream fieldStream; 110 protected StringBuffer fieldBuffer; 111 112 115 public StringBuffer getBuffer() { 116 if ( fieldBuffer == null) 117 { 118 fieldBuffer = new StringBuffer (); 119 } 120 return fieldBuffer; 121 } 122 125 public void setBuffer(StringBuffer inBuffer) { 126 fieldBuffer = inBuffer; 127 } 128 131 public InputStream getStream() { 132 return fieldStream; 133 } 134 137 public void setStream(InputStream inStream) { 138 fieldStream = inStream; 139 } 140 public void run() 141 { 142 try 143 { 144 int nextChar; 145 StringBuffer b = new StringBuffer (); 146 while( (nextChar = getStream().read()) != -1 ) 147 { 148 if( nextChar == '\n') 149 { 150 String line = b.toString(); 151 getBuffer().append(line); 152 getBuffer().append((char)nextChar); 153 154 log.info(line); 155 b = new StringBuffer (); 156 } 157 else 158 { 159 b.append((char)nextChar); 160 } 161 } 162 getBuffer().append(b.toString()); 163 } 164 catch ( IOException ex) 165 { 166 log.error(ex); 168 } 169 } 170 } 171 172 173 } 174 | Popular Tags |