KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > util > Exec


1 /*
2  * Created on May 4, 2006
3  */

4 package com.openedit.util;
5
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import com.openedit.OpenEditException;
14
15 public class Exec
16 {
17     private static final Log log = LogFactory.getLog(Exec.class);
18     protected boolean fieldTrackOutput;
19     protected String JavaDoc fieldErrorOutput;
20     protected String JavaDoc fieldStandardOutput;
21
22     public boolean runExec(List JavaDoc com) throws OpenEditException
23     {
24         setErrorOutput(null);
25         setStandardOutput(null);
26         String JavaDoc[] inCommand = (String JavaDoc[]) com.toArray(new String JavaDoc[com.size()]);
27         try
28         {
29             log.debug("Running: " + com);
30             Process JavaDoc proc = Runtime.getRuntime().exec(inCommand);
31             int ret = 0;
32             if (log.isDebugEnabled() || isTrackOutput())
33             {
34                 //run again to catch the error this time (keeps things fast due to thread starts)
35
InputStreamHandler reader1 = new InputStreamHandler();
36                 reader1.setStream(proc.getInputStream());
37
38                 InputStreamHandler errreader = new InputStreamHandler();
39                 errreader.setStream(proc.getErrorStream());
40                 reader1.start();
41                 
42                 errreader.start();
43
44                 ret = proc.waitFor();
45
46                 reader1.join();
47                 errreader.join();
48                 String JavaDoc stdo = reader1.getBuffer().toString();
49                 if (stdo.length() > 0)
50                 {
51                     setStandardOutput(stdo);
52                 }
53                 String JavaDoc stder = errreader.getBuffer().toString();
54                 if (stder.length() > 0)
55                 {
56                     setErrorOutput(stdo);
57                 }
58             }
59             else
60             {
61                 ret = proc.waitFor();
62             }
63             if( ret != 0 )
64             {
65                 log.error("error on: " + com);
66                 return false;
67             }
68             return true;
69
70         }
71         catch (Exception JavaDoc ex)
72         {
73             throw new OpenEditException(ex);
74         }
75     }
76
77     public boolean isTrackOutput()
78     {
79         return fieldTrackOutput;
80     }
81
82     public void setTrackOutput(boolean inTrackOutput)
83     {
84         fieldTrackOutput = inTrackOutput;
85     }
86
87     public String JavaDoc getErrorOutput()
88     {
89         return fieldErrorOutput;
90     }
91
92     public void setErrorOutput(String JavaDoc inErrorOutput)
93     {
94         fieldErrorOutput = inErrorOutput;
95     }
96
97     public String JavaDoc getStandardOutput()
98     {
99         return fieldStandardOutput;
100     }
101
102     public void setStandardOutput(String JavaDoc inStandardOutput)
103     {
104         fieldStandardOutput = inStandardOutput;
105     }
106
107     class InputStreamHandler extends Thread JavaDoc {
108
109         protected InputStream JavaDoc fieldStream;
110         protected StringBuffer JavaDoc fieldBuffer;
111         
112         /**
113          * @return Returns the buffer.
114          */

115         public StringBuffer JavaDoc getBuffer() {
116             if ( fieldBuffer == null)
117             {
118                 fieldBuffer = new StringBuffer JavaDoc();
119             }
120             return fieldBuffer;
121         }
122         /**
123          * @param inBuffer The buffer to set.
124          */

125         public void setBuffer(StringBuffer JavaDoc inBuffer) {
126             fieldBuffer = inBuffer;
127         }
128         /**
129          * @return Returns the stream.
130          */

131         public InputStream JavaDoc getStream() {
132             return fieldStream;
133         }
134         /**
135          * @param inStream The stream to set.
136          */

137         public void setStream(InputStream JavaDoc inStream) {
138             fieldStream = inStream;
139         }
140         public void run()
141         {
142             try
143             {
144                 int nextChar;
145                 StringBuffer JavaDoc b = new StringBuffer JavaDoc();
146                 while( (nextChar = getStream().read()) != -1 )
147                 {
148                     if( nextChar == '\n')
149                     {
150                         String JavaDoc line = b.toString();
151                         getBuffer().append(line);
152                         getBuffer().append((char)nextChar);
153
154                         log.info(line);
155                         b = new StringBuffer JavaDoc();
156                     }
157                     else
158                     {
159                         b.append((char)nextChar);
160                     }
161                 }
162                 getBuffer().append(b.toString());
163             }
164             catch ( IOException JavaDoc ex)
165             {
166                 //ignore?
167
log.error(ex);
168             }
169         }
170     }
171
172     
173 }
174
Popular Tags