Changeset 13564


Ignore:
Timestamp:
2007-01-11T13:24:17+13:00 (15 years ago)
Author:
shaoqun
Message:

added the code to allow using user specified browser programe

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/server/BrowserLauncher.java

    r13229 r13564  
    11package org.greenstone.server;
    22
     3import java.io.BufferedReader;
     4import java.io.InputStream;
     5import java.io.InputStreamReader;
     6
    37import org.greenstone.gsdl3.util.Misc;
    4 
     8import org.apache.log4j.*;
    59
    610public class BrowserLauncher
    711    extends Thread {
    812   
    9     private Process process = null;
    10     /** The initial command string given to this sub-process. */
    11     private String command = null;
    12     private String url = null;
    13     private String[] commands = null;
     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 = "";
    1421
    15     public BrowserLauncher(String url) {
     22    public BrowserLauncher(String browserPath, String url) {
    1623    this.url = url;
    17     setBrowserCommand();
    18     System.err.println("new browser launcher");
    19     printArray(commands);
     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
    2036    }
    2137
    2238    // we should try and use settings from the settings panel??
    2339    protected void setBrowserCommand() {
    24 
    25     // Try from a config setting??
    26 
    2740    if(Misc.isWindows()) {
    2841        // we use cmd and start
    2942        if (Misc.isWindows9x()) {
    30         this.commands = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
     43        this.command = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
    3144        } else {
    32         this.commands = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
     45        this.command = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
    3346        }
    3447    } else if (Misc.isMac()) {
    35         this.commands = StaticStrings.MAC_OPEN_COMMAND; // "open %1"
     48        this.command = StaticStrings.MAC_OPEN_COMMAND; // "open %1"
    3649    } else {
    37         // try mozilla for now
    38         this.commands = new String [] {"mozilla", "%1"};
    39 //      // we try to look for a browser
    40 //      String [] browsers = new String [] {"mozilla", "netscape"};
    41 //      for (int i=0; i<browsers.length; i++) {
    42 //      if (isAvailable(browsers[i])) {
    43 //          command = browsers[i]+ " %1";
    44 //          break;
    45 //      }
    46 //      }
     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        }
    4757    }
    4858
    49     for (int i=0; i<commands.length; i++) {
    50         if (commands[i].equals("%1")) {
    51         commands[i] = url;
    52         break;
    53         }
    54     }
    55 
    56    
     59    this.command = this.command.replaceAll("%1",this.url);
    5760    }
    5861
    59 //     protected boolean isAvailable(String program) {
    60 //  try {
    61 //      ExternalProgram e = new ExternalProgram("which", program);
    62 //      e.exitProgram();
    63 //      String out = e.getLineOfProgramOutput();
    64 //      if (out == null) {
    65 //      return false;
    66 //      }
    67 //      return true;
    68 //  } catch (Exception exc) {
    69 //      return false;
    70 //  }
    71 //     }
    72 
    73     /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
    74      * @see java.lang.Exception
    75      * @see java.lang.Process
    76      * @see java.lang.Runtime
    77      * @see java.lang.System
    78      * @see java.util.Vector
    79      */
    80     public void run() {
    81     System.err.println("in run method");
    82     // Call an external process using the args.
    83     if(commands == null) {
    84         //apps.remove(this);
    85         return;
    86     }
     62    protected boolean isAvailable(String program) {
    8763    try {
    88         String prog_name = commands[0];
    89         String lower_name = prog_name.toLowerCase();
    90         if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
    91         System.err.println("found mozilla or netscape, trying remote it");
    92         // mozilla and netscape, try using a remote command to get things in the same window
    93         String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
    94         printArray(new_commands);
    95 
    96         Runtime rt = Runtime.getRuntime();
    97         process = rt.exec(new_commands);
    98         int exitCode = process.waitFor();
    99         if (exitCode != 0) { // if Netscape or mozilla was not open
    100             System.err.println("couldn't do remote, trying original command");
    101             printArray(commands);
    102             process = rt.exec(commands); // try the original command
    103         }
    104         } else {
    105         // just run what we have been given
    106         StringBuffer whole_command = new StringBuffer();
    107         for(int i = 0; i < commands.length; i++) {
    108             whole_command.append(commands[i]);
    109             whole_command.append(" ");
    110         }
    111         System.err.println("Running " + whole_command.toString());
    112         Runtime rt = Runtime.getRuntime();
    113         process = rt.exec(commands);
    114         process.waitFor();
    115         }
    116     }
    117        
    118     catch (Exception exception) {
    119         exception.printStackTrace();
    120     }
    121     // Remove ourself from Gatherer list of threads.
    122     //apps.remove(this);
    123     // Call exit if we were the last outstanding child process thread.
    124     //if (apps.size() == 0 && exit == true) {
    125     //    System.exit(exit_status);
    126     //}
    127     }
    128     public void printArray(String [] array) {
    129     for(int i = 0; i < array.length; i++) {
    130         System.err.println(array[i]+" ");
    131     }
    132     }
    133     public void stopBrowserApplication() {
    134     if(process != null) {
    135         process.destroy();
     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;
    13677    }
    13778    }
    13879
     80    public  int getState(){
     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    }
    139131}
Note: See TracChangeset for help on using the changeset viewer.