source: main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/Command.java@ 22063

Last change on this file since 22063 was 22063, checked in by sjm84, 14 years ago

This now prints a processes STDIN and STDERR simultaneously

File size: 4.4 KB
Line 
1package org.greenstone.admin.guiext;
2
3import org.w3c.dom.Element;
4
5import java.io.BufferedReader;
6import java.io.InputStreamReader;
7import java.io.File;
8
9import java.util.HashMap;
10
11import javax.swing.JFrame;
12import javax.swing.JTextArea;
13import javax.swing.JOptionPane;
14
15public class Command implements Runnable
16{
17 HashMap _osCommands = new HashMap();
18 CommandStep _parent = null;
19
20 public Command(Element commandElement, CommandStep parent)
21 {
22 _parent = parent;
23
24 if(commandElement != null){
25 Element[] commands = ExtXMLHelper.getMultipleChildElements(commandElement, "os", true);
26 for(int i = 0; i < commands.length; i++){
27 Element currentCommand = commands[i];
28 String os = currentCommand.getAttribute("name");
29
30 if(os.equals("")){
31 System.err.println("A <" + ExtXMLHelper.COMMAND + "> element does not have an \"os\" attribute");
32 }
33 else{
34 _osCommands.put(os, ExtXMLHelper.getValueFromSingleElement(currentCommand, true));
35 }
36 }
37 }
38 else{
39 System.err.println("This <" + ExtXMLHelper.COMMAND + "> element is null");
40 }
41 }
42
43 public void run()
44 {
45 JTextArea messageArea = _parent.getMessageArea();
46 String command = getCommandForCurrentOS();
47
48 if(command == null){
49 System.err.println("No commands exist for your current operating system, to avoid seeing this warning please place <os name=\"default\"/> into the set of commands for this step in the extension_project_list.xml file located in your extension directory");
50 _parent.threadError();
51 return;
52 }
53 else if(command == ""){
54 _parent.threadError();
55 return;
56 }
57
58 File workingDirectory = new File(_parent.getParent().getParent().getExtensionDirectory());
59 Process commandLineProc = null;
60 try{
61 commandLineProc = null;
62
63 if(System.getProperty("os.name").contains("Windows")){
64 String[] args = new String[3];
65 args[0] = "cmd.exe";
66 args[1] = "/C";
67 args[2] = command;
68
69 String allArgs = new String();
70 for(int i = 0; i < args.length; i++){
71 if(i != 0){allArgs += " ";}
72 allArgs += args[i];
73 }
74
75 messageArea.append("\nExecuting \"" + allArgs + "\" on the command line\n");
76
77 commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);
78 }
79 else{
80 String[] args = new String[3];
81 args[0] = "sh";
82 args[1] = "-c";
83 args[2] = command;
84
85 String allArgs = new String();
86 for(int i = 0; i < args.length; i++){
87 if(i != 0){allArgs += " ";}
88 allArgs += args[i];
89 }
90
91 messageArea.append("\nExecuting \"" + allArgs + "\" on the command line\n");
92 commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);
93 }
94
95 BufferedReader stdInput = new BufferedReader(new InputStreamReader(commandLineProc.getInputStream()));
96
97 Thread stdPrinter = new PrinterThread(messageArea, stdInput);
98 stdPrinter.start();
99
100 BufferedReader stdError = new BufferedReader(new InputStreamReader(commandLineProc.getErrorStream()));
101
102 Thread errPrinter = new PrinterThread(messageArea, stdError);
103 errPrinter.start();
104
105 int success = commandLineProc.waitFor();
106
107 if(success != 0){
108 System.err.println("Command line process \"" + command + "\" returned unsuccessfully with the value \"" + success + "\"");
109 _parent.threadError();
110 return;
111 }
112 }
113 catch(Exception ex){
114 ex.printStackTrace();
115 _parent.threadError();
116 return;
117 }
118 _parent.startNextThread();
119 }
120
121 public boolean executeCommand()
122 {
123 return false;
124 }
125
126 public CommandStep getParent()
127 {
128 return _parent;
129 }
130
131 public String getCommandForCurrentOS()
132 {
133 String currentos = System.getProperty("os.name");
134
135 String command = (String)_osCommands.get(currentos);
136 if(command != null){
137 return command;
138 }
139
140 if(currentos.contains("Windows")){
141 command = (String)_osCommands.get("Windows");
142
143 if(command != null){
144 return command;
145 }
146 }
147 return (String)_osCommands.get("default");
148 }
149
150 public class PrinterThread extends Thread
151 {
152 JTextArea _messageArea = null;
153 BufferedReader _output = null;
154
155 public PrinterThread(JTextArea messageArea, BufferedReader output)
156 {
157 _messageArea = messageArea;
158 _output = output;
159 }
160
161 public void run()
162 {
163 String s = "";
164
165 try{
166 while ((s = _output.readLine()) != null) {
167 _messageArea.append(s + "\n");
168 _messageArea.setSelectionEnd(_messageArea.getDocument().getLength());
169 }
170 }
171 catch(Exception ex){
172 ex.printStackTrace();
173 }
174 }
175 }
176}
Note: See TracBrowser for help on using the repository browser.