1 | package org.greenstone.server;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.util.Misc;
|
---|
4 |
|
---|
5 |
|
---|
6 | public class BrowserLauncher
|
---|
7 | extends Thread {
|
---|
8 |
|
---|
9 | private Process process = null;
|
---|
10 | /** The initial command string given to this sub-process. */
|
---|
11 | private String command = null;
|
---|
12 | private String url = null;
|
---|
13 | private String[] commands = null;
|
---|
14 |
|
---|
15 | public BrowserLauncher(String url) {
|
---|
16 | this.url = url;
|
---|
17 | setBrowserCommand();
|
---|
18 | System.err.println("new browser launcher");
|
---|
19 | printArray(commands);
|
---|
20 | }
|
---|
21 |
|
---|
22 | // we should try and use settings from the settings panel??
|
---|
23 | protected void setBrowserCommand() {
|
---|
24 |
|
---|
25 | // Try from a config setting??
|
---|
26 |
|
---|
27 | if(Misc.isWindows()) {
|
---|
28 | // we use cmd and start
|
---|
29 | if (Misc.isWindows9x()) {
|
---|
30 | this.commands = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
|
---|
31 | } else {
|
---|
32 | this.commands = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
|
---|
33 | }
|
---|
34 | } else if (Misc.isMac()) {
|
---|
35 | this.commands = StaticStrings.MAC_OPEN_COMMAND; // "open %1"
|
---|
36 | } else {
|
---|
37 | // try mozilla for now
|
---|
38 | this.commands = new String [] {"mozilla", "%1"};
|
---|
39 | // // we try to look for a browser
|
---|
40 | // String [] browsers = new String [] {"mozilla", "netscape"};
|
---|
41 | // for (int i=0; i<browsers.length; i++) {
|
---|
42 | // if (isAvailable(browsers[i])) {
|
---|
43 | // command = browsers[i]+ " %1";
|
---|
44 | // break;
|
---|
45 | // }
|
---|
46 | // }
|
---|
47 | }
|
---|
48 |
|
---|
49 | for (int i=0; i<commands.length; i++) {
|
---|
50 | if (commands[i].equals("%1")) {
|
---|
51 | commands[i] = url;
|
---|
52 | break;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | }
|
---|
58 |
|
---|
59 | // protected boolean isAvailable(String program) {
|
---|
60 | // try {
|
---|
61 | // ExternalProgram e = new ExternalProgram("which", program);
|
---|
62 | // e.exitProgram();
|
---|
63 | // String out = e.getLineOfProgramOutput();
|
---|
64 | // if (out == null) {
|
---|
65 | // return false;
|
---|
66 | // }
|
---|
67 | // return true;
|
---|
68 | // } catch (Exception exc) {
|
---|
69 | // return false;
|
---|
70 | // }
|
---|
71 | // }
|
---|
72 |
|
---|
73 | /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
|
---|
74 | * @see java.lang.Exception
|
---|
75 | * @see java.lang.Process
|
---|
76 | * @see java.lang.Runtime
|
---|
77 | * @see java.lang.System
|
---|
78 | * @see java.util.Vector
|
---|
79 | */
|
---|
80 | public void run() {
|
---|
81 | System.err.println("in run method");
|
---|
82 | // Call an external process using the args.
|
---|
83 | if(commands == null) {
|
---|
84 | //apps.remove(this);
|
---|
85 | return;
|
---|
86 | }
|
---|
87 | try {
|
---|
88 | String prog_name = commands[0];
|
---|
89 | String lower_name = prog_name.toLowerCase();
|
---|
90 | if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
|
---|
91 | System.err.println("found mozilla or netscape, trying remote it");
|
---|
92 | // mozilla and netscape, try using a remote command to get things in the same window
|
---|
93 | String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
|
---|
94 | printArray(new_commands);
|
---|
95 |
|
---|
96 | Runtime rt = Runtime.getRuntime();
|
---|
97 | process = rt.exec(new_commands);
|
---|
98 | int exitCode = process.waitFor();
|
---|
99 | if (exitCode != 0) { // if Netscape or mozilla was not open
|
---|
100 | System.err.println("couldn't do remote, trying original command");
|
---|
101 | printArray(commands);
|
---|
102 | process = rt.exec(commands); // try the original command
|
---|
103 | }
|
---|
104 | } else {
|
---|
105 | // just run what we have been given
|
---|
106 | StringBuffer whole_command = new StringBuffer();
|
---|
107 | for(int i = 0; i < commands.length; i++) {
|
---|
108 | whole_command.append(commands[i]);
|
---|
109 | whole_command.append(" ");
|
---|
110 | }
|
---|
111 | System.err.println("Running " + whole_command.toString());
|
---|
112 | Runtime rt = Runtime.getRuntime();
|
---|
113 | process = rt.exec(commands);
|
---|
114 | process.waitFor();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | catch (Exception exception) {
|
---|
119 | exception.printStackTrace();
|
---|
120 | }
|
---|
121 | // Remove ourself from Gatherer list of threads.
|
---|
122 | //apps.remove(this);
|
---|
123 | // Call exit if we were the last outstanding child process thread.
|
---|
124 | //if (apps.size() == 0 && exit == true) {
|
---|
125 | // System.exit(exit_status);
|
---|
126 | //}
|
---|
127 | }
|
---|
128 | public void printArray(String [] array) {
|
---|
129 | for(int i = 0; i < array.length; i++) {
|
---|
130 | System.err.println(array[i]+" ");
|
---|
131 | }
|
---|
132 | }
|
---|
133 | public void stopBrowserApplication() {
|
---|
134 | if(process != null) {
|
---|
135 | process.destroy();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | }
|
---|