// Path: src/main/java/uk/bl/odin/orcid/domain/CacheManager.java
// public class CacheManager {
// 
// 	private final Cache<SearchKey, OrcidSearchResults> searchCache;
// 
// 	private final Cache<String, OrcidProfile> profileCache;
// 
// 	@Inject
// 	public CacheManager(@Named("OrcidCacheTimeout") Integer timeOutInMinutes,
// 			@Named("OrcidCacheMaxsize") Integer maximumSize) {
// 		searchCache = CacheBuilder.newBuilder().expireAfterWrite(timeOutInMinutes, TimeUnit.MINUTES)
// 				.maximumSize(maximumSize).build();
// 		profileCache = CacheBuilder.newBuilder().expireAfterWrite(timeOutInMinutes, TimeUnit.MINUTES)
// 				.maximumSize(maximumSize).build();
// 	}
// 
// 	public Cache<SearchKey, OrcidSearchResults> getSearchCache() {
// 		return searchCache;
// 	}
// 
// 	public Cache<String, OrcidProfile> getProfilecache() {
// 		return profileCache;
// 	}
// 
// }
// 
// Path: src/main/java/uk/bl/odin/orcid/domain/IsOrcidWorkProvider.java
// public interface IsOrcidWorkProvider {
// 	public IsOrcidWork fetch(String id) throws IOException;
// }


import java.util.logging.Level;
import java.util.logging.Logger;
import org.restlet.Context;
import uk.bl.odin.orcid.client.OrcidOAuthClient;
import uk.bl.odin.orcid.client.OrcidPublicClient;
import uk.bl.odin.orcid.domain.CacheManager;
import uk.bl.odin.orcid.domain.IsOrcidWorkProvider;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
package uk.bl.odin.orcid;





/**
 * Manages dependencies. Uses context passed from Servlet Init params.
 * 
 * @author tom
 * 
 */
public class GuiceConfigModule extends AbstractModule {

	private static final Logger log = Logger.getLogger(GuiceConfigModule.class.getName());
	private Context context;
	public static final String CONFIG_KEY_OrcidClientID = "OrcidClientID";
	public static final String CONFIG_KEY_OrcidClientSecret = "OrcidClientSecret";
	public static final String CONFIG_KEY_OrcidReturnURI = "OrcidReturnURI";
	public static final String CONFIG_KEY_OrcidSandbox = "OrcidSandbox";
	public static final String CONFIG_KEY_OrcidWorkProvider = "OrcidWorkProvider";

	public static final String CONFIG_KEY_CacheTimeout = "OrcidCacheTimeout";
	public static final String CONFIG_KEY_CacheMaxsize = "OrcidCacheMaxsize";

	public GuiceConfigModule(Context context) {
		super();
		this.context = context;
	}

