KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > contrib > ExecChdirOperator


1 // $Id: ExecChdirOperator.java,v 1.1 2002/01/28 16:30:10 ramsdell Exp $
2

3 /* Contributed by Lachlan O'Dea (quelgar) on Sourceforge. */
4
5 /*
6 I've found that it can be useful to execute some non-Java commands in
7 a different working directory. This class supports this in a platform
8 independent way by using the new Runtime.exec() method introduced in
9 JDK 1.3.
10
11 The class works the same way as the standard exec command, except that
12 the first parameter must be the desired working directory.
13
14 I basically just copied the ExecOperator class and made the minimum
15 number of changes required to support specification of a working
16 directory. If this functionality were to be included as part of jmk, a
17 more sophisticated approach would probably be desirable.
18 */

19
20 package edu.neu.ccs.jmk.contrib;
21
22 import java.io.*;
23 import edu.neu.ccs.jmk.*;
24
25 /**
26  * This operator is used to implement exec commands.
27  * It executes a command in a separate process using
28  * the runtime's exec method.
29  * @version November 1997
30  * @author John D. Ramsdell
31  */

32 final public class ExecChdirOperator
33 implements Operator
34 {
35
36   /**
37    * Get the name of this operator.
38    * @return the name of this operator
39    */

40   public String JavaDoc getName() {
41     return "execdir";
42   }
43
44   /**
45    * Executes a command in a separate process using
46    * the runtime's exec method.
47    * @see java.lang.Runtime#exec(String[])
48    * @param args operands given to exec method of the runtime
49    * @param out place to write messages
50    * @exception CommandFailedException if operation failed
51    */

52   public void exec(String JavaDoc[] argsWithDir, PrintWriter out)
53        throws CommandFailedException
54   {
55     String JavaDoc msg = null; // For error messages
56
String JavaDoc[] args = new String JavaDoc[argsWithDir.length - 1];
57     try {
58       // Create the child process.
59
File dir = new File(argsWithDir[0]);
60       System.arraycopy(argsWithDir, 1, args, 0, args.length);
61       argsWithDir = null;
62       Process JavaDoc p = Runtime.getRuntime().exec(args, null, dir);
63       // Start a thread which copies the input stream to out.
64
Thread JavaDoc t = new Thread JavaDoc(new BufferedCopy(p.getInputStream(), out));
65       t.start();
66       // Copy the error stream.
67
new BufferedCopy(p.getErrorStream(), out).run();
68       int exitCode = p.waitFor(); // Wait for the termination of the child
69
t.join(); // Wait for the end of the input stream
70
if (exitCode != 0) {
71     if (args.length > 0) { // Copy command when an error occurs
72
msg = args[0];
73       for (int i = 1; i < args.length; i++)
74         msg += " " + args[i];
75     }
76     else
77       msg = "No args to exec";
78       }
79     }
80     catch (IOException ioEx) { // from exec
81
msg = "In exec: " + ioEx.toString();
82     }
83     catch (InterruptedException JavaDoc iEx) { // from waitFor
84
msg = "In waitFor: " + iEx.toString();
85     }
86     if (msg != null)
87       throw new CommandFailedException(msg);
88   }
89
90   /**
91    * An object which copies the input stream to out.
92    * The copy can be run by a thread.
93    */

94   private static class BufferedCopy
95   implements Runnable JavaDoc
96   {
97     private InputStream is;
98     private PrintWriter out;
99
100     BufferedCopy(InputStream is, PrintWriter out) {
101       this.is = is;
102       this.out = out;
103     }
104
105     public void run() {
106       BufferedReader br = new BufferedReader(new InputStreamReader(is));
107       try {
108     for (;;) {
109       String JavaDoc line = br.readLine();
110       if (line == null)
111         break;
112       out.println(line);
113     }
114     br.close();
115       }
116       catch (IOException ex) { }
117     }
118   }
119
120 }
121
Popular Tags