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 | public 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 | messageArea.append("\nExecuting \"" + command + "\" on the command line\n");
|
---|
62 | commandLineProc = Runtime.getRuntime().exec(command, null, workingDirectory);
|
---|
63 |
|
---|
64 | BufferedReader stdInput = new BufferedReader(new InputStreamReader(commandLineProc.getInputStream()));
|
---|
65 | BufferedReader errInput = new BufferedReader(new InputStreamReader(commandLineProc.getErrorStream()));
|
---|
66 | String s= "";
|
---|
67 |
|
---|
68 | while ((s = stdInput.readLine()) != null) {
|
---|
69 | messageArea.append(s + "\n");
|
---|
70 | messageArea.setSelectionEnd(messageArea.getDocument().getLength());
|
---|
71 | }
|
---|
72 |
|
---|
73 | boolean error = false;
|
---|
74 | while ((s = errInput.readLine()) != null){
|
---|
75 | messageArea.append(s + "\n");
|
---|
76 | error = true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if(error){
|
---|
80 | JOptionPane.showMessageDialog(new JFrame(), "There was an error while running the command line process");
|
---|
81 | _parent.threadError();
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | int success = commandLineProc.waitFor();
|
---|
86 |
|
---|
87 | if(success != 0){
|
---|
88 | System.err.println("Command line process \"" + command + "\" returned unsuccessfully with the value \"" + success + "\"");
|
---|
89 | _parent.threadError();
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | catch(Exception ex){
|
---|
94 | ex.printStackTrace();
|
---|
95 | _parent.threadError();
|
---|
96 | return;
|
---|
97 | }
|
---|
98 | _parent.startNextThread();
|
---|
99 | }
|
---|
100 |
|
---|
101 | public boolean executeCommand()
|
---|
102 | {
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public CommandStep getParent()
|
---|
107 | {
|
---|
108 | return _parent;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public String getCommandForCurrentOS()
|
---|
112 | {
|
---|
113 | String command = (String)_osCommands.get(System.getProperty("os.name"));
|
---|
114 | if(command != null){
|
---|
115 | return command;
|
---|
116 | }
|
---|
117 | return (String)_osCommands.get("default");
|
---|
118 | }
|
---|
119 | } |
---|