package fi.csc.chipster.toolbox;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import fi.csc.chipster.rest.Config;
import fi.csc.chipster.rest.RestUtils;
import fi.csc.chipster.toolbox.resource.ModuleResource;
import fi.csc.chipster.toolbox.resource.ToolResource;

/**
 * Main class.
 *
 */
public class ToolboxService {

	@SuppressWarnings("unused")
	private Logger logger = LogManager.getLogger();

	private Config config;

	public ToolboxService(Config config) {
		this.config = config;
	}

    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this
	 * application.
     * @return Grizzly HTTP server.
     * @throws URISyntaxException 
     */
    public HttpServer startServer() throws IOException, URISyntaxException {

    	Toolbox toolbox = new Toolbox(new File("../chipster-tools/modules"));
    	final ResourceConfig rc = RestUtils.getDefaultResourceConfig()
        	.register(new ToolResource(toolbox))
        	.register(new ModuleResource(toolbox));
        	//.register(new LoggingFilter())

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
        return GrizzlyHttpServerFactory.createHttpServer(
                URI.create(getBaseUri()), rc);
    }

    /**
     * Main method.
     * @throws URISyntaxException 
     */
    public static void main(String[] args) throws IOException, URISyntaxException {

        final HttpServer server = new ToolboxService(new Config()).startServer();
        RestUtils.waitForShutdown("toolbox", server);
    }

	public void close() {
	}

	public final String getBaseUri() {
		return this.config.getString("toolbox-bind");
	}
}


@@ -743,16 +743,49 @@
 config;%0A
+%09private HttpServer httpServer;%0A%09
 %0A%09public
@@ -1036,26 +1036,20 @@
  public 
-HttpServer
+void
  startSe
@@ -1093,24 +1093,59 @@
 xception %7B%0A%0A
+    %09// TODO service locator%0A    %09%0A
     %09Toolbox
@@ -1542,18 +1542,96 @@
     
-    return
+%09URI baseUri = URI.create(this.config.getString(%22toolbox-bind%22));%0A    %09this.httpServer =
  Gri
@@ -1673,49 +1673,15 @@
 ver(
-%0A                URI.create(getB
+b
 aseUri
-())
 , rc
@@ -1841,25 +1841,24 @@
 Exception %7B%0A
-%0A
         fina
@@ -1859,26 +1859,30 @@
   final 
-Http
+Toolbox
 Serv
+ic
 e
-r
  server 
@@ -1915,16 +1915,32 @@
 onfig())
+;%0A        server
 .startSe
@@ -2002,131 +2002,174 @@
 rver
-);%0A    %7D%0A%0A%09public void close() %7B%0A%09%7D%0A%0A%09public final String getBaseUri() %7B%0A%09%09return this.config.getString(%22toolbox-bind%22
+.getHttpServer());%0A    %7D%0A%0A%09private HttpServer getHttpServer() %7B%0A%09%09return this.httpServer;%0A%09%7D%0A%0A%09public void close() %7B%0A%09%09RestUtils.shutdown(%22toolbox%22, httpServer
 );%0A%09%7D%0A
+%09%0A
 %7D%0A%0A

Refactor ToolboxService basics