/*
 * Created on 2004-6-28
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.huihoo.willow.naming;

import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.huihoo.willow.session.SessionManager;
import org.huihoo.willow.util.StringManager;

import org.huihoo.willow.client.NamingImpl;

import workflow.WorkflowException;
import workflow.store.RealmDatabase;
import workflow.store.spi.SpiUserDatabase;
import workflow.services.WorkflowService;

public class WillowContext
{
	private static Log log = LogFactory.getLog(WillowContext.class);

	// ----------------------------------------------------------- Constructors
	public WillowContext()
	{
		this(null);
	}

	public WillowContext(String name)
	{
		this.name = name;
		this.wfbindings = new Hashtable();
	}

	// ----------------------------------------------------- Instance Variables
	protected String name;
	protected StringManager sm = StringManager.getManager(Constants.PACKAGE);

	protected Hashtable wfbindings;

	protected SpiUserDatabase   spiUserDatabase;
	protected RealmDatabase     globalRealmDatabase;
	protected SessionManager    globalSessionManager;
	protected NamingImpl        globalNamingImpl;
	
	// --------------------------------------------------------- Public Methods
	public SpiUserDatabase getSpiUserDatabase()
	{
		return spiUserDatabase;
	}

	public void setSpiUserDatabase(SpiUserDatabase userDatabase)
	{
		this.spiUserDatabase = userDatabase;
	}
		
	public RealmDatabase getGlobalRealmDatabase()
	{
		return globalRealmDatabase;
	}

	public void setRealmDatabase(RealmDatabase realmDatabase)
	{
		this.globalRealmDatabase = realmDatabase;
	}

	public SessionManager getGlobalSessionManager()
	{
		return globalSessionManager;
	}
	
	public void setGlobalSessionManager(SessionManager sessionManager)
	{
		this.globalSessionManager = sessionManager;
	}
	
	public NamingImpl getGlobalNamingImpl()
	{
		return globalNamingImpl;
	}
	
	public void setGlobalNamingImpl(NamingImpl namingImpl)
	{
		this.globalNamingImpl = namingImpl;
	}
	// -------------------------------------------------------- WillowContext Methods

	public void bind(String name, WorkflowService workflow) throws WorkflowException
	{
		bind(name, workflow, false);
	}

	public void rebind(String name, WorkflowService workflow) throws WorkflowException
	{
		bind(name, workflow, true);
	}

	public void unbind(String name) throws WorkflowException
	{
		if (name == null || "".equals(name))
		{
			throw new WorkflowException(sm.getString("willowContext.invalidName"));
		}

		Object entry = wfbindings.get(name);

		if (entry == null)
		{
			throw new WorkflowException(sm.getString("willowContext.nameNotBound", name));
		}

		wfbindings.remove(name);
	}

	public Enumeration list() throws WorkflowException
	{
		return wfbindings.elements();
	}

	public WorkflowService lookup(String name) throws WorkflowException
	{
		if (name == null || "".equals(name))
		{
			throw new WorkflowException(sm.getString("willowContext.invalidName"));
		}

		WorkflowService entry = (WorkflowService) wfbindings.get(name);

		if (entry == null)
		{
			throw new WorkflowException(sm.getString("willowContext.nameNotBound", name));
		}
		return entry;
	}

	protected void bind(String name, WorkflowService obj, boolean rebind) throws WorkflowException
	{
		if (name == null || "".equals(name))
		{
			throw new WorkflowException(sm.getString("willowContext.invalidName"));
		}

		WorkflowService entry = (WorkflowService) wfbindings.get(name);

		if ((!rebind) && (entry != null))
		{
			throw new WorkflowException(sm.getString("willowContext.alreadyBound", name));
		}
		else
		{
			wfbindings.put(name, obj);
		}
	}
}
