package de.avgl.dmp.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Properties;

import javax.ws.rs.core.UriBuilder;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import com.fasterxml.jackson.core.JsonGenerator;

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

	private static final org.apache.log4j.Logger	log	= org.apache.log4j.Logger.getLogger(Main.class);

	// Base URI the Grizzly HTTP server will listen on
	public static final String						BASE_URI;

	static {

		final InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("dmp.properties");
		final Properties properties = new Properties();

		try {

			properties.load(inStream);
		} catch (IOException e) {

			log.debug("could not load properties");
		}

		final String host = properties.getProperty("backend_http_server_host");
		final int port = Integer.valueOf(properties.getProperty("backend_http_server_port")).intValue();

		final URI baseUri = UriBuilder.fromUri("http://" + host).port(port).path("dmp/").build();

		BASE_URI = baseUri.toString();
	}

	/**
	 * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
	 *
	 * @return Grizzly HTTP server.
	 */
	public static HttpServer startServer() {
		// create a resource config that scans for JAX-RS resources and
		// providers
		// in de.avgl.dmp.controller.resources package
		final ResourceConfig rc = new ResourceConfig()
		// .register(JacksonJaxbJsonProvider.class)
				.packages("de.avgl.dmp.controller.resources");

		// create and start a new instance of grizzly http server
		// exposing the Jersey application at BASE_URI
		final HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);

		return httpServer;
	}

	/**
	 * Main method.
	 *
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		final HttpServer server = startServer();
		System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
		System.in.read();
		server.stop();
	}
}

@@ -353,58 +353,8 @@
 g;%0A%0A
-import com.fasterxml.jackson.core.JsonGenerator;%0A%0A
 /**%0A
@@ -557,54 +557,487 @@
 lic 
-static final String%09%09%09%09%09%09BASE_URI;%0A%0A%09static %7B%0A
+final String%09%09%09%09%09%09BASE_URI;%0A%0A%09public Main(Properties properties) %7B%0A%09%09final String host = properties.getProperty(%22backend_http_server_host%22);%0A%09%09final int port = Integer.valueOf(properties.getProperty(%22backend_http_server_port%22));%0A%0A%09%09final URI baseUri = UriBuilder.fromUri(%22http://%22 + host).port(port).path(%22dmp/%22).build();%0A%0A%09%09BASE_URI = baseUri.toString();%0A%09%7D%0A%0A%09public String getBaseUri() %7B%0A%09%09return BASE_URI;%0A%09%7D%0A%0A%09private static Properties loadProperties(String propertiesPath) %7B
 %0A%09%09f
@@ -1135,13 +1135,8 @@
 eam(
-%22dmp.
 prop
@@ -1137,25 +1137,28 @@
 m(properties
-%22
+Path
 );%0A%09%09final P
@@ -1321,310 +1321,126 @@
 %0A%0A%09%09
-final String host = properties.getProperty(%22backend_http_server_host%22);%0A%09%09final int port = Integer.valueOf(properties.getProperty(%22backend_http_server_port%22)).intValue();%0A%0A%09%09final URI baseUri = UriBuilder.fromUri(%22http://%22 + host).port(port).path(%22dmp/%22).build();%0A%0A%09%09BASE_URI = baseUri.toString(
+return properties;%0A%09%7D%0A%0A%09private static Properties loadProperties() %7B%0A%09%09return loadProperties(%22dmp.properties%22
 );%0A%09%7D%0A%0A
+%0A%0A
 %09/**
@@ -1576,23 +1576,16 @@
 %09public 
-static 
 HttpServ
@@ -1782,16 +1782,18 @@
 onfig()%0A
+%09%09
 %09%09// .re
@@ -1995,39 +1995,17 @@
 URI%0A
+%0A
 %09%09
-final HttpServer httpServer =
+return
  Gri
@@ -2074,28 +2074,336 @@
 c);%0A
-%0A%09%09return httpServer
+%09%7D%0A%0A%09public static Main create(int port) %7B%0A%09%09final Properties properties = loadProperties();%0A%09%09properties.setProperty(%22backend_http_server_port%22, Integer.valueOf(port).toString());%0A%0A%09%09return new Main(properties);%0A%09%7D%0A%0A%09public static Main create() %7B%0A%09%09final Properties properties = loadProperties();%0A%0A%09%09return new Main(properties)
 ;%0A%09%7D
@@ -2536,16 +2536,51 @@
 ption %7B%0A
+%09%09final Main main = Main.create();%0A
 %09%09final 
@@ -2599,16 +2599,21 @@
 erver = 
+main.
 startSer
@@ -2748,24 +2748,33 @@
 it...%22, 
-BASE_URI
+main.getBaseUri()
 ));%0A%09%09Sy

refactor Jersey Main to allow specifying a port