KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > ProcessUtils


1 package snow.utils;
2
3 import java.io.*;
4 import java.util.*;
5
6
7 /**
8 */

9 public final class ProcessUtils
10 {
11   private ProcessUtils() {}
12
13   /** executes the command and gobbles the whole output, don't waits.
14   * @return the process, can be used to waitFor, but not to gobble because
15   * it is already gobbled to nowhere !
16   */

17   public static Process JavaDoc executeProcessAndGobble(List<String JavaDoc> processArgs, File execDir, String JavaDoc descr) throws Exception JavaDoc
18   {
19     //StringBuilder txt = new StringBuilder();
20

21     Process JavaDoc proc = null;
22     if(execDir!=null)
23     {
24        proc = Runtime.getRuntime().exec( processArgs.toArray(new String JavaDoc[processArgs.size()]), null, execDir );
25     }
26     else
27     {
28        proc = Runtime.getRuntime().exec( processArgs.toArray(new String JavaDoc[processArgs.size()]) );
29     }
30
31     StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), null);
32     sg1.setName("Gobbler "+descr+" (in)");
33     sg1.start();
34     StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), null);
35     sg2.start();
36     sg2.setName("Gobbler "+descr+" (err)");
37
38     return proc;
39   }
40
41   /** execute the process and drain the output...
42   */

43   public static Process JavaDoc executeProcessAndGobble(List<String JavaDoc> processArgs, String JavaDoc descr) throws Exception JavaDoc
44   {
45      return executeProcessAndGobble(processArgs, null, descr);
46   }
47
48   /** @return a text with the full output of the process.
49       Err and Out mixed.
50       Use it only for small debugs or options reading from a program 10 sec timeout
51   */

52   public static String JavaDoc readWholeProcessStack(List<String JavaDoc> processArgs) throws Exception JavaDoc
53   {
54     //StringBuilder txt = new StringBuilder();
55
Process JavaDoc proc = Runtime.getRuntime().exec( processArgs.toArray(new String JavaDoc[processArgs.size()]) );
56
57     ByteArrayOutputStream baos = new ByteArrayOutputStream();
58     StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), baos);
59     sg1.start();
60     StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), baos);
61     sg2.start();
62     sg1.join(10000L);
63     sg2.join(10000L);
64
65     // Well: process make an exception if not exited (if timeout passed above !). this is good...
66
int ev = proc.exitValue();
67
68     return new String JavaDoc( baos.toByteArray() ) ;
69
70     //return txt.toString();
71
}
72
73
74   public static String JavaDoc readWholeProcessStack(List<String JavaDoc> processArgs, File execDir) throws Exception JavaDoc
75   {
76      return readWholeProcessStack(processArgs, execDir, null, false);
77   }
78
79   /** @return a text with the full output of the process.
80       Err and Out mixed.
81       Use it only for small debugs or options reading from a program 10 sec timeout
82   */

83   public static String JavaDoc readWholeProcessStack(List<String JavaDoc> processArgs, File execDir, List<String JavaDoc> toType, boolean errorIfNotStatusExit0) throws Exception JavaDoc
84   {
85     Process JavaDoc proc = Runtime.getRuntime().exec( processArgs.toArray(new String JavaDoc[processArgs.size()]),
86       null, // inherits current process env.
87
execDir );
88
89     ByteArrayOutputStream baos = new ByteArrayOutputStream();
90     StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), baos);
91     sg1.start();
92     StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), baos);
93     sg2.start();
94
95     if(toType!=null)
96     {
97        for(String JavaDoc tt : toType)
98        {
99           proc.getOutputStream().write(tt.getBytes());
100           proc.getOutputStream().flush();
101        }
102     }
103
104     // Wait until completion.
105
sg1.join(10000L);
106     sg2.join(10000L);
107
108
109     // Well: process make an exception if not exited (if timeout passed above !). this is good...
110
int ev = proc.exitValue();
111
112     String JavaDoc stack = new String JavaDoc( baos.toByteArray() );
113
114     if(errorIfNotStatusExit0 && ev!=0) throw new RuntimeException JavaDoc("Exit value was "+ev+"\nstack: "+stack);
115
116     return stack ;
117   }
118
119   /** use this to avoid hanging processes. They must be drained !!! (see java puzzlers )
120   */

121   public static String JavaDoc readWholeProcessStack(Process JavaDoc proc, long timeout) throws Exception JavaDoc
122   {
123     ByteArrayOutputStream baos = new ByteArrayOutputStream();
124     StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), baos);
125     sg1.start();
126     StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), baos);
127     sg2.start();
128     if(timeout>0)
129     {
130       sg1.join(timeout);
131       sg2.join(timeout);
132     }
133     else
134     {
135       sg1.join();
136       sg2.join();
137     }
138
139     return new String JavaDoc( baos.toByteArray() ) ;
140   }
141
142
143   /** ignores the whole outputs. Must be called when processes are created and output is not read,
144   * otherwise, probmels araises, see Java puzzlers
145   */

146   public static void drainProcess(Process JavaDoc proc, boolean waitCompletion)
147   {
148     StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), null);
149     sg1.start();
150     StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), null);
151     sg2.start();
152
153     if(waitCompletion)
154     {
155        try
156        {
157          sg1.join();
158          sg2.join();
159        } catch(InterruptedException JavaDoc ie) { ie.printStackTrace(); }
160     }
161   }
162
163
164
165
166
167 } // ProcessUtils
Popular Tags