1 package org
.argeo
.slc
.maven
.plugin
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.InputStreamReader
;
7 import java
.io
.OutputStream
;
10 import org
.apache
.commons
.io
.IOUtils
;
12 public class SystemCall
implements Runnable
{
13 private String execDir
;
15 private String cmd
= null;
16 private String
[] command
= null;
18 private boolean synchronous
= true;
19 private boolean captureStdIn
= false;
25 public SystemCall(String execDir
, String cmd
, boolean captureStdIn
) {
27 this.execDir
= execDir
;
29 this.synchronous
= true;
30 this.captureStdIn
= captureStdIn
;
33 public SystemCall(String execDir
, String
[] command
, boolean captureStdIn
) {
35 this.execDir
= execDir
;
36 this.command
= command
;
37 this.synchronous
= true;
38 this.captureStdIn
= captureStdIn
;
43 // Execution directory
45 if (execDir
!= null) {
46 // Replace '/' by local file separator, for portability
47 execDir
.replace('/', File
.separatorChar
);
48 dir
= new File(execDir
).getCanonicalFile();
51 final Process process
;
54 process
= Runtime
.getRuntime().exec(cmd
, null, dir
);
55 } else if (command
!= null) {
56 process
= Runtime
.getRuntime().exec(command
, null, dir
);
59 throw new RuntimeException("No command specified");
62 Runtime
.getRuntime().addShutdownHook(new Thread() {
65 if (process
!= null) {
68 } catch (Exception e
) {
70 System
.err
.println("Killed process " + process
);
76 // Manage standard streams
77 StreamReaderThread stdOutThread
= new StreamReaderThread(process
78 .getInputStream(), false);
80 // TODO: understand why streams are not properly flushed
81 StreamReaderThread stdErrThread
= new StreamReaderThread(process
82 .getInputStream(), false);
85 new StdInThread(process
.getOutputStream()).start();
87 // Wait for the end of the process
89 int exitCode
= process
.waitFor();
91 Thread
.sleep(5000);// leave the process a chance to log
92 System
.err
.println("Process return exit code " + exitCode
);
95 // asynchronous: return
97 } catch (Exception e
) {
98 throw new RuntimeException("Could not execute command " + cmd
, e
);
104 * Shortcut method returning the current exec dir if the specified one is
107 private String
getUsedDir(File dir
) {
109 return System
.getProperty("user.dir");
111 return dir
.getPath();
114 public void setCmd(String command
) {
118 public void setExecDir(String execdir
) {
119 this.execDir
= execdir
;
122 public void setCommand(String
[] command
) {
123 this.command
= command
;
126 protected synchronized void write(String str
, boolean isErr
) {
128 System
.err
.print(str
);
131 System
.out
.print(str
);
137 protected class StreamReaderThread
extends Thread
{
138 private final InputStream stream
;
139 private final boolean isErr
;
141 public StreamReaderThread(InputStream stream
, boolean isErr
) {
142 this.stream
= stream
;
147 // BufferedReader in = null;
148 InputStreamReader isr
= null;
150 isr
= new InputStreamReader(stream
);
152 StringBuffer buf
= new StringBuffer(256);
154 while ((c
= isr
.read()) != -1) {
159 boolean write
= false;
160 if (lastCr
== '>' && cr
== ' ')
167 write(buf
.toString(), isErr
);
168 buf
= new StringBuffer(256);
172 // System.err.print(cr);
173 // if (cr == '\n' || cr == '>')
174 // System.err.flush();
176 // System.out.print(cr);
177 // if (cr == '\n' || cr == '>')
178 // System.out.flush();
181 // String line = null;
182 // while ((line = in.readLine()) != null) {
183 // stdOutCallback(line);
186 } catch (IOException e
) {
188 // because the other methods
189 // to check whether the stream
190 // is closed would probably
194 IOUtils
.closeQuietly(isr
);
199 protected class StdInThread
extends Thread
{
200 private final OutputStream stream
;
202 public StdInThread(OutputStream stream
) {
203 this.stream
= stream
;
207 // BufferedReader in = null;
208 // Writer out = null;
209 InputStreamReader isr
= null;
211 // out = new OutputStreamWriter(stream);
212 isr
= new InputStreamReader(System
.in
);
214 while ((c
= isr
.read()) != -1) {
220 * in = new BufferedReader(new InputStreamReader(System.in));
221 * String line = null; while ((line = in.readLine()) != null) {
222 * out.write(line); out.write("\n"); out.flush(); }
224 } catch (IOException e
) {
225 throw new RuntimeException("Could not write to stdin stream", e
);
228 IOUtils
.closeQuietly(isr
);
229 // IOUtils.closeQuietly(in);
230 // IOUtils.closeQuietly(out);