package fi.csc.chipster.rest;

import javax.servlet.ServletException;
import javax.websocket.DeploymentException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import fi.csc.chipster.auth.AuthenticationService;
import fi.csc.chipster.filebroker.FileBroker;
import fi.csc.chipster.proxy.ChipsterProxyServer;
import fi.csc.chipster.scheduler.Scheduler;
import fi.csc.chipster.servicelocator.ServiceLocator;
import fi.csc.chipster.sessiondb.RestException;
import fi.csc.chipster.sessiondb.SessionDb;

public class ServerLauncher {
	
	// this must not be static, otherwise logging configuration fails
	private final Logger logger = LogManager.getLogger();

	private AuthenticationService auth;
	private ServiceLocator serviceLocator;
	private SessionDb sessionDb;
	private Scheduler scheduler;
	private ChipsterProxyServer proxy;
	private FileBroker fileBroker;
	
	
	public ServerLauncher(Config config, boolean verbose) throws ServletException, DeploymentException, RestException, InterruptedException {
		if (verbose) {
			logger.info("starting authentication-service");
		}		
		auth = new AuthenticationService(config);
		auth.startServer();
		
		if (verbose) {
			logger.info("starting service-locator");
		}		
		serviceLocator = new ServiceLocator(config);
		serviceLocator.startServer();
		
		if (verbose) {
			logger.info("starting session-db");
		}		
		sessionDb = new SessionDb(config);
		sessionDb.startServer();
		
		if (verbose) {
			logger.info("starting file-broker");
		}		
		fileBroker = new FileBroker(config);
		fileBroker.startServer();
		
		if (verbose) {
			logger.info("starting scheduler");
		}		
		scheduler = new Scheduler(config);
		scheduler.startServer();
		
		if (verbose) {
			logger.info("starting proxy");
		}		
		proxy = new ChipsterProxyServer(config);
		proxy.startServer();
		
		if (verbose) {
			logger.info("up and running");
		}
	}		

	public void stop() {
		
		if (proxy != null) {
			proxy.close();
		}
		if (scheduler != null) {
			scheduler.close();			
		}
		if (fileBroker != null) {
			fileBroker.close();
		}
		if (sessionDb != null) {
			sessionDb.close();
		}
		if (serviceLocator != null) {
			serviceLocator.close();
		}
		if (auth != null) {
			auth.close();			
		}
	}	
		
	public static void main(String[] args) throws ServletException, DeploymentException, InterruptedException, RestException {
		Config config = new Config();
		new ServerLauncher(config, true);
	}

	public SessionDb getSessionDb() {
		return sessionDb;
	}
}

@@ -24,16 +24,81 @@
 .rest;%0A%0A
+import java.io.IOException;%0Aimport java.net.URISyntaxException;%0A%0A
 import j
@@ -120,32 +120,32 @@
 rvletException;%0A
-
 import javax.web
@@ -594,16 +594,63 @@
 ssionDb;
+%0Aimport fi.csc.chipster.toolbox.ToolboxService;
 %0A%0Apublic
@@ -935,16 +935,49 @@
 eduler;%0A
+%09private ToolboxService toolbox;%0A
 %09private
@@ -1172,24 +1172,57 @@
 tedException
+, IOException, URISyntaxException
  %7B%0A%09%09if (ver
@@ -1883,32 +1883,158 @@
 .startServer();%0A
+%0A%09%09if (verbose) %7B%0A%09%09%09logger.info(%22starting toolbox%22);%0A%09%09%7D%09%09%0A%09%09toolbox = new ToolboxService(config);%0A%09%09toolbox.startServer();%0A%0A
 %09%09%0A%09%09if (verbose
@@ -2273,24 +2273,76 @@
 lose();%0A%09%09%7D%0A
+%09%09if (toolbox != null) %7B%0A%09%09%09toolbox.close();%09%09%09%0A%09%09%7D%0A
 %09%09if (schedu
@@ -2609,16 +2609,16 @@
 %0A%09%7D%09%0A%09%09%0A
-
 %09public 
@@ -2726,24 +2726,57 @@
 estException
+, IOException, URISyntaxException
  %7B%0A%09%09Config 

Add toolbox to ServerLauncher