1 | package org.greenstone.util;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.InputStream;
|
---|
5 | import java.io.InputStreamReader;
|
---|
6 |
|
---|
7 | import org.greenstone.server.BaseProperty;
|
---|
8 | import org.greenstone.util.Misc;
|
---|
9 | import org.apache.log4j.*;
|
---|
10 |
|
---|
11 | public class BrowserLauncher
|
---|
12 | extends Thread {
|
---|
13 | private String url = "about:blank" ;
|
---|
14 | static Logger logger = Logger.getLogger(org.greenstone.util.BrowserLauncher.class.getName());
|
---|
15 | private String[] default_browsers = new String[]{"firefox","mozilla"};
|
---|
16 | private String command = "";
|
---|
17 | private int state = -1; //0: launch success, 1: launch failed
|
---|
18 | public final static int LAUNCHSUCCESS = 0;
|
---|
19 | public final static int LAUNCHFAILED = 1;
|
---|
20 | private String browserPath = "";
|
---|
21 |
|
---|
22 | public BrowserLauncher(String browserPath, String url) {
|
---|
23 | this.url = url;
|
---|
24 | this.browserPath = browserPath;
|
---|
25 | //use the default browser
|
---|
26 | if (this.browserPath.equals("")){
|
---|
27 | setBrowserCommand();
|
---|
28 | }
|
---|
29 | else{
|
---|
30 | this.command = this.browserPath + " " + this.url;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public BrowserLauncher(){
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | // we should try and use settings from the settings panel??
|
---|
39 | protected void setBrowserCommand() {
|
---|
40 | if(Misc.isWindows()) {
|
---|
41 | // we use cmd and start
|
---|
42 | if (Misc.isWindows9x()) {
|
---|
43 | this.command = BaseProperty.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
|
---|
44 | } else {
|
---|
45 | this.command = BaseProperty.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
|
---|
46 | }
|
---|
47 | } else if (Misc.isMac()) {
|
---|
48 | this.command = BaseProperty.MAC_OPEN_COMMAND; // "open %1"
|
---|
49 | } else {
|
---|
50 | // we try to look for a browser
|
---|
51 | for (int i=0; i<default_browsers.length; i++) {
|
---|
52 | if (isAvailable(default_browsers[i])) {
|
---|
53 | this.command = default_browsers[i] + " %1";
|
---|
54 | break;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | this.command = this.command.replaceAll("%1",this.url);
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected boolean isAvailable(String program) {
|
---|
63 | try {
|
---|
64 | Runtime run = Runtime.getRuntime();
|
---|
65 | Process process = run.exec("which "+ program);
|
---|
66 | BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
---|
67 | String line = null;
|
---|
68 | while ((line = br.readLine()) != null){
|
---|
69 | if (line.indexOf("no "+program) !=-1){
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | return true;
|
---|
75 | } catch (Exception e) {
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public int getBrowserState(){
|
---|
81 | return state;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void run() {
|
---|
85 | // Call an external process using the args.
|
---|
86 | if(command.equals("")) {
|
---|
87 | state = LAUNCHFAILED;
|
---|
88 | logger.error("launching command is empty: no browser found.");
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | try {
|
---|
93 | int exitCode = 0;
|
---|
94 | String prog_name = this.command.substring(0,this.command.indexOf(" "));
|
---|
95 | String lower_name = prog_name.toLowerCase();
|
---|
96 | if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("firefox") != -1) {
|
---|
97 | logger.info("found mozilla or firefox, trying to remotely launch it");
|
---|
98 | // mozilla and netscape, try using a remote command to get things in the same window
|
---|
99 | String new_command = prog_name +" -raise -remote openURL("+url+",new-tab)";
|
---|
100 | logger.info(new_command);
|
---|
101 | Runtime rt = Runtime.getRuntime();
|
---|
102 | Process process = rt.exec(new_command);
|
---|
103 | state = LAUNCHSUCCESS;
|
---|
104 | exitCode = process.waitFor();
|
---|
105 | logger.info("ExitCode:" + exitCode);
|
---|
106 | if (exitCode != 0) { // if Netscape or mozilla was not open
|
---|
107 | logger.info("couldn't do remote, trying original command");
|
---|
108 | logger.info(this.command);
|
---|
109 | process = rt.exec(this.command); // try the original command
|
---|
110 | state = LAUNCHSUCCESS;
|
---|
111 | //for some reason the following part is not executed sometimes.
|
---|
112 | exitCode = process.waitFor();
|
---|
113 | }
|
---|
114 | } else {
|
---|
115 | logger.info(this.command);
|
---|
116 | Runtime rt = Runtime.getRuntime();
|
---|
117 | Process process = rt.exec(this.command);
|
---|
118 | state = LAUNCHSUCCESS;
|
---|
119 | //for some reason the following part is not executed sometimes.
|
---|
120 | exitCode = process.waitFor();
|
---|
121 | }
|
---|
122 |
|
---|
123 | logger.info("ExitCode:" + exitCode);
|
---|
124 | if (exitCode != 0) { // if trying to launch the browser above failed
|
---|
125 | state = LAUNCHFAILED;
|
---|
126 | logger.error("Failed to launch web browser when running command:");
|
---|
127 | logger.error("\t" + this.command);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | catch (Exception e) {
|
---|
131 | logger.error(e);
|
---|
132 | state = LAUNCHFAILED;
|
---|
133 | }
|
---|
134 |
|
---|
135 | }
|
---|
136 | }
|
---|