KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ShellCommand


1 /*
2  * ShellCommand.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: ShellCommand.java,v 1.4 2003/06/16 15:21:29 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 public final class ShellCommand implements Runnable JavaDoc
31 {
32     private final String JavaDoc cmdline;
33     private final File workingDirectory;
34     private final String JavaDoc input;
35     private final StringBuffer JavaDoc output = new StringBuffer JavaDoc();
36     private int exitValue = -1;
37
38     public ShellCommand(String JavaDoc cmdline)
39     {
40         this(cmdline, null, null);
41     }
42
43     public ShellCommand(String JavaDoc cmdline, File workingDirectory)
44     {
45         this(cmdline, workingDirectory, null);
46     }
47
48     public ShellCommand(String JavaDoc cmdline, File workingDirectory, String JavaDoc input)
49     {
50         this.cmdline = cmdline;
51         this.workingDirectory = workingDirectory;
52         this.input = input;
53     }
54
55     public final String JavaDoc getOutput()
56     {
57         return output.toString();
58     }
59
60     public final int exitValue()
61     {
62         return exitValue;
63     }
64
65     private void appendOutput(String JavaDoc s)
66     {
67         output.append(s);
68     }
69
70     public void run()
71     {
72         Process JavaDoc process = null;
73         try {
74             if (cmdline != null) {
75                 if (Platform.isPlatformUnix()) {
76                     if (workingDirectory != null) {
77                         FastStringBuffer sb = new FastStringBuffer("\\cd \"");
78                         sb.append(workingDirectory.canonicalPath());
79                         sb.append("\" && ");
80                         sb.append(cmdline);
81                         String JavaDoc[] cmdarray = {"/bin/sh", "-c", sb.toString()};
82                         process = Runtime.getRuntime().exec(cmdarray);
83                     } else {
84                         String JavaDoc[] cmdarray = {"/bin/sh", "-c", cmdline};
85                         process = Runtime.getRuntime().exec(cmdarray);
86                     }
87                 } else if (Platform.isPlatformWindows()) {
88                     ArrayList JavaDoc list = new ArrayList JavaDoc();
89                     list.add("cmd.exe");
90                     list.add("/c");
91                     if (workingDirectory != null) {
92                         FastStringBuffer sb = new FastStringBuffer("cd /d \"");
93                         sb.append(workingDirectory.canonicalPath());
94                         sb.append("\" && ");
95                         sb.append(cmdline);
96                         list.addAll(Utilities.tokenize(sb.toString()));
97                     } else
98                         list.addAll(Utilities.tokenize(cmdline));
99                     final int size = list.size();
100                     String JavaDoc[] cmdarray = new String JavaDoc[size];
101                     for (int i = 0; i < size; i++)
102                         cmdarray[i] = (String JavaDoc) list.get(i);
103                     process = Runtime.getRuntime().exec(cmdarray);
104                 }
105             }
106         }
107         catch (IOException JavaDoc e) {
108             Log.error(e);
109         }
110         if (process != null) {
111             ShellCommandReaderThread stdoutThread =
112                 new ShellCommandReaderThread(process.getInputStream());
113             stdoutThread.start();
114             ShellCommandReaderThread stderrThread =
115                 new ShellCommandReaderThread(process.getErrorStream());
116             stderrThread.start();
117             if (input != null) {
118                 BufferedWriter JavaDoc writer =
119                     new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(process.getOutputStream()));
120                 try {
121                     writer.write(input);
122                     writer.flush();
123                     writer.close();
124                 }
125                 catch (IOException JavaDoc e) {
126                     Log.error(e);
127                 }
128             }
129             try {
130                 exitValue = process.waitFor();
131             }
132             catch (InterruptedException JavaDoc e) {
133                 Log.error(e);
134             }
135             try {
136                 stdoutThread.join();
137             }
138             catch (InterruptedException JavaDoc e) {
139                 Log.error(e);
140             }
141             try {
142                 stderrThread.join();
143             }
144             catch (InterruptedException JavaDoc e) {
145                 Log.error(e);
146             }
147         }
148     }
149
150     private class ShellCommandReaderThread extends ReaderThread
151     {
152         // If this constructor is private, we run into jikes 1.15 bug #2256.
153
ShellCommandReaderThread(InputStream JavaDoc inputStream)
154         {
155             super(inputStream);
156         }
157
158         public void update(final String JavaDoc s)
159         {
160             appendOutput(s);
161         }
162     }
163
164     public static void shellCommand()
165     {
166         if (!Platform.isPlatformUnix())
167             return;
168         final Editor editor = Editor.currentEditor();
169         InputDialog d =
170             new InputDialog(editor, "Command:", "Shell Command", null);
171         d.setHistory(new History("shellCommand.command"));
172         editor.centerDialog(d);
173         d.show();
174         String JavaDoc command = d.getInput();
175         if (command == null)
176             return;
177         command = command.trim();
178         if (command.length() == 0)
179             return;
180         AsynchronousShellCommand.startShellCommand(editor, command);
181     }
182
183     public static void shellCommand(String JavaDoc command)
184     {
185         if (!Platform.isPlatformUnix())
186             return;
187         AsynchronousShellCommand.startShellCommand(Editor.currentEditor(),
188             command);
189     }
190 }
191
Popular Tags