source: greenstone3/trunk/src/java/org/greenstone/server/BrowserLauncher.java@ 20899

Last change on this file since 20899 was 20899, checked in by ak19, 15 years ago

Updated so that the GSI dialog does not silently fail when it fails to launch the browser.

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