package be.ac.ulb.code.wit;

import java.io.IOException;

import org.glassfish.grizzly.http.server.HttpServer;

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;

/**
 * Entry-point for the RESTful server.
 */
public final class ResourcesServer {
	private ResourcesServer() { /* Do not instantiate */
	}

	protected static HttpServer startServer() throws IOException {
		System.out.println("Starting grizzly...");
		ResourceConfig rc = new PackagesResourceConfig(
				"be.ac.ulb.code.wit.resources");
		return GrizzlyServerFactory.createHttpServer(
				ServiceConfiguration.BASE_URI, rc);
	}

	/**
	 * Entry point of the server. This create a grizzly server with jax-rs
	 * support, and specify the handlers to be used.
	 * 
	 * @param args
	 *            Unused.
	 * @throws IOException
	 *             Thrown when the server fails to run.
	 */
	public static void main(final String[] args) throws IOException {
		HttpServer httpServer = startServer();
		System.out.println(String.format(
				"Jersey app started with WADL available at "
						+ "%s/application.wadl\nHit enter to stop it...",
				ServiceConfiguration.BASE_URI));
		System.in.read();
		httpServer.stop();
	}
}
