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
;
9 import org
.apache
.commons
.io
.IOUtils
;
11 public class SystemCall
implements Runnable
{
12 private String execDir
;
14 private String cmd
= null;
15 private String
[] command
= null;
17 private boolean synchronous
= true;
18 private boolean captureStdIn
= false;
24 public SystemCall(String execDir
, String cmd
, boolean captureStdIn
) {
26 this.execDir
= execDir
;
28 this.synchronous
= true;
29 this.captureStdIn
= captureStdIn
;
32 public SystemCall(String execDir
, String
[] command
, boolean captureStdIn
) {
34 this.execDir
= execDir
;
35 this.command
= command
;
36 this.synchronous
= true;
37 this.captureStdIn
= captureStdIn
;
42 // Execution directory
44 if (execDir
!= null) {
45 // Replace '/' by local file separator, for portability
46 execDir
.replace('/', File
.separatorChar
);
47 dir
= new File(execDir
).getCanonicalFile();
50 final Process process
;
53 process
= Runtime
.getRuntime().exec(cmd
, null, dir
);
54 } else if (command
!= null) {
55 process
= Runtime
.getRuntime().exec(command
, null, dir
);
58 throw new RuntimeException("No command specified");
61 Runtime
.getRuntime().addShutdownHook(new Thread() {
64 if (process
!= null) {
67 } catch (Exception e
) {
69 System
.err
.println("Killed process " + process
);
75 // Manage standard streams
76 StreamReaderThread stdOutThread
= new StreamReaderThread(process
77 .getInputStream(), false);
79 // TODO: understand why streams are not properly flushed
80 StreamReaderThread stdErrThread
= new StreamReaderThread(process
81 .getInputStream(), false);
84 new StdInThread(process
.getOutputStream()).start();
86 // Wait for the end of the process
88 int exitCode
= process
.waitFor();
90 Thread
.sleep(5000);// leave the process a chance to log
91 System
.err
.println("Process return exit code " + exitCode
);
94 // asynchronous: return
96 } catch (Exception e
) {
97 throw new RuntimeException("Could not execute command " + cmd
, e
);
103 * Shortcut method returning the current exec dir if the specified one is
106 private String
getUsedDir(File dir
) {
108 return System
.getProperty("user.dir");
110 return dir
.getPath();
113 public void setCmd(String command
) {
117 public void setExecDir(String execdir
) {
118 this.execDir
= execdir
;
121 public void setCommand(String
[] command
) {
122 this.command
= command
;
125 protected synchronized void write(String str
, boolean isErr
) {
127 System
.err
.print(str
);
130 System
.out
.print(str
);
136 protected class StreamReaderThread
extends Thread
{
137 private final InputStream stream
;
138 private final boolean isErr
;
140 public StreamReaderThread(InputStream stream
, boolean isErr
) {
141 this.stream
= stream
;
146 // BufferedReader in = null;
147 InputStreamReader isr
= null;
149 isr
= new InputStreamReader(stream
);
151 StringBuffer buf
= new StringBuffer(256);
153 while ((c
= isr
.read()) != -1) {
158 boolean write
= false;
159 if (lastCr
== '>' && cr
== ' ')
166 write(buf
.toString(), isErr
);
167 buf
= new StringBuffer(256);
171 // System.err.print(cr);
172 // if (cr == '\n' || cr == '>')
173 // System.err.flush();
175 // System.out.print(cr);
176 // if (cr == '\n' || cr == '>')
177 // System.out.flush();
180 // String line = null;
181 // while ((line = in.readLine()) != null) {
182 // stdOutCallback(line);
185 } catch (IOException e
) {
187 // because the other methods
188 // to check whether the stream
189 // is closed would probably
193 IOUtils
.closeQuietly(isr
);
198 protected class StdInThread
extends Thread
{
199 private final OutputStream stream
;
201 public StdInThread(OutputStream stream
) {
202 this.stream
= stream
;
206 // BufferedReader in = null;
207 // Writer out = null;
208 InputStreamReader isr
= null;
210 // out = new OutputStreamWriter(stream);
211 isr
= new InputStreamReader(System
.in
);
213 while ((c
= isr
.read()) != -1) {
219 * in = new BufferedReader(new InputStreamReader(System.in));
220 * String line = null; while ((line = in.readLine()) != null) {
221 * out.write(line); out.write("\n"); out.flush(); }
223 } catch (IOException e
) {
224 throw new RuntimeException("Could not write to stdin stream", e
);
227 IOUtils
.closeQuietly(isr
);
228 // IOUtils.closeQuietly(in);
229 // IOUtils.closeQuietly(out);