[13229] | 1 | package org.greenstone.server;
|
---|
| 2 |
|
---|
[13564] | 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.io.InputStream;
|
---|
| 5 | import java.io.InputStreamReader;
|
---|
| 6 |
|
---|
[13229] | 7 | import org.greenstone.gsdl3.util.Misc;
|
---|
[13564] | 8 | import org.apache.log4j.*;
|
---|
[13229] | 9 |
|
---|
| 10 | public class BrowserLauncher
|
---|
| 11 | extends Thread {
|
---|
| 12 |
|
---|
[13564] | 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 = "";
|
---|
[13229] | 21 |
|
---|
[13564] | 22 | public BrowserLauncher(String browserPath, String url) {
|
---|
[13229] | 23 | this.url = url;
|
---|
[13564] | 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 | }
|
---|
[13229] | 32 | }
|
---|
| 33 |
|
---|
[13564] | 34 | public BrowserLauncher(){
|
---|
| 35 |
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[13229] | 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()) {
|
---|
[18558] | 43 | this.command = BaseServer.Property.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
|
---|
[13229] | 44 | } else {
|
---|
[18558] | 45 | this.command = BaseServer.Property.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
|
---|
[13229] | 46 | }
|
---|
| 47 | } else if (Misc.isMac()) {
|
---|
[18558] | 48 | this.command = BaseServer.Property.MAC_OPEN_COMMAND; // "open %1"
|
---|
[13229] | 49 | } else {
|
---|
[13564] | 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 | }
|
---|
[13229] | 57 | }
|
---|
| 58 |
|
---|
[13564] | 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;
|
---|
[13229] | 77 | }
|
---|
[13564] | 78 | }
|
---|
[13229] | 79 |
|
---|
[13602] | 80 | public int getBrowserState(){
|
---|
[13564] | 81 | return state;
|
---|
[13229] | 82 | }
|
---|
[13564] | 83 |
|
---|
[13229] | 84 | public void run() {
|
---|
| 85 | // Call an external process using the args.
|
---|
[13564] | 86 | if(command.equals("")) {
|
---|
| 87 | state = LAUNCHFAILED;
|
---|
| 88 | logger.error("launching command is empty");
|
---|
[13229] | 89 | return;
|
---|
| 90 | }
|
---|
[13564] | 91 |
|
---|
| 92 | try {
|
---|
| 93 | String prog_name = this.command.substring(0,this.command.indexOf(" "));
|
---|
[13229] | 94 | String lower_name = prog_name.toLowerCase();
|
---|
[13564] | 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);
|
---|
[13229] | 100 | Runtime rt = Runtime.getRuntime();
|
---|
[13564] | 101 | Process process = rt.exec(new_command);
|
---|
| 102 | state = LAUNCHSUCCESS;
|
---|
[13229] | 103 | int exitCode = process.waitFor();
|
---|
[13564] | 104 | logger.info("ExitCode:" + exitCode);
|
---|
[13229] | 105 | if (exitCode != 0) { // if Netscape or mozilla was not open
|
---|
[13564] | 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);
|
---|
[13229] | 113 | }
|
---|
| 114 | } else {
|
---|
[13564] | 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 |
|
---|
[13229] | 123 | }
|
---|
| 124 | }
|
---|
[13564] | 125 | catch (Exception e) {
|
---|
| 126 | logger.error(e);
|
---|
| 127 | state = LAUNCHFAILED;
|
---|
[13229] | 128 | }
|
---|
[13564] | 129 |
|
---|
[13229] | 130 | }
|
---|
| 131 | }
|
---|