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

Last change on this file since 20918 was 20918, checked in by ak19, 14 years ago

Changes to previous commits (to handle spaces in filepaths on Windows) so that things still work on Linux

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