package be.ac.ulb.code.wit;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;

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

	/**
	 * 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 {
		final Map<String, String> initParams = new HashMap<String, String>();

		//Our resources are in the be.ac.ulb.code.wit.resources package
		initParams.put("com.sun.jersey.config.property.packages",
			"be.ac.ulb.code.wit.resources");

		System.out.println("Starting grizzly...");
		final SelectorThread threadSelector = GrizzlyWebContainerFactory.create(
			ServiceConfiguration.BASE_URI + "/", initParams);
		System.out.println(String.format(
			"Jersey app started with WADL available at %sapplication.wadl%n" +
			"Hit enter to stop it...", ServiceConfiguration.BASE_URI));
		System.in.read();
		threadSelector.stopEndpoint();
	}
}
