Index: /trunk/gsdl3/src/java/org/greenstone/server/Server.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/server/Server.java	(revision 13559)
+++ /trunk/gsdl3/src/java/org/greenstone/server/Server.java	(revision 13560)
@@ -7,57 +7,212 @@
 import java.io.InputStream;
 import java.util.Properties;
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.apache.log4j.*;
 
 import org.greenstone.gsdl3.util.Dictionary;
-
-//import org.greenstone.gsdl3.util.GlobalProperties;
-
-public class Server
-{
-    public static Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
+import org.greenstone.gsdl3.util.GlobalProperties;
+
+public class Server {
+     /** Ant command for managing Tomcat */
+    static final private String START_CMD = "start";
+    static final private String RESTART_CMD = "restart";
+    static final private String CONFIGURE_CMD = "configure";
+    static final private String STOP_CMD = "stop";
+
+    public static final int SERVER_STARTED = 0;
+    public static final int SERVER_START_FAILED = 1;
+    public static final int BROWSER_LAUNCHED = 2;
+    public static final int BROWSER_LAUNCH_FAILED = 3; 
+    
+    private int server_state = -1;    
+
+    private boolean configure_required = false;
+
     public static File build_properties_file;
-    public static String images_path; // should be in classpath??
     public static Properties build_properties;
     public static Dictionary dictionary;
-
+     
+    private ServerControl server_control;    
+
+    static Logger logger = Logger.getLogger(org.greenstone.server.Server.class.getName());
+    
     public Server(String gsdl3_src_home)
     {
-	//this.gsdl3_home = gsdl3_home;
-	//this.gsdl3_src_home = gsdl3_src_home;
-	
-	//this.global_properties=gsdl3_home+File.separatorChar+"WEB-INF"+File.separatorChar+"classes"+File.separatorChar+"global.properties";
+	
 	String build_properties_path=gsdl3_src_home+File.separatorChar+"build.properties";
 	this.build_properties_file = new File(build_properties_path);
 	if (!this.build_properties_file.exists()) {
-	    System.err.println("Can't find build.properties file "+build_properties_path);
+	    logger.fatal("Can't find build.properties file "+build_properties_path);
 	    System.exit(1);
 	}
+	
 	build_properties = new Properties();
 	reloadBuildProperties();
-	this.images_path = gsdl3_src_home+File.separatorChar+"resources"+File.separatorChar+"images"+File.separatorChar;
 	
 	// make it dynamic language
 	this.dictionary = new Dictionary("server", "en", this.getClass().getClassLoader());
-	ServerControl serverControl = new ServerControl();
-    }
-    
-    public static void reloadBuildProperties() {
+	
+	server_control = new ServerControl(this);
+	
+	String auto_start = build_properties.getProperty(StaticStrings.AUTOSTART_PROPERTY);
+	if (auto_start != null && auto_start.equals("true")) {
+	    restart();
+	}
+	else{
+	    start();
+	}  
+         
+   }
+
+    public int getServerState(){
+	return server_state;
+    }
+
+    private int runAnt(String cmd){
+	RunAnt runAnt = new RunAnt();
+	runAnt.setAntCmd(cmd);
+	runAnt.run();
+   	return runAnt.getState();
+    }
+
+
+    public void reconfigRequired(){
+	configure_required = true;
+    }
+
+    public void start() {
+        int state = -1;
+        server_state = -1;
+	server_control.updateControl();
+	stop();
+	
+  	// reconfigure if necessary
+        if (configure_required){
+	    server_control.displayMessage(dictionary.get("ServerControl.Configuring"));
+	    state = runAnt(CONFIGURE_CMD);
+	    
+	   if (state != RunAnt.SUCCESS){
+	       recordError(CONFIGURE_CMD);
+	   }
+	   GlobalProperties.reload(); // properties file may have changed, so reload it
+	   configure_required = false;
+	}
+	else{
+	    recordSuccess(CONFIGURE_CMD); 
+	}
+	
+	server_control.displayMessage(dictionary.get("ServerControl.Starting"));
+        state = runAnt(START_CMD);
+
+	if (state != RunAnt.SUCCESS){
+	    recordError(START_CMD);
+            server_state = SERVER_START_FAILED;  
+            
+	}	            
+	else{
+	    recordSuccess(START_CMD); 
+            server_state = SERVER_STARTED; 
+       	} 
+
+       	server_control.updateControl();
+    }
+    
+    private void recordError(String message){
+        message = dictionary.get("ServerControl.Error",new String[]{message,GlobalProperties.getGSDL3Home()});
+	server_control.displayMessage(message);
+	logger.error(dictionary.get("ServerControl.Failed",new String[]{message}));
+    }
+
+
+    private void recordError(String message, Exception e){
+        message = dictionary.get("ServerControl.Error",new String[]{message,GlobalProperties.getGSDL3Home()});
+	server_control.displayMessage(message);
+	logger.error(dictionary.get("ServerControl.Failed",new String[]{message}),e);
+    }
+
+    private void recordSuccess(String message){
+        message = dictionary.get("ServerControl.Success",new String[]{message});
+	server_control.displayMessage(message);
+	logger.info(message);
+    }
+
+   
+    public void launchBrowser() {
+        server_state = -1;
+        server_control.updateControl();   
+        String message = dictionary.get("ServerControl.LaunchingBrowser");
+    	server_control.displayMessage(message); 
+	String url = GlobalProperties.getGSDL3WebAddress()+ build_properties.getProperty(StaticStrings.DEFAULT_SERVLET_PROPERTY);
+	BrowserLauncher launcher = new BrowserLauncher(build_properties.getProperty(StaticStrings.BROWSER_PATH),url);
+        logger.info(message);
+
+	launcher.start();
+     
+        //wait for a while
+        while(launcher.getState() == -1){
+            try{
+		Thread.sleep(3000);
+	    }
+	    catch(Exception e){
+		logger.error(e);
+	    }
+         } 
+
+	if (launcher.getState() != BrowserLauncher.LAUNCHSUCCESS ){
+	    recordError(dictionary.get("ServerControl.LaunchBrowser")); 
+	    server_state = BROWSER_LAUNCH_FAILED; 
+	}
+	else{
+	    recordSuccess(dictionary.get("ServerControl.LaunchBrowser")); 
+	    server_state = BROWSER_LAUNCHED;
+	}    
+        
+	server_control.updateControl();        
+    }
+    
+    public void restart(){
+	start();       
+        if (server_state == SERVER_STARTED){
+	    launchBrowser();
+	}  
+    }
+
+    public void stop() {
+         server_control.displayMessage(dictionary.get("ServerControl.Stopping"));
+	 int state = runAnt(STOP_CMD);
+	
+        if (state != RunAnt.SUCCESS){
+	    recordError(STOP_CMD);
+	}
+	else{
+	    recordSuccess(STOP_CMD);
+	}
+
+    }
+    
+    public  void reloadBuildProperties() {
 	try {
-	    InputStream in = Class.forName("org.greenstone.server.Server").getClassLoader().getResourceAsStream("build.properties");
+	    InputStream in = this.getClass().getClassLoader().getResourceAsStream("build.properties");
+	    
 	    if (in != null) {
-		System.err.println("loading build properties");
+		logger.info("loading build properties");
 		build_properties.load(in);
 		in.close();
 	    } else {
-		System.err.println("couldn't load build properties!");
+		logger.error("couldn't load build properties!");
 	    }
 	} catch (Exception e) {
-	    System.err.println("Exception trying to reload build.properties: "+e);
-	}
-
-
-    }
+	    logger.error("Exception trying to reload build.properties: "+e);
+	}
+	
+    }
+
+  
+
     public static void main (String[] args){
-	System.out.println(args[0]);
-	if (args.length != 1) {
+    	if (args.length != 1) {
 	    System.err.println("Usage: java org.greenstone.server.ServerMain <gsdl3 src home> ");
 	    System.exit(1);
@@ -66,9 +221,9 @@
 	File gsdl3_src_dir = new File(args[0]);
 	if (!gsdl3_src_dir.isDirectory()) {
-	    System.err.println("Usage: java org.greenstone.server.ServerMain <gsdl3 src home> ");
 	    System.err.println("src directory does not exist!");
 	    System.exit(1);
 	}
-	Server server = new Server(args[0]);
+	
+	new Server(args[0]);
     }
 }
