1 package net.sf.statsvn.util;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8
9 import net.sf.statcvs.util.LookaheadReader;
10 import net.sf.statsvn.output.SvnConfigurationOptions;
11
12 /**
13 * This class provides a way of launching new processes. It is not the best way
14 * and it surely does not work well in multi-threaded environments. It is
15 * sufficient for StatSVN's single thread.
16 *
17 * We should be launching two threads with readers for both, but we are lazy.
18 * http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html
19 *
20 * @author jkealey <jkealey@shade.ca>
21 *
22 */
23 public final class ProcessUtils {
24 private BufferedInputStream inputStream;
25
26 private BufferedInputStream errorStream;
27
28 /**
29 * A utility class (only static methods) should be final and have
30 * a private constructor.
31 */
32 public ProcessUtils() {
33 }
34
35 public static synchronized ProcessUtils call(final String sCommand) throws IOException {
36 final ProcessUtils util = new ProcessUtils();
37 final Process lastProcess = Runtime.getRuntime().exec(sCommand, null, getWorkingFolder());
38 util.errorStream = new BufferedInputStream(lastProcess.getErrorStream());
39 util.inputStream = new BufferedInputStream(lastProcess.getInputStream());
40
41 return util;
42 }
43
44 public void close() throws IOException {
45 if (errorStream != null) {
46 errorStream.close();
47 errorStream = null;
48 }
49 if (inputStream != null) {
50 inputStream.close();
51 inputStream = null;
52 }
53 }
54
55 private static File getWorkingFolder() {
56 return SvnConfigurationOptions.getCheckedOutDirectoryAsFile();
57 }
58
59 protected boolean hasErrorOccured() throws IOException {
60 return errorStream != null && errorStream.available() > 0;
61 }
62
63 protected String getErrorMessage() {
64 if (errorStream == null) {
65 return null;
66 } else {
67 final LookaheadReader diffReader = new LookaheadReader(new InputStreamReader(errorStream));
68 final StringBuffer builder = new StringBuffer();
69 try {
70 while (diffReader.hasNextLine()) {
71 builder.append(diffReader.nextLine());
72 }
73 } catch (final IOException e) {
74 SvnConfigurationOptions.getTaskLogger().error(e.toString());
75 }
76
77 return builder.toString();
78 }
79 }
80
81 /**
82 * @return the errorStream
83 */
84 public BufferedInputStream getErrorStream() {
85 return errorStream;
86 }
87
88 /**
89 * @return the inputStream
90 */
91 public BufferedInputStream getInputStream() {
92 return inputStream;
93 }
94
95 /**
96 * @param errorStream the errorStream to set
97 */
98 public void setErrorStream(final InputStream errorStream) {
99 this.errorStream = new BufferedInputStream(errorStream);
100 }
101
102 /**
103 * @param inputStream the inputStream to set
104 */
105 public void setInputStream(final InputStream inputStream) {
106 this.inputStream = new BufferedInputStream(inputStream);
107 }
108 }