Index: /trunk/gsdl3/src/java/org/greenstone/server/BrowserLauncher.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/server/BrowserLauncher.java	(revision 13563)
+++ /trunk/gsdl3/src/java/org/greenstone/server/BrowserLauncher.java	(revision 13564)
@@ -1,139 +1,131 @@
 package org.greenstone.server;
 
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
 import org.greenstone.gsdl3.util.Misc;
-
+import org.apache.log4j.*;
 
 public class BrowserLauncher 
     extends Thread {
     
-    private Process process = null;
-    /** The initial command string given to this sub-process. */
-    private String command = null;
-    private String url = null;
-    private String[] commands = null;
+    private String url = "about:blank" ;
+    static Logger logger = Logger.getLogger(org.greenstone.server.BrowserLauncher.class.getName());
+    private String[] default_browsers = new String[]{"firefox","mozilla"};
+    private String command = "";
+    private int state = -1; //0: launch success, 1: launch failed 
+    public final  static int LAUNCHSUCCESS = 0;
+    public final static int LAUNCHFAILED = 1;
+    private String browserPath = ""; 
 
-    public BrowserLauncher(String url) {
+    public BrowserLauncher(String browserPath, String url) {
 	this.url = url;
-	setBrowserCommand();
-	System.err.println("new browser launcher");
-	printArray(commands);
+        this.browserPath = browserPath;
+        //use the default browser
+	if (this.browserPath.equals("")){
+	    setBrowserCommand();
+	}
+	else{
+	    this.command = this.browserPath + " " + this.url;
+	}
+    }
+
+    public BrowserLauncher(){
+
     }
 
     // we should try and use settings from the settings panel??
     protected void setBrowserCommand() {
-
-	// Try from a config setting??
-
 	if(Misc.isWindows()) {
 	    // we use cmd and start
 	    if (Misc.isWindows9x()) {
-		this.commands = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
+		this.command = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
 	    } else {
-		this.commands = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
+		this.command = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
 	    }
 	} else if (Misc.isMac()) {
-	    this.commands = StaticStrings.MAC_OPEN_COMMAND; // "open %1"
+	    this.command = StaticStrings.MAC_OPEN_COMMAND; // "open %1"
 	} else {
-	    // try mozilla for now
-	    this.commands = new String [] {"mozilla", "%1"};
-// 	    // we try to look for a browser
-// 	    String [] browsers = new String [] {"mozilla", "netscape"};
-// 	    for (int i=0; i<browsers.length; i++) {
-// 		if (isAvailable(browsers[i])) {
-// 		    command = browsers[i]+ " %1";
-// 		    break;
-// 		}
-// 	    }
+	     // we try to look for a browser
+ 	    for (int i=0; i<default_browsers.length; i++) {
+ 		if (isAvailable(default_browsers[i])) {
+ 		    this.command = default_browsers[i] + " %1";
+ 		    break;
+ 		}
+ 	    }
 	}
 
-	for (int i=0; i<commands.length; i++) {
-	    if (commands[i].equals("%1")) {
-		commands[i] = url;
-		break;
-	    }
-	}
-
-	
+	this.command = this.command.replaceAll("%1",this.url);
     }
 
-//     protected boolean isAvailable(String program) {
-// 	try {
-// 	    ExternalProgram e = new ExternalProgram("which", program);
-// 	    e.exitProgram();
-// 	    String out = e.getLineOfProgramOutput();
-// 	    if (out == null) {
-// 		return false;
-// 	    }
-// 	    return true;
-// 	} catch (Exception exc) {
-// 	    return false;
-// 	}
-//     }
-
-    /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
-     * @see java.lang.Exception
-     * @see java.lang.Process
-     * @see java.lang.Runtime
-     * @see java.lang.System
-     * @see java.util.Vector
-     */
-    public void run() {
-	System.err.println("in run method");
-	// Call an external process using the args.
-	if(commands == null) {
-	    //apps.remove(this);
-	    return;
-	}
+    protected boolean isAvailable(String program) {
 	try {
-	    String prog_name = commands[0];
-	    String lower_name = prog_name.toLowerCase();
-	    if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) { 
-		System.err.println("found mozilla or netscape, trying remote it");
-		// mozilla and netscape, try using a remote command to get things in the same window
-		String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
-		printArray(new_commands);
-
-		Runtime rt = Runtime.getRuntime();
-		process = rt.exec(new_commands);
-		int exitCode = process.waitFor();
-		if (exitCode != 0) { // if Netscape or mozilla was not open
-		    System.err.println("couldn't do remote, trying original command");
-		    printArray(commands);
-		    process = rt.exec(commands); // try the original command
-		}
-	    } else {
-		// just run what we have been given
-		StringBuffer whole_command = new StringBuffer();
-		for(int i = 0; i < commands.length; i++) {
-		    whole_command.append(commands[i]);
-		    whole_command.append(" ");
-		}
-		System.err.println("Running " + whole_command.toString());
-		Runtime rt = Runtime.getRuntime();
-		process = rt.exec(commands);
-		process.waitFor();
-	    }
-	}
-	    
-	catch (Exception exception) {
-	    exception.printStackTrace();
-	}
-	// Remove ourself from Gatherer list of threads.
-	//apps.remove(this);
-	// Call exit if we were the last outstanding child process thread.
-	//if (apps.size() == 0 && exit == true) {
-	//    System.exit(exit_status);
-	//}
-    }
-    public void printArray(String [] array) {
-	for(int i = 0; i < array.length; i++) {
-	    System.err.println(array[i]+" ");
-	}
-    }
-    public void stopBrowserApplication() {
-	if(process != null) {
-	    process.destroy();
+             Runtime run = Runtime.getRuntime();
+             Process process = run.exec("which "+ program);
+             BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
+	     String line = null;
+	     while ((line = br.readLine()) != null){
+		 if (line.indexOf("no "+program) !=-1){
+		     return false;
+		 }
+	     }    	
+	     
+	    return true;
+	} catch (Exception e) {
+	    return false;
 	}
     }
 
+    public  int getState(){
+	return state;
+    }
+    
+    public void run() {
+	// Call an external process using the args.
+	if(command.equals("")) {
+	    state = LAUNCHFAILED;
+            logger.error("launching command is empty");
+	    return;
+	}
+	
+        try {
+	    String prog_name = this.command.substring(0,this.command.indexOf(" "));
+	    String lower_name = prog_name.toLowerCase();
+	    if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("firefox") != -1) { 
+		logger.info("found mozilla or firefox, trying remote it");
+                // mozilla and netscape, try using a remote command to get things in the same window
+		String new_command = prog_name +" -raise -remote openURL("+url+",new-tab)";
+		logger.info(new_command);
+		Runtime rt = Runtime.getRuntime();
+		Process process = rt.exec(new_command);
+               	state = LAUNCHSUCCESS;
+		int exitCode = process.waitFor();
+		logger.info("ExitCode:" + exitCode);              
+		if (exitCode != 0) { // if Netscape or mozilla was not open
+		    logger.info("couldn't do remote, trying original command");
+                    logger.info(this.command);
+		    process = rt.exec(this.command); // try the original command
+                    state = LAUNCHSUCCESS;
+		    //for some reseaons the following part is not executed somtimes.
+                    exitCode = process.waitFor();
+                    logger.info("ExitCode:" + exitCode);
+		}
+	    } else {
+		logger.info(this.command);
+                Runtime rt = Runtime.getRuntime();
+		Process process = rt.exec(this.command);
+                state = LAUNCHSUCCESS;
+              	//for some reseaons the following part is not executed sometimes.
+              	int exitCode = process.waitFor();
+		logger.info("ExitCode:" + exitCode);
+		
+	    }
+	}
+	catch (Exception e) {
+	    logger.error(e);
+            state = LAUNCHFAILED;
+	}
+	
+    }
 }
