/*
 * Created on 2004-6-9
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package workflow.client;

import java.util.Hashtable;

import javax.naming.NamingException;

import workflow.WorkflowException;
import workflow.client.factory.*;
import workflow.live.LiveService;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class InitialLiveClient implements LiveClient
{
	/**
	 * The environment associated with this InitialContext.
	 * It is initialized to null and is updated by the constructor
	 * that accepts an environment or by the <tt>init()</tt> method.
	 * @see #addToEnvironment
	 * @see #removeFromEnvironment
	 * @see #getEnvironment
	 */
	protected Hashtable myProps = null;

	/**
		* Field holding the result of calling WorkflowNamingManager.getInitialWorkflowClient().
		* It is set by defaultInitWFClient() the first time getDefaultInitCtx()
		* is called. Subsequent invocations of getInitialWorkflowClient() return
		* the value of defaultInitWFClient.
		* @see #defaultInitWFClient
		*/
	protected LiveClient defaultInitWFClient = null;

	/**
		* Field indicating whether the initial workflowclient has been obtained
		* by calling WorkflowNamingManager.getInitialWorkflowClient().
		* If true, its result is in <code>defaultInitWFClient</code>.
		*/
	protected boolean gotDefault = false;

	public LiveService     findService(String serviceName) throws WorkflowException
	{
		return defaultInitWFClient.findService(serviceName);
	}	 	
	public LiveService[]   findServices() throws WorkflowException
	{
		return defaultInitWFClient.findServices();
	}
	
	public String getSessionID() throws WorkflowException
	{
		return defaultInitWFClient.getSessionID();
	}
	
	public int getMaxInactiveInterval()throws WorkflowException
	{
		return defaultInitWFClient.getMaxInactiveInterval();
	}

	public void setMaxInactiveInterval(int interval)throws WorkflowException
	{
		defaultInitWFClient.setMaxInactiveInterval(interval);
	}
		
	public void  keepAlive() throws WorkflowException 
	{
		defaultInitWFClient.keepAlive();
	}
	
	public void   disconnect() throws WorkflowException
	{
		defaultInitWFClient.disconnect();
	}
	
//=====================================================================
//       constructor  
//=====================================================================	
	public InitialLiveClient() throws WorkflowException, NamingException
	{
		init(null);
	}

	public InitialLiveClient(Hashtable environment) throws WorkflowException, NamingException
	{
		init(environment);
	}

	protected void init(Hashtable environment) throws WorkflowException, NamingException
	{
		myProps = ResourceManager.getInitialEnvironment(environment);

		if (myProps.get(WorkflowClient.INITIAL_CLIENT_FACTORY) != null)
		{
			// user has specified initial client factory; try to get it
			getDefaultInitWFClient();
		}
		else
		{
			throw new NoWorkflowClientException();
		}
	}
	
	protected LiveClient getDefaultInitWFClient() throws WorkflowException, NamingException
	{
		if (!gotDefault)
		{
			defaultInitWFClient = LiveNamingManager.getInitialWfiClient(myProps);
			gotDefault = true;
		}
		if (defaultInitWFClient == null)
		{
			throw new NoWorkflowClientException();
		}

		return defaultInitWFClient;
	}
}
