KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > ExecOperator


1 // $Id: ExecOperator.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // Executes a command in a separate process.
4

5 /*
6  * Copyright 1997 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

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

34 final class ExecOperator
35 implements Operator
36 {
37
38   /**
39    * Get the name of this operator.
40    * @return the name of this operator
41    */

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

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

92   private static class BufferedCopy
93   implements Runnable JavaDoc
94   {
95     private InputStream is;
96     private PrintWriter out;
97
98     BufferedCopy(InputStream is, PrintWriter out) {
99       this.is = is;
100       this.out = out;
101     }
102
103     public void run() {
104       BufferedReader br = new BufferedReader(new InputStreamReader(is));
105       try {
106     for (;;) {
107       String JavaDoc line = br.readLine();
108       if (line == null)
109         break;
110       out.println(line);
111     }
112     br.close();
113       }
114       catch (IOException ex) { }
115     }
116   }
117
118   /**
119    * An entry point for testing exec commands.
120    * It simply passes the arguments to the exec operation.
121    * @param args operands for the command
122    */

123   public static void main(String JavaDoc[] args) {
124     PrintWriter out = new PrintWriter(System.out, true);
125     try {
126       new ExecOperator().exec(args, out);
127     }
128     catch (Throwable JavaDoc t) {
129       System.err.println("Internal error: " + t.getMessage());
130       t.printStackTrace();
131     }
132     out.println("Exec command completed");
133   }
134 }
135
Popular Tags