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

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

Allows "Windows" as a valid os name for <os> tags

File size: 3.5 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 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 currentos = System.getProperty("os.name");
114
115 String command = (String)_osCommands.get(currentos);
116 if(command != null){
117 return command;
118 }
119
120 if(currentos.contains("Windows")){
121 command = (String)_osCommands.get("Windows");
122
123 if(command != null){
124 return command;
125 }
126 }
127 return (String)_osCommands.get("default");
128 }
129}
Note: See TracBrowser for help on using the repository browser.