1 20 21 package org.continuent.sequoia.controller.backup.backupers; 22 23 import java.io.BufferedReader ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.OutputStream ; 28 import java.util.ArrayList ; 29 30 import org.continuent.sequoia.common.log.Trace; 31 32 40 public class NativeCommandOutputThread extends Thread 41 { 42 private static final int MAX_OUTPUT_LINES = 25; 44 45 ArrayList errors = new ArrayList (); 46 ArrayList output = new ArrayList (); 47 InputStream inputStream = null; 48 OutputStream outputStream = null; 49 50 static Trace logger = Trace 52 .getLogger(NativeCommandOutputThread.class 53 .getName()); 54 55 61 public NativeCommandOutputThread(InputStream inStream) 62 { 63 inputStream = inStream; 64 } 65 66 75 public NativeCommandOutputThread(InputStream inStream, 76 OutputStream outStream) 77 { 78 inputStream = inStream; 79 outputStream = outStream; 80 } 81 82 87 public void run() 88 { 89 if (logger.isDebugEnabled()) 90 logger.debug("Starting NativeCommandOutputThread: " + this.getName()); 91 92 if (outputStream == null) 93 doNormalOutput(); 94 else 95 doRedirectOutput(); 96 97 if (logger.isDebugEnabled()) 98 logger.debug("Terminating NativeCommandOutputThread: " + this.toString()); 99 } 100 101 104 protected void doNormalOutput() 105 { 106 BufferedReader buffRead = new BufferedReader (new InputStreamReader (inputStream)); 107 108 String line = null; 109 110 try 111 { 112 while ((line = buffRead.readLine()) != null) 113 { 114 if (line.indexOf("FATAL: ") > -1 || line.indexOf("ERROR: ") > -1) 116 { 117 errors.add(line); 118 } 119 if (output.size() < MAX_OUTPUT_LINES) 120 { 121 output.add(line); 122 } 123 124 if (logger.isDebugEnabled()) 125 logger.debug(this.getName() + ": " + line); 126 } 127 } 128 catch (IOException ioe) 129 { 130 logger.warn(ioe.getMessage(), ioe); 131 } 132 finally 133 { 134 try 136 { 137 buffRead.close(); 138 } 139 catch (IOException ioe) 140 { 141 logger.warn(ioe.getMessage(), ioe); 142 } 143 } 144 } 145 146 150 protected void doRedirectOutput() 151 { 152 try 153 { 154 byte[] buff = new byte[1024]; 155 int len = 0; 156 while ((len = this.inputStream.read(buff)) > 0) 157 { 158 outputStream.write(buff, 0, len); 159 } 160 } 161 catch (IOException ioe) 162 { 163 logger.warn(ioe.getMessage(), ioe); 164 } 165 finally 166 { 167 try 169 { 170 inputStream.close(); 171 outputStream.close(); 172 } 173 catch (IOException ioe) 174 { 175 logger.warn(ioe.getMessage(), ioe); 176 } 177 } 178 } 179 180 185 public ArrayList getErrors() 186 { 187 return errors; 188 } 189 190 195 public ArrayList getOutput() 196 { 197 return output; 198 } 199 } 200 | Popular Tags |