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

Last change on this file since 18558 was 18558, checked in by davidb, 15 years ago

Restructuring of code to support a web server version for Greenstone2 and well as Greenstone3

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 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
13 private String url = "about:blank" ;
14 static Logger logger = Logger.getLogger(org.greenstone.server.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 = BaseServer.Property.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
44 } else {
45 this.command = BaseServer.Property.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
46 }
47 } else if (Misc.isMac()) {
48 this.command = BaseServer.Property.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");
89 return;
90 }
91
92 try {
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 remote 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 int 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 reseaons the following part is not executed somtimes.
111 exitCode = process.waitFor();
112 logger.info("ExitCode:" + exitCode);
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 reseaons the following part is not executed sometimes.
120 int exitCode = process.waitFor();
121 logger.info("ExitCode:" + exitCode);
122
123 }
124 }
125 catch (Exception e) {
126 logger.error(e);
127 state = LAUNCHFAILED;
128 }
129
130 }
131}
Note: See TracBrowser for help on using the repository browser.