1 | package org.greenstone.admin.guiext;
|
---|
2 |
|
---|
3 | import org.w3c.dom.Element;
|
---|
4 |
|
---|
5 | import java.io.BufferedReader;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.io.File;
|
---|
8 |
|
---|
9 | import java.util.HashMap;
|
---|
10 |
|
---|
11 | import javax.swing.JFrame;
|
---|
12 | import javax.swing.JTextArea;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.greenstone.util.SafeProcess;
|
---|
16 |
|
---|
17 | public class Command implements Runnable
|
---|
18 | {
|
---|
19 | HashMap<String, String> _osCommands = new HashMap<String, String>();
|
---|
20 | CommandStep _parent = null;
|
---|
21 |
|
---|
22 | public Command(Element commandElement, CommandStep parent)
|
---|
23 | {
|
---|
24 | _parent = parent;
|
---|
25 |
|
---|
26 | if(commandElement != null){
|
---|
27 | Element[] commands = ExtXMLHelper.getMultipleChildElements(commandElement, "os", true);
|
---|
28 | for(int i = 0; i < commands.length; i++){
|
---|
29 | Element currentCommand = commands[i];
|
---|
30 | String os = currentCommand.getAttribute("name");
|
---|
31 |
|
---|
32 | if(os.equals("")){
|
---|
33 | System.err.println("A <" + ExtXMLHelper.COMMAND + "> element does not have an \"os\" attribute");
|
---|
34 | }
|
---|
35 | else{
|
---|
36 | _osCommands.put(os, ExtXMLHelper.getValueFromSingleElement(currentCommand, false));
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|
40 | else{
|
---|
41 | System.err.println("This <" + ExtXMLHelper.COMMAND + "> element is null");
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void run()
|
---|
46 | {
|
---|
47 | JTextArea messageArea = _parent.getMessageArea();
|
---|
48 | String command = getCommandForCurrentOS();
|
---|
49 |
|
---|
50 | if(command == null){
|
---|
51 | 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");
|
---|
52 | _parent.threadError();
|
---|
53 | return;
|
---|
54 | }
|
---|
55 | else if(command == ""){
|
---|
56 | _parent.threadError();
|
---|
57 | return;
|
---|
58 | }
|
---|
59 |
|
---|
60 | File workingDirectory = new File(_parent.getParent().getParent().getExtensionDirectory());
|
---|
61 | SafeProcess commandLineProc = null;
|
---|
62 | try{
|
---|
63 | commandLineProc = null;
|
---|
64 | String[] args = new String[3];
|
---|
65 |
|
---|
66 | if(System.getProperty("os.name").contains("Windows")){
|
---|
67 | args[0] = "cmd.exe";
|
---|
68 | args[1] = "/C";
|
---|
69 | args[2] = command;
|
---|
70 |
|
---|
71 | String allArgs = new String();
|
---|
72 | for(int i = 0; i < args.length; i++){
|
---|
73 | if(i != 0){allArgs += " ";}
|
---|
74 | allArgs += args[i];
|
---|
75 | }
|
---|
76 |
|
---|
77 | messageArea.append("\nExecuting \"" + allArgs + "\" on the command line\n");
|
---|
78 |
|
---|
79 | //commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);
|
---|
80 | }
|
---|
81 | else{
|
---|
82 | args[0] = "sh";
|
---|
83 | args[1] = "-c";
|
---|
84 | args[2] = command;
|
---|
85 |
|
---|
86 | String allArgs = new String();
|
---|
87 | for(int i = 0; i < args.length; i++){
|
---|
88 | if(i != 0){allArgs += " ";}
|
---|
89 | allArgs += args[i];
|
---|
90 | }
|
---|
91 |
|
---|
92 | messageArea.append("\nExecuting \"" + allArgs + "\" on the command line\n");
|
---|
93 | //commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);
|
---|
94 | }
|
---|
95 |
|
---|
96 | commandLineProc = new SafeProcess(args, null, workingDirectory);
|
---|
97 |
|
---|
98 | /*
|
---|
99 | BufferedReader stdInput = new BufferedReader(new InputStreamReader(commandLineProc.getInputStream()));
|
---|
100 |
|
---|
101 | Thread stdPrinter = new PrinterThread(messageArea, stdInput);
|
---|
102 | stdPrinter.start();
|
---|
103 |
|
---|
104 | BufferedReader stdError = new BufferedReader(new InputStreamReader(commandLineProc.getErrorStream()));
|
---|
105 |
|
---|
106 | Thread errPrinter = new PrinterThread(messageArea, stdError);
|
---|
107 | errPrinter.start();
|
---|
108 |
|
---|
109 | int success = commandLineProc.waitFor();
|
---|
110 | */
|
---|
111 |
|
---|
112 | // Replacing the above and its use of the PrinterThread inner class with SafeProcess.java:
|
---|
113 | SafeProcess.LineByLineHandler outLineHandler = new ProcessLineHandler(messageArea, SafeProcess.STDOUT);
|
---|
114 | SafeProcess.LineByLineHandler errLineHandler = new ProcessLineHandler(messageArea, SafeProcess.STDERR);
|
---|
115 |
|
---|
116 | int success = commandLineProc.runProcess(outLineHandler, errLineHandler);
|
---|
117 |
|
---|
118 | if(success != 0){
|
---|
119 | System.err.println("Command line process \"" + command + "\" returned unsuccessfully with the value \"" + success + "\"");
|
---|
120 | _parent.threadError();
|
---|
121 | return;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | catch(Exception ex){
|
---|
125 | ex.printStackTrace();
|
---|
126 | _parent.threadError();
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | _parent.startNextThread();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public boolean executeCommand()
|
---|
133 | {
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public CommandStep getParent()
|
---|
138 | {
|
---|
139 | return _parent;
|
---|
140 | }
|
---|
141 |
|
---|
142 | public String getCommandForCurrentOS()
|
---|
143 | {
|
---|
144 | String currentos = System.getProperty("os.name");
|
---|
145 |
|
---|
146 | String command = _osCommands.get(currentos);
|
---|
147 | if(command != null){
|
---|
148 | return command;
|
---|
149 | }
|
---|
150 |
|
---|
151 | if(currentos.contains("Windows")){
|
---|
152 | command = _osCommands.get("Windows");
|
---|
153 |
|
---|
154 | if(command != null){
|
---|
155 | return command;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | return _osCommands.get("default");
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | public class PrinterThread extends Thread
|
---|
163 | {
|
---|
164 | JTextArea _messageArea = null;
|
---|
165 | BufferedReader _output = null;
|
---|
166 |
|
---|
167 | public PrinterThread(JTextArea messageArea, BufferedReader output)
|
---|
168 | {
|
---|
169 | _messageArea = messageArea;
|
---|
170 | _output = output;
|
---|
171 | }
|
---|
172 |
|
---|
173 | public void run()
|
---|
174 | {
|
---|
175 | String s = "";
|
---|
176 |
|
---|
177 | try{
|
---|
178 | while ((s = _output.readLine()) != null) {
|
---|
179 | _messageArea.append(s + "\n");
|
---|
180 | _messageArea.setSelectionEnd(_messageArea.getDocument().getLength());
|
---|
181 | }
|
---|
182 | }
|
---|
183 | catch(Exception ex){
|
---|
184 | ex.printStackTrace();
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }*/
|
---|
188 |
|
---|
189 | public class ProcessLineHandler extends SafeProcess.LineByLineHandler
|
---|
190 | {
|
---|
191 | // These members need to be final in order to synchronize on them
|
---|
192 | private final JTextArea _messageArea;
|
---|
193 |
|
---|
194 | public ProcessLineHandler(JTextArea messageArea, int src)
|
---|
195 | {
|
---|
196 | super(src); // will set this.source to STDERR or STDOUT
|
---|
197 | _messageArea = messageArea;
|
---|
198 | }
|
---|
199 |
|
---|
200 | public void gotLine(String line) { // first non-null line
|
---|
201 |
|
---|
202 | // messageArea needs to be synchronized, since both the process'
|
---|
203 | // stderr and stdout will be attempting to append to it
|
---|
204 |
|
---|
205 | synchronized(_messageArea) {
|
---|
206 | _messageArea.append(line + "\n");
|
---|
207 | _messageArea.setSelectionEnd(_messageArea.getDocument().getLength());
|
---|
208 | }
|
---|
209 | }
|
---|
210 | public void gotException(Exception e) {
|
---|
211 | e.printStackTrace();
|
---|
212 | }
|
---|
213 |
|
---|
214 | }
|
---|
215 | }
|
---|