package org.sakaiproject.tool.mytool;

import org.sakaiproject.service.framework.log.Logger;

import org.sakaiproject.service.grid.GridService;

import org.globus.myproxy.*;
import org.gridforum.jgss.ExtendedGSSManager;
import org.gridforum.jgss.ExtendedGSSCredential;
import org.ietf.jgss.GSSCredential;

public class MyGridTool
{
	/** Dependency: The GridService */
	protected GridService gridservice = null;

	/** Dependency: The Logger */
	protected Logger logger = null;

	/** UI Model */
	protected String proxyId  = null;
	protected String proxyPw  = null;
	protected String proxyHost  = null;

	protected String outputMessage = null;

	/**
	 * noarg constructor, required for managed beans.
	 */
	public MyGridTool()
	{

	}

	/**
	 * @return
	 */
	public String getoutputMessage()
	{
		return outputMessage;
	}

	public void setoutputMessage(String newMessage)
	{
		outputMessage = newMessage;
	}

	public GridService getGridservice()
	{
		return gridservice;
	}

	/**
	 * Process the "DoIt" action.
	 * @return the navigation outcome.
	 */
	public String processActionDoIt()
	{
		outputMessage = "";

		// Log.debug("Host, Id="+proxyHost+", "+proxyId);
		// Log.debug("X="+proxyPw);

		if ( proxyId.length() == 0 || proxyPw.length() == 0 ) 
		{
			outputMessage = "Please enter a Host, ID, and  Password";
			return "main";
		}

		try {
			GSSCredential proxy = gridservice.getGSSCredential(proxyId,proxyPw,proxyHost);
			outputMessage = "Subject = "+proxy.getName();
       			logger.debug(outputMessage);
		} catch (Throwable t)
		{
			outputMessage = "Threw exception on getGSSCredential";
			logger.info(this+" Threw exception on getGSSCredential");
		}

		// this becomes the next tool mode
		return "main";
	}

	/**
	 * Establish the GridService dependency
	 * @param gridservice the gridservice instance
	 */
	public void setGridservice(GridService gridservice)
	{
		this.gridservice = gridservice;
	}

	/**
	 * Inject the Logger dependency
	 * @param logger - The logger Instance
	 */
	public void setLogger(Logger logger)
	{
		this.logger = logger;
	}

	/**
	 * Return the logger service
	 */
	public Logger getLogger()
	{
		return logger;
	}

	/**
	 * Setter for the outputMessage
	 */
	public void setOutputMessage(String outputMessage)
	{
		this.outputMessage = outputMessage;
	}

	/**
	 * Getter for the outputMessage
	 */
	public String getOutputMessage()
	{
		return outputMessage;
	}

	/**
	 * Setter for the outputMessage
	 */
	public void setProxyId(String proxyId)
	{
		this.proxyId = proxyId;
	}

	/**
	 * Getter for the proxyId
	 */
	public String getProxyId()
	{
		return proxyId;
	}

	/**
	 * Setter for the proxyPw
	 */
	public void setProxyPw(String proxyPw)
	{
		this.proxyPw = proxyPw;
	}

	/**
	 * Getter for the proxyPw
	 */
	public String getProxyPw()
	{
		return proxyPw;
	}

	/**
	 * Setter for the proxyHost
	 */
	public void setProxyHost(String proxyHost)
	{
		this.proxyHost = proxyHost;
	}

	/**
	 * Getter for the proxyHost
	 */
	public String getProxyHost()
	{
		return proxyHost;
	}

}

