1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/**************************************************************************
/* This class implements the processing of os-commands using Runtime.exec()
/*
/* Copyright (c) 2009 by Bernhard Bablok (mail@bablokb.de)
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU Library General Public License as published
/* by the Free Software Foundation; either version 2 of the License or
/* (at your option) any later version.
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/* GNU Library General Public License for more details.
/*
/* You should have received a copy of the GNU Library General Public License
/* along with this program; see the file COPYING.LIB. If not, write to
/* the Free Software Foundation Inc., 59 Temple Place - Suite 330,
/* Boston, MA 02111-1307 USA
/**************************************************************************/
package org.im4java.process;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import nat.ConfigNat;
/**
This class implements the processing of os-commands using Runtime.exec().
The class does not use the newer ProcessBuilder, since it has no
knowledge about the valid os-arguments of the generated command.
@version $Revision: 1.16 $
@author $Author: bablokb, Bruno Mascret (warnings, Image Magick Path) $
*/
public class ProcessStarter {
//////////////////////////////////////////////////////////////////////////////
/**
Buffer size of process input-stream (used for reading the
output (sic!) of the process). Currently 64KB.
*/
public static final int BUFFER_SIZE=65536;
//////////////////////////////////////////////////////////////////////////////
/**
The InputProvider for the ProcessStarter (if used as a pipe).
*/
private InputProvider iInputProvider = null;
//////////////////////////////////////////////////////////////////////////////
/**
The OutputConsumer for the ProcessStarter (if used as a pipe).
*/
private OutputConsumer iOutputConsumer = null;
//////////////////////////////////////////////////////////////////////////////
/**
The ErrorConsumer for the stderr of the ProcessStarter.
*/
private ErrorConsumer iErrorConsumer = null;
////////////////////////////////////////////////////////////////////////////
/**
Execution-mode. If true, run asynchronously.
*/
private boolean iAsyncMode = false;
/** Path to convert */
private String convertPath = "convert";
////////////////////////////////////////////////////////////////////////////
/**
The ProcessListeners for this ProcessStarter.
*/
private LinkedList<ProcessListener> iProcessListener;
//////////////////////////////////////////////////////////////////////////////
/**
Constructor.
Set the image magick path if windows
@author bruno, NAT
*/
protected ProcessStarter() {
iProcessListener = new LinkedList<ProcessListener>();
//give the good path for windows
if(System.getProperty("os.name").startsWith("Windows"))
{
if(!ConfigNat.getCurrentConfig().getImageMagickDir().equals(""))
{
convertPath = "\""+ConfigNat.getCurrentConfig().getImageMagickDir()+"\\convert.exe\"";
}
else //on essaie de lire le répertoire dans une variable d'environnement
{
convertPath = "\""+System.getenv("IMAGEMAGICK_HOME").replaceAll("\"", "")+"\\convert.exe\"";
}
}
}
//////////////////////////////////////////////////////////////////////////////
/**
Set the InputProvider for the ProcessStarter (if used as a pipe).
* @param pInputProvider the InputProvider to be set
*/
public void setInputProvider(InputProvider pInputProvider) {
iInputProvider = pInputProvider;
}
//////////////////////////////////////////////////////////////////////////////
/**
* @return the iInputProvider
*/
public InputProvider getIInputProvider() {
return iInputProvider;
}
/**
Set the OutputConsumer for the ProcessStarter (if used as a pipe).
* @param pOutputConsumer the OutputConsumer to be set
*/
public void setOutputConsumer(OutputConsumer pOutputConsumer) {
iOutputConsumer = pOutputConsumer;
}
//////////////////////////////////////////////////////////////////////////////
/**
Set the ErrorConsumer for the stderr of the ProcessStarter.
* @param pErrorConsumer the ErrorConsumer to be set
*/
public void setErrorConsumer(ErrorConsumer pErrorConsumer) {
iErrorConsumer = pErrorConsumer;
}
/**
* return {@link #iProcessListener}
* @return ProcessListener linked list
*/
public LinkedList<ProcessListener> getIProcessListener()
{
return iProcessListener;
}
//////////////////////////////////////////////////////////////////////////////
/**
* Add a ProcessListener to this ProcessStarter.
*
@param pProcessListener the ProcessListener to add
*/
public void addProcessListener(ProcessListener pProcessListener) {
iProcessListener.add(pProcessListener);
}
/////////////////////////////////////////////////////////////////////////////
/**
Pipe input to the command. This is done asynchronously.
* @param pOutputStream the OutputConsumer to use
* @throws IOException if {@link InputProvider#provideInput(OutputStream)} failed
*/
private void processInput(OutputStream pOutputStream) throws IOException {
final BufferedOutputStream bos =
new BufferedOutputStream(pOutputStream,BUFFER_SIZE);
(new Thread() {
@Override
public void run() {
try {
getIInputProvider().provideInput(bos);
} catch (IOException iex) {
// we do nothing, since we are in an asynchronous thread anyway
}
}
}).run();
bos.close();
if (pOutputStream != null) {
pOutputStream.close();
}
}
//////////////////////////////////////////////////////////////////////////////
/**
Let the OutputConsumer process the output of the command.
* @param pInputStream the InputStream to use
* @param pConsumer the OutputConsumer to use
* @throws IOException if IO problems with BufferedInputStream
*/
private void processOutput(InputStream pInputStream,
OutputConsumer pConsumer) throws IOException{
BufferedInputStream bis = new BufferedInputStream(pInputStream,BUFFER_SIZE);
pConsumer.consumeOutput(bis);
bis.close();
if (pInputStream != null) {
pInputStream.close();
}
}
//////////////////////////////////////////////////////////////////////////////
/**
Let the ErrorConsumer process the stderr-stream.
* @param pInputStream the InputStream to use
* @param pConsumer the ErrorConsumer to use
* @throws IOException if IO problems with BufferedInputStream
*/
private void processError(InputStream pInputStream,
ErrorConsumer pConsumer) throws IOException{
BufferedInputStream bis = new BufferedInputStream(pInputStream,BUFFER_SIZE);
pConsumer.consumeError(bis);
bis.close();
if (pInputStream != null) {
pInputStream.close();
}
}
//////////////////////////////////////////////////////////////////////////////
/**
Execute the command.
* @param pArgs arguments for command
* @return process return value
* @throws IOException IO problems
* @throws InterruptedException Interruptions problems
*/
protected int run(final LinkedList<String> pArgs)
throws IOException, InterruptedException {
int ret = 0;
if (! iAsyncMode) {
Process pr = startProcess(pArgs);
ret = waitForProcess(pr);
} else {
Runnable r = new Runnable() {
public void run() {
int rc;
ProcessEvent pe = new ProcessEvent();
try {
Process pr = startProcess(pArgs);
for (ProcessListener pl:getIProcessListener()) {
pl.processStarted(pr);
}
rc = waitForProcess(pr);
pe.setReturnCode(rc);
} catch (Exception e) {
pe.setException(e);
}
for (ProcessListener pl:getIProcessListener()) {
pl.processTerminated(pe);
}
}
};
(new Thread(r)).start();
ret=0;
}
return ret;
}
//////////////////////////////////////////////////////////////////////////////
/**
Execute the command.
* @param pArgs list of arguments for the process
* @return return value of {@link ProcessBuilder#start()}
* @throws IOException IO problems
* @throws InterruptedException Interruption problems
*/
public Process startProcess(LinkedList<String> pArgs)
throws IOException, InterruptedException {
pArgs.set(0, convertPath);
ProcessBuilder builder = new ProcessBuilder(pArgs);
/*
for(String s:builder.command()){System.out.println(s);}*/
return builder.start();
}
//////////////////////////////////////////////////////////////////////////////
/**
Perform process input/output and wait for process to terminate.
* @param pProcess the process
* @return the process exit value
* @throws IOException IO problems
* @throws InterruptedException Interrupted Exception problems
*/
public int waitForProcess(Process pProcess)
throws IOException, InterruptedException {
if (getIInputProvider() != null) {
processInput(pProcess.getOutputStream());
}
if (iOutputConsumer != null) {
processOutput(pProcess.getInputStream(),iOutputConsumer);
}
if (iErrorConsumer != null) {
processError(pProcess.getErrorStream(),iErrorConsumer);
}
pProcess.waitFor();
int rc=pProcess.exitValue();
// just to be on the safe side
try {
pProcess.getInputStream().close();
pProcess.getOutputStream().close();
pProcess.getErrorStream().close();
} catch (Exception e) {e.printStackTrace();
}
return rc;
}
/////////////////////////////////////////////////////////////////////////////
/**
@param pAsyncMode the iAsyncMode to set
*/
public void setAsyncMode(boolean pAsyncMode) {
iAsyncMode = pAsyncMode;
}
////////////////////////////////////////////////////////////////////////////
/**
@return the iAsyncMode
*/
public boolean isAsyncMode() {
return iAsyncMode;
}
}
|