source: main/trunk/greenstone3/src/java/org/greenstone/util/BrowserLauncher.java@ 31665

Last change on this file since 31665 was 31665, checked in by ak19, 7 years ago

GS3 source code now updated to use SafeProcess instead of Process (calling Runtime.exec() directly). The use of SafeProcess in RunTarget and BrowserLauncher has been tested on Linux. GDBMWrapper, MapRetrieve and admin/guiext's Command.java are not tested. For GDBMWrapper, because it's in a bit of code that works with txtgz databases. MapRetrive.java and Command.java are untested because I don't know how to test them.

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