	/**
	 * Validates configuration and creates dependencies. Uses config from
	 * web.xml
	 * 
	 * Init params
	 * <ul>
	 * <li>"OrcidWorkProvider" fully qualified class name for
	 * IsOrcidWorkProvider instance</li>
	 * <li>"OrcidClientID", "OrcidClientSecret", "OrcidReturnURI" ORCID OAuth
	 * params</li>
	 * <li>"OrcidSandbox" true for sandbox, otherwise use live api</li>
	 * </ul>
	 * 
	 * Binds a singleton OrcidOAuthClient & the configured IsOrcidWorkProvider
	 * class as not-a-singleton Binds passed config as
	 * Named("{init_param_name}")
	 * 
	 */
	@Override
	protected void configure() {
		// validate OAuth configuration
		if (context.getParameters().getFirst(CONFIG_KEY_OrcidClientID) == null
				|| context.getParameters().getFirst(CONFIG_KEY_OrcidClientSecret) == null
				|| context.getParameters().getFirst(CONFIG_KEY_OrcidReturnURI) == null
				|| context.getParameters().getFirst(CONFIG_KEY_OrcidSandbox) == null) {
			log.severe("Init params are:  " + context.getParameters().toString());
			throw new IllegalStateException("cannot create OrcidOAuthClient - missing init parameter(s)");
		}
		if (context.getParameters().getFirst(CONFIG_KEY_OrcidWorkProvider) == null) {
			log.severe("Init params are:  " + context.getParameters().toString());
			throw new IllegalStateException("cannot create OrcidWorkProvier - missing init parameter");
		}

		// Resolve OrcidWork provider
// Path: src/main/java/uk/bl/odin/orcid/domain/CacheManager.java
// public class CacheManager {
// 
// 	private final Cache<SearchKey, OrcidSearchResults> searchCache;
// 
// 	private final Cache<String, OrcidProfile> profileCache;
// 
// 	@Inject
// 	public CacheManager(@Named("OrcidCacheTimeout") Integer timeOutInMinutes,
// 			@Named("OrcidCacheMaxsize") Integer maximumSize) {
// 		searchCache = CacheBuilder.newBuilder().expireAfterWrite(timeOutInMinutes, TimeUnit.MINUTES)
// 				.maximumSize(maximumSize).build();
// 		profileCache = CacheBuilder.newBuilder().expireAfterWrite(timeOutInMinutes, TimeUnit.MINUTES)
// 				.maximumSize(maximumSize).build();
// 	}
// 
// 	public Cache<SearchKey, OrcidSearchResults> getSearchCache() {
// 		return searchCache;
// 	}
// 
// 	public Cache<String, OrcidProfile> getProfilecache() {
// 		return profileCache;
// 	}
// 
// }
// 
// Path: src/main/java/uk/bl/odin/orcid/domain/IsOrcidWorkProvider.java
// public interface IsOrcidWorkProvider {
// 	public IsOrcidWork fetch(String id) throws IOException;
// }


// Path: src/main/java/uk/bl/odin/orcid/GuiceConfigModule.java
import java.util.logging.Level;
import java.util.logging.Logger;
import org.restlet.Context;
import uk.bl.odin.orcid.client.OrcidOAuthClient;
import uk.bl.odin.orcid.client.OrcidPublicClient;
import uk.bl.odin.orcid.domain.CacheManager;
import uk.bl.odin.orcid.domain.IsOrcidWorkProvider;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

package uk.bl.odin.orcid;





/**
 * Manages dependencies. Uses context passed from Servlet Init params.
 * 
 * @author tom
 * 
 */
public class GuiceConfigModule extends AbstractModule {

	private static final Logger log = Logger.getLogger(GuiceConfigModule.class.getName());
	private Context context;
	public static final String CONFIG_KEY_OrcidClientID = "OrcidClientID";
	public static final String CONFIG_KEY_OrcidClientSecret = "OrcidClientSecret";
	public static final String CONFIG_KEY_OrcidReturnURI = "OrcidReturnURI";
	public static final String CONFIG_KEY_OrcidSandbox = "OrcidSandbox";
	public static final String CONFIG_KEY_OrcidWorkProvider = "OrcidWorkProvider";

	public static final String CONFIG_KEY_CacheTimeout = "OrcidCacheTimeout";
	public static final String CONFIG_KEY_CacheMaxsize = "OrcidCacheMaxsize";

	public GuiceConfigModule(Context context) {
		super();
		this.context = context;
	}

	/**
	 * Validates configuration and creates dependencies. Uses config from
	 * web.xml
	 * 
	 * Init params
	 * <ul>
	 * <li>"OrcidWorkProvider" fully qualified class name for
	 * IsOrcidWorkProvider instance</li>
	 * <li>"OrcidClientID", "OrcidClientSecret", "OrcidReturnURI" ORCID OAuth
	 * params</li>
	 * <li>"OrcidSandbox" true for sandbox, otherwise use live api</li>
	 * </ul>
	 * 
	 * Binds a singleton OrcidOAuthClient & the configured IsOrcidWorkProvider
	 * class as not-a-singleton Binds passed config as
	 * Named("{init_param_name}")
	 * 
	 */
	@Override
	protected void configure() {
		// validate OAuth configuration
		if (context.getParameters().getFirst(CONFIG_KEY_OrcidClientID) == null
				|| context.getParameters().getFirst(CONFIG_KEY_OrcidClientSecret) == null
				|| context.getParameters().getFirst(CONFIG_KEY_OrcidReturnURI) == null
				|| context.getParameters().getFirst(CONFIG_KEY_OrcidSandbox) == null) {
			log.severe("Init params are:  " + context.getParameters().toString());
			throw new IllegalStateException("cannot create OrcidOAuthClient - missing init parameter(s)");
		}
		if (context.getParameters().getFirst(CONFIG_KEY_OrcidWorkProvider) == null) {
			log.severe("Init params are:  " + context.getParameters().toString());
			throw new IllegalStateException("cannot create OrcidWorkProvier - missing init parameter");
		}

		// Resolve OrcidWork provider
		IsOrcidWorkProvider provider;