1 17 18 21 22 package org.quartz.jobs; 23 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.quartz.Job; 32 import org.quartz.JobDataMap; 33 import org.quartz.JobExecutionContext; 34 import org.quartz.JobExecutionException; 35 36 52 public class NativeJob implements Job { 53 54 private final Log log = LogFactory.getLog(getClass()); 55 56 63 64 68 public static final String PROP_COMMAND = "command"; 69 70 74 public static final String PROP_PARAMETERS = "parameters"; 75 76 77 84 public static final String PROP_WAIT_FOR_PROCESS = "waitForProcess"; 85 86 94 public static final String PROP_CONSUME_STREAMS = "consumeStreams"; 95 96 97 104 105 public void execute(JobExecutionContext context) 106 throws JobExecutionException { 107 108 JobDataMap data = context.getMergedJobDataMap(); 109 110 String command = data.getString(PROP_COMMAND); 111 112 String parameters = data.getString(PROP_PARAMETERS); 113 114 if (parameters == null) { 115 parameters = ""; 116 } 117 118 boolean wait = true; 119 if(data.containsKey(PROP_WAIT_FOR_PROCESS)) { 120 wait = data.getBooleanValue(PROP_WAIT_FOR_PROCESS); 121 } 122 boolean consumeStreams = false; 123 if(data.containsKey(PROP_CONSUME_STREAMS)) { 124 consumeStreams = data.getBooleanValue(PROP_CONSUME_STREAMS); 125 } 126 127 Integer exitCode = this.runNativeCommand(command, parameters, wait, consumeStreams); 128 context.setResult(exitCode); 129 130 } 131 132 protected Log getLog() { 133 return log; 134 } 135 136 private Integer runNativeCommand(String command, String parameters, boolean wait, boolean consumeStreams) throws JobExecutionException { 137 138 String [] cmd = null; 139 String [] args = new String [2]; 140 Integer result = null; 141 args[0] = command; 142 args[1] = parameters; 143 144 145 try { 146 String osName = System.getProperty("os.name"); 148 149 if (osName.equals("Windows NT")) { 151 if (cmd == null) { 152 cmd = new String [args.length + 2]; 153 } 154 cmd[0] = "cmd.exe"; 155 cmd[1] = "/C"; 156 for (int i = 0; i < args.length; i++) { 157 cmd[i + 2] = args[i]; 158 } 159 } else if (osName.equals("Windows 95")) { if (cmd == null) { 161 cmd = new String [args.length + 2]; 162 } 163 cmd[0] = "command.com"; 164 cmd[1] = "/C"; 165 for (int i = 0; i < args.length; i++) { 166 cmd[i + 2] = args[i]; 167 } 168 } else if (osName.equals("Windows 2003")) { if (cmd == null) { 170 cmd = new String [args.length + 2]; 171 } 172 cmd[0] = "cmd.exe"; 173 cmd[1] = "/C"; 174 175 for (int i = 0; i < args.length; i++) { 176 cmd[i + 2] = args[i]; 177 } 178 } else if (osName.equals("Windows 2000")) { if (cmd == null) { 180 cmd = new String [args.length + 2]; 181 } 182 cmd[0] = "cmd.exe"; 183 cmd[1] = "/C"; 184 185 for (int i = 0; i < args.length; i++) { 186 cmd[i + 2] = args[i]; 187 } 188 } else if (osName.equals("Windows XP")) { if (cmd == null) { 190 cmd = new String [args.length + 2]; 191 } 192 cmd[0] = "cmd.exe"; 193 cmd[1] = "/C"; 194 195 for (int i = 0; i < args.length; i++) { 196 cmd[i + 2] = args[i]; 197 } 198 } else { cmd = args; 200 } 201 202 Runtime rt = Runtime.getRuntime(); 203 getLog().info("About to run" + cmd[0] + cmd[1]); 205 Process proc = rt.exec(cmd); 206 StreamConsumer stdoutConsumer = new StreamConsumer(proc.getInputStream(), "stdout"); 208 209 if(consumeStreams) { 211 StreamConsumer stderrConsumer = new StreamConsumer(proc.getErrorStream(), "stderr"); 212 stdoutConsumer.start(); 213 stderrConsumer.start(); 214 } 215 216 if(wait) { 217 result = new Integer (proc.waitFor()); 218 } 219 221 } catch (Exception x) { 222 throw new JobExecutionException("Error launching native command: ", x, false); 223 } 224 225 return result; 226 } 227 228 234 class StreamConsumer extends Thread { 235 InputStream is; 236 String type; 237 238 241 public StreamConsumer(InputStream inputStream, String type) { 242 this.is = inputStream; 243 this.type = type; 244 } 245 246 250 public void run() { 251 BufferedReader br = null; 252 try { 253 br = new BufferedReader (new InputStreamReader (is)); 254 String line = null; 255 256 while ((line = br.readLine()) != null) { 257 if(type.equalsIgnoreCase("stderr")) { 258 getLog().warn(type + ">" + line); 259 } else { 260 getLog().info(type + ">" + line); 261 } 262 } 263 } catch (IOException ioe) { 264 getLog().error("Error consuming " + type + " stream of spawned process.", ioe); 265 } finally { 266 if(br != null) { 267 try { br.close(); } catch(Exception ignore) {} 268 } 269 } 270 } 271 } 272 273 } 274 | Popular Tags |